]> spindle.queued.net Git - midori/commitdiff
Implement View source with external Text editor
authorChristian Dywan <christian@twotoasts.de>
Mon, 20 Oct 2008 19:32:25 +0000 (21:32 +0200)
committerChristian Dywan <christian@twotoasts.de>
Mon, 20 Oct 2008 19:32:25 +0000 (21:32 +0200)
midori/midori-browser.c
midori/midori-preferences.c
midori/midori-websettings.c

index e0ffcb4f8493068a16de5c4a723708d348aba898..66936e7cc7b390c0d0b986fd46c70a9228409513 100644 (file)
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
 #include <string.h>
+#ifdef HAVE_UNISTD_H
+    #include <unistd.h>
+#endif
+
+#if HAVE_LIBSOUP
+    #include <libsoup/soup.h>
+#endif
 
 struct _MidoriBrowser
 {
@@ -72,6 +79,10 @@ struct _MidoriBrowser
     KatzeArray* trash;
     KatzeArray* search_engines;
     KatzeArray* history;
+
+    #if HAVE_LIBSOUP
+    SoupSession* session;
+    #endif
 };
 
 G_DEFINE_TYPE (MidoriBrowser, midori_browser, GTK_TYPE_WINDOW)
@@ -1642,27 +1653,98 @@ _action_zoom_normal_activate (GtkAction*     action,
         midori_view_set_zoom_level (MIDORI_VIEW (view), 1.0f);
 }
 
+#if HAVE_LIBSOUP
+static void
+midori_browser_got_body_cb (SoupMessage*   msg,
+                            MidoriBrowser* browser)
+{
+    SoupURI* soup_uri;
+    gchar* uri;
+    gchar* filename;
+    gchar* unique_filename;
+    gchar* text_editor;
+    gint fd;
+    FILE* fp;
+
+    if (msg->response_body->length > 0)
+    {
+        soup_uri = soup_message_get_uri (msg);
+        uri = soup_uri_to_string (soup_uri, FALSE);
+        filename = g_strdup_printf ("%uXXXXXX", g_str_hash (uri));
+        g_free (uri);
+        if (((fd = g_file_open_tmp (filename, &unique_filename, NULL)) != -1))
+        {
+            if ((fp = fdopen (fd, "w")))
+            {
+                fwrite (msg->response_body->data,
+                    1, msg->response_body->length, fp);
+                fclose (fp);
+                g_object_get (browser->settings,
+                    "text-editor", &text_editor, NULL);
+                sokoke_spawn_program (text_editor, unique_filename);
+                g_free (unique_filename);
+                g_free (text_editor);
+            }
+            close (fd);
+        }
+        g_free (filename);
+    }
+}
+#endif
+
 static void
 _action_source_view_activate (GtkAction*     action,
                               MidoriBrowser* browser)
 {
+    gchar* text_editor;
+    const gchar* current_uri;
+    #if HAVE_LIBSOUP
+    SoupMessage* msg;
+    #endif
     GtkWidget* view;
     GtkWidget* source_view;
+    gchar* filename;
     gchar* uri;
     gint n;
 
     if (!(view = midori_browser_get_current_tab (browser)))
         return;
 
-    uri = g_strdup_printf ("view-source:%s",
-        midori_view_get_display_uri (MIDORI_VIEW (view)));
-    source_view = midori_view_new ();
-    midori_view_set_settings (MIDORI_VIEW (source_view), browser->settings);
-    midori_view_set_uri (MIDORI_VIEW (source_view), uri);
-    g_free (uri);
-    gtk_widget_show (source_view);
-    n = midori_browser_add_tab (browser, source_view);
-    midori_browser_set_current_page (browser, n);
+    g_object_get (browser->settings, "text-editor", &text_editor, NULL);
+    if (text_editor && *text_editor)
+    {
+        current_uri = midori_view_get_display_uri (MIDORI_VIEW (view));
+        #if HAVE_LIBSOUP
+        if (g_str_has_prefix (current_uri, "http://") ||
+            g_str_has_prefix (current_uri, "https://"))
+        {
+            msg = soup_message_new ("GET", current_uri);
+            g_signal_connect (msg, "got-body",
+                G_CALLBACK (midori_browser_got_body_cb), browser);
+            soup_session_queue_message (browser->session, msg, NULL, NULL);
+            g_free (text_editor);
+            return;
+        }
+        #endif
+        if (g_str_has_prefix (current_uri, "file://"))
+        {
+            filename = g_filename_from_uri (current_uri, NULL, NULL);
+            sokoke_spawn_program (text_editor, filename);
+        }
+    }
+    else
+    {
+        uri = g_strdup_printf ("view-source:%s",
+            midori_view_get_display_uri (MIDORI_VIEW (view)));
+        source_view = midori_view_new ();
+        midori_view_set_settings (MIDORI_VIEW (source_view), browser->settings);
+        midori_view_set_uri (MIDORI_VIEW (source_view), uri);
+        g_free (uri);
+        gtk_widget_show (source_view);
+        n = midori_browser_add_tab (browser, source_view);
+        midori_browser_set_current_page (browser, n);
+    }
+    g_free (text_editor);
 }
 
 static void
