3 from __future__ import with_statement
4 from __future__ import division
8 from PyQt4 import QtCore
11 import telepathy as _telepathy
12 telepathy = _telepathy
17 import util.tp_utils as telepathy_utils
18 import util.misc as misc_utils
21 _moduleLogger = logging.getLogger(__name__)
24 class _MissedCallWatcher(QtCore.QObject):
26 callMissed = QtCore.pyqtSignal()
29 QtCore.QObject.__init__(self)
30 self._isStarted = False
32 self._newChannelSignaller = telepathy_utils.NewChannelSignaller(self._on_new_channel)
33 self._outstandingRequests = []
36 self._newChannelSignaller.start()
37 self._isStarted = True
40 if not self._isStarted:
41 _moduleLogger.info("voicemail monitor stopped without starting")
43 _moduleLogger.info("Stopping voicemail refresh")
44 self._newChannelSignaller.stop()
46 # I don't want to trust whether the cancel happens within the current
47 # callback or not which could be the deciding factor between invalid
48 # iterators or infinite loops
49 localRequests = [r for r in self._outstandingRequests]
50 for request in localRequests:
51 localRequests.cancel()
53 self._isStarted = False
55 @misc_utils.log_exception(_moduleLogger)
56 def _on_new_channel(self, bus, serviceName, connObjectPath, channelObjectPath, channelType):
57 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
60 cmName = telepathy_utils.cm_from_path(connObjectPath)
61 if cmName == constants._telepathy_implementation_name_:
62 _moduleLogger.debug("Ignoring channels from self to prevent deadlock")
65 conn = telepathy.client.Connection(serviceName, connObjectPath)
67 chan = telepathy.client.Channel(serviceName, channelObjectPath)
68 except dbus.exceptions.UnknownMethodException:
69 _moduleLogger.exception("Client might not have implemented a deprecated method")
71 missDetection = telepathy_utils.WasMissedCall(
72 bus, conn, chan, self._on_missed_call, self._on_error_for_missed
74 self._outstandingRequests.append(missDetection)
76 @misc_utils.log_exception(_moduleLogger)
77 def _on_missed_call(self, missDetection):
78 _moduleLogger.info("Missed a call")
79 self.callMissed.emit()
80 self._outstandingRequests.remove(missDetection)
82 @misc_utils.log_exception(_moduleLogger)
83 def _on_error_for_missed(self, missDetection, reason):
84 _moduleLogger.debug("Error: %r claims %r" % (missDetection, reason))
85 self._outstandingRequests.remove(missDetection)
88 class _DummyMissedCallWatcher(QtCore.QObject):
90 callMissed = QtCore.pyqtSignal()
93 QtCore.QObject.__init__(self)
94 self._isStarted = False
97 self._isStarted = True
100 if not self._isStarted:
101 _moduleLogger.info("voicemail monitor stopped without starting")
103 _moduleLogger.info("Stopping voicemail refresh")
104 self._isStarted = False
107 if telepathy is not None:
108 MissedCallWatcher = _MissedCallWatcher
110 MissedCallWatcher = _DummyMissedCallWatcher
113 if __name__ == "__main__":