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