Tidy up LinkedInApi so that it does authentication in conjunction with
[hermes] / package / src / org / maemo / hermes / engine / linkedin / provider.py
index f4f871a..a25db8b 100644 (file)
@@ -1,6 +1,7 @@
 import gnome.gconf
 import gobject, gtk, hildon
 import time, thread
+import webbrowser
 import org.maemo.hermes.engine.provider
 import org.maemo.hermes.engine.linkedin.service
 from org.maemo.hermes.engine.linkedin.api import LinkedInApi
@@ -27,6 +28,13 @@ class Provider(org.maemo.hermes.engine.provider.Provider):
     
     
     # -----------------------------------------------------------------------
+    def get_account_detail(self):
+        """Return the name of the linked LinkedIn account."""
+        
+        return self._gc.get_string(LinkedInApi.GCONF_USER)
+    
+    
+    # -----------------------------------------------------------------------
     def has_preferences(self):
         """Whether or not this provider has any preferences. If it does not,
            open_preferences must NOT be called; as the behaviour is undetermined."""
@@ -38,20 +46,16 @@ class Provider(org.maemo.hermes.engine.provider.Provider):
     def open_preferences(self, parent):
         """Open the preferences for this provider as a child of the 'parent' widget."""
 
+        self.main_window = parent
         api = LinkedInApi(gconf = self._gc) 
         dialog = gtk.Dialog(self.get_name(), parent)
         dialog.add_button(_('Disable'), gtk.RESPONSE_NO)
         dialog.add_button(_('Enable'), gtk.RESPONSE_YES)
     
-        authenticated = api.get_access_token_from_gconf() is not None 
-        title = authenticated and _("Clear authorisation") or _("Authorise")
         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
-                               hildon.BUTTON_ARRANGEMENT_VERTICAL,
-                               title = title)
-        if authenticated:
-            button.connect('clicked', lambda e: api.remove_access_token_from_gconf)
-        else:
-            button.connect('clicked', lambda e: api.authenticate(lambda: None, self.block_for_auth))
+                               hildon.BUTTON_ARRANGEMENT_VERTICAL)
+        self._handle_button(None, api, button)
+        button.connect('clicked', self._handle_button, api, button)
             
         dialog.vbox.add(gtk.Label(""))
         dialog.vbox.add(button)
@@ -64,47 +68,44 @@ class Provider(org.maemo.hermes.engine.provider.Provider):
             return None
     
         return result == gtk.RESPONSE_YES
-        # FIXME: do auth:
-        # get request token
-        # get auth token
-        # open browser to have user allow data access
-        # user inputs the 5 char  
+
+
+    # -----------------------------------------------------------------------
+    def _handle_button(self, e, api, button):
+        """Ensure the button state is correct."""
+        
+        authenticated = api.get_access_token_from_gconf() is not None
+        if e is not None:
+            if authenticated:
+                api.remove_access_token_from_gconf()
+            else:
+                api.authenticate(lambda: None, self.block_for_auth)
         
+            authenticated = api.get_access_token_from_gconf() is not None
+        
+        button.set_title(authenticated and _("Clear authorisation") or _("Authorise"))
+
         
     # -----------------------------------------------------------------------
-    def block_for_auth(self, prompt = True, main = False, lock = None):
+    def block_for_auth(self, url):
         """Part of the GUI callback API."""
 
-        print "Get input:", prompt, main, lock
-        return "12345"
-#        if main:
-#            note = gtk.Dialog(_('Service authorisation'), self.main_window)
-#            note.add_button(_("Validate"), gtk.RESPONSE_OK)
-#            if prompt:
-#                input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
-#                input.set_property('is-focus', False)
-#                note.set_title(_("Verification code from web browser"))
-#                note.vbox.add(input)
-#            else:
-#                note.vbox.add(gtk.Label(_("\nPress 'Validate' once account has\nbeen authorised in web browser.\n")))
-#    
-#            note.show_all()
-#            result = note.run()
-#            note.hide()
-#            lock.release()
-#            if prompt and result == gtk.RESPONSE_OK:
-#                print input.get_text()
-#                return input.get_text()
-#            else:
-#                return None
-#        
-#        else:
-#            time.sleep(2)
-#            lock = thread.allocate_lock()
-#            lock.acquire()
-#            gobject.idle_add(self.block_for_auth, prompt, True, lock)
-#            lock.acquire()
-#            lock.release()
+        webbrowser.open(url)
+        time.sleep(2)
+        note = gtk.Dialog(_('Service authorisation'), self.main_window)
+        note.add_button(_("Validate"), gtk.RESPONSE_OK)
+        input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
+        input.set_property('is-focus', False)
+        note.set_title(_("Verification code from web browser"))
+        note.vbox.add(input)
+
+        note.show_all()
+        result = note.run()
+        note.hide()
+        if result == gtk.RESPONSE_OK:
+            return input.get_text()
+        else:
+            return None
 
     
     # -----------------------------------------------------------------------