[gnome-clocks] Implement alarm repeat



commit 730d0f13760025e5a0ad16b77c860f37976400b4
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Sep 1 01:01:32 2012 +0200

    Implement alarm repeat
    
    Take days into accout for the calculating the alarm expiration time and
    schedule a new alarm for the nearest available date when the alarm
    rings.

 gnomeclocks/alarm.py |   33 ++++++++++++++++++++-------------
 1 files changed, 20 insertions(+), 13 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index f30616f..64a5f77 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -83,13 +83,23 @@ class AlarmItem:
         if not self.days:
             self.days = AlarmItem.EVERY_DAY
 
+        self.time = None
         if not hour == None and not minute == None:
-            t = datetime.strptime("%02i:%02i" % (hour, minute), "%H:%M")
-            self.time = datetime.combine(datetime.today(), t.time())
-            self.expired = datetime.now() > self.time
-        else:
-            self.time = None
-            self.expired = True
+            self.update_expiration_time()
+
+    def update_expiration_time(self):
+        now = datetime.now()
+        dt = now.replace(hour=self.hour, minute=self.minute)
+        # check if it can ring later today
+        if dt.weekday() not in self.days or dt <= now:
+            # otherwise if it can ring this week
+            next_days = [ d for d in self.days if d > dt.weekday() ]
+            if next_days:
+                dt += timedelta(days=(next_days[0] - dt.weekday()))
+            # otherwise next week
+            else:
+                dt += timedelta(weeks=1, days=(self.days[0] - dt.weekday()))
+        self.time = dt
 
     def get_time_as_string(self):
         if SystemSettings.get_clock_format() == "12h":
@@ -138,13 +148,11 @@ class AlarmItem:
                 days.append(LocalizedWeekdays.SUN)
             return ", ".join(days)
 
-    # 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
+        if datetime.now() > self.time:
+            self.update_expiration_time()
+            return True
+        return False
 
 
 class AlarmDialog(Gtk.Dialog):
@@ -493,7 +501,6 @@ class StandaloneAlarm(Gtk.Box):
     def _on_snooze_clicked(self, button):
         # Add 9 minutes, but without saving the change permanently
         self.alarm.time += timedelta(minutes=9)
-        self.alarm.expired = False
         self.alert.stop()
 
     def get_name(self):



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