f16978992abbdbb815a66bf5609e63bdb21732c3
[theonering] / src / connection_manager.py
1 import logging
2
3 import gobject
4 import telepathy
5
6 import constants
7 import connection
8
9
10 _moduleLogger = logging.getLogger("connection_manager")
11
12
13 class TheOneRingConnectionManager(telepathy.server.ConnectionManager):
14
15         def __init__(self, shutdown_func=None):
16                 telepathy.server.ConnectionManager.__init__(self, constants._telepathy_implementation_name_)
17
18                 # self._protos is from super
19                 self._protos[constants._telepathy_protocol_name_] = connection.TheOneRingConnection
20                 self._on_shutdown = shutdown_func
21                 _moduleLogger.info("Connection manager created")
22
23         def GetParameters(self, proto):
24                 """
25                 For org.freedesktop.telepathy.ConnectionManager
26
27                 @returns the mandatory and optional parameters for creating a connection
28                 """
29                 if proto not in self._protos:
30                         raise telepathy.NotImplemented('unknown protocol %s' % proto)
31
32                 result = []
33                 ConnectionClass = self._protos[proto]
34                 mandatoryParameters = ConnectionClass._mandatory_parameters
35                 optionalParameters = ConnectionClass._optional_parameters
36                 defaultParameters = ConnectionClass._parameter_defaults
37
38                 for parameterName, parameterType in mandatoryParameters.iteritems():
39                         flags = telepathy.CONN_MGR_PARAM_FLAG_REQUIRED
40                         if parameterName == "password":
41                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
42                         param = (
43                                 parameterName,
44                                 flags,
45                                 parameterType,
46                                 '',
47                         )
48                         result.append(param)
49
50                 for parameterName, parameterType in optionalParameters.iteritems():
51                         if parameterName in defaultParameters:
52                                 flags = telepathy.CONN_MGR_PARAM_FLAG_HAS_DEFAULT
53                                 if parameterName == "password":
54                                         flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
55                                 default = defaultParameters[parameterName]
56                         else:
57                                 flags = 0
58                                 default = ""
59                         param = (
60                                 parameterName,
61                                 flags,
62                                 parameterName,
63                                 default,
64                         )
65                         result.append(param)
66
67                 return result
68
69         def disconnected(self, conn):
70                 """
71                 Overrides telepathy.server.ConnectionManager
72                 """
73                 result = telepathy.server.ConnectionManager.disconnected(self, conn)
74                 gobject.timeout_add(5000, self.shutdown)
75
76         def quit(self):
77                 """
78                 Terminates all connections. Must be called upon quit
79                 """
80                 for connection in self._connections:
81                         connection.Disconnect()
82                 _moduleLogger.info("Connection manager quitting")
83
84         def _shutdown(self):
85                 if (
86                         self._on_shutdown is not None and
87                         len(self._connections) == 0
88                 ):
89                         self._on_shutdown()
90                 return False