Obscuring credentials even further (Note: This is not meant to make them completely...
[doneit] / src / gtk_rtmilk.py
index 737e597..2a0b39b 100644 (file)
@@ -1,6 +1,7 @@
 import webbrowser
 import datetime
 import urlparse
+import base64
 
 import gobject
 import gtk
@@ -54,11 +55,11 @@ def get_token(username, apiKey, secret):
        return token
 
 
-def start_session(credentialsDialog):
+def get_credentials(credentialsDialog):
        # @todo Figure out storage of credentials
        username, password = credentialsDialog.request_credentials()
        token = get_token(username, rtmilk.RtMilkManager.API_KEY, rtmilk.RtMilkManager.SECRET)
-       return rtmilk.RtMilkManager(username, password, token)
+       return username, password, token
 
 
 class GtkRtMilk(object):
@@ -138,19 +139,65 @@ class GtkRtMilk(object):
                self._onClearId = None
                self._onPasteId = None
 
-               self._credentials = gtk_toolbox.LoginWindow(widgetTree)
+               self._credentialsDialog = gtk_toolbox.LoginWindow(widgetTree)
+               self._credentials = "", "", ""
                self._manager = None
 
        @staticmethod
        def name():
                return "Remember The Milk"
 
-       def enable(self):
+       def load_settings(self, config):
+               """
+               @note Thread Agnostic
+               """
+               blobs = (
+                       config.get(self.name(), "bin_blob_%i" % i)
+                       for i in xrange(len(self._credentials))
+               )
+               creds = (
+                       base64.b64decode(blob)
+                       for blob in blobs
+               )
+               self._credentials = tuple(creds)
+
+       def save_settings(self, config):
+               """
+               @note Thread Agnostic
+               """
+               config.add_section(self.name())
+               for i, value in enumerate(self._credentials):
+                       blob = base64.b64encode(value)
+                       config.set(self.name(), "bin_blob_%i" % i, blob)
+
+       def login(self):
                """
                @note UI Thread
                """
-               self._manager = start_session(self._credentials)
+               if self._manager is not None:
+                       return
+
+               credentials = self._credentials
+               while True:
+                       try:
+                               self._manager = rtmilk.RtMilkManager(*credentials)
+                               self._credentials = credentials
+                               return # Login succeeded
+                       except rtmapi.AuthStateMachine.NoData:
+                               # Login failed, grab new credentials
+                               credentials = get_credentials(self._credentialsDialog)
+
+       def logout(self):
+               """
+               @note Thread Agnostic
+               """
+               self._credentials = "", "", ""
+               self._manager = None
 
+       def enable(self):
+               """
+               @note UI Thread
+               """
                self._projectsList.clear()
                self._populate_projects()
 
@@ -233,6 +280,7 @@ class GtkRtMilk(object):
                return currentProjectName
 
        def _populate_items(self):
+               # @todo Look using a button for notes and links, and labels for all else
                currentProject = self._get_project()
                projId = self._manager.lookup_project(currentProject)["id"]
                for taskDetails in self._manager.get_tasks_with_details(projId):
@@ -264,6 +312,7 @@ class GtkRtMilk(object):
                self._reset_task_list()
 
        def _on_item_select(self, treeView, path, viewColumn):
+               # @todo See if there is a way to get a right click / tap'n'hold for more task goodness
                taskId = self._itemList[path[0]][self.ID_IDX]
 
                if viewColumn is self._priorityColumn: