[hamster-applet] as things have become much more simpler, dispatcher is no more - using gobject signals



commit 10a985a3f20ee7d5a885398a5d69d1c2fb98ecac
Author: Toms Bauģis <toms baugis gmail com>
Date:   Wed Apr 14 21:02:10 2010 +0100

    as things have become much more simpler, dispatcher is no more - using gobject signals

 src/hamster-standalone               |   20 +++++----------
 src/hamster/Makefile.am              |    1 -
 src/hamster/applet.py                |   40 +++++++++++++++------------------
 src/hamster/client.py                |   17 ++++++++++---
 src/hamster/configuration.py         |   20 +++++++++-------
 src/hamster/dispatcher.py            |   41 ----------------------------------
 src/hamster/edit_activity.py         |    5 ----
 src/hamster/idle.py                  |   16 +++++++-----
 src/hamster/overview.py              |   14 ++++-------
 src/hamster/preferences.py           |    2 +-
 src/hamster/stats.py                 |    9 ++-----
 src/hamster/widgets/activityentry.py |    9 +------
 src/hamster/widgets/tags.py          |   11 +-------
 13 files changed, 70 insertions(+), 135 deletions(-)
---
diff --git a/src/hamster-standalone b/src/hamster-standalone
index 6830968..0a4dd3e 100755
--- a/src/hamster-standalone
+++ b/src/hamster-standalone
@@ -85,8 +85,8 @@ class ProjectHamster(object):
         try:
             dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
             # Set up connection to the screensaver
-            self.dbusIdleListener = idle.DbusIdleListener(runtime.dispatcher)
-            runtime.dispatcher.add_handler('active_changed', self.on_idle_changed)
+            self.dbusIdleListener = idle.DbusIdleListener()
+            self.dbusIdleListener.connect('idle-changed', self.on_idle_changed)
 
         except dbus.DBusException, e:
             logging.error("Can't init dbus: %s" % e)
@@ -100,7 +100,7 @@ class ProjectHamster(object):
         self.hotkey = conf.get("keybinding")
         self.bind_hotkey()
 
-        runtime.dispatcher.add_handler('conf_changed', self.on_conf_changed)
+        conf.connect('conf-changed', self.on_conf_changed)
 
         # Load today's data, activities and set label
         self.last_activity = None
@@ -109,8 +109,8 @@ class ProjectHamster(object):
         # refresh hamster every 60 seconds to update duration
         gobject.timeout_add_seconds(60, self.refresh_hamster)
 
-        runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
-        runtime.dispatcher.add_handler('day_updated', self.after_fact_update)
+        runtime.storage.connect('activities-changed',self.after_activity_update)
+        runtime.storage.connect('facts-changed',self.after_fact_update)
 
         self.screen = None
         if self.workspace_tracking:
@@ -308,7 +308,6 @@ class ProjectHamster(object):
                                      ", ".join(fact["tags"]),
                                      category_name = fact["category"],
                                      description = fact["description"])
-            runtime.dispatcher.dispatch('panel_visible', False)
 
 
     """button events"""
@@ -325,7 +324,6 @@ class ProjectHamster(object):
         dialogs.stats.show(self.window)
 
     def on_menu_preferences_activate(self, menu_item):
-        runtime.dispatcher.dispatch('panel_visible', False)
         dialogs.prefs.show(self.window)
 
     def on_menu_help_contents_activate(self, *args):
@@ -336,7 +334,7 @@ class ProjectHamster(object):
         self.new_name.refresh_activities()
         self.load_day()
 
-    def after_fact_update(self, event, date):
+    def after_fact_update(self, event):
         self.load_day()
 
     def on_idle_changed(self, event, state):
@@ -426,9 +424,7 @@ class ProjectHamster(object):
             self.notification.show()
 
     """global shortcuts"""
-    def on_conf_changed(self, event, data):
-        key, value = data
-
+    def on_conf_changed(self, event, key, value):
         if key == "enable_timeout":
             self.timeout_enabled = value
         elif key == "notify_on_idle":
