Implemented DnD in the backend and in the presence code
[theonering] / src / simple_presence.py
1 import logging
2
3 import telepathy
4
5 import gtk_toolbox
6 import handle
7
8
9 _moduleLogger = logging.getLogger("simple_presence")
10
11
12 class TheOneRingPresence(object):
13         ONLINE = 'available'
14         BUSY = 'dnd'
15
16         TO_PRESENCE_TYPE = {
17                 ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE,
18                 BUSY: telepathy.constants.CONNECTION_PRESENCE_TYPE_BUSY,
19         }
20
21
22 class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
23
24         def __init__(self):
25                 telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
26
27                 dbus_interface = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence'
28
29                 self._implement_property_get(dbus_interface, {'Statuses' : self._get_statuses})
30
31         @property
32         def session(self):
33                 """
34                 @abstract
35                 """
36                 raise NotImplementedError()
37
38         @property
39         def handle(self):
40                 """
41                 @abstract
42                 """
43                 raise NotImplementedError("Abstract property called")
44
45         @gtk_toolbox.log_exception(_moduleLogger)
46         def GetPresences(self, contacts):
47                 """
48                 @return {ContactHandle: (Status, Presence Type, Message)}
49                 """
50                 presences = {}
51                 for handleId in contacts:
52                         h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
53                         if isinstance(h, handle.ConnectionHandle):
54                                 isDnd = self.session.backend.is_dnd()
55                                 presence = TheOneRingPresence.BUSY if isDnd else TheOneRingPresence.ONLINE
56                                 personalMessage = u""
57                                 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
58                         else:
59                                 presence = TheOneRingPresence.ONLINE
60                                 personalMessage = u""
61                                 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
62
63                         presences[h] = (presenceType, presence, personalMessage)
64                 return presences
65
66         @gtk_toolbox.log_exception(_moduleLogger)
67         def SetPresence(self, status, message):
68                 if message:
69                         raise telepathy.errors.InvalidArgument("Messages aren't supported")
70
71
72                 if status == TheOneRingPresence.ONLINE:
73                         self.gvoice_backend.set_dnd(False)
74                 elif status == TheOneRingPresence.BUSY:
75                         self.gvoice_backend.set_dnd(True)
76                 else:
77                         raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
78                 _moduleLogger.info("Setting Presence to '%s'" % status)
79
80
81         def _get_statuses(self):
82                 """
83                 Property mapping presence statuses available to the corresponding presence types
84
85                 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
86                 """
87                 return {
88                         TheOneRingPresence.ONLINE: (
89                                 telepathy.CONNECTION_PRESENCE_TYPE_AVAILABLE,
90                                 True, False
91                         ),
92                         TheOneRingPresence.BUSY: (
93                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
94                                 True, False
95                         ),
96                 }
97