Adding old-school presence so I can get presence support working in Empathy, wahooo
authorEd Page <eopage@byu.net>
Thu, 24 Dec 2009 14:20:54 +0000 (08:20 -0600)
committerEd Page <eopage@byu.net>
Thu, 24 Dec 2009 14:20:54 +0000 (08:20 -0600)
src/connection.py
src/presence.py [new file with mode: 0644]
src/simple_presence.py

index 5af02ba..f359cd5 100644 (file)
@@ -11,6 +11,7 @@ import gvoice
 import handle
 import aliasing
 import simple_presence
+import presence
 import capabilities
 import channel_manager
 
@@ -22,6 +23,7 @@ class TheOneRingConnection(
        telepathy.server.Connection,
        aliasing.AliasingMixin,
        simple_presence.SimplePresenceMixin,
+       presence.PresenceMixin,
        capabilities.CapabilitiesMixin,
 ):
 
@@ -52,6 +54,7 @@ class TheOneRingConnection(
                        )
                        aliasing.AliasingMixin.__init__(self)
                        simple_presence.SimplePresenceMixin.__init__(self)
+                       presence.PresenceMixin.__init__(self)
                        capabilities.CapabilitiesMixin.__init__(self)
 
                        self._manager = weakref.proxy(manager)
diff --git a/src/presence.py b/src/presence.py
new file mode 100644 (file)
index 0000000..f078e27
--- /dev/null
@@ -0,0 +1,50 @@
+import logging
+
+import telepathy
+
+import gtk_toolbox
+import simple_presence
+import gvoice.state_machine as state_machine
+
+
+_moduleLogger = logging.getLogger('presence')
+
+
+class PresenceMixin(telepathy.server.ConnectionInterfacePresence, simple_presence.TheOneRingPresence):
+
+       def __init__(self):
+               telepathy.server.ConnectionInterfacePresence.__init__(self)
+               simple_presence.TheOneRingPresence.__init__(self)
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def GetStatuses(self):
+               # the arguments are in common to all on-line presences
+               arguments = {}
+
+               return dict(
+                       (localType, (telepathyType, True, True, arguments))
+                       for (localType, telepathyType) in self.TO_PRESENCE_TYPE.iteritems()
+               )
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def RequestPresence(self, contactIds):
+               presences = self.__get_presences(contactIds)
+               self.PresenceUpdate(presences)
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def GetPresence(self, contactIds):
+               return self.__get_presences(contactIds)
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def SetStatus(self, statuses):
+               assert len(statuses) == 1
+               status, arguments = statuses.items()[0]
+               assert len(arguments) == 0
+               self.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()
+               )
index dc44399..e5d5973 100644 (file)
@@ -25,17 +25,6 @@ class TheOneRingPresence(object):
                OFFLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE,
        }
 
-
-class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
-
-       def __init__(self):
-               telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
-
-               self._implement_property_get(
-                       telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
-                       {'Statuses' : self._get_statuses}
-               )
-
        @property
        def session(self):
                """
@@ -56,13 +45,12 @@ class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
                """
                raise NotImplementedError("Abstract function called")
 
-       @gtk_toolbox.log_exception(_moduleLogger)
-       def GetPresences(self, contacts):
+       def get_presences(self, contactIds):
                """
                @return {ContactHandle: (Status, Presence Type, Message)}
                """
                presences = {}
-               for handleId in contacts:
+               for handleId in contactIds:
                        h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
                        if isinstance(h, handle.ConnectionHandle):
                                isDnd = self.session.backend.is_dnd()
@@ -76,35 +64,58 @@ class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
                                                presence = TheOneRingPresence.AWAY
                                        else:
                                                raise telepathy.errors.InvalidArgument("Unsupported state on the state machine: %s" % state)
-                               personalMessage = u""
                                presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
                        else:
                                presence = TheOneRingPresence.ONLINE
-                               personalMessage = u""
                                presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
 
-                       presences[h] = (presenceType, presence, personalMessage)
+                       presences[h] = (presenceType, presence)
                return presences
 
-       @gtk_toolbox.log_exception(_moduleLogger)
-       def SetPresence(self, status, message):
-               if message:
-                       raise telepathy.errors.InvalidArgument("Messages aren't supported")
-
-               if status == TheOneRingPresence.ONLINE:
+       def set_presence(self, status):
+               if status == self.ONLINE:
                        self.session.backend.set_dnd(False)
                        self.session.stateMachine.set_state(state_machine.StateMachine.STATE_ACTIVE)
-               elif status == TheOneRingPresence.AWAY:
+               elif status == self.AWAY:
                        self.session.stateMachine.set_state(state_machine.StateMachine.STATE_IDLE)
-               elif status == TheOneRingPresence.HIDDEN:
+               elif status == self.HIDDEN:
                        self.session.backend.set_dnd(True)
-               elif status == TheOneRingPresence.OFFLINE:
+               elif status == self.OFFLINE:
                        self.Disconnect()
                else:
                        raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
                _moduleLogger.info("Setting Presence to '%s'" % status)
 
 
+class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence, TheOneRingPresence):
+
+       def __init__(self):
+               telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
+               TheOneRingPresence.__init__(self)
+
+               self._implement_property_get(
+                       telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
+                       {'Statuses' : self._get_statuses}
+               )
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def GetPresences(self, contacts):
+               """
+               @return {ContactHandle: (Status, Presence Type, Message)}
+               """
+               personalMessage = u""
+               return dict(
+                       (h, (presenceType, presence, personalMessage))
+                       for (h, (presenceType, presence)) in self.get_presences(contacts).iteritems()
+               )
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def SetPresence(self, status, message):
+               if message:
+                       raise telepathy.errors.InvalidArgument("Messages aren't supported")
+
+               self.set_presence(status)
+
        def _get_statuses(self):
                """
                Property mapping presence statuses available to the corresponding presence types
@@ -113,5 +124,5 @@ class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
                """
                return dict(
                        (localType, (telepathyType, True, False))
-                       for (localType, telepathyType) in TheOneRingPresence.TO_PRESENCE_TYPE.iteritems()
+                       for (localType, telepathyType) in self.TO_PRESENCE_TYPE.iteritems()
                )