X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fsimple_presence.py;h=e5d5973281100cfcc2d45e19ff429f46ee0c50cf;hp=ff67a4f26ddf99999b38f2861256bf369ef9ccdf;hb=d0c410418ea6a64e3b6b72ba132a14bcaebbf771;hpb=921437b90fb31fa0080ac3a706624ef66ccea7ae diff --git a/src/simple_presence.py b/src/simple_presence.py index ff67a4f..e5d5973 100644 --- a/src/simple_presence.py +++ b/src/simple_presence.py @@ -4,32 +4,27 @@ import telepathy import gtk_toolbox import handle +import gvoice.state_machine as state_machine _moduleLogger = logging.getLogger("simple_presence") class TheOneRingPresence(object): + + # Note: these strings are also in the theonering.profile file ONLINE = 'available' - IDLE = 'idle' - BUSY = 'dnd' + AWAY = 'away' + HIDDEN = 'hidden' + OFFLINE = 'offline' TO_PRESENCE_TYPE = { ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE, - IDLE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY, - BUSY: telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN, + AWAY: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY, + HIDDEN: telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN, + OFFLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE, } - -class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence): - - def __init__(self): - telepathy.server.ConnectionInterfaceSimplePresence.__init__(self) - - dbus_interface = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence' - - self._implement_property_get(dbus_interface, {'Statuses' : self._get_statuses}) - @property def session(self): """ @@ -44,48 +39,83 @@ class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence): """ raise NotImplementedError("Abstract property called") - @gtk_toolbox.log_exception(_moduleLogger) - def GetPresences(self, contacts): + def Disconnect(self): + """ + @abstract + """ + raise NotImplementedError("Abstract function called") + + 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() if isDnd: - presence = TheOneRingPresence.BUSY + presence = TheOneRingPresence.HIDDEN else: - # @todo switch this over to also supporting idle - presence = TheOneRingPresence.ONLINE - personalMessage = u"" + state = self.session.stateMachine.get_state() + if state == state_machine.StateMachine.STATE_ACTIVE: + presence = TheOneRingPresence.ONLINE + elif state == state_machine.StateMachine.STATE_IDLE: + presence = TheOneRingPresence.AWAY + else: + raise telepathy.errors.InvalidArgument("Unsupported state on the state machine: %s" % state) 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) - elif status == TheOneRingPresence.IDLE: - # @todo Add idle support - raise telepathy.errors.InvalidArgument("Not Supported Yet") - elif status == TheOneRingPresence.BUSY: + 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.backend.set_dnd(True) + 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 @@ -94,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() )