7 import gvoice.state_machine as state_machine
10 _moduleLogger = logging.getLogger("simple_presence")
13 class TheOneRingPresence(object):
15 # Note: these strings are also in the theonering.profile file
22 ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE,
23 AWAY: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY,
24 HIDDEN: telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN,
25 OFFLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE,
33 raise NotImplementedError()
39 raise NotImplementedError("Abstract function called")
41 def handle(self, handleType, handleId):
45 raise NotImplementedError("Abstract function called")
47 def get_presences(self, contactIds):
49 @return {ContactHandle: (Status, Presence Type, Message)}
52 for handleId in contactIds:
53 h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
54 if isinstance(h, handle.ConnectionHandle):
55 isDnd = self.session.backend.is_dnd()
57 presence = TheOneRingPresence.HIDDEN
59 state = self.session.stateMachine.state
60 if state == state_machine.StateMachine.STATE_ACTIVE:
61 presence = TheOneRingPresence.ONLINE
62 elif state == state_machine.StateMachine.STATE_IDLE:
63 presence = TheOneRingPresence.AWAY
65 raise telepathy.errors.InvalidArgument("Unsupported state on the state machine: %s" % state)
66 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
68 presence = TheOneRingPresence.ONLINE
69 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
71 presences[h] = (presenceType, presence)
74 def set_presence(self, status):
75 if status == self.ONLINE:
76 self.session.backend.set_dnd(False)
77 self.session.stateMachine.set_state(state_machine.StateMachine.STATE_ACTIVE)
78 elif status == self.AWAY:
79 self.session.stateMachine.set_state(state_machine.StateMachine.STATE_IDLE)
80 elif status == self.HIDDEN:
81 self.session.backend.set_dnd(True)
82 elif status == self.OFFLINE:
85 raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
86 _moduleLogger.info("Setting Presence to '%s'" % status)
89 class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence, TheOneRingPresence):
92 telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
93 TheOneRingPresence.__init__(self)
95 self._implement_property_get(
96 telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
97 {'Statuses' : self._get_statuses}
100 @gtk_toolbox.log_exception(_moduleLogger)
101 def GetPresences(self, contacts):
103 @return {ContactHandle: (Status, Presence Type, Message)}
105 personalMessage = u""
107 (h, (presenceType, presence, personalMessage))
108 for (h, (presenceType, presence)) in self.get_presences(contacts).iteritems()
111 @gtk_toolbox.log_exception(_moduleLogger)
112 def SetPresence(self, status, message):
114 raise telepathy.errors.InvalidArgument("Messages aren't supported")
116 self.set_presence(status)
118 def _get_statuses(self):
120 Property mapping presence statuses available to the corresponding presence types
122 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
125 (localType, (telepathyType, True, False))
126 for (localType, telepathyType) in self.TO_PRESENCE_TYPE.iteritems()