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