[shotwell] Introduce common class for WebKit authenticator



commit 71f3951a5c9263b73e579435e7d738776679ba2f
Author: Jens Georg <mail jensge org>
Date:   Sat Oct 1 18:16:14 2016 +0200

    Introduce common class for WebKit authenticator
    
    This was implemented/copied three times...
    
    Signed-off-by: Jens Georg <mail jensge org>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=772295

 Makefile.am                                        |    1 +
 plugins/common/RESTSupport.vala                    |   56 +--------------
 plugins/common/WebAuthenticationPane.vala          |   75 ++++++++++++++++++++
 .../YandexPublishing.vala                          |   69 +++---------------
 .../shotwell-publishing/FacebookPublishing.vala    |   70 +++----------------
 5 files changed, 100 insertions(+), 171 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 9487d19..e47126b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -54,6 +54,7 @@ lib_LTLIBRARIES = \
 plugins_common_libshotwell_plugin_common_la_SOURCES = \
        plugins/common/RESTSupport.vala \
        plugins/common/Resources.vala \
+       plugins/common/WebAuthenticationPane.vala \
        plugins/shotwell-plugin-dev-1.0.vapi
 
 $(top_srcdir)/plugins/shotwell-plugin-common.vapi plugins/shotwell-plugin-common.h: \
diff --git a/plugins/common/RESTSupport.vala b/plugins/common/RESTSupport.vala
index 838192e..64351ac 100644
--- a/plugins/common/RESTSupport.vala
+++ b/plugins/common/RESTSupport.vala
@@ -733,37 +733,21 @@ public abstract class GooglePublisher : Object, Spit.Publishing.Publisher {
         }
     }
     
-    private class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
+    private class WebAuthenticationPane : Shotwell.Plugins.Common.WebAuthenticationPane {
         public static bool cache_dirty = false;
         
-        private WebKit.WebView webview;
-        private Gtk.Box pane_widget;
-        private string auth_sequence_start_url;
-
         public signal void authorized(string auth_code);
 
         public WebAuthenticationPane(string auth_sequence_start_url) {
-            this.auth_sequence_start_url = auth_sequence_start_url;
-
-            pane_widget = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
-
-            webview = new WebKit.WebView();
-            webview.get_settings().enable_plugins = false;
-
-            webview.load_changed.connect(on_page_load_changed);
-            webview.context_menu.connect(() => { return false; });
-
-            pane_widget.pack_start(webview, true, true, 0);
+            Object (login_uri : auth_sequence_start_url);
         }
         
         public static bool is_cache_dirty() {
             return cache_dirty;
         }
         
-        private void on_page_load() {
-            pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.LEFT_PTR));
-            
-            string page_title = webview.get_title();
+        public override void on_page_load() {
+            string page_title = get_view ().get_title();
             if (page_title.index_of("state=connect") > 0) {
                 int auth_code_field_start = page_title.index_of("code=");
                 if (auth_code_field_start < 0)
@@ -777,38 +761,6 @@ public abstract class GooglePublisher : Object, Spit.Publishing.Publisher {
                 authorized(auth_code);
             }
         }
-
-        private void on_load_started() {
-            pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.WATCH));
-        }
-
-        private void on_page_load_changed (WebKit.LoadEvent load_event) {
-            switch (load_event) {
-                case WebKit.LoadEvent.STARTED:
-                    on_load_started();
-                    break;
-                case WebKit.LoadEvent.FINISHED:
-                    on_page_load();
-                    break;
-            }
-
-            return;
-        }
-        
-        public Spit.Publishing.DialogPane.GeometryOptions get_preferred_geometry() {
-            return Spit.Publishing.DialogPane.GeometryOptions.NONE;
-        }
-        
-        public Gtk.Widget get_widget() {
-            return pane_widget;
-        }
-
-        public void on_pane_installed() {
-            webview.load_uri(auth_sequence_start_url);
-        }
-
-        public void on_pane_uninstalled() {
-        }
     }
     
     private class GetAccessTokensTransaction : Publishing.RESTSupport.Transaction {
diff --git a/plugins/common/WebAuthenticationPane.vala b/plugins/common/WebAuthenticationPane.vala
new file mode 100644
index 0000000..0a77cf6
--- /dev/null
+++ b/plugins/common/WebAuthenticationPane.vala
@@ -0,0 +1,75 @@
+/* Copyright 2016 Jens Georg <mail jensge org>
+ *
+ * This software is licensed under the GNU LGPL (version 2.1 or later).
+ * See the COPYING file in this distribution.
+ */
+using Spit.Publishing;
+
+namespace Shotwell.Plugins.Common {
+    public abstract class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
+        public DialogPane.GeometryOptions preferred_geometry {
+            get; construct; default = DialogPane.GeometryOptions.NONE;
+        }
+
+        public string login_uri { owned get; construct; }
+
+        private WebKit.WebView webview;
+        private Gtk.Box pane_widget;
+
+        public override void constructed () {
+            base.constructed ();
+
+            this.webview = new WebKit.WebView ();
+            this.webview.get_settings ().enable_plugins = false;
+
+            this.webview.load_changed.connect (this.on_page_load_changed);
+            this.webview.context_menu.connect ( () => { return false; });
+
+            this.pane_widget = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
+            this.pane_widget.pack_start (this.webview, true, true, 0);
+        }
+
+        public abstract void on_page_load ();
+
+        protected void set_cursor (Gdk.CursorType type) {
+            var window = pane_widget.get_window ();
+            var display = window.get_display ();
+            var cursor = new Gdk.Cursor.for_display (display, type);
+            window.set_cursor (cursor);
+        }
+
+        private void on_page_load_changed (WebKit.LoadEvent load_event) {
+            switch (load_event) {
+                case WebKit.LoadEvent.STARTED:
+                case WebKit.LoadEvent.REDIRECTED:
+                    this.set_cursor (Gdk.CursorType.WATCH);
+                    break;
+                case WebKit.LoadEvent.FINISHED:
+                    this.set_cursor (Gdk.CursorType.LEFT_PTR);
+                    this.on_page_load ();
+                    break;
+                default:
+                    break;
+            }
+        }
+
+        public WebKit.WebView get_view () {
+            return this.webview;
+        }
+
+        public DialogPane.GeometryOptions get_preferred_geometry() {
+            return this.preferred_geometry;
+        }
+
+        public Gtk.Widget get_widget() {
+            return pane_widget;
+        }
+
+        public void on_pane_installed () {
+            this.get_view ().load_uri (this.login_uri);
+        }
+
+        public void on_pane_uninstalled() {
+        }
+   }
+}
diff --git a/plugins/shotwell-publishing-extras/YandexPublishing.vala 
b/plugins/shotwell-publishing-extras/YandexPublishing.vala
index 2ee67cf..2eb78f3 100644
--- a/plugins/shotwell-publishing-extras/YandexPublishing.vala
+++ b/plugins/shotwell-publishing-extras/YandexPublishing.vala
@@ -92,46 +92,29 @@ internal class Session : Publishing.RESTSupport.Session {
     }
 }
 
