b82449c932e5558810d2aadc403ccd2e8a1e6f97
[embrace.git] / bin / embrace
1 #!/usr/bin/env ruby
2
3 # vim:syn=ruby
4 #
5 # Copyright (c) 2006 Tilman Sauerbeck (tilman at code-monkey de)
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of version 2 of the GNU General Public License as
9 # published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19
20 require "ecore"
21 require "ecore_x"
22 require "ecore_evas"
23 require "singleton"
24 require "yaml"
25 require "embrace/imap"
26
27 PKG_NAME = "embrace"
28 DATADIR = "/usr/local/share/#{PKG_NAME}/"
29
30 class Evas::EvasObject
31         def move_relative(obj, x, y)
32                 # FIXME investigate whether there's an easier way
33                 move(*obj.geometry[0..1].zip([x, y]).map { |(a, b)| a + b })
34         end
35
36         def center(obj)
37                 a = geometry
38                 b = obj.geometry
39
40                 move_relative(obj, (b[2] / 2) - (a[2] / 2),
41                                    (b[3] / 2) - (a[3] / 2))
42         end
43
44         def alpha=(alpha)
45                 set_color(*(get_color[0..-2] << alpha))
46         end
47 end
48
49 module Embrace
50         VERSION = "0.0.1"
51         ICON_FILE = DATADIR + "l33t_MAI_envelope.png"
52         MAX_ICONS = 11
53
54         class ZeroToOneAnimator < Ecore::Animator
55                 def initialize(duration)
56                         @finished = nil
57
58                         started = Time.now
59
60                         super() do
61                                 v = [(Time.now - started) / duration, 1.0].min
62
63                                 yield v
64
65                                 @finished.call(self) unless (v < 1.0) || @finished.nil?
66                                 v < 1.0
67                         end
68                 end
69
70                 def delete
71                         super
72
73                         @finished = nil
74                 end
75
76                 def on_finished(&block)
77                         @finished = block
78                 end
79         end
80
81         # an animator that runs for the specified number of seconds,
82         # and yields values between 0 and 255
83         class AlphaAnimator < ZeroToOneAnimator
84                 def initialize(duration, *objects)
85                         super(duration) do |v|
86                                 objects.each { |o| o.alpha = (255 * v).to_i }
87                         end
88                 end
89         end
90
91         class MoveAnimator < ZeroToOneAnimator
92                 def initialize(duration, movement, *objects)
93                         zipped = objects.zip(objects.map { |o| o.geometry[0..1] })
94
95                         super(duration) do |v|
96                                 # decelerate
97                                 v = Math.sin(v * Math::PI / 2.0)
98
99                                 zipped.each do |(o, orig_pos)|
100                                         o.move(orig_pos.first,
101                                                orig_pos.last + (movement * v).to_i)
102                                 end
103                         end
104                 end
105         end
106
107         class MailboxIcon < Evas::Smart
108                 attr_accessor :slot
109
110                 def initialize(evas, label)
111                         super(evas)
112
113                         @slot = nil
114                         @alpha_anim = nil
115
116                         @img = Evas::Image.new(evas)
117                         @label = Evas::Text.new(evas)
118
119                         @objects = [@img, @label]
120                         @objects.each { |o| add_member(o) }
121
122                         @img.set_color(255, 255, 255, 0)
123                         @label.set_color(255, 255, 255, 0)
124                         @label.set_color(255, 0, 0, 0)
125
126                         @img.set_file(ICON_FILE)
127                         @img.set_fill(0, 0, *@img.get_size)
128
129                         @label.text = label
130                         @label.set_font("VeraBd", 10)
131
132                         resize(*@img.get_size)
133                 end
134
135                 def label
136                         @label.text
137                 end
138
139                 # smart callbacks
140                 def on_show
141                         @objects.each { |o| o.show }
142
143                         @alpha_anim ||= AlphaAnimator.new(2, @img, @label)
144                         @alpha_anim.on_finished { @alpha_anim = nil }
145                 end
146
147                 def on_hide
148                         @objects.each { |o| o.hide }
149
150                         @alpha_anim && @alpha_anim.delete
151                         @alpha_anim = nil
152                 end
153
154                 def on_delete
155                         @objects.each { |o| o.delete }
156                         @objects.clear
157
158                         @alpha_anim && @alpha_anim.delete
159
160                         @img = @label = @alpha_anim = nil
161                 end
162
163                 def on_move(x, y)
164                         @objects.each { |o| o.move(x, y) }
165
166                         @label.center(self)
167                 end
168
169                 def on_resize(w, h)
170                         @img.resize(w, h)
171                 end
172         end
173
174         class FixedSizeArray < Array
175                 def initialize(siz)
176                         super
177                 end
178
179                 def each
180                         super { |item| yield item unless item.nil? }
181                 end
182
183                 def delete_at(i)
184                         self[i] = nil
185                 end
186
187                 undef :push
188                 undef :<<
189                 undef :unshift
190
191                 undef :pop
192                 undef :shift
193                 undef :delete
194         end
195
196         class Container < Evas::Smart
197                 class ContainerError < StandardError; end
198                 class ContainerFullError < ContainerError; end
199                 class ContainerLockedError < ContainerError; end
200
201                 include Enumerable
202
203                 def initialize(evas)
204                         super
205
206                         @bg = Evas::Rectangle.new(evas)
207                         @bg.set_color(0, 0, 0, 255)
208
209                         add_member(@bg)
210
211                         @icons = FixedSizeArray.new(MAX_ICONS)
212                         @about_to_add = []
213                         @animators = []
214
215                         @lock_count = 0
216                 end
217
218                 def each
219                         @icons.each { |i| yield i unless i.nil? }
220                 end
221
222                 def <<(i)
223                         Kernel.raise(ContainerFullError) if slots_left.zero?
224                         Kernel.raise(ContainerLockedError) if @lock_count > 0
225
226                         i.move_relative(self, 0, 0)
227                         i.slot = next_slot
228                         i.clip = self
229                         i.show
230
231                         # check whether we need to need to move this icon
232                         if slots_left == 1
233                                 @icons[i.slot] = i
234                                 return
235                         end
236
237                         movement = Main.instance.icon_height * (slots_left - 1)
238
239                         @about_to_add << i
240
241                         move_time = 0.25 * slots_left
242                         @animators << MoveAnimator.new(move_time, movement, i)
243
244                         @animators.last.on_finished do |ani|
245                                 @animators.delete(ani)
246                                 @icons[i.slot] = i
247
248                                 # FIXME check whether we can always shift the array instead
249                                 #puts "really added #{i.label} now (slot #{i.slot})"
250                                 @about_to_add.delete(i)
251                         end
252                 end
253
254                 def delete(icon)
255                         # icons that are placed above the one that's deleted need
256                         # to be moved
257                         i = @icons.index(icon)
258                         return (block_given? ? yield : nil) if i.nil?
259
260                         delete_at(i)
261                 end
262
263                 def delete_at(i)
264                         @icons[i].delete
265                         @icons.delete_at(i)
266
267                         ar = @icons[i..-1].reject { |i| i.nil? }
268                         return if ar.nil?
269
270                         @lock_count += 1
271
272                         @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
273                         @animators.last.on_finished do |ani|
274                                 @animators.delete(ani)
275                                 @lock_count -= 1
276                         end
277                 end
278
279                 def length
280                         @icons.nitems
281                 end
282
283                 # smart callbacks
284                 def on_show
285                         @bg.show
286                 end
287
288                 def on_hide
289                         @bg.hide
290                 end
291
292                 def on_delete
293                         @bg.delete
294                         @bg = nil
295                 end
296
297                 def on_move(x, y)
298                         @bg.move(x, y)
299                 end
300
301                 def on_resize(w, h)
302                         @bg.resize(w, h)
303                 end
304
305                 private
306                 def slots_left
307                         MAX_ICONS - @icons.nitems - @about_to_add.length
308                 end
309
310                 def next_slot
311                         @icons.nitems + @about_to_add.length
312                 end
313         end
314
315         class Main < Ecore::Evas::SoftwareX11
316                 include Singleton
317
318                 attr_reader :container
319
320                 def initialize
321                         super
322
323                         self.title = "Embrace"
324                         self.borderless = true
325
326                         @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
327
328                         on_resize { @container.resize(*geometry[2, 3]) }
329
330                         @container = Container.new(evas)
331                         @container.move(0, 0)
332                         @container.layer = -1
333                         @container.show
334
335                         size = [@icon_dim.first, icon_height * MAX_ICONS]
336                         resize(*size)
337                         set_size_min(*size)
338                         set_size_max(*size)
339
340                         evas.font_path_append("/usr/lib/X11/fonts/TTF")
341
342                         @handlers = [
343                                 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
344                                                         &method(:on_mailbox_status)),
345                                 Ecore::EventHandler.new(IMAP::FinishedEvent,
346                                                         &method(:on_finished))
347                         ]
348
349                         s = File.expand_path("~/.e/apps/embrace/config.yaml")
350                         @config = YAML.load(File.read(s))
351
352                         @server = nil
353                         @timer = Ecore::Timer.new(30, &method(:on_timer))
354                         on_timer
355                 end
356
357                 def icon_height
358                         @icon_dim.last
359                 end
360
361                 def run
362                         Ecore.main_loop_begin
363                 end
364
365                 private
366                 def add_icon(name)
367                         @container << MailboxIcon.new(evas, name)
368                 end
369
370                 def on_timer
371                         return unless @server.nil?
372
373                         @server = IMAP::Session.new(@config)
374
375                         true
376                 end
377
378                 def on_mailbox_status(ev)
379                         md = ev.name.match(/^Lists.(.+)$/)
380                         if md.nil?
381                                 lbl = ev.name
382                         else
383                                 lbl = md.captures.first
384                         end
385
386                         found = @container.find { |i| i.label == lbl }
387
388                         begin
389                                 if ev.count.zero?
390                                         unless found.nil?
391                                                 #puts "removing icon #{lbl}"
392                                                 @container.delete(found)
393                                         else
394                                                 #puts "count == 0, but icon not found (#{lbl})"
395                                         end
396                                 elsif found.nil?
397                                         #puts "adding icon #{lbl}"
398                                         add_icon(lbl)
399                                 else
400                                         #puts "count > 0, but already there (#{lbl})"
401                                 end
402                         rescue Exception => e
403                                 puts e.message
404                         end
405
406                         false
407                 end
408
409                 def on_finished(ev)
410                         @server = nil
411                 end
412         end
413 end
414
415 Embrace::Main.instance.show
416 Embrace::Main.instance.run