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