Getting auto-voicemail update working
[gc-dialer] / src / call_handler.py
1 #!/usr/bin/env python
2
3 from __future__ import with_statement
4 from __future__ import division
5
6 import logging
7
8 from PyQt4 import QtCore
9 import dbus
10 try:
11         import telepathy as _telepathy
12         import util.tp_utils as telepathy_utils
13         telepathy = _telepathy
14 except ImportError:
15         telepathy = None
16
17 import util.misc as misc_utils
18
19
20 _moduleLogger = logging.getLogger(__name__)
21
22
23 class _FakeSignaller(object):
24
25         def start(self):
26                 pass
27
28         def stop(self):
29                 pass
30
31
32 class _MissedCallWatcher(QtCore.QObject):
33
34         callMissed = QtCore.pyqtSignal()
35
36         def __init__(self):
37                 QtCore.QObject.__init__(self)
38                 self._isStarted = False
39                 self._isSupported = True
40
41                 self._newChannelSignaller = telepathy_utils.NewChannelSignaller(self._on_new_channel)
42                 self._outstandingRequests = []
43
44         @property
45         def isSupported(self):
46                 return self._isSupported
47
48         def start(self):
49                 try:
50                         self._newChannelSignaller.start()
51                 except RuntimeError:
52                         _moduleLogger.exception("Missed call detection not supported")
53                         self._newChannelSignaller = _FakeSignaller()
54                         self._isSupported = False
55                 self._isStarted = True
56
57         def stop(self):
58                 if not self._isStarted:
59                         _moduleLogger.info("voicemail monitor stopped without starting")
60                         return
61                 _moduleLogger.info("Stopping voicemail refresh")
62                 self._newChannelSignaller.stop()
63
64                 # I don't want to trust whether the cancel happens within the current
65                 # callback or not which could be the deciding factor between invalid
66                 # iterators or infinite loops
67                 localRequests = [r for r in self._outstandingRequests]
68                 for request in localRequests:
69                         localRequests.cancel()
70
71                 self._isStarted = False
72
73         @misc_utils.log_exception(_moduleLogger)
74         def _on_new_channel(self, bus, serviceName, connObjectPath, channelObjectPath, channelType):
75                 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
76                         return
77
78                 conn = telepathy.client.Connection(serviceName, connObjectPath)
79                 try:
80                         chan = telepathy.client.Channel(serviceName, channelObjectPath)
81                 except dbus.exceptions.UnknownMethodException:
82                         _moduleLogger.exception("Client might not have implemented a deprecated method")
83                         return
84                 missDetection = telepathy_utils.WasMissedCall(
85                         bus, conn, chan, self._on_missed_call, self._on_error_for_missed
86                 )
87                 self._outstandingRequests.append(missDetection)
88
89         @misc_utils.log_exception(_moduleLogger)
90         def _on_missed_call(self, missDetection):
91                 _moduleLogger.info("Missed a call")
92                 self.callMissed.emit()
93                 self._outstandingRequests.remove(missDetection)
94
95         @misc_utils.log_exception(_moduleLogger)
96         def _on_error_for_missed(self, missDetection, reason):
97                 _moduleLogger.debug("Error: %r claims %r" % (missDetection, reason))
98                 self._outstandingRequests.remove(missDetection)
99
100
101 class _DummyMissedCallWatcher(QtCore.QObject):
102
103         callMissed = QtCore.pyqtSignal()
104
105         def __init__(self):
106                 QtCore.QObject.__init__(self)
107                 self._isStarted = False
108
109         @property
110         def isSupported(self):
111                 return False
112
113         def start(self):
114                 self._isStarted = True
115
116         def stop(self):
117                 if not self._isStarted:
118                         _moduleLogger.info("voicemail monitor stopped without starting")
119                         return
120                 _moduleLogger.info("Stopping voicemail refresh")
121                 self._isStarted = False
122
123
124 if telepathy is not None:
125         MissedCallWatcher = _MissedCallWatcher
126 else:
127         MissedCallWatcher = _DummyMissedCallWatcher
128
129
130 if __name__ == "__main__":
131         pass
132