@@ -467,12 +463,10 @@ class ProjectHamster(object):
                                  self.new_tags.get_text().decode("utf8", "replace"))
         self.new_name.set_text("")
         self.new_tags.set_text("")
-        runtime.dispatcher.dispatch('panel_visible', False)
 
     def on_stop_tracking_clicked(self, widget):
         runtime.storage.stop_tracking()
         self.last_activity = None
-        runtime.dispatcher.dispatch('panel_visible', False)
 
     def on_window_configure_event(self, window, event):
         self.treeview.fix_row_heights()
diff --git a/src/hamster/Makefile.am b/src/hamster/Makefile.am
index acfeec9..f6c107f 100644
--- a/src/hamster/Makefile.am
+++ b/src/hamster/Makefile.am
@@ -7,7 +7,6 @@ CPPFLAGS = \
 hamsterdir = $(pyexecdir)/hamster
 hamster_PYTHON = \
 	defs.py \
-	dispatcher.py \
 	storage.py \
 	db.py \
 	eds.py \
diff --git a/src/hamster/applet.py b/src/hamster/applet.py
index f9bb5cb..310db03 100755
--- a/src/hamster/applet.py
+++ b/src/hamster/applet.py
@@ -198,7 +198,7 @@ class HamsterApplet(object):
         self._gui = stuff.load_ui_file("applet.ui")
         self.window = self._gui.get_object('hamster-window')
         # on close don't destroy the popup, just hide it instead
-        self.window.connect("delete_event", lambda *args: self.__show_toggle(None, False))
+        self.window.connect("delete_event", lambda *args: self.__show_toggle(False))
 
         self.new_name = widgets.ActivityEntry()
         self.new_name.connect("value-entered", self.on_switch_activity_clicked)
@@ -225,8 +225,8 @@ class HamsterApplet(object):
         try:
             dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
             # Set up connection to the screensaver
-            self.dbusIdleListener = idle.DbusIdleListener(runtime.dispatcher)
-            runtime.dispatcher.add_handler('active_changed', self.on_idle_changed)
+            self.dbusIdleListener = idle.DbusIdleListener()
+            self.dbusIdleListener.connect('idle-changed', self.on_idle_changed)
 
         except dbus.DBusException, e:
             logging.error("Can't init dbus: %s" % e)
@@ -240,7 +240,7 @@ class HamsterApplet(object):
         self.hotkey = conf.get("keybinding")
         self.bind_hotkey()
 
-        runtime.dispatcher.add_handler('conf_changed', self.on_conf_changed)
+        conf.connect('conf-changed', self.on_conf_changed)
 
         # Load today's data, activities and set label
         self.last_activity = None
@@ -250,10 +250,8 @@ class HamsterApplet(object):
 
         # refresh hamster every 60 seconds to update duration
         gobject.timeout_add_seconds(60, self.refresh_hamster)
-
-        runtime.dispatcher.add_handler('panel_visible', self.__show_toggle)
-        runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
-        runtime.dispatcher.add_handler('day_updated', self.after_fact_update)
+        runtime.storage.connect('activities-changed',self.after_activity_update)
+        runtime.storage.connect('facts-changed',self.after_fact_update)
 
         self.screen = None
         if self.workspace_tracking:
@@ -343,7 +341,7 @@ class HamsterApplet(object):
         dialogs.edit.show(self.applet, activity_id = self.last_activity['id'])
 
     def switch_cb(self, n, action):
-        self.__show_toggle(None, not self.button.get_active())
+        self.__show_toggle(not self.button.get_active())
 
 
     def load_day(self):
@@ -441,7 +439,7 @@ class HamsterApplet(object):
         fact = self.treeview.get_selected_fact()
         runtime.storage.remove_fact(fact["id"])
 
-    def __show_toggle(self, event, is_active):
+    def __show_toggle(self, is_active):
         """main window display and positioning"""
         self.button.set_active(is_active)
 
