X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fconnection.py;h=41c0a6df85f6f53a1054decdb051ccbf5631d2b1;hp=0f438676e05799588cb0a40cd1cd1009056b52bc;hb=4329f6e4cdab994dbe81ea03e1c36341be37d253;hpb=e2b913d6c65eca22a75ead429543db4f2b2b1f4a diff --git a/src/connection.py b/src/connection.py index 0f43867..41c0a6d 100644 --- a/src/connection.py +++ b/src/connection.py @@ -6,7 +6,6 @@ import telepathy import constants import tp -import util.go_utils as gobject_utils import util.misc as misc_utils import gvoice @@ -29,6 +28,8 @@ _moduleLogger = logging.getLogger(__name__) class TheOneRingOptions(object): + ignoreDND = True + useGVContacts = True assert gvoice.session.Session._DEFAULTS["contacts"][1] == "hours" @@ -43,6 +44,7 @@ class TheOneRingOptions(object): def __init__(self, parameters = None): if parameters is None: return + self.ignoreDND = parameters["ignore-dnd"] self.useGVContacts = parameters["use-gv-contacts"] self.contactsPollPeriodInHours = parameters['contacts-poll-period-in-hours'] self.voicemailPollPeriodInMinutes = parameters['voicemail-poll-period-in-minutes'] @@ -55,9 +57,10 @@ class TheOneRingConnection( avatars.AvatarsMixin, capabilities.CapabilitiesMixin, contacts.ContactsMixin, - presence.PresenceMixin, requests.RequestsMixin, + simple_presence.TheOneRingPresence, simple_presence.SimplePresenceMixin, + presence.PresenceMixin, ): # overiding base class variable @@ -68,6 +71,7 @@ class TheOneRingConnection( # overiding base class variable _optional_parameters = { 'forward': 's', + 'ignore-dnd': 'b', 'use-gv-contacts': 'b', 'contacts-poll-period-in-hours': 'i', 'voicemail-poll-period-in-minutes': 'i', @@ -75,6 +79,7 @@ class TheOneRingConnection( } _parameter_defaults = { 'forward': '', + 'ignore-dnd': TheOneRingOptions.ignoreDND, 'use-gv-contacts': TheOneRingOptions.useGVContacts, 'contacts-poll-period-in-hours': TheOneRingOptions.contactsPollPeriodInHours, 'voicemail-poll-period-in-minutes': TheOneRingOptions.voicemailPollPeriodInMinutes, @@ -86,6 +91,8 @@ class TheOneRingConnection( @misc_utils.log_exception(_moduleLogger) def __init__(self, manager, parameters): + self._loggers = [] + self.check_parameters(parameters) account = unicode(parameters['account']) encodedAccount = parameters['account'].encode('utf-8') @@ -97,7 +104,7 @@ class TheOneRingConnection( # Connection init must come first self.__options = TheOneRingOptions(parameters) self.__session = gvoice.session.Session( - cookiePath = None, + cookiePath = os.path.join(constants._data_path_, "%s.cookies" % account), defaults = { "contacts": (self.__options.contactsPollPeriodInHours, "hours"), "voicemail": (self.__options.voicemailPollPeriodInMinutes, "minutes"), @@ -114,9 +121,10 @@ class TheOneRingConnection( avatars.AvatarsMixin.__init__(self) capabilities.CapabilitiesMixin.__init__(self) contacts.ContactsMixin.__init__(self) - presence.PresenceMixin.__init__(self) requests.RequestsMixin.__init__(self) - simple_presence.SimplePresenceMixin.__init__(self) + simple_presence.TheOneRingPresence.__init__(self, self.__options.ignoreDND) + simple_presence.SimplePresenceMixin.__init__(self, self) + presence.PresenceMixin.__init__(self, self) self.__manager = weakref.proxy(manager) self.__credentials = ( @@ -167,6 +175,12 @@ class TheOneRingConnection( def get_handle_by_name(self, handleType, handleName): requestedHandleName = handleName.encode('utf-8') + + # We need to return an existing or create a new handle. Unfortunately + # handle init's take care of normalizing the handle name. So we have + # to create a new handle regardless and burn some handle id's and burn + # some extra memory of creating objects we throw away if the handle + # already exists. if handleType == telepathy.HANDLE_TYPE_CONTACT: h = handle.create_handle(self, 'contact', requestedHandleName) elif handleType == telepathy.HANDLE_TYPE_LIST: @@ -174,8 +188,25 @@ class TheOneRingConnection( h = handle.create_handle(self, 'list', requestedHandleName) else: raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType) + + for candidate in self._handles.itervalues(): + if candidate.get_name() == h.get_name(): + h = candidate + _moduleLogger.debug("Re-used handle for %s, I hoped this helped" % handleName) + break + return h + def log_to_user(self, component, message): + for logger in self._loggers: + logger.log_message(component, message) + + def add_logger(self, logger): + self._loggers.append(logger) + + def remove_logger(self, logger): + self._loggers.remove(logger) + @property def _channel_manager(self): return self.__channelManager @@ -202,7 +233,7 @@ class TheOneRingConnection( ) @misc_utils.log_exception(_moduleLogger) - def _on_login(self): + def _on_login(self, *args): _moduleLogger.info("Connected, setting up...") try: self.__session.load(self.__cachePath) @@ -236,7 +267,9 @@ class TheOneRingConnection( @misc_utils.log_exception(_moduleLogger) def _on_login_error(self, error): _moduleLogger.error(error) - if isinstance(error, gvoice.backend.NetworkError): + if isinstance(error, StopIteration): + pass + elif isinstance(error, gvoice.backend.NetworkError): self.disconnect(telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR) else: self.disconnect(telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED) @@ -305,7 +338,7 @@ class TheOneRingConnection( self.manager.disconnected(self) self.session.save(self.__cachePath) - self.session.logout() + self.session.shutdown() self.session.close() # In case one of the above items takes too long (which it should never