Starting to generalize the gv backend
[theonering] / src / simple_presence.py
1 import logging
2
3 import telepathy
4
5
6 _moduleLogger = logging.getLogger("simple_presence")
7
8
9 class TheOneRingPresence(object):
10         ONLINE = 'available'
11         BUSY = 'dnd'
12
13         TO_PRESENCE_TYPE = {
14                 ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE,
15                 BUSY: telepathy.constants.CONNECTION_PRESENCE_TYPE_BUSY,
16         }
17
18
19 class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
20
21         def __init__(self):
22                 telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
23
24                 dbus_interface = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence'
25
26                 self._implement_property_get(dbus_interface, {'Statuses' : self._get_statuses})
27
28         @property
29         def gvoice_backend(self):
30                 """
31                 @abstract
32                 """
33                 raise NotImplementedError()
34
35         def GetPresences(self, contacts):
36                 """
37                 @todo Figure out how to know when its self and get whether busy or not
38
39                 @return {ContactHandle: (Status, Presence Type, Message)}
40                 """
41                 presences = {}
42                 for handleId in contacts:
43                         handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
44
45                         presence = TheOneRingPresence.BUSY
46                         personalMessage = u""
47                         presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
48
49                         presences[handle] = (presenceType, presence, personalMessage)
50                 return presences
51
52         def SetPresence(self, status, message):
53                 if message:
54                         raise telepathy.errors.InvalidArgument
55
56                 if status == TheOneRingPresence.ONLINE:
57                         self.gvoice_backend.mark_dnd(True)
58                 elif status == TheOneRingPresence.BUSY:
59                         self.gvoice_backend.mark_dnd(False)
60                 else:
61                         raise telepathy.errors.InvalidArgument
62                 _moduleLogger.info("Setting Presence to '%s'" % status)
63
64
65         def _get_statuses(self):
66                 """
67                 Property mapping presence statuses available to the corresponding presence types
68
69                 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
70                 """
71                 return {
72                         TheOneRingPresence.ONLINE: (
73                                 telepathy.CONNECTION_PRESENCE_TYPE_AVAILABLE,
74                                 True, False
75                         ),
76                         TheOneRingPresence.BUSY: (
77                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
78                                 True, False
79                         ),
80                 }
81