Add Config{Add,Remove}Watch methods.
authorHuang Peng <shawn.p.huang@gmail.com>
Thu, 31 Jul 2008 08:12:01 +0000 (16:12 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Thu, 31 Jul 2008 08:12:01 +0000 (16:12 +0800)
daemon/bus.py
daemon/panel.py
gconf/config.py
ibus/bus.py
ibus/interface/iibus.py

index 87ffeab..42ba787 100644 (file)
@@ -35,9 +35,17 @@ class IBus(ibus.Object):
         super(IBus, self).__init__()
         self.__context_manager = ContextManager()
         self.__factory_manager = FactoryManager()
+
         self.__panel = DummyPanel()
+        self.__panel_handlers = list()
+        self.__install_panel_handlers()
+
         self.__config = DefaultConfig()
+        self.__config_handlers = list()
+        self.__install_config_handlers()
+
         self.__register = Register()
+
         self.__config_watch = dict()
 
         self.__focused_context = None
@@ -257,17 +265,32 @@ class IBus(ibus.Object):
     def register_panel(self, object_path, replace, conn):
         if not isinstance(self.__panel, DummyPanel) and replace == False:
             raise ibus.Exception("has have a panel!")
-        if not isinstance(self.__panel, DummyPanel):
-            self.__panel.destroy()
+        self.__uninstall_panel_handlers()
+        self.__panel.destroy()
+
         self.__panel = Panel(conn, object_path)
-        self.__panel.connect("page-up", self.__panel_page_up_cb)
-        self.__panel.connect("page-down", self.__panel_page_down_cb)
-        self.__panel.connect("cursor-up", self.__panel_cursor_up_cb)
-        self.__panel.connect("cursor-down", self.__panel_cursor_down_cb)
-        self.__panel.connect("property-activate", self.__panel_property_active_cb)
-        self.__panel.connect("property-show", self.__panel_property_show_cb)
-        self.__panel.connect("property-hide", self.__panel_property_hide_cb)
-        self.__panel.connect("destroy", self.__panel_destroy_cb)
+        self.__install_panel_handlers()
+
+    def __install_panel_handlers(self):
+        signals = (
+            ("page-up", self.__panel_page_up_cb),
+            ("page-down", self.__panel_page_down_cb),
+            ("cursor-up", self.__panel_cursor_up_cb),
+            ("cursor-down", self.__panel_cursor_down_cb),
+            ("property-activate", self.__panel_property_active_cb),
+            ("property-show", self.__panel_property_show_cb),
+            ("property-hide", self.__panel_property_hide_cb),
+            ("destroy", self.__panel_destroy_cb)
+        )
+
+        for signal, handler in signals:
+            id = self.__panel.connect(signal, handler)
+            self.__panel_handlers.append(id)
+
+    def __uninstall_panel_handlers(self):
+        map(lambda id:self.__panel.disconnect(id), self.__panel_handlers)
+        self.__panel_handlers = list()
+
 
     def __panel_page_up_cb(self, panel):
         assert panel == self.__panel
@@ -314,21 +337,33 @@ class IBus(ibus.Object):
     def register_config(self, object_path, replace, conn):
         if not isinstance(self.__config, DefaultConfig) and replace == False:
             raise ibus.Exception("has have a config!")
-        if not isinstance(self.__config, DefaultConfig):
-            self.__config.destroy()
+        self.__uninstall_config_handlers()
+        self.__config.destroy()
         self.__config = Config(conn, object_path)
-        self.__config.connect("value-changed", self.__config_value_changed_cb)
-        self.__config.connect("destroy", self.__config_destroy_cb)
+        self.__install_config_handlers()
         for conn in self.__connections:
             conn.emit_dbus_signal("ConfigReload")
 
+    def __install_config_handlers(self):
+        signals = (
+            ("value-changed", self.__config_value_changed_cb),
+            ("destroy", self.__config_destroy_cb)
+        )
+        for signal, handler in signals:
+            id = self.__config.connect(signal, handler)
+            self.__config_handlers.append(id)
+
+    def __uninstall_config_handlers(self):
+        map(lambda id:self.__config.disconnect(id), self.__config_handlers)
+        self.__config_handlers = list()
+
     def config_set_value(self, key, value, conn, **kargs):
         return self.__config.set_value(key, value, **kargs)
 
     def config_get_value(self, key, conn, **kargs):
         return self.__config.get_value(key, **kargs)
 
-    def config_add_watch_dir(self, dir, conn, **kargs):
+    def config_add_watch(self, dir, conn):
         if not dir.endswith("/"):
             dir += "/"
 
@@ -337,7 +372,7 @@ class IBus(ibus.Object):
                 self.__config_watch[dir] = set()
             self.__config_watch[dir].add(conn)
 
-    def config_remove_watch_dir(self, dir, conn, **kargs):
+    def config_remove_watch(self, dir, conn):
         if not dir.endswith("/"):
             dir += "/"
 
@@ -347,8 +382,8 @@ class IBus(ibus.Object):
 
     def __config_value_changed_cb(self, config, key, value):
         for dir in self.__config_watch.keys():
-            if dir.startswith(key):
-                for conn in self.__config[dir]:
+            if key.startswith(dir):
+                for conn in self.__config_watch[dir]:
                     conn.emit_dbus_signal("ConfigValueChanged", key, value)
 
     def __config_destroy_cb(self, config):
@@ -468,6 +503,12 @@ class IBusProxy(ibus.IIBus):
     def GetInputContextStates(self, ic, dbusconn):
         return self.__ibus.get_input_context_states(ic, self.__conn)
 
+    def ConfigAddWatch(self, key, dbusconn):
+        return self.__ibus.config_add_watch(key, self.__conn)
+
+    def ConfigRemoveWatch(self, key, dbusconn):
+        return self.__ibus.config_remove_watch(key, self.__conn)
+
     def ConfigSetValue(self, key, value, dbusconn, reply_cb, error_cb):
         self.__ibus.config_set_value(key, value, self.__conn,
                 reply_handler = reply_cb,
index 082093c..fdee8fe 100644 (file)
@@ -208,6 +208,14 @@ class DummyPanel(ibus.Object):
             gobject.SIGNAL_RUN_FIRST,
             gobject.TYPE_NONE,
             (gobject.TYPE_STRING, )),
+        "property-show" : (
+            gobject.SIGNAL_RUN_FIRST,
+            gobject.TYPE_NONE,
+            (gobject.TYPE_STRING, )),
+        "property-hide" : (
+            gobject.SIGNAL_RUN_FIRST,
+            gobject.TYPE_NONE,
+            (gobject.TYPE_STRING, )),
     }
 
     def set_cursor_location(self, x, y, w, h):
index 10e4917..05bd510 100644 (file)
@@ -51,6 +51,8 @@ class Config(ibus.Object):
             key = "/" + key
         key = GCONF_IBUS_PATH + key
         value = self.__client.get(key)
+        if value == None:
+            raise ibus.IBusException("key = \"%s\" does not exist" % key)
         return self.__to_py_value(value)
 
     def set_value(self, key, value):
@@ -69,8 +71,6 @@ class Config(ibus.Object):
             self.__client = None
 
     def __to_py_value(self, value):
-        if value == None:
-            return value
         if value.type == gconf.VALUE_STRING:
             return value.get_string()
         if value.type == gconf.VALUE_INT:
@@ -120,7 +120,7 @@ class Config(ibus.Object):
     def __value_changed_cb(self, gconf, key, value):
         value = self.__client.get(key)
         value = self.__to_py_value(value)
-        self.emit("value-changed", key, value)
+        self.emit("value-changed", key.replace(GCONF_IBUS_PATH, ""), value)
 
 gobject.type_register(Config)
 
index 382755e..edacdb8 100644 (file)
@@ -38,7 +38,7 @@ class Bus(ibus.Object):
             gobject.TYPE_NONE,
             (gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
         ),
-        "config-relaoded" : (
+        "config-reloaded" : (
             gobject.SIGNAL_RUN_FIRST,
             gobject.TYPE_NONE,
             ()
@@ -125,11 +125,20 @@ class Bus(ibus.Object):
     def get_input_context_states(self, ic):
         return self.__bus.GetInputContextStates(ic)
 
+    def config_add_watch(self, key):
+        return self.__bus.ConfigAddWatch(key)
+
+    def config_remove_watch(self, key):
+        return self.__bus.ConfigRemoveWatch(key)
+
     def config_set_value(self, key, value):
         return self.__bus.ConfigSetValue(key, value)
 
-    def config_get_value(self, key):
-        return self.__bus.ConfigGetValue(key)
+    def config_get_value(self, key, default_value = None):
+        try:
+            return self.__bus.ConfigGetValue(key)
+        except Exception, e:
+            return default_value
 
     def register_list_engines(self):
         return self.__bus.RegisterListEngines()
index 6a406ac..0ed01f7 100644 (file)
@@ -98,6 +98,12 @@ class IIBus(dbus.service.Object):
     @method(in_signature = "s", out_signature = "sb")
     def GetInputContextStates(self, ic, dbusconn): pass
 
+    @method(in_signature = "s")
+    def ConfigAddWatch(self, key, dbusconn): pass
+
+    @method(in_signature = "s")
+    def ConfigRemoveWatch(self, key, dbusconn): pass
+
     @async_method(in_signature = "sv")
     def ConfigSetValue(self, key, value, dbusconn, reply_cb, error_cb): pass