Cleaned up presence and cleaned up the exceptions
[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         @property
38         def handle(self):
39                 """
40                 @abstract
41                 """
42                 raise NotImplementedError("Abstract property called")
43
44         @gtk_toolbox.log_exception(_moduleLogger)
45         def GetPresences(self, contacts):
46                 """
47                 @todo Figure out how to know when its self and get whether busy or not
48
49                 @return {ContactHandle: (Status, Presence Type, Message)}
50                 """
51                 presences = {}
52                 for handleId in contacts:
53                         handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
54
55                         presence = TheOneRingPresence.BUSY
56                         personalMessage = u""
57                         presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
58
59                         presences[handle] = (presenceType, presence, personalMessage)
60                 return presences
61
62         @gtk_toolbox.log_exception(_moduleLogger)
63         def SetPresence(self, status, message):
64                 if message:
65                         raise telepathy.errors.InvalidArgument("Messages aren't supported")
66
67                 if status == TheOneRingPresence.ONLINE:
68                         self.gvoice_backend.mark_dnd(True)
69                 elif status == TheOneRingPresence.BUSY:
70                         raise telepathy.errors.NotAvailable("DnD support not yet added to TheOneRing")
71                         self.gvoice_backend.mark_dnd(False)
72                 else:
73                         raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
74                 _moduleLogger.info("Setting Presence to '%s'" % status)
75
76
77         def _get_statuses(self):
78                 """
79                 Property mapping presence statuses available to the corresponding presence types
80
81                 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
82                 """
83                 return {
84                         TheOneRingPresence.ONLINE: (
85                                 telepathy.CONNECTION_PRESENCE_TYPE_AVAILABLE,
86                                 True, False
87                         ),
88                         TheOneRingPresence.BUSY: (
89                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
90                                 True, False
91                         ),
92                 }
93