Move to new tubes API
[dtube-test.git] / connection_watcher.py
blob4a4c6e0950e86ba376bdb52a901fb6613ecebb85
1 # This should eventually land in telepathy-python, so has the same license:
2 # Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as published
6 # by the Free Software Foundation; either version 2.1 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # FIXME: this sould go upstream, in telepathy-python
20 import logging
22 import dbus
23 import dbus.mainloop.glib
24 import gobject
26 from telepathy.client import Connection
27 from telepathy.interfaces import CONN_INTERFACE
28 from telepathy.constants import CONNECTION_STATUS_CONNECTED, \
29 CONNECTION_STATUS_DISCONNECTED
31 class ConnectionWatcher(gobject.GObject):
32 __gsignals__ = {
33 'connection-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
34 ([gobject.TYPE_PYOBJECT])),
35 'connection-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
36 ([gobject.TYPE_PYOBJECT]))
39 def __init__(self, bus=None):
40 gobject.GObject.__init__(self)
42 if bus is None:
43 self.bus = dbus.Bus()
44 else:
45 self.bus = bus
47 # D-Bus path -> Connection
48 self._connections = {}
50 self.bus.add_signal_receiver(self._status_changed_cb,
51 dbus_interface=CONN_INTERFACE, signal_name='StatusChanged',
52 path_keyword='path')
54 for conn in Connection.get_connections(bus):
55 conn.call_when_ready(self._conn_ready_cb)
57 def _status_changed_cb(self, *args, **kwargs):
58 path = kwargs['path']
59 if not path.startswith('/org/freedesktop/Telepathy/Connection/'):
60 return
62 status, reason_ = args
63 service_name = path.replace('/', '.')[1:]
65 if status == CONNECTION_STATUS_CONNECTED:
66 self._add_connection(service_name, path)
67 elif status == CONNECTION_STATUS_DISCONNECTED:
68 self._remove_connection(service_name, path)
70 def _conn_ready_cb(self, conn):
71 if conn.object_path in self._connections:
72 return
74 self._connections[conn.object_path] = conn
75 self.emit('connection-added', conn)
77 def _add_connection(self, service_name, path):
78 if path in self._connections:
79 return
81 try:
82 Connection(service_name, path, ready_handler=self._conn_ready_cb)
83 except dbus.exceptions.DBusException:
84 logging.debug('%s is propably already gone.', service_name)
86 def _remove_connection(self, service_name, path):
87 conn = self._connections.pop(path, None)
88 if conn is None:
89 return
91 self.emit('connection-removed', conn)
93 def get_connections(self):
94 return self._connections.values()
96 if __name__ == '__main__':
97 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
99 def connection_added_cb(conn_watcher, conn):
100 print "new connection", conn.service_name
102 def connection_removed_cb(conn_watcher, conn):
103 print "removed connection", conn.service_name
105 watcher = ConnectionWatcher()
106 watcher.connect('connection-added', connection_added_cb)
107 watcher.connect('connection-removed', connection_removed_cb)
109 loop = gobject.MainLoop()
110 loop.run()