Updating to latest butterfly fad, should eventually do capabilities too
[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                 secretParameters = ConnectionClass._secret_parameters
49
50                 for parameterName, parameterType in mandatoryParameters.iteritems():
51                         flags = telepathy.CONN_MGR_PARAM_FLAG_REQUIRED
52                         if parameterName in secretParameters:
53                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
54                         param = (parameterName, flags, parameterType, "")
55                         result.append(param)
56
57                 for parameterName, parameterType in optionalParameters.iteritems():
58                         flags = 0
59                         default = ""
60                         if parameterName in secretParameters:
61                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_SECRET
62                         if parameterName in defaultParameters:
63                                 flags |= telepathy.CONN_MGR_PARAM_FLAG_HAS_DEFAULT
64                                 default = defaultParameters[parameterName]
65                         param = (parameterName, flags, parameterType, default)
66                         result.append(param)
67
68                 return result
69
70         def disconnected(self, conn):
71                 """
72                 Overrides tp.ConnectionManager
73                 """
74                 result = tp.ConnectionManager.disconnected(self, conn)
75                 gobject.timeout_add(5000, self._shutdown)
76
77         def quit(self):
78                 """
79                 Terminates all connections. Must be called upon quit
80                 """
81                 for connection in self._connections:
82                         connection.Disconnect()
83                 _moduleLogger.info("Connection manager quitting")
84
85         @gtk_toolbox.log_exception(_moduleLogger)
86         def _shutdown(self):
87                 if (
88                         self._on_shutdown is not None and
89                         len(self._connections) == 0
90                 ):
91                         self._on_shutdown()
92                 return False