Removing contact caching from backend, moving it to session
[gc-dialer] / src / session.py
index 0c650e0..28e56b1 100644 (file)
@@ -32,37 +32,66 @@ class Draft(QtCore.QObject):
 
        def send(self, text):
                assert 0 < len(self._contacts)
-               self.sendingMessage.emit()
-               self.error.emit("Not Implemented")
-               # self.clear()
+               le = concurrent.AsyncLinearExecution(self._pool, self._send)
+               le.start(text)
 
        def call(self):
                assert len(self._contacts) == 1
-               self.calling.emit()
-               self.error.emit("Not Implemented")
-               # self.clear()
+               le = concurrent.AsyncLinearExecution(self._pool, self._call)
+               le.start()
 
        def cancel(self):
-               self.cancelling.emit()
-               self.error.emit("Not Implemented")
+               le = concurrent.AsyncLinearExecution(self._pool, self._cancel)
+               le.start()
 
-       def add_contact(self, contact, details):
-               assert contact not in self._contacts
-               self._contacts[contact] = details
+       def add_contact(self, contactId, title, description, numbersWithDescriptions):
+               assert contactId not in self._contacts
+               contactDetails = title, description, numbersWithDescriptions
+               self._contacts[contactId] = contactDetails
                self.recipientsChanged.emit()
 
-       def remove_contact(self, contact):
-               assert contact not in self._contacts
-               del self._contacts[contact]
+       def remove_contact(self, contactId):
+               assert contactId in self._contacts
+               del self._contacts[contactId]
                self.recipientsChanged.emit()
 
-       def get_contacts(self, contact):
+       def get_contacts(self):
                return self._contacts
 
        def clear(self):
                self._contacts = {}
                self.recipientsChanged.emit()
 
+       def _send(self, text):
+               self.sendingMessage.emit()
+               try:
+                       self.error.emit("Not Implemented")
+                       self.sentMessage.emit()
+                       self.clear()
+               except Exception, e:
+                       self.error.emit(str(e))
+
+       def _call(self):
+               self.calling.emit()
+               try:
+                       self.error.emit("Not Implemented")
+                       self.called.emit()
+                       self.clear()
+               except Exception, e:
+                       self.error.emit(str(e))
+
+       def _cancel(self):
+               self.cancelling.emit()
+               try:
+                       yield (
+                               self._backend.cancel,
+                               (),
+                               {},
+                       )
+                       self.cancelled.emit()
+               except Exception, e:
+                       self.error.emit(str(e))
+
 
 class Session(QtCore.QObject):
 
@@ -95,10 +124,11 @@ class Session(QtCore.QObject):
                self._username = None
                self._draft = Draft(self._pool)
 
-               self._contacts = []
+               self._contacts = {}
                self._messages = []
                self._history = []
                self._dnd = False
+               self._callback = ""
 
        @property
        def state(self):
@@ -113,6 +143,7 @@ class Session(QtCore.QObject):
 
        def login(self, username, password):
                assert self.state == self.LOGGEDOUT_STATE
+               assert username != ""
                if self._cachePath is not None:
                        cookiePath = os.path.join(self._cachePath, "%s.cookies" % username)
                else:
@@ -128,20 +159,22 @@ class Session(QtCore.QObject):
        def logout(self):
                assert self.state != self.LOGGEDOUT_STATE
                self._pool.stop()
-               self.error.emit("Not Implemented")
+               self._loggedInTime = self._LOGGEDOUT_TIME
+               self._backend.persist()
+               self._save_to_cache()
 
        def clear(self):
                assert self.state == self.LOGGEDOUT_STATE
+               self._backend.logout()
                self._backend = None
+               self._clear_cache()
                self._draft.clear()
-               self._contacts = []
-               self.contactsUpdated.emit()
-               self._messages = []
-               self.messagesUpdated.emit()
-               self._history = []
-               self.historyUpdated.emit()
-               self._dnd = False
-               self.dndStateChange.emit(self._dnd)
+
+       def logout_and_clear(self):
+               assert self.state != self.LOGGEDOUT_STATE
+               self._pool.stop()
+               self._loggedInTime = self._LOGGEDOUT_TIME
+               self.clear()
 
        def update_contacts(self):
                le = concurrent.AsyncLinearExecution(self._pool, self._update_contacts)
@@ -169,21 +202,50 @@ class Session(QtCore.QObject):
                self._perform_op_while_loggedin(le)
 
        def set_dnd(self, dnd):
+               # I'm paranoid about our state geting out of sync so we set no matter
+               # what but act as if we have the cannonical state
                assert self.state == self.LOGGEDIN_STATE
-               self.error.emit("Not Implemented")
+               oldDnd = self._dnd
+               try:
+                       yield (
+                               self._backend.set_dnd,
+                               (dnd),
+                               {},
+                       )
+               except Exception, e:
+                       self.error.emit(str(e))
+                       return
+               self._dnd = dnd
+               if oldDnd != self._dnd:
+                       self.dndStateChange.emit(self._dnd)
 
        def get_dnd(self):
                return self._dnd
 
        def get_callback_numbers(self):
-               return []
+               # @todo Remove evilness
+               return self._backend.get_callback_numbers()
 
        def get_callback_number(self):
-               return ""
+               return self._callback
 
-       def set_callback_number(self):
+       def set_callback_number(self, callback):
+               # I'm paranoid about our state geting out of sync so we set no matter
+               # what but act as if we have the cannonical state
                assert self.state == self.LOGGEDIN_STATE
