Reducing login traffic by making DND optional
authorEd Page <eopage@byu.net>
Sat, 19 Jun 2010 21:13:00 +0000 (16:13 -0500)
committerEd Page <eopage@byu.net>
Sat, 19 Jun 2010 21:13:14 +0000 (16:13 -0500)
src/connection.py
src/gvoice/session.py
src/presence.py
src/simple_presence.py
support/theonering.manager

index 080c621..41c0a6d 100644 (file)
@@ -28,6 +28,8 @@ _moduleLogger = logging.getLogger(__name__)
 
 class TheOneRingOptions(object):
 
+       ignoreDND = True
+
        useGVContacts = True
 
        assert gvoice.session.Session._DEFAULTS["contacts"][1] == "hours"
@@ -42,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']
@@ -54,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
@@ -67,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',
@@ -74,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,
@@ -115,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 = (
index 5a825f0..b3fc522 100644 (file)
@@ -218,8 +218,9 @@ class Session(object):
                        return isLoggedIn
 
        def set_dnd(self, doNotDisturb):
-               self._backend.set_dnd(doNotDisturb)
-               self._cachedIsDnd = doNotDisturb
+               if self._cachedIsDnd != doNotDisturb:
+                       self._backend.set_dnd(doNotDisturb)
+                       self._cachedIsDnd = doNotDisturb
 
        def is_dnd(self):
                # To throttle checking with the server, use a 30s cache
index 986abe8..add5892 100644 (file)
@@ -2,17 +2,16 @@ import logging
 
 import tp
 import util.misc as misc_utils
-import simple_presence
 
 
 _moduleLogger = logging.getLogger(__name__)
 
 
-class PresenceMixin(tp.ConnectionInterfacePresence, simple_presence.TheOneRingPresence):
+class PresenceMixin(tp.ConnectionInterfacePresence):
 
-       def __init__(self):
+       def __init__(self, torPresence):
                tp.ConnectionInterfacePresence.__init__(self)
-               simple_presence.TheOneRingPresence.__init__(self)
+               self.__torPresence = torPresence
 
        @misc_utils.log_exception(_moduleLogger)
        def GetStatuses(self):
@@ -21,7 +20,7 @@ class PresenceMixin(tp.ConnectionInterfacePresence, simple_presence.TheOneRingPr
 
                return dict(
                        (localType, (telepathyType, True, True, arguments))
-                       for (localType, telepathyType) in self.TO_PRESENCE_TYPE.iteritems()
+                       for (localType, telepathyType) in self.__torPresence.TO_PRESENCE_TYPE.iteritems()
                )
 
        @misc_utils.log_exception(_moduleLogger)
@@ -38,11 +37,11 @@ class PresenceMixin(tp.ConnectionInterfacePresence, simple_presence.TheOneRingPr
                assert len(statuses) == 1
                status, arguments = statuses.items()[0]
                assert len(arguments) == 0
-               self.set_presence(status)
+               self.__torPresence.set_presence(status)
 
        def __get_presences(self, contacts):
                arguments = {}
                return dict(
                        (h, (0, {presence: arguments}))
-                       for (h, (presenceType, presence)) in self.get_presences(contacts).iteritems()
+                       for (h, (presenceType, presence)) in self.__torPresence.get_presences(contacts).iteritems()
                )
index c71a5c2..fa306bd 100644 (file)
@@ -27,6 +27,9 @@ class TheOneRingPresence(object):
                OFFLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE),
        }
 
+       def __init__(self, ignoreDND):
+               self.__ignoreDND = ignoreDND
+
        @property
        def session(self):
                """
@@ -54,7 +57,7 @@ class TheOneRingPresence(object):
                for handleId in contactIds:
                        h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
                        if isinstance(h, handle.ConnectionHandle):
-                               isDnd = self.session.is_dnd()
+                               isDnd = self.session.is_dnd() if not self.__ignoreDND else False
                                if isDnd:
                                        presence = TheOneRingPresence.HIDDEN
                                else:
@@ -75,12 +78,14 @@ class TheOneRingPresence(object):
 
        def set_presence(self, status):
                if status == self.ONLINE:
-                       self.session.set_dnd(False)
+                       if not self.__ignoreDND:
+                               self.session.set_dnd(False)
                        self.session.stateMachine.set_state(state_machine.StateMachine.STATE_ACTIVE)
                elif status == self.AWAY:
                        self.session.stateMachine.set_state(state_machine.StateMachine.STATE_IDLE)
                elif status == self.HIDDEN:
-                       self.session.set_dnd(True)
+                       if not self.__ignoreDND:
+                               self.session.set_dnd(True)
                elif status == self.OFFLINE:
                        self.Disconnect()
                else:
@@ -88,11 +93,11 @@ class TheOneRingPresence(object):
                _moduleLogger.info("Setting Presence to '%s'" % status)
 
 
-class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence, TheOneRingPresence):
+class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence):
 
-       def __init__(self):
+       def __init__(self, torPresence):
                tp.ConnectionInterfaceSimplePresence.__init__(self)
-               TheOneRingPresence.__init__(self)
+               self.__torPresence = torPresence
 
                self._implement_property_get(
                        tp.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
@@ -108,7 +113,8 @@ class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence, TheOneRingPresen
                return dbus.Dictionary(
                        (
                                (h, dbus.Struct((presenceType, presence, personalMessage), signature="uss"))
-                               for (h, (presenceType, presence)) in self.get_presences(contacts).iteritems()
+                               for (h, (presenceType, presence)) in
+                                       self.__torPresence.get_presences(contacts).iteritems()
                        ),
                        signature="u(uss)"
                )
@@ -118,7 +124,7 @@ class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence, TheOneRingPresen
                if message:
                        raise telepathy.errors.InvalidArgument("Messages aren't supported")
 
-               self.set_presence(status)
+               self.__torPresence.set_presence(status)
 
        def _get_statuses(self):
                """
@@ -128,5 +134,5 @@ class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence, TheOneRingPresen
                """
                return dict(
                        (localType, (telepathyType, True, False))
-                       for (localType, telepathyType) in self.TO_PRESENCE_TYPE.iteritems()
+                       for (localType, telepathyType) in self.__torPresence.TO_PRESENCE_TYPE.iteritems()
                )
index 7099c47..c0204e9 100644 (file)
@@ -8,10 +8,12 @@ param-account = s required
 param-password = s required secret
 param-forward = s
 param-use-gv-contacts = b
+param-ignore-dnd = b
 param-contacts-poll-period-in-hours = i
 param-voicemail-poll-period-in-minutes = i
 param-texts-poll-period-in-minutes = i
 default-forward =
+default-ignore-dnd = true
 default-use-gv-contacts = true
 default-contacts-poll-period-in-hours = 12
 default-voicemail-poll-period-in-minutes = 120