@@ -504,7 +502,7 @@ class HamsterApplet(object):
 
     """events"""
     def on_toggle(self, widget):
-        self.__show_toggle(None, self.button.get_active())
+        self.__show_toggle(self.button.get_active())
 
 
     def on_todays_keys(self, tree, event):
@@ -526,7 +524,7 @@ class HamsterApplet(object):
                                      ", ".join(fact["tags"]),
                                      category_name = fact["category"],
                                      description = fact["description"])
-            runtime.dispatcher.dispatch('panel_visible', False)
+            self.__show_toggle(False)
 
 
     def on_windows_keys(self, tree, event_key):
@@ -535,13 +533,13 @@ class HamsterApplet(object):
               and event_key.state & gtk.gdk.CONTROL_MASK)):
             if self.new_name.popup.get_property("visible") == False \
                and self.new_tags.popup.get_property("visible") == False:
-                runtime.dispatcher.dispatch('panel_visible', False)
+                self.__show_toggle(False)
                 return True
         return False
 
     """button events"""
     def on_overview(self, menu_item):
-        runtime.dispatcher.dispatch('panel_visible', False)
+        self.__show_toggle(False)
         dialogs.overview.show(self.applet)
 
     def show_overview(self, menu_item, verb):
@@ -554,7 +552,7 @@ class HamsterApplet(object):
         dialogs.about.show()
 
     def show_preferences(self, menu_item, verb):
-        runtime.dispatcher.dispatch('panel_visible', False)
+        self.__show_toggle(False)
         dialogs.prefs.show(self.applet)
 
 
@@ -564,7 +562,7 @@ class HamsterApplet(object):
         self.load_day()
         self.update_label()
 
-    def after_fact_update(self, event, date):
+    def after_fact_update(self, event):
         self.load_day()
         self.update_label()
 
@@ -656,12 +654,10 @@ class HamsterApplet(object):
 
     """global shortcuts"""
     def on_keybinding_activated(self):
-        self.__show_toggle(None, not self.button.get_active())
-
+        self.__show_toggle(not self.button.get_active())
 
-    def on_conf_changed(self, event, data):
-        key, value = data
 
+    def on_conf_changed(self, event, key, value):
         if key == "enable_timeout":
             self.timeout_enabled = value
         elif key == "notify_on_idle":
@@ -701,12 +697,12 @@ class HamsterApplet(object):
                                  self.new_tags.get_text().decode("utf8", "replace"))
         self.new_name.set_text("")
         self.new_tags.set_text("")
-        runtime.dispatcher.dispatch('panel_visible', False)
+        self.__show_toggle(False)
 
     def on_stop_tracking_clicked(self, widget):
         runtime.storage.stop_tracking()
         self.last_activity = None
-        runtime.dispatcher.dispatch('panel_visible', False)
+        self.__show_toggle(False)
 
     def on_window_size_request(self, window, event):
         box = self.window.get_allocation()
diff --git a/src/hamster/client.py b/src/hamster/client.py
index 386f3fd..ecd7377 100644
--- a/src/hamster/client.py
+++ b/src/hamster/client.py
@@ -22,6 +22,7 @@
 import datetime as dt
 from calendar import timegm
 import dbus, dbus.mainloop.glib
+import gobject
 
 def debus(value):
     """recasts dbus types to the basic ones. should be quite an overhead"""
@@ -57,8 +58,16 @@ def from_dbus_fact(fact):
                )
 
 
-class Storage(object):
+class Storage(gobject.GObject):
+    __gsignals__ = {
+        "tags-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
+        "facts-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
+        "activities-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
+    }
+
     def __init__(self, parent = None):
+        gobject.GObject.__init__(self)
+
         self.parent = parent
 
         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
@@ -75,13 +84,13 @@ class Storage(object):
 
 
     def on_tags_changed(self):
-        self.parent.dispatch('new_tags_added')
+        self.emit("tags-changed")
 
     def on_facts_changed(self):
