X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fsimple_presence.py;h=fa306bd651486d99cf60b949f9b82871fce69133;hp=4219e84952591f1b3693c5d6bc092bfcc5ca920d;hb=da66a4ebd1deb3939a5bcf4abcd0d6d9708b59d8;hpb=a6498a3dd1db8b4379713ebda993dd6b3ec86b08 diff --git a/src/simple_presence.py b/src/simple_presence.py index 4219e84..fa306bd 100644 --- a/src/simple_presence.py +++ b/src/simple_presence.py @@ -1,63 +1,130 @@ import logging +import dbus import telepathy +import util.misc as misc_utils +import tp +import handle +import gvoice.state_machine as state_machine -class TheOneRingPresence(object): - ONLINE = 'available' - BUSY = 'dnd' - TO_PRESENCE_TYPE = { - ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE, - BUSY: telepathy.constants.CONNECTION_PRESENCE_TYPE_BUSY, - } +_moduleLogger = logging.getLogger(__name__) -class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence): +class TheOneRingPresence(object): - def __init__(self): - telepathy.server.ConnectionInterfaceSimplePresence.__init__(self) + # Note: these strings are also in the theonering.profile file + ONLINE = 'available' + AWAY = 'away' + HIDDEN = 'hidden' + OFFLINE = 'offline' - dbus_interface = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence' + TO_PRESENCE_TYPE = { + ONLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE), + AWAY: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY), + HIDDEN: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN), + OFFLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE), + } - self._implement_property_get(dbus_interface, {'Statuses' : self._get_statuses}) + def __init__(self, ignoreDND): + self.__ignoreDND = ignoreDND @property - def gvoice_backend(self): + def session(self): """ @abstract """ raise NotImplementedError() - def GetPresences(self, contacts): + def Disconnect(self): """ - @todo Figure out how to know when its self and get whether busy or not + @abstract + """ + raise NotImplementedError("Abstract function called") + + def get_handle_by_id(self, handleType, handleId): + """ + @abstract + """ + raise NotImplementedError("Abstract function called") + def get_presences(self, contactIds): + """ @return {ContactHandle: (Status, Presence Type, Message)} """ presences = {} - for handleId in contacts: - handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId) + 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() if not self.__ignoreDND else False + if isDnd: + presence = TheOneRingPresence.HIDDEN + else: + state = self.session.stateMachine.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.AWAY + presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence] + + presences[h] = (presenceType, presence) + return presences - presence = TheOneRingPresence.BUSY - personalMessage = u"" - presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence] + def set_presence(self, status): + if status == self.ONLINE: + 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: + if not self.__ignoreDND: + self.session.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) - presences[handle] = (presenceType, presence, personalMessage) - return presences +class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence): + + def __init__(self, torPresence): + tp.ConnectionInterfaceSimplePresence.__init__(self) + self.__torPresence = torPresence + + self._implement_property_get( + tp.CONNECTION_INTERFACE_SIMPLE_PRESENCE, + {'Statuses' : self._get_statuses} + ) + + @misc_utils.log_exception(_moduleLogger) + def GetPresences(self, contacts): + """ + @return {ContactHandle: (Status, Presence Type, Message)} + """ + personalMessage = u"" + return dbus.Dictionary( + ( + (h, dbus.Struct((presenceType, presence, personalMessage), signature="uss")) + for (h, (presenceType, presence)) in + self.__torPresence.get_presences(contacts).iteritems() + ), + signature="u(uss)" + ) + + @misc_utils.log_exception(_moduleLogger) def SetPresence(self, status, message): if message: - raise telepathy.errors.InvalidArgument - - if status == TheOneRingPresence.ONLINE: - self.gvoice_backend.mark_dnd(True) - elif status == TheOneRingPresence.BUSY: - self.gvoice_backend.mark_dnd(False) - else: - raise telepathy.errors.InvalidArgument - logging.info("Setting Presence to '%s'" % status) + raise telepathy.errors.InvalidArgument("Messages aren't supported") + self.__torPresence.set_presence(status) def _get_statuses(self): """ @@ -65,14 +132,7 @@ class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence): @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)} """ - return { - TheOneRingPresence.ONLINE: ( - telepathy.CONNECTION_PRESENCE_TYPE_AVAILABLE, - True, False - ), - TheOneRingPresence.BUSY: ( - telepathy.CONNECTION_PRESENCE_TYPE_AWAY, - True, False - ), - } - + return dict( + (localType, (telepathyType, True, False)) + for (localType, telepathyType) in self.__torPresence.TO_PRESENCE_TYPE.iteritems() + )