-internal class WebAuthPane : Spit.Publishing.DialogPane, GLib.Object {
-    private WebKit.WebView webview = null;
-    private Gtk.Box pane_widget = null;
-    private Gtk.ScrolledWindow webview_frame = null;
-
+internal class WebAuthPane : Shotwell.Plugins.Common.WebAuthenticationPane {
     private Regex re;
-    private string? login_url = null;
 
     public signal void login_succeeded(string success_url);
     public signal void login_failed();
 
     public WebAuthPane(string login_url) {
-        this.login_url = login_url;
+        Object (login_uri : login_url,
+                preferred_geometry :
+                Spit.Publishing.DialogPane.GeometryOptions.RESIZABLE);
+    }
 
+    public override void constructed () {
         try {
             this.re = new Regex("(.*)#access_token=([a-zA-Z0-9]*)&");
         } catch (RegexError e) {
-            critical("%s", e.message);
+            assert_not_reached ();
         }
 
-        pane_widget = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
-
-        webview_frame = new Gtk.ScrolledWindow(null, null);
-        webview_frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN);
-        webview_frame.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
-
-        webview = new WebKit.WebView();
-        webview.get_settings().enable_plugins = false;
-
-        webview.load_changed.connect(on_page_load_changed);
-        webview.decide_policy.connect(on_decide_policy);
-        webview.context_menu.connect(() => { return false; });
-
-        webview_frame.add(webview);
-        pane_widget.pack_start(webview_frame, true, true, 0);
+        this.get_view ().decide_policy.connect (on_decide_policy);
     }
 
-    private void on_page_load() {
-        pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.LEFT_PTR));
-    }
+    public override void on_page_load () { }
 
     private bool on_decide_policy (WebKit.PolicyDecision decision,
                                    WebKit.PolicyDecisionType type) {
@@ -148,7 +131,7 @@ internal class WebAuthPane : Spit.Publishing.DialogPane, GLib.Object {
                     string access_token = info.fetch_all()[2];
 
                     debug("Load completed: %s", access_token);
-                    pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.LEFT_PTR));
+                    this.set_cursor (Gdk.CursorType.LEFT_PTR);
                     if (access_token != null) {
                         login_succeeded(access_token);
                         decision.ignore();
@@ -166,38 +149,6 @@ internal class WebAuthPane : Spit.Publishing.DialogPane, GLib.Object {
         }
         return true;
     }