-        self.parent.dispatch('day_updated')
+        self.emit("facts-changed")
 
     def on_activities_changed(self):
-        self.parent.dispatch('activity_updated')
+        self.emit("activities-changed")
 
 
     def get_todays_facts(self):
diff --git a/src/hamster/configuration.py b/src/hamster/configuration.py
index 04728dd..59784cf 100644
--- a/src/hamster/configuration.py
+++ b/src/hamster/configuration.py
@@ -27,10 +27,10 @@ import gettext
 import os
 import defs
 from client import Storage
-from dispatcher import Dispatcher
 from xdg.BaseDirectory import xdg_data_home
 import logging
 import datetime as dt
+import gobject
 
 import logging
 log = logging.getLogger("configuration")
@@ -49,7 +49,6 @@ class RuntimeStore(Singleton):
     database_file = None
     last_etag = None
     data_dir = ""
-    dispatcher = None
     storage = None
     conf = None
 
@@ -63,8 +62,7 @@ class RuntimeStore(Singleton):
         else:
             data_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..', 'data'))
         self.data_dir = data_dir
-        self.dispatcher = Dispatcher()
-        self.storage = Storage(self.dispatcher)
+        self.storage = Storage()
 
 
         # figure out the correct database file
@@ -155,7 +153,7 @@ dialogs = Dialogs()
 
 
 
-class GConfStore(Singleton):
+class GConfStore(gobject.GObject, Singleton):
     """
     Settings implementation which stores settings in GConf
     Snatched from the conduit project (http://live.gnome.org/Conduit)
@@ -177,10 +175,14 @@ class GConfStore(Singleton):
         'standalone_window_maximized' :   False,          # Is overview window maximized
     }
 
+    __gsignals__ = {
+        "conf-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT))
+    }
     def __init__(self):
-        self._client = gconf.client_get_default()
-        self._client.add_dir(self.GCONF_DIR[:-1], gconf.CLIENT_PRELOAD_RECURSIVE)
-        self._notifications = []
+          gobject.GObject.__init__(self)
+          self._client = gconf.client_get_default()
+          self._client.add_dir(self.GCONF_DIR[:-1], gconf.CLIENT_PRELOAD_RECURSIVE)
+          self._notifications = []
 
     def _fix_key(self, key):
         """
@@ -203,7 +205,7 @@ class GConfStore(Singleton):
         key = self._fix_key(entry.key)[len(self.GCONF_DIR):]
         value = self._get_value(entry.value, self.DEFAULTS[key])
 
-        runtime.dispatcher.dispatch("conf_changed", (key, value))
+        self.emit('conf-changed', key, value)
 
 
     def _get_value(self, value, default):
diff --git a/src/hamster/edit_activity.py b/src/hamster/edit_activity.py
index d9a231e..653f8e8 100644
--- a/src/hamster/edit_activity.py
+++ b/src/hamster/edit_activity.py
@@ -215,11 +215,6 @@ class CustomFactController:
         else:
             runtime.storage.add_fact(activity, tags, start_time, end_time, description = description)
 
-
-        # hide panel only on add - on update user will want to see changes
-        if not self.fact_id:
-            runtime.dispatcher.dispatch('panel_visible', False)
-
         self.close_window()
 
     def on_activity_list_key_pressed(self, entry, event):
diff --git a/src/hamster/idle.py b/src/hamster/idle.py
index 1fbeefd..886cbcb 100644
--- a/src/hamster/idle.py
+++ b/src/hamster/idle.py
@@ -24,7 +24,7 @@ import gconf
 import datetime as dt
 import gobject
 
-class DbusIdleListener(object):
+class DbusIdleListener(gobject.GObject):
     """
     Listen for idleness coming from org.gnome.ScreenSaver
 
@@ -38,10 +38,13 @@ class DbusIdleListener(object):
     members coming from the org.gnome.ScreenSaver interface and the
     and is_screen_locked members are updated appropriately.
     """
