9 import util.misc as misc_utils
20 import simple_presence
23 import channel_manager
26 _moduleLogger = logging.getLogger(__name__)
29 class TheOneRingOptions(object):
35 assert gvoice.session.Session._DEFAULTS["contacts"][1] == "hours"
36 contactsPollPeriodInHours = gvoice.session.Session._DEFAULTS["contacts"][0]
38 assert gvoice.session.Session._DEFAULTS["voicemail"][1] == "minutes"
39 voicemailPollPeriodInMinutes = gvoice.session.Session._DEFAULTS["voicemail"][0]
41 assert gvoice.session.Session._DEFAULTS["texts"][1] == "minutes"
42 textsPollPeriodInMinutes = gvoice.session.Session._DEFAULTS["texts"][0]
44 def __init__(self, parameters = None):
45 if parameters is None:
47 self.ignoreDND = parameters["ignore-dnd"]
48 self.useGVContacts = parameters["use-gv-contacts"]
49 self.contactsPollPeriodInHours = parameters['contacts-poll-period-in-hours']
50 self.voicemailPollPeriodInMinutes = parameters['voicemail-poll-period-in-minutes']
51 self.textsPollPeriodInMinutes = parameters['texts-poll-period-in-minutes']
54 class TheOneRingConnection(
56 aliasing.AliasingMixin,
58 capabilities.CapabilitiesMixin,
59 contacts.ContactsMixin,
60 requests.RequestsMixin,
61 simple_presence.TheOneRingPresence,
62 simple_presence.SimplePresenceMixin,
63 presence.PresenceMixin,
66 # overiding base class variable
67 _mandatory_parameters = {
71 # overiding base class variable
72 _optional_parameters = {
75 'use-gv-contacts': 'b',
76 'contacts-poll-period-in-hours': 'i',
77 'voicemail-poll-period-in-minutes': 'i',
78 'texts-poll-period-in-minutes': 'i',
80 _parameter_defaults = {
82 'ignore-dnd': TheOneRingOptions.ignoreDND,
83 'use-gv-contacts': TheOneRingOptions.useGVContacts,
84 'contacts-poll-period-in-hours': TheOneRingOptions.contactsPollPeriodInHours,
85 'voicemail-poll-period-in-minutes': TheOneRingOptions.voicemailPollPeriodInMinutes,
86 'texts-poll-period-in-minutes': TheOneRingOptions.textsPollPeriodInMinutes,
88 _secret_parameters = set((
92 @misc_utils.log_exception(_moduleLogger)
93 def __init__(self, manager, parameters):
96 self.check_parameters(parameters)
97 account = unicode(parameters['account'])
98 encodedAccount = parameters['account'].encode('utf-8')
99 encodedPassword = parameters['password'].encode('utf-8')
100 encodedCallback = misc_utils.normalize_number(parameters['forward'].encode('utf-8'))
101 if encodedCallback and not misc_utils.is_valid_number(encodedCallback):
102 raise telepathy.errors.InvalidArgument("Invalid forwarding number")
104 # Connection init must come first
105 self.__options = TheOneRingOptions(parameters)
106 self.__accountName = account
107 self.__session = None
108 tp.Connection.__init__(
110 constants._telepathy_protocol_name_,
112 constants._telepathy_implementation_name_
114 aliasing.AliasingMixin.__init__(self)
115 avatars.AvatarsMixin.__init__(self)
116 capabilities.CapabilitiesMixin.__init__(self)
117 contacts.ContactsMixin.__init__(self)
118 requests.RequestsMixin.__init__(self)
119 simple_presence.TheOneRingPresence.__init__(self, self.__options.ignoreDND)
120 simple_presence.SimplePresenceMixin.__init__(self, self)
121 presence.PresenceMixin.__init__(self, self)
123 self.__manager = weakref.proxy(manager)
124 self.__credentials = (
128 self.__callbackNumberParameter = encodedCallback
129 self.__channelManager = channel_manager.ChannelManager(self)
131 self.__cachePath = os.sep.join((constants._data_path_, "cache", self.username))
133 os.makedirs(self.__cachePath)
138 self.set_self_handle(handle.create_handle(self, 'connection'))
140 autogv.NewGVConversations(weakref.ref(self)),
141 autogv.RefreshVoicemail(weakref.ref(self)),
142 autogv.AutoDisconnect(weakref.ref(self)),
143 autogv.DelayEnableContactIntegration(constants._telepathy_implementation_name_),
146 _moduleLogger.info("Connection to the account %s created" % account)
147 self._timedDisconnect = autogv.TimedDisconnect(weakref.ref(self))
148 self._timedDisconnect.start()
152 return self.__manager
156 return self.__session
160 return self.__options
164 return self.__credentials[0]
167 def callbackNumberParameter(self):
168 return self.__callbackNumberParameter
170 def get_handle_by_name(self, handleType, handleName):
171 requestedHandleName = handleName.encode('utf-8')
173 # We need to return an existing or create a new handle. Unfortunately
174 # handle init's take care of normalizing the handle name. So we have
175 # to create a new handle regardless and burn some handle id's and burn
176 # some extra memory of creating objects we throw away if the handle
178 if handleType == telepathy.HANDLE_TYPE_CONTACT:
179 h = handle.create_handle(self, 'contact', requestedHandleName)
180 elif handleType == telepathy.HANDLE_TYPE_LIST:
181 # Support only server side (immutable) lists
182 h = handle.create_handle(self, 'list', requestedHandleName)
184 raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
186 for candidate in self._handles.itervalues():
187 if candidate.get_name() == h.get_name():
189 _moduleLogger.debug("Re-used handle for %s, I hoped this helped" % handleName)
194 def force_log_display(self):
195 accountNumber = misc_utils.normalize_number(self.session.backend.get_account_number())
196 debugHandle = self.get_handle_by_name(
197 telepathy.HANDLE_TYPE_CONTACT,
200 debugPromptProps = self.generate_props(telepathy.CHANNEL_TYPE_TEXT, debugHandle, False)
201 self.__channelManager.channel_for_props(debugPromptProps, signal=True)
203 def log_to_user(self, component, message):
204 for logger in self._loggers:
205 logger.log_message(component, message)
207 def add_logger(self, logger):
208 self._loggers.append(logger)
210 def remove_logger(self, logger):
211 self._loggers.remove(logger)
214 def _channel_manager(self):
215 return self.__channelManager
217 @misc_utils.log_exception(_moduleLogger)
220 For org.freedesktop.telepathy.Connection
222 if self.__session is None:
223 self.__session = gvoice.session.Session(
224 cookiePath = os.path.join(constants._data_path_, "%s.cookies" % self.__accountName),
226 "contacts": (self.__options.contactsPollPeriodInHours, "hours"),
227 "voicemail": (self.__options.voicemailPollPeriodInMinutes, "minutes"),
228 "texts": (self.__options.textsPollPeriodInMinutes, "minutes"),
232 if self._status != telepathy.CONNECTION_STATUS_DISCONNECTED:
233 _moduleLogger.info("Attempting connect when not disconnected")
235 _moduleLogger.info("Connecting...")
237 telepathy.CONNECTION_STATUS_CONNECTING,
238 telepathy.CONNECTION_STATUS_REASON_REQUESTED
240 self._timedDisconnect.stop()
242 self.__credentials[0],
243 self.__credentials[1],
245 self._on_login_error,
248 @misc_utils.log_exception(_moduleLogger)
249 def _on_login(self, *args):
250 _moduleLogger.info("Connected, setting up...")
252 self.session.load(self.__cachePath)
254 for plumber in self._plumbing:
256 if not self.__callbackNumberParameter:
257 callback = gvoice.backend.get_sane_callback(
260 self.__callbackNumberParameter = misc_utils.normalize_number(callback)
261 self.session.backend.set_callback_number(self.__callbackNumberParameter)
263 subscribeHandle = self.get_handle_by_name(telepathy.HANDLE_TYPE_LIST, "subscribe")
264 subscribeProps = self.generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST, subscribeHandle, False)
265 self.__channelManager.channel_for_props(subscribeProps, signal=True)
267 _moduleLogger.exception("Setup failed")
268 self.disconnect(telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED)
271 _moduleLogger.info("Connected and set up")
273 telepathy.CONNECTION_STATUS_CONNECTED,
274 telepathy.CONNECTION_STATUS_REASON_REQUESTED
277 @misc_utils.log_exception(_moduleLogger)
278 def _on_login_error(self, error):
279 _moduleLogger.error(error)
280 if isinstance(error, StopIteration):
282 elif isinstance(error, gvoice.backend.NetworkError):
283 self.disconnect(telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR)
285 self.disconnect(telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED)
287 @misc_utils.log_exception(_moduleLogger)
288 def Disconnect(self):
290 For org.freedesktop.telepathy.Connection
292 _moduleLogger.info("Kicking off disconnect")
293 self.disconnect(telepathy.CONNECTION_STATUS_REASON_REQUESTED)
295 @misc_utils.log_exception(_moduleLogger)
296 def RequestChannel(self, type, handleType, handleId, suppressHandler):
298 For org.freedesktop.telepathy.Connection
300 @param type DBus interface name for base channel type
301 @param handleId represents a contact, list, etc according to handleType
303 @returns DBus object path for the channel created or retrieved
305 self.check_connected()
306 self.check_handle(handleType, handleId)
308 h = self.get_handle_by_id(handleType, handleId) if handleId != 0 else None
309 props = self.generate_props(type, h, suppressHandler)
310 self._validate_handle(props)
312 chan = self.__channelManager.channel_for_props(props, signal=True)
313 path = chan._object_path
314 _moduleLogger.info("RequestChannel Object Path (%s): %s" % (type.rsplit(".", 1)[-1], path))
317 def generate_props(self, channelType, handleObj, suppressHandler, initiatorHandle=None):
318 targetHandle = 0 if handleObj is None else handleObj.get_id()
319 targetHandleType = telepathy.HANDLE_TYPE_NONE if handleObj is None else handleObj.get_type()
321 telepathy.CHANNEL_INTERFACE + '.ChannelType': channelType,
322 telepathy.CHANNEL_INTERFACE + '.TargetHandle': targetHandle,
323 telepathy.CHANNEL_INTERFACE + '.TargetHandleType': targetHandleType,
324 telepathy.CHANNEL_INTERFACE + '.Requested': suppressHandler
327 if initiatorHandle is not None:
328 props[telepathy.CHANNEL_INTERFACE + '.InitiatorHandle'] = initiatorHandle.id
332 def disconnect(self, reason):
333 _moduleLogger.info("Disconnecting")
335 self._timedDisconnect.stop()
337 # Not having the disconnect first can cause weird behavior with clients
338 # including not being able to reconnect or even crashing
340 telepathy.CONNECTION_STATUS_DISCONNECTED,
344 for plumber in self._plumbing:
347 self.__channelManager.close()
348 self.manager.disconnected(self)
350 self.session.save(self.__cachePath)
351 self.session.shutdown()
354 # In case one of the above items takes too long (which it should never
355 # do), we leave the starting of the shutdown-on-idle counter to the
357 self.manager.disconnect_completed()
359 _moduleLogger.info("Disconnected")