Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/util
diff options
context:
space:
mode:
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2008-10-21 12:28:52 (GMT)
committer Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>2008-10-21 13:45:27 (GMT)
commit44afcfca952ff2bb90cd5771bce2da7c2063a082 (patch)
treedc525077928de625e2b63ce6604f71db2bc0edb9 /src/jarabe/util
parent674fb47bbaec3d11b0fc833dde70b26b361b5d91 (diff)
create jarabe.util.telepathy module and move connection_watcher.py to it
Diffstat (limited to 'src/jarabe/util')
-rw-r--r--src/jarabe/util/Makefile.am6
-rw-r--r--src/jarabe/util/__init__.py19
-rw-r--r--src/jarabe/util/telepathy/Makefile.am4
-rw-r--r--src/jarabe/util/telepathy/__init__.py19
-rw-r--r--src/jarabe/util/telepathy/connection_watcher.py108
5 files changed, 156 insertions, 0 deletions
diff --git a/src/jarabe/util/Makefile.am b/src/jarabe/util/Makefile.am
new file mode 100644
index 0000000..4046fe8
--- /dev/null
+++ b/src/jarabe/util/Makefile.am
@@ -0,0 +1,6 @@
+SUBDIRS = \
+ telepathy
+
+sugardir = $(pythondir)/jarabe/util
+sugar_PYTHON = \
+ __init__.py
diff --git a/src/jarabe/util/__init__.py b/src/jarabe/util/__init__.py
new file mode 100644
index 0000000..1610dd0
--- /dev/null
+++ b/src/jarabe/util/__init__.py
@@ -0,0 +1,19 @@
+"""OLPC Sugar Jarabe utility modules
+"""
+
+# Copyright (C) 2008, One Laptop Per Child
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
diff --git a/src/jarabe/util/telepathy/Makefile.am b/src/jarabe/util/telepathy/Makefile.am
new file mode 100644
index 0000000..d40349d
--- /dev/null
+++ b/src/jarabe/util/telepathy/Makefile.am
@@ -0,0 +1,4 @@
+sugardir = $(pythondir)/jarabe/util/telepathy
+sugar_PYTHON = \
+ __init__.py \
+ connection_watcher.py
diff --git a/src/jarabe/util/telepathy/__init__.py b/src/jarabe/util/telepathy/__init__.py
new file mode 100644
index 0000000..387d09c
--- /dev/null
+++ b/src/jarabe/util/telepathy/__init__.py
@@ -0,0 +1,19 @@
+"""OLPC Sugar Jarabe utility telepathy modules
+"""
+
+# Copyright (C) 2008, One Laptop Per Child
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
diff --git a/src/jarabe/util/telepathy/connection_watcher.py b/src/jarabe/util/telepathy/connection_watcher.py
new file mode 100644
index 0000000..27f506f
--- /dev/null
+++ b/src/jarabe/util/telepathy/connection_watcher.py
@@ -0,0 +1,108 @@
+# This should eventually land in telepathy-python, so has the same license:
+# Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation; either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+# FIXME: should we really put this in sugar-toolkit/src/sugar/presence ?
+
+import dbus
+import dbus.mainloop.glib
+import gobject
+
+from telepathy.client import Connection
+from telepathy.interfaces import CONN_INTERFACE
+from telepathy.constants import CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED
+
+class ConnectionWatcher(gobject.GObject):
+ __gsignals__ = {
+ 'connection-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+ ([gobject.TYPE_PYOBJECT])),
+ 'connection-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+ ([gobject.TYPE_PYOBJECT])),
+ }
+
+ def __init__(self, bus=None):
+ gobject.GObject.__init__(self)
+
+ if bus is None:
+ self.bus = dbus.Bus()
+ else:
+ self.bus = bus
+
+ # D-Bus path -> Connection
+ self._connections = {}
+
+ self.bus.add_signal_receiver(self._status_changed_cb,
+ dbus_interface=CONN_INTERFACE, signal_name='StatusChanged',
+ path_keyword='path')
+
+ for conn in Connection.get_connections(bus):
+ conn.call_when_ready(self._conn_ready_cb)
+
+ def _status_changed_cb(self, *args, **kwargs):
+ path = kwargs['path']
+ if not path.startswith('/org/freedesktop/Telepathy/Connection/'):
+ return
+
+ status, reason = args
+ service_name = path.replace('/', '.')[1:]
+
+ if status == CONNECTION_STATUS_CONNECTED:
+ self._add_connection(service_name, path)
+ elif status == CONNECTION_STATUS_DISCONNECTED:
+ self._remove_connection(service_name, path)
+
+ def _conn_ready_cb(self, conn):
+ if conn.object_path in self._connections:
+ return
+
+ self._connections[conn.object_path] = conn
+ self.emit('connection-added', conn)
+
+ def _add_connection(self, service_name, path):
+ if path in self._connections:
+ return
+
+ try:
+ Connection(service_name, path, ready_handler=self._conn_ready_cb)
+ except dbus.exceptions.DBusException:
+ # The service is probably already gone
+ pass
+
+ def _remove_connection(self, service_name, path):
+ conn = self._connections.pop(path, None)
+ if conn is None:
+ return
+
+ self.emit('connection-removed', conn)
+
+ def get_connections(self):
+ return self._connections.values()
+
+if __name__ == '__main__':
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ def connection_added_cb(watcher, conn):
+ print "new connection", conn.service_name
+
+ def connection_removed_cb(watcher, conn):
+ print "removed connection", conn.service_name
+
+ watcher = ConnectionWatcher()
+ watcher.connect('connection-added', connection_added_cb)
+ watcher.connect('connection-removed', connection_removed_cb)
+
+ loop = gobject.MainLoop()
+ loop.run()