Forgot to add some files to previous commit
[theonering] / src / simple_presence.py
1 import logging
2
3 import telepathy
4
5
6 class ButterflyPresenceMapping(object):
7         ONLINE = 'available'
8         AWAY = 'away'
9         BUSY = 'dnd'
10         IDLE = 'xa'
11         BRB = 'brb'
12         PHONE = 'phone'
13         LUNCH = 'lunch'
14         INVISIBLE = 'hidden'
15         OFFLINE = 'offline'
16
17         to_pymsn = {
18                 ONLINE: pymsn.Presence.ONLINE,
19                 AWAY: pymsn.Presence.AWAY,
20                 BUSY: pymsn.Presence.BUSY,
21                 IDLE: pymsn.Presence.IDLE,
22                 BRB: pymsn.Presence.BE_RIGHT_BACK,
23                 PHONE: pymsn.Presence.ON_THE_PHONE,
24                 LUNCH: pymsn.Presence.OUT_TO_LUNCH,
25                 INVISIBLE: pymsn.Presence.INVISIBLE,
26                 OFFLINE: pymsn.Presence.OFFLINE
27         }
28
29         to_telepathy = {
30                 pymsn.Presence.ONLINE: ONLINE,
31                 pymsn.Presence.AWAY: AWAY,
32                 pymsn.Presence.BUSY: BUSY,
33                 pymsn.Presence.IDLE: IDLE,
34                 pymsn.Presence.BE_RIGHT_BACK: BRB,
35                 pymsn.Presence.ON_THE_PHONE: PHONE,
36                 pymsn.Presence.OUT_TO_LUNCH: LUNCH,
37                 pymsn.Presence.INVISIBLE: INVISIBLE,
38                 pymsn.Presence.OFFLINE: OFFLINE
39         }
40
41         to_presence_type = {
42                 ONLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE,
43                 AWAY: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY,
44                 BUSY: telepathy.constants.CONNECTION_PRESENCE_TYPE_BUSY,
45                 IDLE: telepathy.constants.CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
46                 BRB: telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY,
47                 PHONE: telepathy.constants.CONNECTION_PRESENCE_TYPE_BUSY,
48                 LUNCH: telepathy.constants.CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
49                 INVISIBLE: telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN,
50                 OFFLINE: telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE
51         }
52
53
54 class ButterflySimplePresence(telepathy.server.ConnectionInterfaceSimplePresence):
55
56         def __init__(self):
57                 telepathy.server.ConnectionInterfaceSimplePresence.__init__(self)
58
59                 dbus_interface = 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence'
60
61                 self._implement_property_get(dbus_interface, {'Statuses' : self.get_statuses})
62
63         def GetPresences(self, contacts):
64                 return self.get_simple_presences(contacts)
65
66         def SetPresence(self, status, message):
67                 if status == ButterflyPresenceMapping.OFFLINE:
68                         self.Disconnect()
69
70                 try:
71                         presence = ButterflyPresenceMapping.to_pymsn[status]
72                 except KeyError:
73                         raise telepathy.errors.InvalidArgument
74                 message = message.encode("utf-8")
75
76                 logging.info("Setting Presence to '%s'" % presence)
77                 logging.info("Setting Personal message to '%s'" % message)
78
79                 if self._status != telepathy.CONNECTION_STATUS_CONNECTED:
80                         self._initial_presence = presence
81                         self._initial_personal_message = message
82                 else:
83                         self.msn_client.profile.personal_message = message
84                         self.msn_client.profile.presence = presence
85
86         def get_simple_presences(self, contacts):
87                 presences = {}
88                 for handle_id in contacts:
89                         handle = self.handle(telepathy.HANDLE_TYPE_CONTACT, handle_id)
90                         try:
91                                 contact = handle.contact
92                         except AttributeError:
93                                 contact = handle.profile
94
95                         if contact is not None:
96                                 presence = ButterflyPresenceMapping.to_telepathy[contact.presence]
97                                 personal_message = unicode(contact.personal_message, "utf-8")
98                         else:
99                                 presence = ButterflyPresenceMapping.OFFLINE
100                                 personal_message = u""
101
102                         presence_type = ButterflyPresenceMapping.to_presence_type[presence]
103
104                         presences[handle] = (presence_type, presence, personal_message)
105                 return presences
106
107         def get_statuses(self):
108                 # you get one of these for each status
109                 # {name:(Type, May_Set_On_Self, Can_Have_Message}
110                 return {
111                         ButterflyPresenceMapping.ONLINE:(
112                                 telepathy.CONNECTION_PRESENCE_TYPE_AVAILABLE,
113                                 True, True),
114                         ButterflyPresenceMapping.AWAY:(
115                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
116                                 True, True),
117                         ButterflyPresenceMapping.BUSY:(
118                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
119                                 True, True),
120                         ButterflyPresenceMapping.IDLE:(
121                                 telepathy.CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
122                                 True, True),
123                         ButterflyPresenceMapping.BRB:(
124                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
125                                 True, True),
126                         ButterflyPresenceMapping.PHONE:(
127                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
128                                 True, True),
129                         ButterflyPresenceMapping.LUNCH:(
130                                 telepathy.CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
131                                 True, True),
132                         ButterflyPresenceMapping.INVISIBLE:(
133                                 telepathy.CONNECTION_PRESENCE_TYPE_HIDDEN,
134                                 True, False),
135                         ButterflyPresenceMapping.OFFLINE:(
136                                 telepathy.CONNECTION_PRESENCE_TYPE_OFFLINE,
137                                 True, False)
138                 }
139