[clocks] added storage of world clocks



commit 4ad9aa14da68d4a2209430f53f74acf4b0aa1357
Author: Seif Lotfy <seif lotfy collabora co uk>
Date:   Mon Jun 11 01:25:14 2012 +0200

    added storage of world clocks

 clocks.py  |   19 ++++++++++++-------
 storage.py |   42 +++++++++++++++++++++++++++++++++++++++---
 widgets.py |   16 ++++++++++------
 3 files changed, 61 insertions(+), 16 deletions(-)
---
diff --git a/clocks.py b/clocks.py
index a20e030..547bceb 100644
--- a/clocks.py
+++ b/clocks.py
@@ -119,15 +119,20 @@ class World (Clock):
     def load_clocks(self):
         self.clocks = worldclockstorage.load_clocks ()
         for clock in self.clocks:
-            self.add_clock (clock)
+            d = DigitalClock (clock)
+            view_iter = self.liststore.append([d.drawing.pixbuf, "<b>"+d.location.get_city_name()+"</b>", d])
+            d.set_iter(self.liststore, view_iter)
+            self.show_all()
 
     def add_clock(self, location):
-        d = DigitalClock(location)
-        self.clocks.append(d)
-        view_iter = self.liststore.append([d.drawing.pixbuf, "<b>"+d.location.get_city_name()+"</b>", d])
-        d.set_iter(self.liststore, view_iter)
-        self.show_all()
-        worldclockstorage.save_clocks (location)
+        location_id = location.id + "---" + location.location.get_code ()
+        if not location_id in worldclockstorage.locations_dump:
+            d = DigitalClock(location)
+            self.clocks.append(location)
+            view_iter = self.liststore.append([d.drawing.pixbuf, "<b>"+d.location.get_city_name()+"</b>", d])
+            d.set_iter(self.liststore, view_iter)
+            self.show_all()
+        worldclockstorage.save_clocks (self.clocks)
 
     def open_new_dialog(self):
         parent = self.get_parent().get_parent().get_parent()
diff --git a/storage.py b/storage.py
index 7a27949..1da6dac 100644
--- a/storage.py
+++ b/storage.py
@@ -21,17 +21,53 @@
 import os
 import pickle
 from xdg import BaseDirectory
+from gi.repository import GWeather
 
 DATA_PATH = BaseDirectory.save_data_path("clocks") + "/clocks"
 
+class Location ():
+    def __init__ (self, location):
+        self._id = location.get_city_name ()
+        self._location = location
+
+    @property
+    def id(self):
+        return self._id
+
+    @property
+    def location(self):
+        return self._location
+
 class WorldClockStorage ():
     def __init__ (self):
+        world = GWeather.Location.new_world(True)
+        self.searchEntry = GWeather.LocationEntry.new(world)
+        self.searchEntry.show_all ()
+        self.locations_dump = ""
         pass
 
-    def save_clocks (self, clocks):
-        pass
+    def save_clocks (self, locations):
+        self.locations_dump = locations = "|".join([location.id + "---" + location.location.get_code () for location in locations])
+        f = open (DATA_PATH, "wb")
+        pickle.dump (locations, f)
+        f.close ()
 
     def load_clocks (self):
-        return []
+        try:
+          f = open (DATA_PATH, "rb")
+          self.locations_dump = locations = pickle.load (f)
+          f.close ()
+          locations = locations.split ("|")
+          clocks = []
+          for location in locations:
+              loc = location.split ("---")
+              if self.searchEntry.set_city (loc[0], loc[1]):
+                  loc = self.searchEntry.get_location ()
+                  loc = Location (self.searchEntry.get_location ())
+                  clocks.append (loc)
+          return clocks
+        except Exception, e:
+          print "--", e
+          return []
 
 worldclockstorage = WorldClockStorage ()
diff --git a/widgets.py b/widgets.py
index 12deb46..4d2f66a 100644
--- a/widgets.py
+++ b/widgets.py
@@ -20,8 +20,10 @@
 
 from gi.repository import Gtk, GObject, Gio, PangoCairo, Pango, GWeather
 from gi.repository import Gdk, GdkPixbuf
+from storage import Location
 import cairo, time
 
+
 class NewWorldClockWidget (Gtk.Dialog):
 
     __gsignals__ = {'add-clock': (GObject.SignalFlags.RUN_LAST,
@@ -67,13 +69,14 @@ class NewWorldClockWidget (Gtk.Dialog):
         self.show_all ()
 
     def _on_response_clicked (self, widget, response_id):
-      if response_id == 1:
-        location = self.searchEntry.get_location()
-        self.emit("add-clock", location)
-      self.destroy ()
+        if response_id == 1:
+            location = self.searchEntry.get_location()
+            location = Location (location)
+            self.emit("add-clock", location)
+        self.destroy ()
 
     def _set_city (self, widget):
-        location = widget.get_location()
+        location = self.searchEntry.get_location()
         widget = self.get_widget_for_response (1)
         if location:
             widget.set_sensitive(True)
@@ -85,7 +88,8 @@ class NewWorldClockWidget (Gtk.Dialog):
 
 class DigitalClock ():
     def __init__(self, location):
-        self.location = location
+        self.location = location.location
+        self.id = location.id
         self.timezone = self.location.get_timezone()
         self.offset = self.timezone.get_offset() * 60
         self.isDay = None



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