-
-    private void on_load_started() {
-        pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.WATCH));
-    }
-
-    private void on_page_load_changed (WebKit.LoadEvent load_event) {
-        switch (load_event) {
-            case WebKit.LoadEvent.STARTED:
-                on_load_started();
-                break;
-            case WebKit.LoadEvent.FINISHED:
-                on_page_load();
-                break;
-        }
-
-        return;
-    }
-
-    public Gtk.Widget get_widget() {
-        return pane_widget;
-    }
-
-    public Spit.Publishing.DialogPane.GeometryOptions get_preferred_geometry() {
-        return Spit.Publishing.DialogPane.GeometryOptions.RESIZABLE;
-    }
-
-    public void on_pane_installed() {
-        webview.load_uri(login_url);
-    }
-
-    public void on_pane_uninstalled() {
-    }
 }
 
 internal class PublishOptions {
diff --git a/plugins/shotwell-publishing/FacebookPublishing.vala 
b/plugins/shotwell-publishing/FacebookPublishing.vala
index 608e5c5..2b0812a 100644
--- a/plugins/shotwell-publishing/FacebookPublishing.vala
+++ b/plugins/shotwell-publishing/FacebookPublishing.vala
@@ -811,30 +811,14 @@ public class FacebookPublisher : Spit.Publishing.Publisher, GLib.Object {
     }
 }
 
-internal class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
-    private WebKit.WebView webview = null;
-    private Gtk.Box pane_widget = null;
-    private Gtk.ScrolledWindow webview_frame = null;
+internal class WebAuthenticationPane : Shotwell.Plugins.Common.WebAuthenticationPane {
     private static bool cache_dirty = false;
 
     public signal void login_succeeded(string success_url);
     public signal void login_failed();
 
     public WebAuthenticationPane() {
-        pane_widget = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
-
-        webview_frame = new Gtk.ScrolledWindow(null, null);
-        webview_frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN);
-        webview_frame.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
-
-        webview = new WebKit.WebView();
-        webview.get_settings().enable_plugins = false;
-
-        webview.load_changed.connect(on_page_load_changed);
-        webview.context_menu.connect(() => { return true; });
-
-        webview_frame.add(webview);
-        pane_widget.pack_start(webview_frame, true, true, 0);
+        Object (login_uri : get_login_url ());
     }
 
     private class LocaleLookup {
@@ -858,7 +842,7 @@ internal class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
         
     }
     
-    private LocaleLookup[] locale_lookup_table = {
+    private static LocaleLookup[] locale_lookup_table = {
         new LocaleLookup( "es", "es-la", "ES", "es-es" ),
         new LocaleLookup( "en", "en-gb", "US", "en-us" ),
         new LocaleLookup( "fr", "fr-fr", "CA", "fr-ca" ),
@@ -906,7 +890,7 @@ internal class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
         new LocaleLookup( "ml", "ml-in" )
     };
     
-    private string get_system_locale_as_facebook_locale() {
+    private static string get_system_locale_as_facebook_locale() {
         unowned string? raw_system_locale = Intl.setlocale(LocaleCategory.ALL, "");
         if (raw_system_locale == null || raw_system_locale == "")
             return "www";
@@ -938,15 +922,14 @@ internal class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
         return "www";
     }
 
-    private string get_login_url() {
-        string facebook_locale = get_system_locale_as_facebook_locale();
+    private static string get_login_url() {
+        var facebook_locale = get_system_locale_as_facebook_locale();
+
         return 
"https://%s.facebook.com/dialog/oauth?client_id=%s&redirect_uri=https://www.facebook.com/connect/login_success.html&display=popup&scope=publish_actions,user_photos,user_videos&response_type=token".printf(facebook_locale,
 APPLICATION_ID);
     }
 
-    private void on_page_load() {
-        pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.LEFT_PTR));
-
-        string loaded_url = webview.uri.dup();
+    public override void on_page_load() {
+        string loaded_url = get_view ().uri.dup();
         debug("loaded url: " + loaded_url);
 
         // strip parameters from the loaded url
@@ -959,7 +942,7 @@ internal class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
         // were we redirected to the facebook login success page?
         if (loaded_url.contains("login_success")) {
             cache_dirty = true;
-            login_succeeded(webview.uri);
+            login_succeeded(get_view ().uri);
             return;
         }
 
@@ -970,42 +953,9 @@ internal class WebAuthenticationPane : Spit.Publishing.DialogPane, Object {
         }
     }
 
-    private void on_load_started() {
-        pane_widget.get_window().set_cursor(new Gdk.Cursor(Gdk.CursorType.WATCH));
-    }
-
-    private void on_page_load_changed (WebKit.LoadEvent load_event) {
-        switch (load_event) {
-            case WebKit.LoadEvent.STARTED:
-            case WebKit.LoadEvent.REDIRECTED:
-                on_load_started();
-                break;
-            case WebKit.LoadEvent.FINISHED:
-                on_page_load();
-                break;
-        }
-
-        return;
-    }
-
     public static bool is_cache_dirty() {
         return cache_dirty;
     }
-
-    public Gtk.Widget get_widget() {
-        return pane_widget;
-    }
-
-    public Spit.Publishing.DialogPane.GeometryOptions get_preferred_geometry() {
-        return Spit.Publishing.DialogPane.GeometryOptions.NONE;
-    }
-
-    public void on_pane_installed() {
-        webview.load_uri(get_login_url());
-    }
-
-    public void on_pane_uninstalled() {
-    }
 }
 
 internal class PublishingOptionsPane : Spit.Publishing.DialogPane, GLib.Object {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]