207f773f9d594cbc1358abb0b484cb884ac2ec70
[theonering] / src / connection_manager.py
1 """
2 Empathy Experience:
3         Can't call
4         When first started, reports all read conversations when some might have been read
5         When first started, reports all of an SMS conversation even though some has been reported previously
6         Still leaking one of two contact lists
7 """
8
9 import logging
10
11 import gobject
12 import telepathy
13
14 import constants
15 import tp
16 import gtk_toolbox
17 import connection
18
19
20 _moduleLogger = logging.getLogger("connection_manager")
21
22
23 class TheOneRingConnectionManager(tp.ConnectionManager):
24
25         def __init__(self, shutdown_func=None):
26                 tp.ConnectionManager.__init__(self, constants._telepathy_implementation_name_)
27
28                 # self._protos is from super
29                 self._protos[constants._telepathy_protocol_name_] = connection.TheOneRingConnection
30                 self._on_shutdown = shutdown_func
31                 _moduleLogger.info("Connection manager created")
32
33         @gtk_toolbox.log_exception(_moduleLogger)
34         def GetParameters(self, proto):
35                 """
36                 For org.freedesktop.telepathy.ConnectionManager
37
38                 @returns the mandatory and optional parameters for creating a connection
39                 """
40                 if proto not in self._protos:
41                         raise telepathy.errors.NotImplemented('unknown protocol %s' % proto)
42
43                 result = []
44                 ConnectionClass = self._protos[proto]
45                 mandatoryParameters = ConnectionClass._mandatory_parameters
46                 optionalParameters = ConnectionClass._optional_parameters
47                 defaultParameters = ConnectionClass._parameter_defaults
48
49                 for parameterName, parameterType in mandatoryParameters.iteritems():
50                         flags = telepathy.CONN_MGR_PARAM_FLAG_REQUIRED
51                         if parameterName == "password":
52                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
53                         param = (
54                                 parameterName,
55                                 flags,
56                                 parameterType,
57                                 '',
58                         )
59                         result.append(param)
60
61                 for parameterName, parameterType in optionalParameters.iteritems():
62                         if parameterName in defaultParameters:
63                                 flags = telepathy.CONN_MGR_PARAM_FLAG_HAS_DEFAULT
64                                 if parameterName == "password":
65                                         flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
66                                 default = defaultParameters[parameterName]
67                         else:
68                                 flags = 0
69                                 default = ""
70                         param = (
71                                 parameterName,
72                                 flags,
73                                 parameterName,
74                                 default,
75                         )
76                         result.append(param)
77
78                 return result
79
80         def disconnected(self, conn):
81                 """
82                 Overrides tp.ConnectionManager
83                 """
84                 result = tp.ConnectionManager.disconnected(self, conn)
85                 gobject.timeout_add(5000, self._shutdown)
86
87         def quit(self):
88                 """
89                 Terminates all connections. Must be called upon quit
90                 """
91                 for connection in self._connections:
92                         connection.Disconnect()
93                 _moduleLogger.info("Connection manager quitting")
94
95         @gtk_toolbox.log_exception(_moduleLogger)
96         def _shutdown(self):
97                 if (
98                         self._on_shutdown is not None and
99                         len(self._connections) == 0
100                 ):
101                         self._on_shutdown()
102                 return False