[gnome-clocks] Use WallClock from GnomeDesktop



commit 30f927a6c45f80a0271f637d81e5dd29e069ab72
Author: Volker Sobek <reklov live com>
Date:   Mon Dec 3 14:09:16 2012 +0100

    Use WallClock from GnomeDesktop
    
    This commit ensures World and Alarm only update/check when the
    WallClock time changes, since both only care about minutes.

 gnomeclocks/alarm.py |   20 +++++++++++---------
 gnomeclocks/utils.py |   36 +++++++++++++++++++++++++++++++++++-
 gnomeclocks/world.py |   16 +++++++++-------
 3 files changed, 55 insertions(+), 17 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index aeeb05b..2b59c88 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -20,13 +20,16 @@ import os
 import errno
 import time
 import json
-from datetime import datetime, timedelta
+from datetime import timedelta
 from gi.repository import GLib, GObject, Gdk, GdkPixbuf, Gtk
 from clocks import Clock
-from utils import Dirs, SystemSettings, TimeString, LocalizedWeekdays, Alert
+from utils import Alert, Dirs, LocalizedWeekdays, SystemSettings, TimeString, WallClock
 from widgets import SelectableIconView, ContentView
 
 
+wallclock = WallClock.get_default()
+
+
 class AlarmsStorage:
     def __init__(self):
         self.filename = os.path.join(Dirs.get_user_data_dir(), "alarms.json")
@@ -85,7 +88,7 @@ class AlarmItem:
         self.alert = Alert("alarm-clock-elapsed", name)
 
     def _update_expiration_time(self):
-        now = datetime.now()
+        now = wallclock.datetime
         dt = now.replace(hour=self.hour, minute=self.minute, second=0, microsecond=0)
         # check if it can ring later today
         if dt.weekday() not in self.days or dt <= now:
@@ -132,13 +135,12 @@ class AlarmItem:
         self.alert.stop()
 
     def check_expired(self):
-        t = datetime.now()
-        if t > self.alarm_time:
+        if wallclock.datetime > self.alarm_time:
             self.alert.show()
             self._reset_snooze(self.alarm_time)
             self._update_expiration_time()
             return True
-        elif self.is_snoozing and t > self.snooze_time:
+        elif self.is_snoozing and wallclock.datetime > self.snooze_time:
             self.alert.show()
             self._reset_snooze(self.snooze_time)
             return True
@@ -174,7 +176,7 @@ class AlarmDialog(Gtk.Dialog):
             name = alarm.name
             days = alarm.days
         else:
-            t = time.localtime()
+            t = wallclock.localtime
             h = t.tm_hour
             m = t.tm_min
             name = _("New Alarm")
@@ -412,7 +414,7 @@ class Alarm(Clock):
         self.standalone = AlarmStandalone(self)
         self.notebook.append_page(self.standalone, None)
 
-        self.timeout_id = GLib.timeout_add(1000, self._check_alarms)
+        wallclock.connect("time-changed", self._check_alarms)
 
     def _thumb_data_func(self, view, cell, store, i, data):
         alarm = store.get_value(i, 2)
@@ -439,7 +441,7 @@ class Alarm(Clock):
     def alarm_ringing(self):
         self.set_mode(Clock.Mode.STANDALONE)
 
-    def _check_alarms(self):
+    def _check_alarms(self, *args):
         for a in self.alarms:
             if a.check_expired():
                 self.standalone.set_alarm(a, True)
diff --git a/gnomeclocks/utils.py b/gnomeclocks/utils.py
index 6512f31..39716ae 100644
--- a/gnomeclocks/utils.py
+++ b/gnomeclocks/utils.py
@@ -21,7 +21,7 @@ import time
 import datetime
 import pycanberra
 from xdg import BaseDirectory
-from gi.repository import Gio, Notify
+from gi.repository import GObject, Gio, GnomeDesktop, Notify
 
 
 def N_(message): return message
@@ -133,6 +133,40 @@ class LocalizedWeekdays:
             return 0
 
 
