Re-arranging for packaging prep
[theonering] / src / presence.py
1 import logging
2
3 import telepathy
4
5 import simple_presence
6
7
8 class ButterflyPresence(telepathy.server.ConnectionInterfacePresence):
9
10         def __init__(self):
11                 telepathy.server.ConnectionInterfacePresence.__init__(self)
12
13         def GetStatuses(self):
14                 # the arguments are in common to all on-line presences
15                 arguments = {'message' : 's'}
16
17                 # you get one of these for each status
18                 # {name:(type, self, exclusive, {argument:types}}
19                 return {
20                         simple_presence.ButterflyPresenceMapping.ONLINE:(
21                                 telepathy.CONNECTION_PRESENCE_TYPE_AVAILABLE,
22                                 True, True, arguments),
23                         simple_presence.ButterflyPresenceMapping.AWAY:(
24                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
25                                 True, True, arguments),
26                         simple_presence.ButterflyPresenceMapping.BUSY:(
27                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
28                                 True, True, arguments),
29                         simple_presence.ButterflyPresenceMapping.IDLE:(
30                                 telepathy.CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
31                                 True, True, arguments),
32                         simple_presence.ButterflyPresenceMapping.BRB:(
33                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
34                                 True, True, arguments),
35                         simple_presence.ButterflyPresenceMapping.PHONE:(
36                                 telepathy.CONNECTION_PRESENCE_TYPE_AWAY,
37                                 True, True, arguments),
38                         simple_presence.ButterflyPresenceMapping.LUNCH:(
39                                 telepathy.CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY,
40                                 True, True, arguments),
41                         simple_presence.ButterflyPresenceMapping.INVISIBLE:(
42                                 telepathy.CONNECTION_PRESENCE_TYPE_HIDDEN,
43                                 True, True, {}),
44                         simple_presence.ButterflyPresenceMapping.OFFLINE:(
45                                 telepathy.CONNECTION_PRESENCE_TYPE_OFFLINE,
46                                 True, True, {})
47                 }
48
49         def RequestPresence(self, contacts):
50                 presences = self.get_presences(contacts)
51                 self.PresenceUpdate(presences)
52
53         def GetPresence(self, contacts):
54                 return self.get_presences(contacts)
55
56         def SetStatus(self, statuses):
57                 status, arguments = statuses.items()[0]
58                 if status == simple_presence.ButterflyPresenceMapping.OFFLINE:
59                         self.Disconnect()
60
61                 presence = simple_presence.ButterflyPresenceMapping.to_pymsn[status]
62                 message = arguments.get('message', u'').encode("utf-8")
63
64                 logging.info("Setting Presence to '%s'" % presence)
65                 logging.info("Setting Personal message to '%s'" % message)
66
67                 if self._status != telepathy.CONNECTION_STATUS_CONNECTED:
68                         self._initial_presence = presence
69                         self._initial_personal_message = message
70                 else:
71                         self.msn_client.profile.personal_message = message
72                         self.msn_client.profile.presence = presence
73
74         def get_presences(self, contacts):
75                 presences = {}
76                 for handleId in contacts:
77                         h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
78                         try:
79                                 contact = h.contact
80                         except AttributeError:
81                                 contact = h.profile
82
83                         if contact is not None:
84                                 presence = simple_presence.ButterflyPresenceMapping.to_telepathy[contact.presence]
85                                 personal_message = unicode(contact.personal_message, "utf-8")
86                         else:
87                                 presence = simple_presence.ButterflyPresenceMapping.OFFLINE
88                                 personal_message = u""
89
90                         arguments = {}
91                         if personal_message:
92                                 arguments = {'message' : personal_message}
93
94                         presences[h] = (0, {presence : arguments}) # TODO: Timestamp
95                 return presences