Implementing DBus.
authorHuang Peng <shawn.p.huang@gmail.com>
Sun, 21 Sep 2008 09:32:14 +0000 (17:32 +0800)
committerHuang Peng <shawn.p.huang@gmail.com>
Sun, 21 Sep 2008 09:32:14 +0000 (17:32 +0800)
daemon/_dbus.py
daemon/bus.py
daemon/ibusdaemon.py

index 1b36627..63ab8e8 100644 (file)
@@ -23,6 +23,57 @@ import dbus
 import dbus.service
 import ibus
 
+class DBusReal(ibus.Object):
+    def __init__(self):
+        super(DBusReal, self).__init__()
+        self.__id_dict = dict()
+        self.__conn_dict = dict()
+        self.__id = 0
+
+    def register_connection(self, ibusconn):
+        if ibusconn in self.__conn_dict:
+            return self.__conn_dict[ibusconn]
+        self.__id += 1
+        id = "%d" % self.__id
+        self.__id_dict[id] = ibusconn
+        self.__conn_dict[ibusconn] = id
+        ibusconn.connect("destroy", self.__ibusconn_destroy_cb, id)
+        return id
+
+    def list_names(self):
+        return self.__id_dict.keys()
+
+    def list_activatable_names(self):
+        return self.__id_dict.keys()
+
+    def name_has_owner(self, name):
+        return name in self.__id_dict
+
+    def get_name_owner(self, name):
+        if name == dbus.BUS_DAEMON_NAME:
+            return dbus.BUS_DAEMON_NAME
+        elif name == ibus.IBUS_NAME:
+            return ibus.IBUS_NAME
+
+        raise dbus.DBusException(
+                "org.freedesktop.DBus.Error.NameHasNoOwner: Could not get owner of name '%s': no such name" % name)
+
+    def start_service_by_name(self, name, flags):
+        return 0
+
+    def get_connection_unix_user(self, connection_name):
+        return 1
+
+    def add_match(self, rule):
+        pass
+
+    def remove_match(self, rule):
+        pass
+
+    def __ibusconn_destroy_cb(self, ibusconn, id):
+        del self.__id_dict[id]
+        del self.__conn_dict[ibusconn]
+
 class DBus(dbus.service.Object):
 
     method = lambda **args: \
@@ -32,56 +83,55 @@ class DBus(dbus.service.Object):
     signal = lambda **args: \
         dbus.service.signal(dbus_interface = dbus.BUS_DAEMON_IFACE, \
         **args)
-    __id = 0
 
-    def __init__(self, conn):
-        super(DBus, self).__init__(conn, dbus.BUS_DAEMON_PATH)
+    __bus = DBusReal()
+
+    def __init__(self, ibusconn):
+        super(DBus, self).__init__(ibusconn.get_dbusconn(), dbus.BUS_DAEMON_PATH)
+        self.__ibusconn = ibusconn
+        self.__name = ""
 
     @method(out_signature="s")
     def Hello(self):
-        DBus.__id += 1
-        return "%d" % DBus.__id
+        if self.__name == None:
+            self.__name = DBus.__bus.register_connection(self.__ibusconn)
+        return self.__name
 
     @method(out_signature="as")
     def ListNames(self):
-        return []
+        return DBus.__bus.list_names()
 
     @method(out_signature="as")
     def ListActivatableNames(self):
-        return []
+        return DBus.__bus.list_activatable_names()
 
     @method(in_signature="s", out_signature="as")
     def NameHasOwner(self, name):
-        return []
+        return DBus.__bus.name_has_owner(name)
 
     @method(in_signature="si", out_signature="i")
     def StartServiceByName(self, name, flags):
-        pass
+        return DBus.__bus.start_service_by_name(name, flags)
 
     @method(in_signature="s", out_signature="s")
     def GetNameOwner(self, name):
-        if name == dbus.BUS_DAEMON_NAME:
-            return dbus.BUS_DAEMON_NAME
-        elif name == ibus.IBUS_NAME:
-            return ibus.IBUS_NAME
+        return DBus.__bus.get_name_owner(name)
 
-        raise dbus.DBusException(
-                "org.freedesktop.DBus.Error.NameHasNoOwner: Could not get owner of name '%s': no such name" % name)
     @method(in_signature="s", out_signature="i")
     def GetConnectionUnixUser(self, connection_name):
-        pass
+        return DBus.__bus.get_connection_unix_user(connection_name)
 
     @method(in_signature="s")
     def AddMatch(self, rule):
-        pass
+        return  DBus.__bus.add_match(rule)
 
     @method(in_signature="s")
     def RemoveMatch(self, rule):
-        pass
+        return  DBus.__bus.remove_match(rule)
 
     @method(out_signature="s")
     def GetId(self):
-        pass
+        return self.__name
 
     @signal(signature="sss")
     def NameOwnerChanged(self, name, old_owner, new_owner):
index 8df7f04..d7a6eca 100644 (file)
@@ -24,7 +24,6 @@ import dbus
 import ibus
 from ibus import keysyms
 from ibus import modifier
-from connection import Connection
 from contextmanager import ContextManager
 from factorymanager import FactoryManager
 from panel import Panel, DummyPanel
@@ -93,8 +92,7 @@ class IBus(ibus.Object):
                 shortcuts.append((keyval, keymask))
         return shortcuts
 
-    def new_connection(self, dbusconn):
-        conn = Connection(dbusconn)
+    def new_connection(self, conn):
         IBusProxy(self, conn)
         conn.connect("destroy", self.__conn_destroy_cb)
         self.__connections.append(conn)
index 1d670f8..421cca3 100644 (file)
@@ -29,6 +29,7 @@ import dbus.mainloop.glib
 import ibus
 from _dbus import DBus
 from bus import IBus
+from connection import Connection
 
 
 class IBusServer(dbus.server.Server):
@@ -54,8 +55,9 @@ class IBusServer(dbus.server.Server):
                 pass
 
     def connection_added(self, dbusconn):
-        self.__ibus.new_connection(dbusconn)
-        DBus(dbusconn)
+        conn = Connection(dbusconn)
+        self.__ibus.new_connection(conn)
+        DBus(conn)
 
     def connection_removed(self, dbusconn):
         # do nothing.