[Xfce4-commits] <midori:master> Move data source saving into MidoriView

Christian Dywan noreply at xfce.org
Sun Feb 19 23:00:01 CET 2012


Updating branch refs/heads/master
         to f549824b6601386d9118637f59d0b6b6bed6c770 (commit)
       from bbbfa0d677654b032c5c590fa415008ededcb710 (commit)

commit f549824b6601386d9118637f59d0b6b6bed6c770
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sun Feb 19 22:43:38 2012 +0100

    Move data source saving into MidoriView

 midori/midori-browser.c |  135 +++-------------------------------------------
 midori/midori-view.c    |  103 ++++++++++++++++++++++++++++++++++++
 midori/midori-view.h    |    5 ++
 3 files changed, 117 insertions(+), 126 deletions(-)

diff --git a/midori/midori-browser.c b/midori/midori-browser.c
index 1a17445..eaeac38 100644
--- a/midori/midori-browser.c
+++ b/midori/midori-browser.c
@@ -37,10 +37,6 @@
     #include <unistd.h>
 #endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
 #ifdef HAVE_HILDON_2_2
     #include <dbus/dbus.h>
     #include <mce/mode-names.h>
@@ -991,14 +987,9 @@ midori_browser_prepare_download (MidoriBrowser*  browser,
     return TRUE;
 }
 
-static gchar*
-midori_browser_save_source (const gchar* uri,
-                            const gchar* data,
-                            const size_t len,
-                            const gchar* outfile);
-
 static void
 midori_browser_save_uri (MidoriBrowser* browser,
+                         MidoriView*    view,
                          const gchar*   uri)
 {
     static gchar* last_dir = NULL;
@@ -1007,7 +998,6 @@ midori_browser_save_uri (MidoriBrowser* browser,
     gchar* filename;
     gchar* dirname;
     gchar* last_slash;
-    gchar* folder;
 
     if (!gtk_widget_get_visible (GTK_WIDGET (browser)))
         return;
@@ -1045,24 +1035,11 @@ midori_browser_save_uri (MidoriBrowser* browser,
 
     if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
     {
-        GtkWidget* view;
-        GtkWidget* web_view;
-        WebKitWebDataSource *data_source;
-        WebKitWebFrame *frame;
-        const GString *data;
-
         filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
-        folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
-        view = midori_browser_get_current_tab (browser);
-        web_view = midori_view_get_web_view (MIDORI_VIEW (view));
-        frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view));
-        data_source = webkit_web_frame_get_data_source (frame);
-        data = webkit_web_data_source_get_data (data_source);
-        if (data)
-            midori_browser_save_source (uri, data->str, data->len, filename);
+        midori_view_save_source (view, uri, filename);
 
-        g_free (last_dir);
-        last_dir = folder;
+        katze_assign (last_dir,
+            gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog)));
     }
     gtk_widget_destroy (dialog);
 }
@@ -1072,10 +1049,8 @@ midori_view_save_as_cb (GtkWidget*   menuitem,
                         const gchar* uri,
                         GtkWidget*   view)
 {
-    MidoriBrowser* browser;
-
-    browser = midori_browser_get_for_widget (menuitem);
-    midori_browser_save_uri (browser, uri);
+    MidoriBrowser* browser = midori_browser_get_for_widget (view);
+    midori_browser_save_uri (browser, MIDORI_VIEW (view), uri);
 }
 
 static gchar*
@@ -2371,7 +2346,8 @@ static void
 _action_save_as_activate (GtkAction*     action,
                           MidoriBrowser* browser)
 {
-    midori_browser_save_uri (browser, midori_browser_get_current_uri (browser));
+    GtkWidget* view = midori_browser_get_current_tab (browser);
+    midori_browser_save_uri (browser, MIDORI_VIEW (view), NULL);
 }
 
 static void
@@ -3365,112 +3341,19 @@ _action_view_encoding_activate (GtkAction*     action,
     }
 }
 
