Adding support for PySide's handling of QVariant
[gc-dialer] / src / session.py
index f113ae8..4b8a4bf 100644 (file)
@@ -12,7 +12,8 @@ try:
 except ImportError:
        import pickle
 
-from PyQt4 import QtCore
+import util.qt_compat as qt_compat
+QtCore = qt_compat.QtCore
 
 from util import qore_utils
 from util import qui_utils
@@ -37,15 +38,15 @@ class _DraftContact(object):
 
 class Draft(QtCore.QObject):
 
-       sendingMessage = QtCore.pyqtSignal()
-       sentMessage = QtCore.pyqtSignal()
-       calling = QtCore.pyqtSignal()
-       called = QtCore.pyqtSignal()
-       cancelling = QtCore.pyqtSignal()
-       cancelled = QtCore.pyqtSignal()
-       error = QtCore.pyqtSignal(str)
+       sendingMessage = qt_compat.Signal()
+       sentMessage = qt_compat.Signal()
+       calling = qt_compat.Signal()
+       called = qt_compat.Signal()
+       cancelling = qt_compat.Signal()
+       cancelled = qt_compat.Signal()
+       error = qt_compat.Signal(str)
 
-       recipientsChanged = QtCore.pyqtSignal()
+       recipientsChanged = qt_compat.Signal()
 
        def __init__(self, pool, backend, errorLog):
                QtCore.QObject.__init__(self)
@@ -198,19 +199,19 @@ class Session(QtCore.QObject):
 
        # @todo Somehow add support for csv contacts
 
-       stateChange = QtCore.pyqtSignal(str)
-       loggedOut = QtCore.pyqtSignal()
-       loggedIn = QtCore.pyqtSignal()
-       callbackNumberChanged = QtCore.pyqtSignal(str)
+       stateChange = qt_compat.Signal(str)
+       loggedOut = qt_compat.Signal()
+       loggedIn = qt_compat.Signal()
+       callbackNumberChanged = qt_compat.Signal(str)
 
-       accountUpdated = QtCore.pyqtSignal()
-       messagesUpdated = QtCore.pyqtSignal()
-       newMessages = QtCore.pyqtSignal()
-       historyUpdated = QtCore.pyqtSignal()
-       dndStateChange = QtCore.pyqtSignal(bool)
-       voicemailAvailable = QtCore.pyqtSignal(str, str)
+       accountUpdated = qt_compat.Signal()
+       messagesUpdated = qt_compat.Signal()
+       newMessages = qt_compat.Signal()
+       historyUpdated = qt_compat.Signal()
+       dndStateChange = qt_compat.Signal(bool)
+       voicemailAvailable = qt_compat.Signal(str, str)
 
-       error = QtCore.pyqtSignal(str)
+       error = qt_compat.Signal(str)
 
        LOGGEDOUT_STATE = "logged out"
        LOGGINGIN_STATE = "logging in"
@@ -220,6 +221,11 @@ class Session(QtCore.QObject):
        MESSAGE_VOICEMAILS = "Voicemail"
        MESSAGE_ALL = "All"
 
+       HISTORY_RECEIVED = "Received"
+       HISTORY_MISSED = "Missed"
+       HISTORY_PLACED = "Placed"
+       HISTORY_ALL = "All"
+
        _OLDEST_COMPATIBLE_FORMAT_VERSION = misc_utils.parse_version("1.3.0")
 
        _LOGGEDOUT_TIME = -1
@@ -235,7 +241,12 @@ class Session(QtCore.QObject):
                self._cachePath = cachePath
                self._voicemailCachePath = None
                self._username = None
+               self._password = None
                self._draft = Draft(self._pool, self._backend, self._errorLog)
+               self._delayedRelogin = QtCore.QTimer()
+               self._delayedRelogin.setInterval(0)
+               self._delayedRelogin.setSingleShot(True)
+               self._delayedRelogin.timeout.connect(self._on_delayed_relogin)
 
                self._contacts = {}
                self._accountUpdateTime = datetime.datetime(1971, 1, 1)
@@ -308,6 +319,10 @@ class Session(QtCore.QObject):
                le = concurrent.AsyncLinearExecution(self._pool, self._update_account), (), {}
                self._perform_op_while_loggedin(le)
 
+       def refresh_connection(self):
+               le = concurrent.AsyncLinearExecution(self._pool, self._refresh_authentication)
+               le.start()
+
        def get_contacts(self):
                return self._contacts
 