+class WallClock(GObject.GObject):
+    _instance = None
+
+    def __init__(self):
+        if WallClock._instance:
+           raise TypeError("Initialized twice")
+        GObject.GObject.__init__(self)
+        self._wc = GnomeDesktop.WallClock()
+        self._wc.connect("notify::clock", self._on_notify_clock)
+        self._update()
+
+    def _on_notify_clock(self, *args):
+        self._update()
+        self.emit("time-changed")
+
+    def _update(self):
+        # provide various types/objects of the same time, to be used directly
+        # in AlarmItem and ClockItem, so they don't need to call these
+        # functions themselves all the time (they only care about minutes).
+        self.time = time.time()
+        self.localtime = time.localtime(self.time)
+        self.datetime = datetime.datetime.fromtimestamp(self.time)
+
+    @GObject.Signal
+    def time_changed(self):
+        pass
+
+    @staticmethod
+    def get_default():
+        if WallClock._instance is None:
+            WallClock._instance = WallClock()
+        return WallClock._instance
+
+
 class Alert:
     def __init__(self, soundid, msg):
         try:
diff --git a/gnomeclocks/world.py b/gnomeclocks/world.py
index 0fdbd7f..efb45f6 100644
--- a/gnomeclocks/world.py
+++ b/gnomeclocks/world.py
@@ -23,13 +23,14 @@ import json
 from gi.repository import GLib, GObject, Gio, Gdk, GdkPixbuf, Gtk
 from gi.repository import GWeather
 from clocks import Clock
-from utils import Dirs, TimeString
+from utils import Dirs, TimeString, WallClock
 from widgets import SelectableIconView, ContentView
 
 
 # keep the GWeather world around as a singletom, otherwise
 # if is garbage collected get_city_name etc fail.
 gweather_world = GWeather.Location.new_world(True)
+wallclock = WallClock.get_default()
 
 
 class WorldClockStorage:
@@ -138,11 +139,11 @@ class ClockItem:
 
         weather_timezone = self.location.get_timezone()
         timezone = GLib.TimeZone.new(weather_timezone.get_tzid())
-        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, time.time())
+        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, wallclock.time)
         location_offset = timezone.get_offset(i)
 
         timezone = GLib.TimeZone.new_local()
-        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, time.time())
+        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, wallclock.time)
         here_offset = timezone.get_offset(i)
 
         self.offset = location_offset - here_offset
@@ -167,14 +168,14 @@ class ClockItem:
 
     def _get_location_time(self, secs=None):
         if not secs:
-            secs = time.time()
+            secs = wallclock.time
         t = secs + self.offset
         t = time.localtime(t)
         return t
 
     def _get_day_string(self):
         clock_time_day = self.location_time.tm_yday
-        local_time_day = time.localtime().tm_yday
+        local_time_day = wallclock.localtime.tm_yday
 
         # if its 31st Dec here and 1st Jan there, clock_time_day = 1,
         # local_time_day = 365/366
@@ -210,6 +211,7 @@ class ClockItem:
         self.day_string = self._get_day_string()
         self._update_sunrise_sunset()
 
+
 class ClockStandalone(Gtk.EventBox):
     def __init__(self):
         Gtk.EventBox.__init__(self)
@@ -329,7 +331,7 @@ class World(Clock):
         self.standalone = ClockStandalone()
         self.notebook.append_page(self.standalone, None)
 
-        self.timeout_id = GLib.timeout_add(1000, self._update_clocks)
+        wallclock.connect("time-changed", self._update_clocks)
 
     def _thumb_data_func(self, view, cell, store, i, data):
         clock = store.get_value(i, 2)
@@ -352,7 +354,7 @@ class World(Clock):
         elif mode is Clock.Mode.SELECTION:
             self.iconview.set_selection_mode(True)
 
-    def _update_clocks(self):
+    def _update_clocks(self, *args):
         for c in self.clocks:
             c.update_time()
         self.iconview.queue_draw()



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