First pass at refresh on missed calls
authorEd Page <eopage@byu.net>
Thu, 24 Feb 2011 23:55:23 +0000 (17:55 -0600)
committerEd Page <eopage@byu.net>
Wed, 13 Apr 2011 03:08:47 +0000 (22:08 -0500)
src/call_handler.py [new file with mode: 0644]
src/dialcentral_qt.py

diff --git a/src/call_handler.py b/src/call_handler.py
new file mode 100644 (file)
index 0000000..689e87a
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/env python
+
+from __future__ import with_statement
+from __future__ import division
+
+import logging
+
+from PyQt4 import QtCore
+import dbus
+try:
+       import telepathy as _telepathy
+       telepathy = _telepathy
+except ImportError:
+       _telepathy = None
+
+import constants
+import util.tp_utils as telepathy_utils
+import util.misc as misc_utils
+
+
+_moduleLogger = logging.getLogger(__name__)
+
+
+class _MissedCallWatcher(QtCore.QObject):
+
+       callMissed = QtCore.pyqtSignal()
+
+       def __init__(self):
+               QtCore.QObject.__init__(self)
+               self._isStarted = False
+
+               self._newChannelSignaller = telepathy_utils.NewChannelSignaller(self._on_new_channel)
+               self._outstandingRequests = []
+
+       def start(self):
+               self._newChannelSignaller.start()
+               self._isStarted = True
+
+       def stop(self):
+               if not self._isStarted:
+                       _moduleLogger.info("voicemail monitor stopped without starting")
+                       return
+               _moduleLogger.info("Stopping voicemail refresh")
+               self._newChannelSignaller.stop()
+
+               # I don't want to trust whether the cancel happens within the current
+               # callback or not which could be the deciding factor between invalid
+               # iterators or infinite loops
+               localRequests = [r for r in self._outstandingRequests]
+               for request in localRequests:
+                       localRequests.cancel()
+
+               self._isStarted = False
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_new_channel(self, bus, serviceName, connObjectPath, channelObjectPath, channelType):
+               if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
+                       return
+
+               cmName = telepathy_utils.cm_from_path(connObjectPath)
+               if cmName == constants._telepathy_implementation_name_:
+                       _moduleLogger.debug("Ignoring channels from self to prevent deadlock")
+                       return
+
+               conn = telepathy.client.Connection(serviceName, connObjectPath)
+               try:
+                       chan = telepathy.client.Channel(serviceName, channelObjectPath)
+               except dbus.exceptions.UnknownMethodException:
+                       _moduleLogger.exception("Client might not have implemented a deprecated method")
+                       return
+               missDetection = telepathy_utils.WasMissedCall(
+                       bus, conn, chan, self._on_missed_call, self._on_error_for_missed
+               )
+               self._outstandingRequests.append(missDetection)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_missed_call(self, missDetection):
+               _moduleLogger.info("Missed a call")
+               self.callMissed.emit()
+               self._outstandingRequests.remove(missDetection)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_error_for_missed(self, missDetection, reason):
+               _moduleLogger.debug("Error: %r claims %r" % (missDetection, reason))
+               self._outstandingRequests.remove(missDetection)
+
+
+class _DummyMissedCallWatcher(QtCore.QObject):
+
+       callMissed = QtCore.pyqtSignal()
+
+       def __init__(self):
+               QtCore.QObject.__init__(self)
+               self._isStarted = False
+
+       def start(self):
+               self._isStarted = True
+
+       def stop(self):
+               if not self._isStarted:
+                       _moduleLogger.info("voicemail monitor stopped without starting")
+                       return
+               _moduleLogger.info("Stopping voicemail refresh")
+               self._isStarted = False
+
+
+if telepathy is not None:
+       MissedCallWatcher = _MissedCallWatcher
+else:
+       MissedCallWatcher = _DummyMissedCallWatcher
+
+
+if __name__ == "__main__":
+       pass
+
index 8523ad5..4626fe7 100755 (executable)
@@ -11,8 +11,11 @@ import logging
 
 from PyQt4 import QtGui
 from PyQt4 import QtCore
+import dbus
+import dbus.mainloop.glib
 
 import constants
+import call_handler
 from util import qtpie
 from util import qwrappers
 from util import qui_utils
@@ -350,6 +353,12 @@ class MainWindow(qwrappers.WindowWrapper):
                self._session.loggedIn.connect(self._on_login)
                self._session.loggedOut.connect(self._on_logout)
                self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
+               self._voicemailRefreshDelay = QtCore.QTimer()
+               self._voicemailRefreshDelay.setInterval(30 * 1000)
+               self._voicemailRefreshDelay.timeout.connect(self._on_call_missed)
+               self._voicemailRefreshDelay.setSingleShot(True)
+               self._callHandler = call_handler.MissedCallWatcher()
+               self._callHandler.callMissed.connect(self._voicemailRefreshDelay.start)
                self._defaultCredentials = "", ""
                self._curentCredentials = "", ""
                self._currentTab = 0
@@ -577,6 +586,12 @@ class MainWindow(qwrappers.WindowWrapper):
                else:
                        _moduleLogger.info("Unknown response")
 
+       @QtCore.pyqtSlot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_call_missed(self):
+               with qui_utils.notify_error(self._errorLog):
+                       self._session.update_messages(True)
+
        @QtCore.pyqtSlot(str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_session_error(self, message):
@@ -597,6 +612,7 @@ class MainWindow(qwrappers.WindowWrapper):
                        for tab in self._tabsContents:
                                tab.enable()
                        self._initialize_tab(self._currentTab)
+                       self._callHandler.start()
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
@@ -604,6 +620,7 @@ class MainWindow(qwrappers.WindowWrapper):
                with qui_utils.notify_error(self._errorLog):
                        for tab in self._tabsContents:
                                tab.disable()
+                       self._callHandler.stop()
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
@@ -669,6 +686,7 @@ class MainWindow(qwrappers.WindowWrapper):
 
 def run():
        app = QtGui.QApplication([])
+       l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        handle = Dialcentral(app)
        qtpie.init_pies()
        return app.exec_()