-    def __init__(self, dispatcher):
-        self.screensaver_uri = "org.gnome.ScreenSaver"
+    __gsignals__ = {
+        "idle-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
+    }
+    def __init__(self):
+        gobject.GObject.__init__(self)
 
-        self.dispatcher = dispatcher
+        self.screensaver_uri = "org.gnome.ScreenSaver"
         self.screen_locked = False
         self.idle_from = None
         self.timeout_minutes = 0 # minutes after session is considered idle
@@ -106,7 +109,7 @@ class DbusIdleListener(object):
                 # SessionIdleChanged signal kicks in
                 def dispatch_active_changed(idle_state):
                     if not self.idle_was_there:
-                        self.dispatcher.dispatch('active_changed', idle_state)
+                        self.emit('idle-changed', idle_state)
                     self.idle_was_there = False
 
                 gobject.timeout_add_seconds(1, dispatch_active_changed, idle_state)
@@ -114,7 +117,7 @@ class DbusIdleListener(object):
             else:
                 # dispatch idle status change to interested parties
                 self.idle_was_there = True
-                self.dispatcher.dispatch('active_changed', idle_state)
+                self.emit('idle-changed', idle_state)
 
         elif member == "Lock":
             # in case of lock, lock signal will be sent first, followed by
@@ -135,4 +138,3 @@ class DbusIdleListener(object):
             # Only subtract idle time from the running task when
             # idleness is due to time out, not a screen lock.
             return self.idle_from - dt.timedelta(minutes = self.timeout_minutes)
-
diff --git a/src/hamster/overview.py b/src/hamster/overview.py
index cb811c6..cd9d9bc 100644
--- a/src/hamster/overview.py
+++ b/src/hamster/overview.py
@@ -101,9 +101,10 @@ class Overview(object):
         self.get_widget("by_day_box").add(self.timechart)
 
         self._gui.connect_signals(self)
-        runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
-        runtime.dispatcher.add_handler('day_updated', self.after_activity_update)
-        runtime.dispatcher.add_handler('conf_changed', self.on_conf_change)
+        runtime.storage.connect('activities-changed',self.after_activity_update)
+        runtime.storage.connect('facts-changed',self.after_activity_update)
+
+        conf.connect('conf-changed', self.on_conf_change)
 
         if conf.get("overview_window_maximized"):
             self.window.maximize()
@@ -180,8 +181,7 @@ class Overview(object):
         self.get_widget("range_title").set_text(self.title)
 
 
-    def on_conf_change(self, event, data):
-        key, value = data
+    def on_conf_change(self, event, key, value):
         if key == "day_start_minutes":
             self.day_start = dt.time(value / 60, value % 60)
             self.timechart.day_start = self.day_start
@@ -351,10 +351,6 @@ class Overview(object):
         self.close_window()
 
     def close_window(self):
-        runtime.dispatcher.del_handler('activity_updated', self.after_activity_update)
-        runtime.dispatcher.del_handler('day_updated', self.after_activity_update)
-        runtime.dispatcher.del_handler('conf_changed', self.on_conf_change)
-
         # properly saving window state and position
         maximized = self.window.get_window().get_state() & gtk.gdk.WINDOW_STATE_MAXIMIZED
         conf.set("overview_window_maximized", maximized)
diff --git a/src/hamster/preferences.py b/src/hamster/preferences.py
index 4106c4f..ea2a033 100755
--- a/src/hamster/preferences.py
+++ b/src/hamster/preferences.py
@@ -94,7 +94,7 @@ appearances = ["text", "icon", "both"]
 
 from configuration import runtime, conf
 import widgets
