Lots of work to try and get duplex conversations going plus disabled cookies
[theonering] / src / gvoice / session.py
index 674baf7..19ffbe0 100644 (file)
@@ -4,6 +4,8 @@ import logging
 
 import backend
 import addressbook
+import conversations
+import state_machine
 
 
 _moduleLogger = logging.getLogger("gvoice.session")
@@ -11,39 +13,51 @@ _moduleLogger = logging.getLogger("gvoice.session")
 
 class Session(object):
 
-       def __init__(self, cookiePath):
-               self._cookiePath = cookiePath
+       def __init__(self, cookiePath = None):
                self._username = None
                self._password = None
-               self._backend = None
-               self._addressbook = None
+
+               self._backend = backend.GVoiceBackend(cookiePath)
+               self._addressbook = addressbook.Addressbook(self._backend)
+               self._conversations = conversations.Conversations(self._backend)
+               self._stateMachine = state_machine.StateMachine([self.addressbook], [self.conversations])
+
+               self._conversations.updateSignalHandler.register_sink(
+                       self._stateMachine.request_reset_timers
+               )
 
        def login(self, username, password):
                self._username = username
                self._password = password
-               self._backend = backend.GVoiceBackend(self._cookiePath)
                if not self._backend.is_authed():
                        self._backend.login(self._username, self._password)
 
+               self._stateMachine.start()
+
        def logout(self):
+               self._loggedIn = False
+               self._stateMachine.stop()
+               self._backend.logout()
+
                self._username = None
                self._password = None
-               self._backend = None
-               self._addressbook = None
 
        def is_logged_in(self):
-               if self._backend is None:
+               if self._username is None and self._password is None:
+                       _moduleLogger.info("Hasn't even attempted to login yet")
                        return False
                elif self._backend.is_authed():
                        return True
                else:
                        try:
                                loggedIn = self._backend.login(self._username, self._password)
-                       except RuntimeError:
+                       except RuntimeError, e:
+                               _moduleLogger.exception("Re-authenticating and erroring")
                                loggedIn = False
                        if loggedIn:
                                return True
                        else:
+                               _moduleLogger.info("Login failed")
                                self.logout()
                                return False
 
@@ -60,8 +74,11 @@ class Session(object):
                """
                Delay initialized addressbook
                """
-               if self._addressbook is None:
-                       _moduleLogger.info("Initializing addressbook")
-                       self._addressbook = addressbook.Addressbook(self.backend)
-                       self._addressbook.update()
                return self._addressbook
+
+       @property
+       def conversations(self):
+               """
+               Delay initialized addressbook
+               """
+               return self._conversations