[Xfce4-commits] <midori:master> Move thumbnail generation into SpeedDial class

Christian Dywan noreply at xfce.org
Wed Sep 5 00:08:03 CEST 2012


Updating branch refs/heads/master
         to 6210c59e3166a3098982d1ddb8006dfb7f4c5d36 (commit)
       from f2960a910d39c9078feb4606cde1efbb5de08714 (commit)

commit 6210c59e3166a3098982d1ddb8006dfb7f4c5d36
Author: Christian Dywan <christian at twotoasts.de>
Date:   Wed Sep 5 00:03:55 2012 +0200

    Move thumbnail generation into SpeedDial class

 midori/midori-browser.c      |    7 +--
 midori/midori-speeddial.vala |  101 ++++++++++++++++++++++++++--
 midori/midori-view.c         |  150 ------------------------------------------
 midori/midori-view.h         |    3 -
 wscript                      |    3 +
 5 files changed, 98 insertions(+), 166 deletions(-)

diff --git a/midori/midori-browser.c b/midori/midori-browser.c
index 0536cd9..4182e5a 100644
--- a/midori/midori-browser.c
+++ b/midori/midori-browser.c
@@ -1227,15 +1227,10 @@ midori_browser_add_speed_dial (MidoriBrowser* browser)
 
     if ((img = midori_view_get_snapshot (MIDORI_VIEW (view), 240, 160)))
     {
-        gchar* slot_id = midori_speed_dial_get_next_free_slot (browser->dial);
-        gchar* dial_id = g_strdup_printf ("Dial %s", slot_id + 1);
-        midori_speed_dial_add (browser->dial, dial_id,
+        midori_speed_dial_add (browser->dial,
             midori_view_get_display_uri (MIDORI_VIEW (view)),
             midori_view_get_display_title (MIDORI_VIEW (view)), img);
-        g_free (dial_id);
-        midori_view_save_speed_dial_config (MIDORI_VIEW (view));
         g_object_unref (img);
-        g_free (slot_id);
     }
 }
 
