6 import util.misc as misc_utils
9 import gvoice.state_machine as state_machine
12 _moduleLogger = logging.getLogger(__name__)
15 class TheOneRingPresence(object):
17 # Note: these strings are also in the theonering.profile file
24 ONLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE),
25 AWAY: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY),
26 HIDDEN: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN),
27 OFFLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE),
30 def __init__(self, ignoreDND):
31 self.__ignoreDND = ignoreDND
38 raise NotImplementedError()
44 raise NotImplementedError("Abstract function called")
46 def get_handle_by_id(self, handleType, handleId):
50 raise NotImplementedError("Abstract function called")
52 def get_presences(self, contactIds):
54 @return {ContactHandle: (Status, Presence Type, Message)}
57 for handleId in contactIds:
58 h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
59 if isinstance(h, handle.ConnectionHandle):
60 isDnd = self.session.is_dnd() if not self.__ignoreDND else False
62 presence = TheOneRingPresence.HIDDEN
64 state = self.session.stateMachine.state
65 if state == state_machine.StateMachine.STATE_ACTIVE:
66 presence = TheOneRingPresence.ONLINE
67 elif state == state_machine.StateMachine.STATE_IDLE:
68 presence = TheOneRingPresence.AWAY
70 raise telepathy.errors.InvalidArgument("Unsupported state on the state machine: %s" % state)
71 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
73 presence = TheOneRingPresence.AWAY
74 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
76 presences[h] = (presenceType, presence)
79 def set_presence(self, status):
80 if status == self.ONLINE:
81 if not self.__ignoreDND:
82 self.session.set_dnd(False)
83 self.session.stateMachine.set_state(state_machine.StateMachine.STATE_ACTIVE)
84 elif status == self.AWAY:
85 self.session.stateMachine.set_state(state_machine.StateMachine.STATE_IDLE)
86 elif status == self.HIDDEN:
87 if not self.__ignoreDND:
88 self.session.set_dnd(True)
89 elif status == self.OFFLINE:
92 raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
93 _moduleLogger.info("Setting Presence to '%s'" % status)
96 class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence):
98 def __init__(self, torPresence):
99 tp.ConnectionInterfaceSimplePresence.__init__(self)
100 self.__torPresence = torPresence
102 self._implement_property_get(
103 tp.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
104 {'Statuses' : self._get_statuses}
107 @misc_utils.log_exception(_moduleLogger)
108 def GetPresences(self, contacts):
110 @return {ContactHandle: (Status, Presence Type, Message)}
112 personalMessage = u""
113 return dbus.Dictionary(
115 (h, dbus.Struct((presenceType, presence, personalMessage), signature="uss"))
116 for (h, (presenceType, presence)) in
117 self.__torPresence.get_presences(contacts).iteritems()
122 @misc_utils.log_exception(_moduleLogger)
123 def SetPresence(self, status, message):
125 raise telepathy.errors.InvalidArgument("Messages aren't supported")
127 self.__torPresence.set_presence(status)
129 def _get_statuses(self):
131 Property mapping presence statuses available to the corresponding presence types
133 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
136 (localType, (telepathyType, True, False))
137 for (localType, telepathyType) in self.__torPresence.TO_PRESENCE_TYPE.iteritems()