ff67a4f26ddf99999b38f2861256bf369ef9ccdf
[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         IDLE = 'idle'
15         BUSY = 'dnd'
16
17         TO_PRESENCE_TYPE = {
18                 ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE,
19                 IDLE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY,
20                 BUSY: telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN,
21         }
22
23
24 class SimplePresenceMixin(telepathy.server.ConnectionInterfaceSimplePresence):
25
26         def __init__(self):
27                 telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
28
29                 dbus_interface = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence'
30
31                 self._implement_property_get(dbus_interface, {'Statuses' : self._get_statuses})
32
33         @property
34         def session(self):
35                 """
36                 @abstract
37                 """
38                 raise NotImplementedError()
39
40         @property
41         def handle(self):
42                 """
43                 @abstract
44                 """
45                 raise NotImplementedError("Abstract property called")
46
47         @gtk_toolbox.log_exception(_moduleLogger)
48         def GetPresences(self, contacts):
49                 """
50                 @return {ContactHandle: (Status, Presence Type, Message)}
51                 """
52                 presences = {}
53                 for handleId in contacts:
54                         h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
55                         if isinstance(h, handle.ConnectionHandle):
56                                 isDnd = self.session.backend.is_dnd()
57                                 if isDnd:
58                                         presence = TheOneRingPresence.BUSY
59                                 else:
60                                         # @todo switch this over to also supporting idle
61                                         presence = TheOneRingPresence.ONLINE
62                                 personalMessage = u""
63                                 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
64                         else:
65                                 presence = TheOneRingPresence.ONLINE
66                                 personalMessage = u""
67                                 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
68
69                         presences[h] = (presenceType, presence, personalMessage)
70                 return presences
71
72         @gtk_toolbox.log_exception(_moduleLogger)
73         def SetPresence(self, status, message):
74                 if message:
75                         raise telepathy.errors.InvalidArgument("Messages aren't supported")
76
77                 if status == TheOneRingPresence.ONLINE:
78                         self.session.backend.set_dnd(False)
79                 elif status == TheOneRingPresence.IDLE:
80                         # @todo Add idle support
81                         raise telepathy.errors.InvalidArgument("Not Supported Yet")
82                 elif status == TheOneRingPresence.BUSY:
83                         self.session.backend.set_dnd(True)
84                 else:
85                         raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
86                 _moduleLogger.info("Setting Presence to '%s'" % status)
87
88
89         def _get_statuses(self):
90                 """
91                 Property mapping presence statuses available to the corresponding presence types
92
93                 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
94                 """
95                 return dict(
96                         (localType, (telepathyType, True, False))
97                         for (localType, telepathyType) in TheOneRingPresence.TO_PRESENCE_TYPE.iteritems()
98                 )