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