Pulling in latest skeleton code
[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                 self._quitImmediately = False
26                 _moduleLogger.info("Connection manager created")
27
28         @misc_utils.log_exception(_moduleLogger)
29         def GetParameters(self, proto):
30                 """
31                 For org.freedesktop.telepathy.ConnectionManager
32
33                 @returns the mandatory and optional parameters for creating a connection
34                 """
35                 if proto not in self._protos:
36                         raise telepathy.errors.NotImplemented('unknown protocol %s' % proto)
37
38                 result = []
39                 ConnectionClass = self._protos[proto]
40                 mandatoryParameters = ConnectionClass._mandatory_parameters
41                 optionalParameters = ConnectionClass._optional_parameters
42                 defaultParameters = ConnectionClass._parameter_defaults
43                 secretParameters = ConnectionClass._secret_parameters
44
45                 for parameterName, parameterType in mandatoryParameters.iteritems():
46                         flags = telepathy.CONN_MGR_PARAM_FLAG_REQUIRED
47                         if parameterName in secretParameters:
48                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
49                         param = (parameterName, flags, parameterType, "")
50                         result.append(param)
51
52                 for parameterName, parameterType in optionalParameters.iteritems():
53                         flags = 0
54                         default = ""
55                         if parameterName in secretParameters:
56                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
57                         if parameterName in defaultParameters:
58                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_HAS_DEFAULT
59                                 default = defaultParameters[parameterName]
60                         param = (parameterName, flags, parameterType, default)
61                         result.append(param)
62
63                 return result
64
65         def disconnect_completed(self):
66                 if self._quitImmediately:
67                         self._shutdown()
68                 else:
69                         gobject_utils.timeout_add_seconds(self.IDLE_TIMEOUT, self._shutdown)
70
71         def quit(self):
72                 """
73                 Terminates all connections. Must be called upon quit
74                 """
75                 for conn in self._connections:
76                         conn.Disconnect()
77                 _moduleLogger.info("Connection manager quitting")
78
79         def quit_now(self):
80                 self._quitImmediately = True
81                 self.quit()
82
83         @misc_utils.log_exception(_moduleLogger)
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