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