Release 2.1
[memo.git] / timer.py
blob8507aa7a166da287d0b0cbf0abad15e3b6bf7377
1 # Copyright (C) 2006, Thomas Leonard
3 from rox import g, Dialog, info, confirm
4 import gobject
5 import time
6 import dbus_notify
8 edit_timer_box = None
10 class TimerButton(g.Button):
11 """An 'egg-timer' button for the main window'"""
13 def __init__(self):
14 g.Button.__init__(self, 'T')
15 self.end_time = None
16 self.timeout = None
17 self.unset_flags(g.CAN_FOCUS)
19 tips = g.Tooltips()
20 tips.set_tip(self, _('Click here to set the count-down timer.'))
22 self.connect('clicked', edit_timer)
24 def set_timer(self, secs):
25 self.end_time = time.time() + secs
26 def update_timer():
27 wait = self.end_time - time.time()
28 if wait < 0:
29 self.timeout = None
30 self.alarm()
31 return False
32 if wait >= 60:
33 text = '%dm%d' % (int(wait / 60), int(wait % 60))
34 else:
35 text = '%ss' % int(wait)
36 self.set_label(text)
37 return True
38 assert self.timeout is None
39 if update_timer():
40 self.timeout = gobject.timeout_add(1000, update_timer)
42 def alarm(self):
43 self.clear_timer()
44 if dbus_notify.is_available():
45 dbus_notify.timer()
46 else:
47 info(_('Memo : Time is up!'))
49 def clear_timer(self):
50 self.set_label('T')
51 self.end_time = None
52 if self.timeout:
53 gobject.source_remove(self.timeout)
54 self.timeout = None
56 def edit_timer(timer):
57 global edit_timer_box
58 if edit_timer_box:
59 edit_timer_box.destroy()
61 if timer.end_time:
62 if confirm(_('The timer is already set - clear it?'), g.STOCK_CLEAR):
63 timer.clear_timer()
64 return
66 edit_timer_box = Dialog(title = _('Memo Timer'), parent = timer.get_toplevel(), flags = g.DIALOG_NO_SEPARATOR)
67 def destroyed(box):
68 global edit_timer_box
69 assert edit_timer_box is box
70 edit_timer_box = None
71 edit_timer_box.connect('destroy', destroyed)
73 def response(d, resp):
74 if resp == int(g.RESPONSE_OK):
75 timer.set_timer(min.value * 60 + sec.value)
76 d.destroy()
77 edit_timer_box.connect('response', response)
79 vbox = g.VBox(False, 0)
80 vbox.set_border_width(8)
81 edit_timer_box.vbox.pack_start(vbox, True, True, 0)
82 vbox.pack_start(g.Label(_('Set the count-down timer and click OK.')), True, True, 0)
84 hbox = g.HBox(False, 0)
85 vbox.pack_start(hbox, False, True, 8)
87 min = g.Adjustment(0, 0, 999, 1, 1)
88 spin = g.SpinButton(min)
89 spin.set_digits(0)
90 spin.set_activates_default(True)
91 hbox.pack_start(spin, True, True, 0)
92 hbox.pack_start(g.Label(_('min ')), False, True, 2)
94 sec = g.Adjustment(0, 0, 59, 1, 1)
95 spin = g.SpinButton(sec)
96 spin.set_digits(0)
97 spin.set_activates_default(True)
98 hbox.pack_start(spin, True, True, 0)
99 hbox.pack_start(g.Label(_('sec')), False, True, 2)
101 edit_timer_box.add_button(g.STOCK_CANCEL, g.RESPONSE_CANCEL)
102 edit_timer_box.add_button(g.STOCK_OK, g.RESPONSE_OK)
103 edit_timer_box.set_default_response(g.RESPONSE_OK)
105 edit_timer_box.show_all()