~ubuntu-branches/ubuntu/utopic/telepathy-python/utopic

« back to all changes in this revision

Viewing changes to examples/watch.py

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2008-02-21 10:42:31 UTC
  • mfrom: (1.2.1 upstream) (7.1.10 hardy)
  • Revision ID: james.westby@ubuntu.com-20080221104231-88bloeih42cmsb0x
* New upstream version 0.15.0
* Don't mention Cohoba and telepathy-msn in description (-msn is now
  -butterfly, and Cohoba is obsolete)
* Standards-Version: 3.7.3 (no changes)
* Add XS-Dm-Upload-Allowed: yes so I can upload it in future

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""
 
3
Example of how to discover exisiting Telepathy connections on the bus, and be
 
4
notified when connections appear and disappear.
 
5
"""
 
6
 
 
7
import dbus
 
8
import dbus.glib
 
9
import gobject
 
10
 
 
11
from telepathy.interfaces import CONN_INTERFACE
 
12
from telepathy.client import Connection
 
13
 
 
14
conn_prefix = 'org.freedesktop.Telepathy.Connection.'
 
15
connection_status = ['Connected', 'Connecting', 'Disconnected']
 
16
 
 
17
class Watcher:
 
18
    def __init__(self, bus):
 
19
        self.bus = bus
 
20
        connections = Connection.get_connections()
 
21
 
 
22
        for conn in connections:
 
23
            self._watch_conn(conn)
 
24
            status = connection_status[conn[CONN_INTERFACE].GetStatus()]
 
25
            print 'found connection: %s (%s)' % (conn.service_name, status)
 
26
 
 
27
        dbus = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
 
28
        dbus.connect_to_signal('NameOwnerChanged', self._name_owner_changed_cb)
 
29
 
 
30
    def _watch_conn(self, conn):
 
31
        name = conn.service_name[len(conn_prefix):]
 
32
        conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
 
33
            lambda status, reason:
 
34
                self._status_changed_cb(name, status, reason))
 
35
 
 
36
    def _name_owner_changed_cb(self, service, old, new):
 
37
        if service.startswith(conn_prefix):
 
38
            name = service[len(conn_prefix):]
 
39
 
 
40
            if old == '':
 
41
                conn = Connection(service)
 
42
                self._watch_conn(conn)
 
43
                status = connection_status[conn[CONN_INTERFACE].GetStatus()]
 
44
                print 'new connection: %s (%s)' % (name, status)
 
45
            elif new == '':
 
46
                print 'connection gone: %s' % name
 
47
 
 
48
    def _status_changed_cb(self, name, status, reason):
 
49
        print 'status changed: %s: %s' % (
 
50
            name, connection_status[status])
 
51
 
 
52
if __name__ == '__main__':
 
53
    Watcher(dbus.Bus())
 
54
    loop = gobject.MainLoop()
 
55
 
 
56
    try:
 
57
        loop.run()
 
58
    except KeyboardInterrupt:
 
59
        pass
 
60