@@ -3258,6 +3340,10 @@ midori_browser_init (MidoriBrowser* browser)
     GtkRcStyle* rcstyle;
     GtkAction* action;
 
+    #if HAVE_LIBSOUP
+    browser->session = soup_session_async_new ();
+    #endif
+
     browser->settings = midori_web_settings_new ();
     browser->proxy_array = katze_array_new (KATZE_TYPE_ARRAY);
     browser->bookmarks = NULL;
@@ -3758,6 +3844,10 @@ midori_browser_finalize (GObject* object)
     if (browser->history)
         g_object_unref (browser->history);
 
+    #if HAVE_LIBSOUP
+    g_object_unref (browser->session);
+    #endif
+
     G_OBJECT_CLASS (midori_browser_parent_class)->finalize (object);
 }
 
index a117d4a197c74df18e8c7eb1b9fdc62917eb34a3..97e815b039134737991ef1281b8a75788b2f1fa1 100644 (file)
@@ -336,7 +336,7 @@ midori_preferences_set_settings (MidoriPreferences* preferences,
     FILLED_ADD (entry, 1, 2, 1, 2);
     /* TODO: We need something like "use current website" */
     FRAME_NEW (_("Transfers"));
-    TABLE_NEW (1, 2);
+    TABLE_NEW (3, 2);
     label = katze_property_label (settings, "download-folder");
     INDENTED_ADD (label, 0, 1, 0, 1);
     button = katze_property_proxy (settings, "download-folder", "folder");
@@ -355,6 +355,20 @@ midori_preferences_set_settings (MidoriPreferences* preferences,
     g_signal_connect (entry, "focus-out-event",
         G_CALLBACK (proxy_download_manager_icon_cb), button);
     FILLED_ADD (hbox, 1, 2, 1, 2);
+    label = katze_property_label (settings, "text-editor");
+    INDENTED_ADD (label, 0, 1, 2, 3);
+    hbox = gtk_hbox_new (FALSE, 4);
+    button = gtk_image_new ();
+    gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
+        GTK_ICON_SIZE_MENU, &icon_width, &icon_height);
+    gtk_widget_set_size_request (button, icon_width, icon_height);
+    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 4);
+    entry = katze_property_proxy (settings, "text-editor", NULL);
+    gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
+    proxy_download_manager_icon_cb (entry, NULL, GTK_IMAGE (button));
+    g_signal_connect (entry, "focus-out-event",
+        G_CALLBACK (proxy_download_manager_icon_cb), button);
+    FILLED_ADD (hbox, 1, 2, 2, 3);
 
     /* Page "Appearance" */
     PAGE_NEW (GTK_STOCK_SELECT_FONT, _("Appearance"));
index dc0b5a53fd22a1f97f3cd3c13c7b0f1fa8698893..ff3bb185f2e12fe4071455c268b05d1b19730e6d 100644 (file)
@@ -46,6 +46,7 @@ struct _MidoriWebSettings
     gchar* homepage;
     gchar* download_folder;
     gchar* download_manager;
+    gchar* text_editor;
     gchar* location_entry_search;
     MidoriPreferredEncoding preferred_encoding;
 
@@ -102,6 +103,7 @@ enum
     PROP_HOMEPAGE,
     PROP_DOWNLOAD_FOLDER,
     PROP_DOWNLOAD_MANAGER,
+    PROP_TEXT_EDITOR,
     PROP_LOCATION_ENTRY_SEARCH,
     PROP_PREFERRED_ENCODING,
 
@@ -454,6 +456,15 @@ midori_web_settings_class_init (MidoriWebSettingsClass* class)
                                      NULL,
                                      flags));
 
+    g_object_class_install_property (gobject_class,
+                                     PROP_TEXT_EDITOR,
+                                     g_param_spec_string (
+                                     "text-editor",
+                                     _("Text Editor"),
+                                     _("An external text editor"),
+                                     NULL,
+                                     flags));
+
     g_object_class_install_property (gobject_class,
                                      PROP_LOCATION_ENTRY_SEARCH,
                                      g_param_spec_string (
@@ -749,6 +760,9 @@ midori_web_settings_set_property (GObject*      object,
     case PROP_DOWNLOAD_MANAGER:
         katze_assign (web_settings->download_manager, g_value_dup_string (value));
         break;
+    case PROP_TEXT_EDITOR:
+        katze_assign (web_settings->text_editor, g_value_dup_string (value));
+        break;
     case PROP_LOCATION_ENTRY_SEARCH:
         katze_assign (web_settings->location_entry_search, g_value_dup_string (value));
         break;
@@ -915,6 +929,9 @@ midori_web_settings_get_property (GObject*    object,
     case PROP_DOWNLOAD_MANAGER:
         g_value_set_string (value, web_settings->download_manager);
         break;
+    case PROP_TEXT_EDITOR:
+        g_value_set_string (value, web_settings->text_editor);
+        break;
     case PROP_LOCATION_ENTRY_SEARCH:
         g_value_set_string (value, web_settings->location_entry_search);
         break;