5 import util.misc as misc_utils
8 import gvoice.state_machine as state_machine
11 _moduleLogger = logging.getLogger(__name__)
14 class TheOneRingPresence(object):
16 # Note: these strings are also in the theonering.profile file
23 ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE,
24 AWAY: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY,
25 HIDDEN: telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN,
26 OFFLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE,
34 raise NotImplementedError()
40 raise NotImplementedError("Abstract function called")
42 def get_handle_by_id(self, handleType, handleId):
46 raise NotImplementedError("Abstract function called")
48 def get_presences(self, contactIds):
50 @return {ContactHandle: (Status, Presence Type, Message)}
53 for handleId in contactIds:
54 h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
55 if isinstance(h, handle.ConnectionHandle):
56 isDnd = self.session.is_dnd()
58 presence = TheOneRingPresence.HIDDEN
60 state = self.session.stateMachine.state
61 if state == state_machine.StateMachine.STATE_ACTIVE:
62 presence = TheOneRingPresence.ONLINE
63 elif state == state_machine.StateMachine.STATE_IDLE:
64 presence = TheOneRingPresence.AWAY
66 raise telepathy.errors.InvalidArgument("Unsupported state on the state machine: %s" % state)
67 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
69 presence = TheOneRingPresence.AWAY
70 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
72 presences[h] = (presenceType, presence)
75 def set_presence(self, status):
76 if status == self.ONLINE:
77 self.session.set_dnd(False)
78 self.session.stateMachine.set_state(state_machine.StateMachine.STATE_ACTIVE)
79 elif status == self.AWAY:
80 self.session.stateMachine.set_state(state_machine.StateMachine.STATE_IDLE)
81 elif status == self.HIDDEN:
82 self.session.set_dnd(True)
83 elif status == self.OFFLINE:
86 raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
87 _moduleLogger.info("Setting Presence to '%s'" % status)
90 class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence, TheOneRingPresence):
93 tp.ConnectionInterfaceSimplePresence.__init__(self)
94 TheOneRingPresence.__init__(self)
96 self._implement_property_get(
97 tp.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
98 {'Statuses' : self._get_statuses}
101 @misc_utils.log_exception(_moduleLogger)
102 def GetPresences(self, contacts):
104 @return {ContactHandle: (Status, Presence Type, Message)}
106 personalMessage = u""
108 (h, (presenceType, presence, personalMessage))
109 for (h, (presenceType, presence)) in self.get_presences(contacts).iteritems()
112 @misc_utils.log_exception(_moduleLogger)
113 def SetPresence(self, status, message):
115 raise telepathy.errors.InvalidArgument("Messages aren't supported")
117 self.set_presence(status)
119 def _get_statuses(self):
121 Property mapping presence statuses available to the corresponding presence types
123 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
126 (localType, (telepathyType, True, False))
127 for (localType, telepathyType) in self.TO_PRESENCE_TYPE.iteritems()