Failed attempt at fixing the file transfer stuff
[theonering] / src / simple_presence.py
1 import logging
2
3 import dbus
4 import telepathy
5
6 import util.misc as misc_utils
7 import tp
8 import handle
9 import gvoice.state_machine as state_machine
10
11
12 _moduleLogger = logging.getLogger(__name__)
13
14
15 class TheOneRingPresence(object):
16
17         # Note: these strings are also in the theonering.profile file
18         ONLINE = 'available'
19         AWAY = 'away'
20         HIDDEN = 'hidden'
21         OFFLINE = 'offline'
22
23         TO_PRESENCE_TYPE = {
24                 ONLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_AVAILABLE),
25                 AWAY: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_AWAY),
26                 HIDDEN: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_HIDDEN),
27                 OFFLINE: dbus.UInt32(telepathy.constants.CONNECTION_PRESENCE_TYPE_OFFLINE),
28         }
29
30         def __init__(self, ignoreDND):
31                 self.__ignoreDND = ignoreDND
32
33         @property
34         def session(self):
35                 """
36                 @abstract
37                 """
38                 raise NotImplementedError()
39
40         def Disconnect(self):
41                 """
42                 @abstract
43                 """
44                 raise NotImplementedError("Abstract function called")
45
46         def get_handle_by_id(self, handleType, handleId):
47                 """
48                 @abstract
49                 """
50                 raise NotImplementedError("Abstract function called")
51
52         def get_presences(self, contactIds):
53                 """
54                 @return {ContactHandle: (Status, Presence Type, Message)}
55                 """
56                 presences = {}
57                 for handleId in contactIds:
58                         h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
59                         if isinstance(h, handle.ConnectionHandle):
60                                 isDnd = self.session.is_dnd() if not self.__ignoreDND else False
61                                 if isDnd:
62                                         presence = TheOneRingPresence.HIDDEN
63                                 else:
64                                         state = self.session.stateMachine.state
65                                         if state == state_machine.StateMachine.STATE_ACTIVE:
66                                                 presence = TheOneRingPresence.ONLINE
67                                         elif state == state_machine.StateMachine.STATE_IDLE:
68                                                 presence = TheOneRingPresence.AWAY
69                                         else:
70                                                 raise telepathy.errors.InvalidArgument("Unsupported state on the state machine: %s" % state)
71                                 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
72                         else:
73                                 presence = TheOneRingPresence.AWAY
74                                 presenceType = TheOneRingPresence.TO_PRESENCE_TYPE[presence]
75
76                         presences[h] = (presenceType, presence)
77                 return presences
78
79         def set_presence(self, status):
80                 if status == self.ONLINE:
81                         if not self.__ignoreDND:
82                                 self.session.set_dnd(False)
83                         self.session.stateMachine.set_state(state_machine.StateMachine.STATE_ACTIVE)
84                 elif status == self.AWAY:
85                         self.session.stateMachine.set_state(state_machine.StateMachine.STATE_IDLE)
86                 elif status == self.HIDDEN:
87                         if not self.__ignoreDND:
88                                 self.session.set_dnd(True)
89                 elif status == self.OFFLINE:
90                         self.Disconnect()
91                 else:
92                         raise telepathy.errors.InvalidArgument("Unsupported status: %r" % status)
93                 _moduleLogger.info("Setting Presence to '%s'" % status)
94
95
96 class SimplePresenceMixin(tp.ConnectionInterfaceSimplePresence):
97
98         def __init__(self, torPresence):
99                 tp.ConnectionInterfaceSimplePresence.__init__(self)
100                 self.__torPresence = torPresence
101
102                 self._implement_property_get(
103                         tp.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
104                         {'Statuses' : self._get_statuses}
105                 )
106
107         @misc_utils.log_exception(_moduleLogger)
108         def GetPresences(self, contacts):
109                 """
110                 @return {ContactHandle: (Status, Presence Type, Message)}
111                 """
112                 personalMessage = u""
113                 return dbus.Dictionary(
114                         (
115                                 (h, dbus.Struct((presenceType, presence, personalMessage), signature="uss"))
116                                 for (h, (presenceType, presence)) in
117                                         self.__torPresence.get_presences(contacts).iteritems()
118                         ),
119                         signature="u(uss)"
120                 )
121
122         @misc_utils.log_exception(_moduleLogger)
123         def SetPresence(self, status, message):
124                 if message:
125                         raise telepathy.errors.InvalidArgument("Messages aren't supported")
126
127                 self.__torPresence.set_presence(status)
128
129         def _get_statuses(self):
130                 """
131                 Property mapping presence statuses available to the corresponding presence types
132
133                 @returns {Name: (Telepathy Type, May Set On Self, Can Have Message)}
134                 """
135                 return dict(
136                         (localType, (telepathyType, True, False))
137                         for (localType, telepathyType) in self.__torPresence.TO_PRESENCE_TYPE.iteritems()
138                 )