diff --git a/midori/midori-speeddial.vala b/midori/midori-speeddial.vala
index f8baf19..31a2cbc 100644
--- a/midori/midori-speeddial.vala
+++ b/midori/midori-speeddial.vala
@@ -23,6 +23,18 @@ namespace Midori {
         string filename;
         public GLib.KeyFile keyfile;
         string? html = null;
+        List<Spec> thumb_queue = null;
+        WebKit.WebView thumb_view = null;
+        Spec? spec = null;
+
+        public class Spec {
+            public string dial_id;
+            public string uri;
+            public Spec (string dial_id, string uri) {
+                this.dial_id = dial_id;
+                this.uri = uri;
+            }
+        }
 
         public SpeedDial (string new_filename, string? fallback = null) {
             filename = new_filename;
@@ -114,7 +126,12 @@ namespace Midori {
             return "s%u".printf (slot_count + 1);
         }
 
-        public void add (string id, string uri, string title, Gdk.Pixbuf img) {
+        public void add (string uri, string title, Gdk.Pixbuf img) {
+            string id = "Dial " + get_next_free_slot ();
+            add_with_id (id, uri, title, img);
+        }
+
+        public void add_with_id (string id, string uri, string title, Gdk.Pixbuf img) {
             keyfile.set_string (id, "uri", uri);
             keyfile.set_string (id, "title", title);
 
@@ -127,6 +144,7 @@ namespace Midori {
             catch (Error error) {
                 critical ("Failed to save speed dial thumbnail: %s", error.message);
             }
+            save ();
         }
 
         public unowned string get_html (bool close_buttons_left, GLib.Object view) throws Error {
@@ -204,8 +222,7 @@ namespace Midori {
                             catch (FileError error) {
                                 encoded = null;
                                 if (load_missing)
-                                    /* FIXME: midori_view_speed_dial_get_thumb (view, tile, uri); */
-                                    critical ("FIXME midori_view_speed_dial_get_thumb");
+                                    get_thumb (tile, uri);
                             }
                             markup.append_printf ("""
                                 <div class="shortcut" id="s%u"><div class="preview">
@@ -254,7 +271,7 @@ namespace Midori {
                 }
                 else if (action == "add") {
                     keyfile.set_string (dial_id, "uri", parts[2]);
-                    /* FIXME midori_view_speed_dial_get_thumb (view, dial_id, parts[2]); */
+                    get_thumb (dial_id, parts[2]);
                 }
                 else if (action == "rename") {
                     uint offset = parts[0].length + parts[1].length + 2;
@@ -276,13 +293,83 @@ namespace Midori {
                     keyfile.set_string (dial2_id, "title", title);
                 }
             }
-
+            save ();
         }
 
-        public void save () throws Error {
+        void save () {
             html = null;
 
-            FileUtils.set_contents (filename, keyfile.to_data ());
+            try {
+                FileUtils.set_contents (filename, keyfile.to_data ());
+            }
+            catch (Error error) {
+                critical ("Failed to update speed dial: %s", error.message);
+            }
+            /* FIXME Refresh all open views */
+        }
+
+        void load_status (GLib.Object thumb_view_, ParamSpec pspec) {
+            if (thumb_view.load_status != WebKit.LoadStatus.FINISHED)
+                return;
+
+            return_if_fail (spec != null);
+            #if HAVE_OFFSCREEN
+            var img = (thumb_view.parent as Gtk.OffscreenWindow).get_pixbuf ();
+            var pixbuf_scaled = img.scale_simple (240, 160, Gdk.InterpType.TILES);
+            img = pixbuf_scaled;
+            #else
+            thumb_view.realize ();
+            var img = midori_view_web_view_get_snapshot (thumb_view, 240, 160);
+            #endif
+            unowned string title = thumb_view.get_title ();
+            add_with_id (spec.dial_id, spec.uri, title ?? spec.uri, img);
+
+            thumb_queue.remove (spec);
+            if (thumb_queue != null && thumb_queue.data != null) {
+                spec = thumb_queue.data;
+                thumb_view.load_uri (spec.uri);
+            }
+            else
+                /* disconnect_by_func (thumb_view, load_status) */;
+        }
+
+        void get_thumb (string dial_id, string uri) {
+            if (thumb_view == null) {
+                thumb_view = new WebKit.WebView ();
+                var settings = new WebKit.WebSettings ();
+                settings. set ("enable-scripts", false,
+                               "enable-plugins", false,
+                               "auto-load-images", true,
+                               "enable-html5-database", false,
+                               "enable-html5-local-storage", false);
+                if (settings.get_class ().find_property ("enable-java-applet") != null)
+                    settings.set ("enable-java-applet", false);
+                thumb_view.settings = settings;
+                #if HAVE_OFFSCREEN
+                var offscreen = new Gtk.OffscreenWindow ();
+                offscreen.add (thumb_view);
+                thumb_view.set_size_request (800, 600);
+                offscreen.show_all ();
+                #else
+                /* What we are doing here is a bit of a hack. In order to render a
+                   thumbnail we need a new view and load the url in it. But it has
+                   to be visible and packed in a container. So we secretly pack it
+                   into the notebook of the parent browser. */
+                notebook.add (thumb_view);
+                thumb_view.destroy.connect (Gtk.widget_destroyed);
+                /* We use an empty label. It's not invisible but hard to spot. */
+                notebook.set_tab_label (thumb_view, new Gtk.EventBox ());
+                thumb_view.show ();
+                #endif
+            }
+
+            thumb_queue.append (new Spec (dial_id, uri));
+            if (thumb_queue.nth_data (1) != null)
+                return;
+
+            spec = thumb_queue.data;
+            thumb_view.notify["load-status"].connect (load_status);
+            thumb_view.load_uri (spec.uri);
         }
     }
 }
diff --git a/midori/midori-view.c b/midori/midori-view.c
index 7cbf17e..41c6ba4 100644
--- a/midori/midori-view.c
+++ b/midori/midori-view.c
@@ -77,11 +77,6 @@ midori_view_web_view_get_snapshot (GtkWidget* web_view,
                                    gint       width,
                                    gint       height);
 
-static void
-midori_view_speed_dial_get_thumb (MidoriView* view,
-                                  gchar*      dial_id,
-                                  gchar*      url);
-
 static gboolean
 midori_view_display_error (MidoriView*     view,
                            const gchar*    uri,
@@ -225,9 +220,6 @@ enum {
 
 static guint signals[LAST_SIGNAL];
 
-static GtkWidget* thumb_view = NULL;
-static GList* thumb_queue = NULL;
-
 static void
 midori_view_finalize (GObject* object);
 
@@ -3310,9 +3302,7 @@ webkit_web_view_console_message_cb (GtkWidget*   web_view,
     {
         MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
         MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial");
-
         midori_speed_dial_save_message (dial, message, NULL);
-        midori_view_save_speed_dial_config (view);
     }
     else
         g_signal_emit (view, signals[CONSOLE_MESSAGE], 0, message, line, source_id);
@@ -6168,143 +6158,3 @@ midori_view_get_security (MidoriView* view)
     return view->security;
 }
 
-static void
-thumb_view_load_status_cb (WebKitWebView* thumb_view_,
-                           GParamSpec*    pspec,
-                           MidoriView*    view)
-{
-    GdkPixbuf* img;
-    #if HAVE_OFFSCREEN
-    GdkPixbuf* pixbuf_scaled;
-    #endif
-    gchar* spec;
-    gchar* url;
-    gchar* dial_id;
-    MidoriBrowser* browser;
-    MidoriSpeedDial* dial;
-    const gchar* title;
-
-    if (webkit_web_view_get_load_status (thumb_view_) != WEBKIT_LOAD_FINISHED)
-        return;
-
-    browser = midori_browser_get_for_widget (GTK_WIDGET (view));
-    dial = katze_object_get_object (browser, "speed-dial");
-
-    spec = g_object_get_data (G_OBJECT (thumb_view), "spec");
-    url = strstr (spec, "|") + 1;
-    dial_id = g_strndup (spec, url - spec - 1);
-
-    #if HAVE_OFFSCREEN
-    img = gtk_offscreen_window_get_pixbuf (GTK_OFFSCREEN_WINDOW (
-        gtk_widget_get_parent (GTK_WIDGET (thumb_view))));
-    pixbuf_scaled = gdk_pixbuf_scale_simple (img, 240, 160, GDK_INTERP_TILES);
-    katze_object_assign (img, pixbuf_scaled);
-    #else
-    gtk_widget_realize (thumb_view);
-    img = midori_view_web_view_get_snapshot (thumb_view, 240, 160);
-    #endif
-
-    title = webkit_web_view_get_title (WEBKIT_WEB_VIEW (thumb_view));
-    midori_speed_dial_add (dial, dial_id, url, title ? title : url, img);
-    g_object_unref (img);
-    midori_view_save_speed_dial_config (view);
-
-    thumb_queue = g_list_remove (thumb_queue, spec);
-    if (thumb_queue != NULL)
-    {
-        g_object_set_data_full (G_OBJECT (thumb_view), "spec",
-                                thumb_queue->data, (GDestroyNotify)g_free);
-        webkit_web_view_load_uri (WEBKIT_WEB_VIEW (thumb_view),
-                                  strstr (thumb_queue->data, "|") + 1);
-    }
-    else
-        g_signal_handlers_disconnect_by_func (
-            thumb_view, thumb_view_load_status_cb, view);
-}
-
-/**
- * midori_view_speed_dial_get_thumb
- * @view: a #MidoriView
- * @dom_id: Id of the shortcut on speed_dial page in wich to inject content
- * @url: url of the shortcut
- */
-static void
-midori_view_speed_dial_get_thumb (MidoriView* view,
-                                  gchar*      dial_id,
-                                  gchar*      url)
-{
-    WebKitWebSettings* settings;
-    GtkWidget* browser;
-    #if !HAVE_OFFSCREEN
-    GtkWidget* notebook;
-    GtkWidget* label;
-
-    browser = gtk_widget_get_toplevel (GTK_WIDGET (view));
-    if (!GTK_IS_WINDOW (browser))
-        return;
-
-    /* What we are doing here is a bit of a hack. In order to render a
-       thumbnail we need a new view and load the url in it. But it has
-       to be visible and packed in a container. So we secretly pack it
-       into the notebook of the parent browser. */
-    notebook = katze_object_get_object (browser, "notebook");
-    if (!notebook)
-        return;
-    #endif
-
-    if (!thumb_view)
-    {
-        thumb_view = webkit_web_view_new ();
-        settings = g_object_new (WEBKIT_TYPE_WEB_SETTINGS,
-            "enable-scripts", FALSE,
-            "enable-plugins", FALSE, "auto-load-images", TRUE,
-            "enable-html5-database", FALSE, "enable-html5-local-storage", FALSE,
-        #if WEBKIT_CHECK_VERSION (1, 1, 22)
-            "enable-java-applet", FALSE,
-        #endif
-            NULL);
-        webkit_web_view_set_settings (WEBKIT_WEB_VIEW (thumb_view), settings);
-        #if HAVE_OFFSCREEN
-        browser = gtk_offscreen_window_new ();
-        gtk_container_add (GTK_CONTAINER (browser), thumb_view);
-        gtk_widget_set_size_request (thumb_view, 800, 600);
-        gtk_widget_show_all (browser);
-        #else
-        gtk_container_add (GTK_CONTAINER (notebook), thumb_view);
-        g_signal_connect (thumb_view, "destroy",
-                          G_CALLBACK (gtk_widget_destroyed),
-                          &thumb_view);
-        /* We use an empty label. It's not invisible but at least hard to spot. */
-        label = gtk_event_box_new ();
-        gtk_notebook_set_tab_label (GTK_NOTEBOOK (notebook), thumb_view, label);
-        gtk_widget_show (thumb_view);
-        #endif
-    }
-    #if !HAVE_OFFSCREEN
-    g_object_unref (notebook);
-    #endif
-
-    thumb_queue = g_list_append (thumb_queue, g_strconcat (dial_id, "|", url, NULL));
-    if (g_list_nth_data (thumb_queue, 1) != NULL)
-        return;
-
-    g_object_set_data_full (G_OBJECT (thumb_view), "spec",
-                            thumb_queue->data, (GDestroyNotify)g_free);
-    g_signal_connect (thumb_view, "notify::load-status",
-        G_CALLBACK (thumb_view_load_status_cb), view);
-    webkit_web_view_load_uri (WEBKIT_WEB_VIEW (thumb_view), url);
-}
-
-void
-midori_view_save_speed_dial_config (MidoriView* view)
-{
-    guint i = 0;
-    MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
-    MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial");
-    GtkWidget* tab;
-
-    midori_speed_dial_save (dial, NULL);
-    while ((tab = midori_browser_get_nth_tab (browser, i++)))
-        if (midori_view_is_blank (MIDORI_VIEW (tab)))
-            midori_view_reload (MIDORI_VIEW (tab), FALSE);
-}
diff --git a/midori/midori-view.h b/midori/midori-view.h
index 60fa0b7..cf5cfb3 100644
--- a/midori/midori-view.h
+++ b/midori/midori-view.h
@@ -290,9 +290,6 @@ midori_view_add_info_bar               (MidoriView*        view,
                                         const gchar*       first_button_text,
                                         ...);
 
-void
-midori_view_save_speed_dial_config     (MidoriView*        view);
-
 const gchar*
 midori_view_fallback_extension         (MidoriView*        view,
                                         const gchar*       extension);
diff --git a/wscript b/wscript
index 44bf224..69a0cc4 100644
--- a/wscript
+++ b/wscript
@@ -262,11 +262,14 @@ def configure (conf):
         if check_version (conf.check_cfg (modversion='webkitgtk-3.0'), 1, 5, 1):
             check_pkg ('javascriptcoregtk-3.0', '1.5.1', args=args)
         conf.env.append_value ('VALAFLAGS', '-D HAVE_GTK3')
+        conf.env.append_value ('VALAFLAGS', '-D HAVE_OFFSCREEN')
     else:
         check_pkg ('gtk+-2.0', '2.16.0', var='GTK')
         check_pkg ('webkit-1.0', '1.1.17', args=args)
         if check_version (conf.check_cfg (modversion='webkit-1.0'), 1, 5, 1):
             check_pkg ('javascriptcoregtk-1.0', '1.5.1', args=args)
+        if check_version ('gtk+-2.0', '2.20.0'):
+            conf.env.append_value ('VALAFLAGS', '-D HAVE_OFFSCREEN')
     conf.env['HAVE_GTK3'] = option_enabled ('gtk3')
     check_pkg ('libsoup-2.4', '2.27.90')
     conf.define ('LIBSOUP_VERSION', conf.check_cfg (modversion='libsoup-2.4'))


More information about the Xfce4-commits mailing list