Bump to 0.8.24
[theonering] / src / gvoice / session.py
index 5fb5a95..d024ea9 100644 (file)
@@ -4,7 +4,6 @@ import os
 import time
 import logging
 
-import backend
 import addressbook
 import conversations
 import state_machine
@@ -37,9 +36,14 @@ class Session(object):
                                        defaults[key] = (state_machine.UpdateStateMachine.INFINITE_PERIOD, unit)
                self._username = None
                self._password = None
+               self._cookiePath = cookiePath
+
+               self._lastDndCheck = 0
+               self._cachedIsDnd = False
 
                self._asyncPool = gobject_utils.AsyncPool()
-               self._backend = backend.GVoiceBackend(cookiePath)
+               import backend
+               self._backend = backend.GVoiceBackend(self._cookiePath)
 
                if defaults["contacts"][0] == state_machine.UpdateStateMachine.INFINITE_PERIOD:
                        contactsPeriodInSeconds = state_machine.UpdateStateMachine.INFINITE_PERIOD
@@ -129,9 +133,6 @@ class Session(object):
                self._masterStateMachine.append_machine(self._voicemailsStateMachine)
                self._masterStateMachine.append_machine(self._textsStateMachine)
 
-               self._lastDndCheck = 0
-               self._cachedIsDnd = False
-
        def load(self, path):
                self._texts.load(os.sep.join((path, "texts.cache")))
                self._voicemails.load(os.sep.join((path, "voicemails.cache")))
@@ -150,25 +151,54 @@ class Session(object):
                self._masterStateMachine.close()
 
        def login(self, username, password, on_success, on_error):
-               self._username = username
-               self._password = password
                self._asyncPool.start()
-               self._asyncPool.add_task(
-                       self._backend.login,
-                       (self._username, self._password),
-                       {},
-                       self.__on_login_success(on_success),
-                       on_error
-               )
 
-       def __on_login_success(self, user_success):
+               le = gobject_utils.AsyncLinearExecution(self._asyncPool, self._login)
+               le.start(username, password, on_success, on_error)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _login(self, username, password, on_success, on_error):
+               self._username = username
+               self._password = password
 
-               @misc_utils.log_exception(_moduleLogger)
-               def _actual_success(*args, **kwds):
-                       self._masterStateMachine.start()
-                       user_success(*args, **kwds)
+               isLoggedIn = False
+
+               if not isLoggedIn and self._backend.is_quick_login_possible():
+                       try:
+                               isLoggedIn = yield (
+                                       self._backend.is_authed,
+                                       (),
+                                       {},
+                               )
+                       except Exception, e:
+                               on_error(e)
+                               return
+                       if isLoggedIn:
+                               _moduleLogger.info("Logged in through cookies")
+
+               if not isLoggedIn:
+                       try:
+                               isLoggedIn = yield (
+                                       self._backend.login,
+                                       (self._username, self._password),
+                                       {},
+                               )
+                       except Exception, e:
+                               on_error(e)
+                               return
+                       if isLoggedIn:
+                               _moduleLogger.info("Logged in through credentials")
+
+               self._masterStateMachine.start()
+               on_success(isLoggedIn)
+
+       def shutdown(self):
+               self._asyncPool.stop()
+               self._masterStateMachine.stop()
+               self._backend.shutdown()
 
-               return _actual_success
+               self._username = None
+               self._password = None
 
        def logout(self):
                self._asyncPool.stop()
@@ -189,8 +219,9 @@ class Session(object):
                        return isLoggedIn
 
        def set_dnd(self, doNotDisturb):
-               self._backend.set_dnd(doNotDisturb)
-               self._cachedIsDnd = doNotDisturb
+               if self._cachedIsDnd != doNotDisturb:
+                       self._backend.set_dnd(doNotDisturb)
+                       self._cachedIsDnd = doNotDisturb
 
        def is_dnd(self):
                # To throttle checking with the server, use a 30s cache