[gnome-clocks: 3/5] Make alarms ring and show a notification



commit 67ef92623e68f8e84f8694441a22001299e02a34
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Aug 24 22:18:39 2012 +0200

    Make alarms ring and show a notification
    
    This is just temporary and makes some bad assumptions (eg that alarms
    just ring once while the app is open), but at least it makes alarms
    ring... a real solution implies delegating the acual alarms to an
    extrnal process that is running even when the app is closed.

 gnomeclocks/alarm.py  |   16 ++++++++++++++--
 gnomeclocks/clocks.py |   24 +++++++++++++++++++++---
 gnomeclocks/utils.py  |    2 +-
 3 files changed, 36 insertions(+), 6 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index 5311618..3aea4c8 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -74,11 +74,14 @@ class AlarmItem:
         self.uid = None
         if h and m:
             if p:
-                self.time = datetime.strptime("%02i:%02i %s" % (h, m, p), "%I:%M %p")
+                t = datetime.strptime("%02i:%02i %s" % (h, m, p), "%I:%M %p")
             else:
-                self.time = datetime.strptime("%02i:%02i" % (h, m), "%H:%M")
+                t = datetime.strptime("%02i:%02i" % (h, m), "%H:%M")
+            self.time = datetime.combine(datetime.today(), t.time())
+            self.expired = datetime.now() > self.time
         else:
             self.time = None
+            self.expired = True
 
     def new_from_vevent(self, vevent):
         self.vevent = vevent
@@ -89,6 +92,7 @@ class AlarmItem:
             self.repeat = ['FR', 'MO', 'SA', 'SU', 'TH', 'TU', 'WE']
         else:
             self.repeat = vevent.rrule.value[19:].split(',')
+        self.expired = datetime.now() > self.time
 
     def get_time_as_string(self):
         if SystemSettings.get_clock_format() == "12h":
@@ -151,3 +155,11 @@ class AlarmItem:
             vevent.add('rrule').value = 'FREQ=WEEKLY;BYDAY=%s' %\
             ','.join(self.repeat)
         return vevent
+
+    # FIXME: this is not a really good way, we assume each alarm
+    # can ring only once while the program is running
+    def check_expired(self):
+        if self.expired:
+            return False
+        self.expired = datetime.now() > self.time
+        return self.expired
diff --git a/gnomeclocks/clocks.py b/gnomeclocks/clocks.py
index 9af2467..48c4c92 100644
--- a/gnomeclocks/clocks.py
+++ b/gnomeclocks/clocks.py
@@ -173,6 +173,7 @@ class Alarm(Clock):
                                        Pixbuf,
                                        str,
                                        GObject.TYPE_PYOBJECT,
+                                       GObject.TYPE_PYOBJECT,
                                        GObject.TYPE_PYOBJECT)
 
         self.iconview = SelectableIconView(self.liststore, 0, 1, 2)
@@ -185,10 +186,24 @@ class Alarm(Clock):
         self.iconview.connect("item-activated", self._on_item_activated)
         self.iconview.connect("selection-changed", self._on_selection_changed)
 
-        self.alarms = []
         self.load_alarms()
         self.show_all()
 
+        self.timeout_id = GObject.timeout_add(1000, self._check_alarms)
+
+    def _check_alarms(self):
+        for i in self.liststore:
+            alarm = self.liststore.get_value(i.iter, 5)
+            if alarm.check_expired():
+                print alarm
+                alert = self.liststore.get_value(i.iter, 4)
+                alert.show()
+        return True
+
+    def _on_notification_activated(self, notif, action, data):
+        win = self.get_toplevel()
+        win.show_clock(self)
+
     def _on_item_inserted(self, model, path, treeiter):
         self.update_empty_view()
 
@@ -197,7 +212,7 @@ class Alarm(Clock):
 
     def _on_item_activated(self, iconview, path):
         alarm = self.liststore[path][-1]
-        self.open_edit_dialog(alarm)
+        self.open_edit_dialog(alarm.get_vevent())
 
     def _on_selection_changed(self, iconview):
         self.emit("selection-changed")
@@ -240,11 +255,14 @@ class Alarm(Clock):
         timestr = alarm.get_time_as_string()
         repeat = alarm.get_alarm_repeat_string()
         widget = AlarmWidget(timestr, repeat)
+        alert = Alert("alarm-clock-elapsed", name,
+                      self._on_notification_activated)
         view_iter = self.liststore.append([False,
                                            widget.get_pixbuf(),
                                            "<b>" + name + "</b>",
                                            widget,
-                                           alarm.get_vevent()])
+                                           alert,
+                                           alarm])
 
     def edit_alarm(self, alarm):
         print "To Do!"
diff --git a/gnomeclocks/utils.py b/gnomeclocks/utils.py
index c092b61..3542ea0 100644
--- a/gnomeclocks/utils.py
+++ b/gnomeclocks/utils.py
@@ -67,7 +67,7 @@ class Alert:
         self.soundid = soundid
 
         self.notification = None
-        if Notify.init("GNOME Clocks"):
+        if Notify.is_initted() or Notify.init("GNOME Clocks"):
             self.notification = Notify.Notification.new("Clocks", msg, 'clocks')
             # the special "default" action should not display a button
             self.notification.add_action("default", "Show", callback, None, None)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]