Bug fixes, code cleanup, debugging help all working together to make it so that I...
[theonering] / src / connection_manager.py
1 """
2 Empathy Experience:
3         .profile file needs to be updated with proper presence
4         Can't reopen a conversation for someone when I've already closed it
5         Can't call
6         When first started, reports all read conversations when some might have been read
7         When first started, reports all of an SMS conversation even though some has been reported previously
8 """
9
10 import logging
11
12 import gobject
13 import telepathy
14
15 import constants
16 import gtk_toolbox
17 import connection
18
19
20 _moduleLogger = logging.getLogger("connection_manager")
21
22
23 class TheOneRingConnectionManager(telepathy.server.ConnectionManager):
24
25         def __init__(self, shutdown_func=None):
26                 telepathy.server.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 telepathy.server.ConnectionManager
83                 """
84                 result = telepathy.server.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         def _shutdown(self):
96                 if (
97                         self._on_shutdown is not None and
98                         len(self._connections) == 0
99                 ):
100                         self._on_shutdown()
101                 return False