3 from __future__ import with_statement
4 from __future__ import division
8 from PyQt4 import QtCore
11 import telepathy as _telepathy
12 import util.tp_utils as telepathy_utils
13 telepathy = _telepathy
17 import util.misc as misc_utils
20 _moduleLogger = logging.getLogger(__name__)
23 class _FakeSignaller(object):
32 class _MissedCallWatcher(QtCore.QObject):
34 callMissed = QtCore.pyqtSignal()
37 QtCore.QObject.__init__(self)
38 self._isStarted = False
39 self._isSupported = True
41 self._newChannelSignaller = telepathy_utils.NewChannelSignaller(self._on_new_channel)
42 self._outstandingRequests = []
45 def isSupported(self):
46 return self._isSupported
50 self._newChannelSignaller.start()
52 _moduleLogger.exception("Missed call detection not supported")
53 self._newChannelSignaller = _FakeSignaller()
54 self._isSupported = False
55 self._isStarted = True
58 if not self._isStarted:
59 _moduleLogger.info("voicemail monitor stopped without starting")
61 _moduleLogger.info("Stopping voicemail refresh")
62 self._newChannelSignaller.stop()
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()
71 self._isStarted = False
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:
78 conn = telepathy.client.Connection(serviceName, connObjectPath)
80 chan = telepathy.client.Channel(serviceName, channelObjectPath)
81 except dbus.exceptions.UnknownMethodException:
82 _moduleLogger.exception("Client might not have implemented a deprecated method")
84 missDetection = telepathy_utils.WasMissedCall(
85 bus, conn, chan, self._on_missed_call, self._on_error_for_missed
87 self._outstandingRequests.append(missDetection)
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)
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)
101 class _DummyMissedCallWatcher(QtCore.QObject):
103 callMissed = QtCore.pyqtSignal()
106 QtCore.QObject.__init__(self)
107 self._isStarted = False
110 def isSupported(self):
114 self._isStarted = True
117 if not self._isStarted:
118 _moduleLogger.info("voicemail monitor stopped without starting")
120 _moduleLogger.info("Stopping voicemail refresh")
121 self._isStarted = False
124 if telepathy is not None:
125 MissedCallWatcher = _MissedCallWatcher
127 MissedCallWatcher = _DummyMissedCallWatcher
130 if __name__ == "__main__":