Include _generated now so I get all the latest fancy stuff
[theonering] / src / tp / connmgr.py
1 # telepathy-python - Base classes defining the interfaces of the Telepathy framework
2 #
3 # Copyright (C) 2005, 2006 Collabora Limited
4 # Copyright (C) 2005, 2006 Nokia Corporation
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
20 import dbus
21 import dbus.service
22
23 from telepathy.errors import NotImplemented
24 from telepathy.interfaces import CONN_MGR_INTERFACE
25
26 from _generated.Connection_Manager \
27         import ConnectionManager as _ConnectionManager
28
29 class ConnectionManager(_ConnectionManager):
30     def __init__(self, name):
31         """
32         Initialise the connection manager.
33         """
34         bus_name = 'org.freedesktop.Telepathy.ConnectionManager.%s' % name
35         object_path = '/org/freedesktop/Telepathy/ConnectionManager/%s' % name
36         _ConnectionManager.__init__(self,
37                                     dbus.service.BusName(bus_name, dbus.Bus(), do_not_queue=True),
38                                     object_path)
39
40         self._connections = set()
41         self._protos = {}
42
43     def connected(self, conn):
44         """
45         Add a connection to the list of connections, emit the appropriate
46         signal.
47         """
48         self._connections.add(conn)
49         self.NewConnection(conn._name.get_name(), conn._object_path, conn._proto)
50
51     def disconnected(self, conn):
52         """
53         Remove a connection from the list of connections.
54         """
55         self._connections.remove(conn)
56         if hasattr(conn, 'remove_from_connection'):
57             # requires dbus-python >= 0.81.1
58             conn.remove_from_connection()
59         del conn
60
61         return False # when called in an idle callback
62
63     @dbus.service.method(CONN_MGR_INTERFACE, in_signature='', out_signature='as')
64     def ListProtocols(self):
65         return self._protos.keys()
66
67     def RequestConnection(self, proto, parameters):
68         if proto in self._protos:
69             conn = self._protos[proto](self, parameters)
70             self.connected(conn)
71             return (conn._name.get_name(), conn._object_path)
72         else:
73             raise NotImplemented('unknown protocol %s' % proto)