-static gchar*
-midori_browser_get_uri_extension (const gchar* uri)
-{
-    gchar* slash;
-    gchar* period;
-    gchar* ext_end;
-
-    /* Find the last slash in the URI and search for the last period
-       *after* the last slash. This is not completely accurate
-       but should cover most (simple) URIs */
-    slash = strrchr (uri, '/');
-    /* Huh, URI without slashes? */
-    if (!slash)
-        return NULL;
-
-    ext_end = period = strrchr (slash, '.');
-    if (!period)
-       return NULL;
-
-    /* Skip the period */
-    ext_end++;
-    /* If *ext_end is 0 here, the URI ended with a period, so skip */
-    if (!*ext_end)
-       return NULL;
-
-    /* Find the end of the extension */
-    while (*ext_end && g_ascii_isalnum (*ext_end))
-        ext_end++;
-
-    *ext_end = 0;
-    return g_strdup (period);
-}
-
-static gchar*
-midori_browser_save_source (const gchar* uri,
-                            const gchar* data,
-                            const size_t len,
-                            const gchar* outfile)
-{
-    gchar* unique_filename;
-    gint fd;
-    FILE* fp;
-    size_t ret;
-
-    if (!outfile)
-    {
-        gchar* filename;
-        gchar* extension;
-
-        extension = midori_browser_get_uri_extension (uri);
-        filename = g_strdup_printf ("%uXXXXXX%s",
-            g_str_hash (uri), extension && *extension ? extension : ".htm");
-        g_free (extension);
-        fd = g_file_open_tmp (filename, &unique_filename, NULL);
-        g_free (filename);
-    }
-    else
-    {
-        unique_filename = g_strdup (outfile);
-        fd = g_open (unique_filename, O_WRONLY|O_CREAT, 0644);
-    }
-
-    if (fd != -1)
-    {
-        if ((fp = fdopen (fd, "w")))
-        {
-            ret = fwrite (data, 1, len, fp);
-            fclose (fp);
-            if ((ret - len) != 0)
-            {
-                g_warning ("Error writing to file %s "
-                           "in midori_browser_source_transfer_cb()", unique_filename);
-                katze_assign (unique_filename, NULL);
-            }
-        }
-        close (fd);
-    }
-    return unique_filename;
-}
-
 static void
 _action_source_view_activate (GtkAction*     action,
                               MidoriBrowser* browser)
 {
-    WebKitWebDataSource *data_source;
-    WebKitWebFrame *frame;
-    const GString *data;
     GtkWidget* view;
-    GtkWidget* web_view;
     gchar* text_editor;
     gchar* filename = NULL;
-    const gchar* uri;
 
     if (!(view = midori_browser_get_current_tab (browser)))
         return;
 
+    filename = midori_view_save_source (MIDORI_VIEW (view), NULL, NULL);
     g_object_get (browser->settings, "text-editor", &text_editor, NULL);
-    uri = midori_view_get_display_uri (MIDORI_VIEW (view));
-    web_view = midori_view_get_web_view (MIDORI_VIEW (view));
-    frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view));
-    data_source = webkit_web_frame_get_data_source (frame);
-    data = webkit_web_data_source_get_data (data_source);
-    if (!data)
-        return;
-
-    filename = midori_browser_save_source (uri, data->str, data->len, NULL);
     if (!(text_editor && *text_editor))
     {
         GtkWidget* source;
diff --git a/midori/midori-view.c b/midori/midori-view.c
index 440bf38..11431cc 100644
--- a/midori/midori-view.c
+++ b/midori/midori-view.c
@@ -30,6 +30,11 @@
 #if HAVE_UNISTD_H
     #include <unistd.h>
 #endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 #ifndef G_OS_WIN32
     #include <sys/utsname.h>
 #endif
@@ -4970,6 +4975,104 @@ midori_view_can_save (MidoriView* view)
     return FALSE;
 }
 
+static gchar*
+midori_view_get_uri_extension (const gchar* uri)
+{
+    gchar* slash;
+    gchar* period;
+    gchar* ext_end;
+
+    /* Find the last slash in the URI and search for the last period
+       *after* the last slash. This is not completely accurate
+       but should cover most (simple) URIs */
+    slash = strrchr (uri, '/');
+    /* Huh, URI without slashes? */
+    if (!slash)
+        return NULL;
+
+    ext_end = period = strrchr (slash, '.');
+    if (!period)
+       return NULL;
+
+    /* Skip the period */
+    ext_end++;
+    /* If *ext_end is 0 here, the URI ended with a period, so skip */
+    if (!*ext_end)
+       return NULL;
+
+    /* Find the end of the extension */
+    while (*ext_end && g_ascii_isalnum (*ext_end))
+        ext_end++;
+
+    *ext_end = 0;
+    return g_strdup (period);
+}
+
+/**
+ * midori_view_save_source:
+ * @view: a #MidoriView
+ * @uri: an alternative destination URI, or %NULL
+ * @outfile: a destination filename, or %NULL
+ *
+ * Saves the data in the view to disk.
+ *
+ * Return value: the destination filename
+ *
+ * Since: 0.4.4
+ **/
+gchar*
+midori_view_save_source (MidoriView* view,
+                         const gchar* uri,
+                         const gchar* outfile)
+{
+    WebKitWebFrame *frame;
+    WebKitWebDataSource *data_source;
+    const GString *data;
+    gchar* unique_filename;
+    gint fd;
+    FILE* fp;
+    size_t ret;
+
+    frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (view->web_view));
+    data_source = webkit_web_frame_get_data_source (frame);
+    data = webkit_web_data_source_get_data (data_source);
+
+    if (uri == NULL)
+        uri = midori_view_get_display_uri (view);
+
+    if (!outfile)
+    {
+        gchar* extension = midori_view_get_uri_extension (uri);
+        gchar* filename = g_strdup_printf ("%uXXXXXX%s",
+            g_str_hash (uri), extension && *extension ? extension : ".htm");
+        g_free (extension);
+        fd = g_file_open_tmp (filename, &unique_filename, NULL);
+        g_free (filename);
+    }
+    else
+    {
+        unique_filename = g_strdup (outfile);
+        fd = g_open (unique_filename, O_WRONLY|O_CREAT, 0644);
+    }
+
+    if (fd != -1)
+    {
+        if ((fp = fdopen (fd, "w")))
+        {
+            ret = fwrite (data->str, 1, data->len, fp);
+            fclose (fp);
+            if ((ret - data->len) != 0)
+            {
+                g_warning ("Error writing to file %s "
+                           "in midori_browser_source_transfer_cb()", unique_filename);
+                katze_assign (unique_filename, NULL);
+            }
+        }
+        close (fd);
+    }
+    return unique_filename;
+}
+
 #define can_do(what) \
 gboolean \
 midori_view_can_##what (MidoriView* view) \
diff --git a/midori/midori-view.h b/midori/midori-view.h
index fcc20ab..192a1fe 100644
--- a/midori/midori-view.h
+++ b/midori/midori-view.h
@@ -197,6 +197,11 @@ midori_view_can_view_source            (MidoriView*        view);
 gboolean
 midori_view_can_save                   (MidoriView*        view);
 
+gchar*
+midori_view_save_source                (MidoriView*        view,
+                                        const gchar*       uri,
+                                        const gchar*       outfile);
+
 gboolean
 midori_view_can_find                   (MidoriView*        view);
 


More information about the Xfce4-commits mailing list