-import dispatcher, storage, stuff
+import storage, stuff
 
 class PreferencesEditor:
     TARGETS = [
diff --git a/src/hamster/stats.py b/src/hamster/stats.py
index 83a8b82..8f06309 100644
--- a/src/hamster/stats.py
+++ b/src/hamster/stats.py
@@ -53,8 +53,8 @@ class Stats(object):
         self.get_widget("explore_everything").add(self.timechart)
         self.get_widget("explore_everything").show_all()
 
-        runtime.dispatcher.add_handler('activity_updated', self.after_fact_update)
-        runtime.dispatcher.add_handler('day_updated', self.after_fact_update)
+        runtime.storage.connect('activities-changed',self.after_fact_update)
+        runtime.storage.connect('facts-changed',self.after_fact_update)
 
         self.init_stats()
 
@@ -406,7 +406,7 @@ than 15 minutes you seem to be a busy bee." % ("<b>%d</b>" % short_percent))
         self.stats(button.year)
 
 
-    def after_fact_update(self, event, date):
+    def after_fact_update(self, event):
         self.stat_facts = runtime.storage.get_facts(dt.date(1970, 1, 1), dt.date.today())
         self.stats()
 
@@ -424,9 +424,6 @@ than 15 minutes you seem to be a busy bee." % ("<b>%d</b>" % short_percent))
         self.close_window()
 
     def close_window(self):
-        runtime.dispatcher.del_handler('activity_updated', self.after_fact_update)
-        runtime.dispatcher.del_handler('day_updated', self.after_fact_update)
-
         if not self.parent:
             gtk.main_quit()
         else:
diff --git a/src/hamster/widgets/activityentry.py b/src/hamster/widgets/activityentry.py
index 42b70b8..50b2c8d 100644
--- a/src/hamster/widgets/activityentry.py
+++ b/src/hamster/widgets/activityentry.py
@@ -91,9 +91,8 @@ class ActivityEntry(gtk.Entry):
         self.connect("key-release-event", self._on_key_release_event)
         self.connect("focus-out-event", self._on_focus_out_event)
         self.connect("changed", self._on_text_changed)
-        self.connect("parent-set", self._on_parent_set)
 
-        runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
+        runtime.storage.connect('activities-changed',self.after_activity_update)
 
         self.show()
         self.populate_suggestions()
@@ -299,9 +298,3 @@ class ActivityEntry(gtk.Entry):
             self.emit("value-entered")
 
         self.news = False
-
-
-    def _on_parent_set(self, old_parent, user_data):
-        # when parent changes to itself, that means that it has been actually deleted
-        if old_parent and old_parent == self.get_toplevel():
-            runtime.dispatcher.del_handler('activity_updated', self.after_activity_update)
diff --git a/src/hamster/widgets/tags.py b/src/hamster/widgets/tags.py
index 16c9e3e..e1c5148 100644
--- a/src/hamster/widgets/tags.py
+++ b/src/hamster/widgets/tags.py
@@ -57,13 +57,12 @@ class TagsEntry(gtk.Entry):
         self.connect("key-press-event", self._on_key_press_event)
         self.connect("key-release-event", self._on_key_release_event)
         self.connect("focus-out-event", self._on_focus_out_event)
-        self.connect("parent-set", self._on_parent_set)
 
-        runtime.dispatcher.add_handler('new_tags_added', self.refresh_tags)
+        runtime.storage.connect('tags-changed', self.refresh_tags)
         self.show()
         self.populate_suggestions()
 
-    def refresh_tags(self, event, data):
+    def refresh_tags(self, event):
         self.tags = None
 
     def get_tags(self):
@@ -206,11 +205,6 @@ class TagsEntry(gtk.Entry):
 
         return False
 
-    def _on_parent_set(self, old_parent, user_data):
-        # when parent changes to itself, that means that it has been actually deleted
-        if old_parent and old_parent == self.get_toplevel():
-            runtime.dispatcher.del_handler('new_tags_added', self.refresh_tags)
-
 
 class TagBox(graphics.Scene):
     __gsignals__ = {
@@ -329,4 +323,3 @@ class Tag(graphics.Sprite):
         self.graphics.set_color((0,0,0), 0)
         self.graphics.rectangle(0, 0, w, h)
         self.graphics.stroke()
-



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