10 except (ImportError, OSError):
15 import util.coroutines as coroutines
16 import util.misc as util_misc
25 import simple_presence
29 import channel_manager
32 _moduleLogger = logging.getLogger("connection")
35 class TheOneRingOptions(object):
39 def __init__(self, parameters = None):
40 if parameters is None:
42 self.useGVContacts = parameters["use-gv-contacts"]
45 class TheOneRingConnection(
47 requests.RequestsMixin,
48 contacts.ContactsMixin,
49 aliasing.AliasingMixin,
50 simple_presence.SimplePresenceMixin,
51 presence.PresenceMixin,
52 capabilities.CapabilitiesMixin,
55 # overiding base class variable
56 _mandatory_parameters = {
60 # overiding base class variable
61 _optional_parameters = {
63 'use-gv-contacts' : 'b',
65 _parameter_defaults = {
67 'use-gv-contacts' : TheOneRingOptions.useGVContacts,
69 _secret_parameters = set((
73 @gtk_toolbox.log_exception(_moduleLogger)
74 def __init__(self, manager, parameters):
75 self.check_parameters(parameters)
76 account = unicode(parameters['account'])
77 encodedAccount = parameters['account'].encode('utf-8')
78 encodedPassword = parameters['password'].encode('utf-8')
79 encodedCallback = util_misc.normalize_number(parameters['forward'].encode('utf-8'))
80 if encodedCallback and not util_misc.is_valid_number(encodedCallback):
81 raise telepathy.errors.InvalidArgument("Invalid forwarding number")
83 # Connection init must come first
84 self.__session = gvoice.session.Session(None)
85 self.__options = TheOneRingOptions(parameters)
86 tp.Connection.__init__(
88 constants._telepathy_protocol_name_,
90 constants._telepathy_implementation_name_
92 requests.RequestsMixin.__init__(self)
93 contacts.ContactsMixin.__init__(self)
94 aliasing.AliasingMixin.__init__(self)
95 simple_presence.SimplePresenceMixin.__init__(self)
96 presence.PresenceMixin.__init__(self)
97 capabilities.CapabilitiesMixin.__init__(self)
99 self.__manager = weakref.proxy(manager)
100 self.__credentials = (
104 self.__callbackNumberParameter = encodedCallback
105 self.__channelManager = channel_manager.ChannelManager(self)
107 if conic is not None:
108 self.__connection = conic.Connection()
110 self.__connection = None
111 self.__cachePath = os.sep.join((constants._data_path_, "cache", self.username))
113 os.makedirs(self.__cachePath)
118 self.set_self_handle(handle.create_handle(self, 'connection'))
120 self.__callback = None
121 _moduleLogger.info("Connection to the account %s created" % account)
125 return self.__manager
129 return self.__session
133 return self.__options
137 return self.__credentials[0]
140 def callbackNumberParameter(self):
141 return self.__callbackNumberParameter
143 def get_handle_by_name(self, handleType, handleName):
144 requestedHandleName = handleName.encode('utf-8')
145 if handleType == telepathy.HANDLE_TYPE_CONTACT:
146 _moduleLogger.debug("get_handle_by_name Contact: %s" % requestedHandleName)
147 h = handle.create_handle(self, 'contact', requestedHandleName)
148 elif handleType == telepathy.HANDLE_TYPE_LIST:
149 # Support only server side (immutable) lists
150 _moduleLogger.debug("get_handle_by_name List: %s" % requestedHandleName)
151 h = handle.create_handle(self, 'list', requestedHandleName)
153 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
157 def _channel_manager(self):
158 return self.__channelManager
160 @gtk_toolbox.log_exception(_moduleLogger)
163 For org.freedesktop.telepathy.Connection
165 _moduleLogger.info("Connecting...")
167 telepathy.CONNECTION_STATUS_CONNECTING,
168 telepathy.CONNECTION_STATUS_REASON_REQUESTED
171 self.__session.load(self.__cachePath)
173 self.__callback = coroutines.func_sink(
174 coroutines.expand_positional(
175 self._on_conversations_updated
178 self.session.voicemails.updateSignalHandler.register_sink(
181 self.session.texts.updateSignalHandler.register_sink(
184 self.session.login(*self.__credentials)
185 if not self.__callbackNumberParameter:
186 callback = gvoice.backend.get_sane_callback(
189 self.__callbackNumberParameter = util_misc.normalize_number(callback)
190 self.session.backend.set_callback_number(self.__callbackNumberParameter)
192 subscribeHandle = self.get_handle_by_name(telepathy.HANDLE_TYPE_LIST, "subscribe")
193 subscribeProps = self._generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST, subscribeHandle, False)
194 self.__channelManager.channel_for_props(subscribeProps, signal=True)
195 publishHandle = self.get_handle_by_name(telepathy.HANDLE_TYPE_LIST, "publish")
196 publishProps = self._generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST, publishHandle, False)
197 self.__channelManager.channel_for_props(publishProps, signal=True)
198 except gvoice.backend.NetworkError, e:
199 _moduleLogger.exception("Connection Failed")
201 telepathy.CONNECTION_STATUS_DISCONNECTED,
202 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
206 _moduleLogger.exception("Connection Failed")
208 telepathy.CONNECTION_STATUS_DISCONNECTED,
209 telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
213 _moduleLogger.info("Connected")
215 telepathy.CONNECTION_STATUS_CONNECTED,
216 telepathy.CONNECTION_STATUS_REASON_REQUESTED
218 if self.__connection is not None:
219 self.__connectionEventId = self.__connection.connect("connection-event", self._on_connection_change)
221 @gtk_toolbox.log_exception(_moduleLogger)
222 def Disconnect(self):
224 For org.freedesktop.telepathy.Connection
229 _moduleLogger.exception("Error durring disconnect")
231 telepathy.CONNECTION_STATUS_DISCONNECTED,
232 telepathy.CONNECTION_STATUS_REASON_REQUESTED
235 @gtk_toolbox.log_exception(_moduleLogger)
236 def RequestChannel(self, type, handleType, handleId, suppressHandler):
238 For org.freedesktop.telepathy.Connection
240 @param type DBus interface name for base channel type
241 @param handleId represents a contact, list, etc according to handleType
243 @returns DBus object path for the channel created or retrieved
245 self.check_connected()
246 self.check_handle(handleType, handleId)
248 h = self.get_handle_by_id(handleType, handleId) if handleId != 0 else None
249 props = self._generate_props(type, h, suppressHandler)
250 self._validate_handle(props)
252 chan = self.__channelManager.channel_for_props(props, signal=True)
253 path = chan._object_path
254 _moduleLogger.info("RequestChannel Object Path (%s): %s" % (type.rsplit(".", 1)[-1], path))
257 def _generate_props(self, channelType, handle, suppressHandler, initiatorHandle=None):
258 targetHandle = 0 if handle is None else handle.get_id()
259 targetHandleType = telepathy.HANDLE_TYPE_NONE if handle is None else handle.get_type()
261 telepathy.CHANNEL_INTERFACE + '.ChannelType': channelType,
262 telepathy.CHANNEL_INTERFACE + '.TargetHandle': targetHandle,
263 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': targetHandleType,
264 telepathy.CHANNEL_INTERFACE + '.Requested': suppressHandler
267 if initiatorHandle is not None:
268 props[telepathy.CHANNEL_INTERFACE + '.InitiatorHandle'] = initiatorHandle.id
272 def _disconnect(self):
273 _moduleLogger.info("Disconnecting")
274 self.session.voicemails.updateSignalHandler.unregister_sink(
277 self.session.texts.updateSignalHandler.unregister_sink(
280 self.__callback = None
282 self.__channelManager.close()
283 self.session.save(self.__cachePath)
284 self.session.logout()
287 self.manager.disconnected(self)
288 _moduleLogger.info("Disconnected")
290 @gtk_toolbox.log_exception(_moduleLogger)
291 def _on_conversations_updated(self, conv, conversationIds):
292 _moduleLogger.debug("Incoming messages from: %r" % (conversationIds, ))
293 for phoneNumber in conversationIds:
294 h = self.get_handle_by_name(telepathy.HANDLE_TYPE_CONTACT, phoneNumber)
295 # Just let the TextChannel decide whether it should be reported to the user or not
296 props = self._generate_props(telepathy.CHANNEL_TYPE_TEXT, h, False)
297 if self.__channelManager.channel_exists(props):
300 # Maemo 4.1's RTComm opens a window for a chat regardless if a
301 # message is received or not, so we need to do some filtering here
302 mergedConv = conv.get_conversation(phoneNumber)
305 for conversation in mergedConv.conversations
306 if not conversation.isRead and not conversation.isArchived
311 chan = self.__channelManager.channel_for_props(props, signal=True)
313 @gtk_toolbox.log_exception(_moduleLogger)
314 def _on_connection_change(self, connection, event):
318 if not self.session.is_logged_in():
319 _moduleLogger.info("Received connection change event when not logged in")
321 status = event.get_status()
322 error = event.get_error()
323 iap_id = event.get_iap_id()
324 bearer = event.get_bearer_type()
326 if status == conic.STATUS_DISCONNECTED:
327 _moduleLogger.info("Disconnecting due to loss of network connection")
329 telepathy.CONNECTION_STATUS_DISCONNECTED,
330 telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
335 _moduleLogger.exception("Error durring disconnect")
337 _moduleLogger.info("Other status: %r" % (status, ))