@@ -326,10 +341,10 @@ class Session(QtCore.QObject):
        def get_when_messages_updated(self):
                return self._messageUpdateTime
 
-       def update_history(self, force = True):
+       def update_history(self, historyType, force = True):
                if not force and self._history:
                        return
-               le = concurrent.AsyncLinearExecution(self._pool, self._update_history), (), {}
+               le = concurrent.AsyncLinearExecution(self._pool, self._update_history), (historyType, ), {}
                self._perform_op_while_loggedin(le)
 
        def get_history(self):
@@ -451,6 +466,7 @@ class Session(QtCore.QObject):
                                        self._loggedInTime = int(time.time())
                                        oldUsername = self._username
                                        self._username = username
+                                       self._password = password
                                        finalState = self.LOGGEDIN_STATE
                                        if oldUsername != self._username:
                                                needOps = not self._load()
@@ -490,6 +506,43 @@ class Session(QtCore.QObject):
                        if accountData is not None and self._callback:
                                self.set_callback_number(self._callback)
 
+       def _update_account(self):
+               try:
+                       with qui_utils.notify_busy(self._errorLog, "Updating Account"):
+                               accountData = yield (
+                                       self._backend[0].refresh_account_info,
+                                       (),
+                                       {},
+                               )
+               except Exception, e:
+                       _moduleLogger.exception("Reporting error to user")
+                       self.error.emit(str(e))
+                       return
+               self._loggedInTime = int(time.time())
+               self._process_account_data(accountData)
+
+       def _refresh_authentication(self):
+               try:
+                       with qui_utils.notify_busy(self._errorLog, "Updating Account"):
+                               accountData = yield (
+                                       self._backend[0].refresh_account_info,
+                                       (),
+                                       {},
+                               )
+                               accountData = None
+               except Exception, e:
+                       _moduleLogger.exception("Passing to user")
+                       self.error.emit(str(e))
+                       # refresh_account_info does not normally throw, so it is fine if we
+                       # just quit early because something seriously wrong is going on
+                       return
+
+               if accountData is not None:
+                       self._loggedInTime = int(time.time())
+                       self._process_account_data(accountData)
+               else:
+                       self._delayedRelogin.start()
+
        def _load(self):
                updateMessages = len(self._messages) != 0
                updateHistory = len(self._history) != 0
@@ -625,21 +678,6 @@ class Session(QtCore.QObject):
                import shutil
                shutil.rmtree(self._voicemailCachePath, True)
 
-       def _update_account(self):
-               try:
-                       assert self.state == self.LOGGEDIN_STATE, "Contacts requires being logged in (currently %s)" % self.state
-                       with qui_utils.notify_busy(self._errorLog, "Updating Account"):
-                               accountData = yield (
-                                       self._backend[0].refresh_account_info,
-                                       (),
-                                       {},
-                               )
-               except Exception, e:
-                       _moduleLogger.exception("Reporting error to user")
-                       self.error.emit(str(e))
-                       return
-               self._process_account_data(accountData)
-
        def _update_messages(self, messageType):
                try:
                        assert self.state == self.LOGGEDIN_STATE, "Messages requires being logged in (currently %s" % self.state
@@ -657,13 +695,13 @@ class Session(QtCore.QObject):
                self.messagesUpdated.emit()
                self._alert_on_messages(self._messages)
 
-       def _update_history(self):
+       def _update_history(self, historyType):
                try:
                        assert self.state == self.LOGGEDIN_STATE, "History requires being logged in (currently %s" % self.state
-                       with qui_utils.notify_busy(self._errorLog, "Updating History"):
+                       with qui_utils.notify_busy(self._errorLog, "Updating '%s' History" % historyType):
                                self._history = yield (
-                                       self._backend[0].get_recent,
-                                       (),
+                                       self._backend[0].get_call_history,
+                                       (historyType, ),
                                        {},
                                )
                except Exception, e:
@@ -776,3 +814,15 @@ class Session(QtCore.QObject):
                                continue
 
                        yield cleaned
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_delayed_relogin(self):
+               try:
+                       username = self._username
+                       password = self._password
+                       self.logout()
+                       self.login(username, password)
+               except Exception, e:
+                       _moduleLogger.exception("Passing to user")
+                       self.error.emit(str(e))
+                       return