[gnome-clocks] Fix the snooze functionality



commit 798753410a6cb35fb4f402240e0876da201313c1
Author: Volker Sobek <reklov live com>
Date:   Wed Nov 14 13:59:43 2012 +0100

    Fix the snooze functionality
    
    AlarmItem: Keep track of the snooze time in a separate variable and
    update alarm time and snooze time independently, as needed. Also add
    snooze() and stop() methods.

 gnomeclocks/alarm.py |   34 +++++++++++++++++++++++++++-------
 1 files changed, 27 insertions(+), 7 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index f50f80f..332aa08 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -84,10 +84,14 @@ class AlarmItem:
             self.days = AlarmItem.EVERY_DAY
 
         self.time = None
+        self.snooze_time = None
+        self.is_snoozing = False
+
         if not hour == None and not minute == None:
-            self.update_expiration_time()
+            self._update_expiration_time()
+            self._update_snooze_expiration_time(self.time)
 
-    def update_expiration_time(self):
+    def _update_expiration_time(self):
         now = datetime.now()
         dt = now.replace(hour=self.hour, minute=self.minute, second=0, microsecond=0)
         # check if it can ring later today
@@ -101,6 +105,16 @@ class AlarmItem:
                 dt += timedelta(weeks=1, days=(self.days[0] - dt.weekday()))
         self.time = dt
 
+    def _update_snooze_expiration_time(self, start_time):
+        self.snooze_time = start_time + timedelta(minutes=9)
+
+    def snooze(self):
+        self.is_snoozing = True
+
+    def stop(self):
+        self.is_snoozing = False
+        self._update_snooze_expiration_time(self.time)
+
     def get_time_as_string(self):
         if SystemSettings.get_clock_format() == "12h":
             return self.time.strftime("%I:%M %p")
@@ -126,10 +140,16 @@ class AlarmItem:
             return ", ".join(days)
 
     def check_expired(self):
-        if datetime.now() > self.time:
-            self.update_expiration_time()
+        t = datetime.now()
+        if self.is_snoozing and t > self.snooze_time:
+            self._update_snooze_expiration_time(self.snooze_time)
+            self.is_snoozing = False
+            return True
+        elif t > self.time:
+            self._update_expiration_time()
             return True
-        return False
+        else:
+            return False
 
 
 class AlarmDialog(Gtk.Dialog):
@@ -482,11 +502,11 @@ class StandaloneAlarm(Gtk.EventBox):
         self.set_ringing(False)
 
     def _on_stop_clicked(self, button):
+        self.alarm.stop()
         self.alert.stop()
 
     def _on_snooze_clicked(self, button):
-        # Add 9 minutes, but without saving the change permanently
-        self.alarm.time += timedelta(minutes=9)
+        self.alarm.snooze()
         self.alert.stop()
 
     def get_name(self):



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