-               self.error.emit("Not Implemented")
+               oldCallback = self._callback
+               try:
+                       yield (
+                               self._backend.set_callback_number,
+                               (callback),
+                               {},
+                       )
+               except Exception, e:
+                       self.error.emit(str(e))
+                       return
+               self._callback = callback
+               if oldCallback != self._callback:
+                       self.callbackNumberChanged.emit(self._callback)
 
        def _login(self, username, password):
                self._loggedInTime = self._LOGGINGIN_TIME
@@ -193,96 +255,154 @@ class Session(QtCore.QObject):
                        isLoggedIn = False
 
                        if not isLoggedIn and self._backend.is_quick_login_possible():
-                               try:
-                                       isLoggedIn = yield (
-                                               self._backend.is_authed,
+                               isLoggedIn = yield (
+                                       self._backend.is_authed,
+                                       (),
+                                       {},
+                               )
+                               if isLoggedIn:
+                                       _moduleLogger.info("Logged in through cookies")
+                               else:
+                                       # Force a clearing of the cookies
+                                       yield (
+                                               self._backend.logout,
                                                (),
                                                {},
                                        )
-                               except Exception, e:
-                                       self.error.emit(str(e))
-                                       return
-                               if isLoggedIn:
-                                       _moduleLogger.info("Logged in through cookies")
 
                        if not isLoggedIn:
-                               try:
-                                       isLoggedIn = yield (
-                                               self._backend.login,
-                                               (username, password),
-                                               {},
-                                       )
-                               except Exception, e:
-                                       self.error.emit(str(e))
-                                       return
+                               isLoggedIn = yield (
+                                       self._backend.login,
+                                       (username, password),
+                                       {},
+                               )
                                if isLoggedIn:
                                        _moduleLogger.info("Logged in through credentials")
 
                        if isLoggedIn:
-                               self._loggedInTime = time.time()
+                               self._loggedInTime = int(time.time())
+                               oldUsername = self._username
                                self._username = username
                                finalState = self.LOGGEDIN_STATE
                                self.loggedIn.emit()
-                               # if the username is the same, do nothing
-                               # else clear the in-memory caches and attempt to load from file-caches
-                               # If caches went from empty to something, fire signals
-                               # Fire off queued async ops
+                               if oldUsername != self._username:
+                                       self._load_from_cache()
+                               loginOps = self._loginOps[:]
+                               del self._loginOps[:]
+                               for asyncOp in loginOps:
+                                       asyncOp.start()
                except Exception, e:
                        self.error.emit(str(e))
                finally:
                        self.stateChange.emit(finalState)
 
+       def _load_from_cache(self):
+               updateContacts = len(self._contacts) != 0
+               updateMessages = len(self._messages) != 0
+               updateHistory = len(self._history) != 0
+               oldDnd = self._dnd
+               oldCallback = self._callback
+
+               self._contacts = {}
+               self._messages = []
+               self._history = []
+               self._dnd = False
+               self._callback = ""
+
+               if updateContacts:
+                       self.contactsUpdated.emit()
+               if updateMessages:
+                       self.messagesUpdated.emit()
+               if updateHistory:
+                       self.historyUpdated.emit()
+               if oldDnd != self._dnd:
+                       self.dndStateChange.emit(self._dnd)
+               if oldCallback != self._callback:
+                       self.callbackNumberChanged.emit(self._callback)
+
+       def _save_to_cache(self):
+               # @todo
+               pass
+
+       def _clear_cache(self):
+               updateContacts = len(self._contacts) != 0
+               updateMessages = len(self._messages) != 0
+               updateHistory = len(self._history) != 0
+               oldDnd = self._dnd
+               oldCallback = self._callback
+
+               self._contacts = {}
+               self._messages = []
+               self._history = []
+               self._dnd = False
+               self._callback = ""
+
+               if updateContacts:
+                       self.contactsUpdated.emit()
+               if updateMessages:
+                       self.messagesUpdated.emit()
+               if updateHistory:
+                       self.historyUpdated.emit()
+               if oldDnd != self._dnd:
+                       self.dndStateChange.emit(self._dnd)
+               if oldCallback != self._callback:
+                       self.callbackNumberChanged.emit(self._callback)
+
+               self._save_to_cache()
+
        def _update_contacts(self):
-               self.error.emit("Not Implemented")
                try:
-                       isLoggedIn = yield (
-                               self._backend.is_authed,
+                       self._contacts = yield (
+                               self._backend.get_contacts,
                                (),
                                {},
                        )
                except Exception, e:
                        self.error.emit(str(e))
                        return
+               self.contactsUpdated.emit()
 
        def _update_messages(self):
-               self.error.emit("Not Implemented")
                try:
-                       isLoggedIn = yield (
-                               self._backend.is_authed,
+                       self._messages = yield (
+                               self._backend.get_messages,
                                (),
                                {},
                        )
                except Exception, e:
                        self.error.emit(str(e))
                        return
+               self.messagesUpdated.emit()
 
        def _update_history(self):
-               self.error.emit("Not Implemented")
                try:
-                       isLoggedIn = yield (
-                               self._backend.is_authed,
+                       self._history = yield (
+                               self._backend.get_recent,
                                (),
                                {},
                        )
                except Exception, e:
                        self.error.emit(str(e))
                        return
+               self.historyUpdated.emit()
 
        def _update_dnd(self):
-               self.error.emit("Not Implemented")
+               oldDnd = self._dnd
                try:
-                       isLoggedIn = yield (
-                               self._backend.is_authed,
+                       self._dnd = yield (
+                               self._backend.is_dnd,
                                (),
                                {},
                        )
                except Exception, e:
                        self.error.emit(str(e))
                        return
+               if oldDnd != self._dnd:
+                       self.dndStateChange(self._dnd)
 
        def _perform_op_while_loggedin(self, op):
                if self.state == self.LOGGEDIN_STATE:
-                       op()
+                       op.start()
                else:
                        self._push_login_op(op)