eae0994566e2b54c2ca6a499816c95c77b4bcd0b
[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                         self.name = label
114
115                         @slot = nil
116                         @alpha_anim = nil
117
118                         @img = Evas::Image.new(evas)
119                         @label = Evas::Text.new(evas)
120
121                         @objects = [@img, @label]
122                         @objects.each { |o| add_member(o) }
123
124                         set_color(255, 255, 255, 0)
125                         @img.set_color(255, 255, 255, 0)
126                         @label.set_color(255, 0, 0, 0)
127
128                         @img.set_file(ICON_FILE)
129                         @img.set_fill(0, 0, *@img.get_size)
130
131                         @label.text = name
132                         @label.set_font("VeraBd", 10)
133
134                         resize(*@img.get_size)
135                 end
136
137                 # smart callbacks
138                 def smart_show
139                         @objects.each { |o| o.show }
140
141                         @alpha_anim ||= AlphaAnimator.new(2, self)
142                         @alpha_anim.on_finished { @alpha_anim = nil }
143                 end
144
145                 def smart_hide
146                         @objects.each { |o| o.hide }
147
148                         @alpha_anim && @alpha_anim.delete
149                         @alpha_anim = nil
150                 end
151
152                 def smart_delete
153                         @objects.each { |o| o.delete }
154                         @objects.clear
155
156                         @alpha_anim && @alpha_anim.delete
157
158                         @img = @label = @alpha_anim = nil
159                 end
160
161                 def smart_move(x, y)
162                         @objects.each { |o| o.move(x, y) }
163
164                         @label.center(self)
165                 end
166
167                 def smart_resize(w, h)
168                         @img.resize(w, h)
169                 end
170
171                 def smart_color_set(r, g, b, a)
172                         @objects.each { |o| o.alpha = a }
173                 end
174         end
175
176         class Container < Evas::Smart
177                 class ContainerError < StandardError; end
178                 class ContainerFullError < ContainerError; end
179                 class ContainerLockedError < ContainerError; end
180
181                 def initialize(evas)
182                         super
183
184                         @bg = Evas::Rectangle.new(evas)
185                         @bg.set_color(0, 0, 0, 8)
186
187                         add_member(@bg)
188
189                         @icons = []
190                         @animators = []
191
192                         @about_to_add = 0
193                         @add_lock_count = 0
194                 end
195
196                 def can_add?
197                         !slots_left.zero? && @add_lock_count.zero?
198                 end
199
200                 def can_delete?
201                         @about_to_add.zero? && @add_lock_count.zero?
202                 end
203
204                 def <<(i)
205                         Kernel.raise(ContainerFullError) if slots_left.zero?
206                         Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
207
208                         i.move_relative(self, 0, 0)
209                         i.slot = next_slot
210                         i.clip = self
211                         i.show
212
213                         # check whether we need to need to move this icon
214                         if slots_left == 1
215                                 @icons[i.slot] = i
216                                 return
217                         end
218
219                         movement = Main.instance.icon_height * (slots_left - 1)
220
221                         @about_to_add += 1
222
223                         move_time = 0.25 * slots_left
224                         @animators << MoveAnimator.new(move_time, movement, i)
225
226                         @animators.last.on_finished do |ani|
227                                 @animators.delete(ani)
228                                 @icons[i.slot] = i
229
230                                 @about_to_add -= 1
231                         end
232                 end
233
234                 def delete(icon)
235                         i = @icons.index(icon)
236                         return (block_given? ? yield : nil) if i.nil?
237
238                         delete_at(i)
239                 end
240
241                 def delete_at(i)
242                         Kernel.raise(ContainerLockedError) unless @about_to_add.zero?
243                         Kernel.raise(ContainerLockedError) unless @add_lock_count.zero?
244
245                         # icons that are placed above the one that's deleted need
246                         # to be moved
247                         ar = @icons[(i + 1)..-1]
248
249                         @icons[i].delete
250                         @icons.delete_at(i)
251
252                         return if ar.empty?
253
254                         @add_lock_count += 1
255
256                         @animators << MoveAnimator.new(2, Main.instance.icon_height, *ar)
257                         @animators.last.on_finished do |ani|
258                                 @animators.delete(ani)
259                                 @add_lock_count -= 1
260                         end
261                 end
262
263                 # smart callbacks
264                 def smart_show
265                         @bg.show
266                 end
267
268                 def smart_hide
269                         @bg.hide
270                 end
271
272                 def smart_delete
273                         @bg.delete
274                         @bg = nil
275                 end
276
277                 def smart_move(x, y)
278                         @bg.move(x, y)
279                 end
280
281                 def smart_resize(w, h)
282                         @bg.resize(w, h)
283                 end
284
285                 private
286                 def slots_left
287                         MAX_ICONS - @icons.nitems - @about_to_add
288                 end
289
290                 def next_slot
291                         @icons.nitems + @about_to_add
292                 end
293         end
294
295         class Main < Ecore::Evas::SoftwareX11
296                 include Singleton
297
298                 attr_reader :container
299
300                 def initialize
301                         super
302
303                         self.has_alpha = true
304                         self.title = "Embrace"
305                         self.borderless = true
306
307                         @icon_dim = IO.read(ICON_FILE, 8, 16).unpack("NN")
308
309                         on_resize { @container.resize(*geometry[2, 3]) }
310
311                         @container = Container.new(evas)
312                         @container.move(0, 0)
313                         @container.layer = -1
314                         @container.show
315
316                         size = [@icon_dim.first, icon_height * MAX_ICONS]
317                         resize(*size)
318                         set_size_min(*size)
319                         set_size_max(*size)
320
321                         evas.font_path_append("/usr/lib/X11/fonts/TTF")
322
323                         @handlers = [
324                                 Ecore::EventHandler.new(IMAP::MailboxStatusEvent,
325                                                         &method(:on_mailbox_status)),
326                                 Ecore::EventHandler.new(IMAP::FinishedEvent,
327                                                         &method(:on_finished))
328                         ]
329
330                         s = File.expand_path("~/.e/apps/embrace/config.yaml")
331                         @config = YAML.load(File.read(s))
332
333                         @server = nil
334                         @timer = Ecore::Timer.new(30, &method(:on_timer))
335                         on_timer
336                 end
337
338                 def icon_height
339                         @icon_dim.last
340                 end
341
342                 def run
343                         Ecore.main_loop_begin
344                 end
345
346                 private
347                 def on_timer
348                         return unless @server.nil?
349
350                         @server = IMAP::Session.new(@config)
351
352                         true
353                 end
354
355                 def on_mailbox_status(ev)
356                         md = ev.name.match(/^Lists.(.+)$/)
357                         if md.nil?
358                                 lbl = ev.name
359                         else
360                                 lbl = md.captures.first
361                         end
362
363                         found = evas.find_object(lbl)
364
365                         if ev.count.zero? && !found.nil? && @container.can_delete?
366                                 @container.delete(found)
367                         elsif !ev.count.zero? && found.nil? && @container.can_add?
368                                 @container << MailboxIcon.new(evas, lbl)
369                         end
370
371                         false
372                 end
373
374                 def on_finished(ev)
375                         @server = nil
376                 end
377         end
378 end
379
380 Embrace::Main.instance.show
381 Embrace::Main.instance.run