]> spindle.queued.net Git - midori/commitdiff
Implement viewing source with libsoup
authorChristian Dywan <christian@twotoasts.de>
Sun, 19 Oct 2008 22:13:41 +0000 (00:13 +0200)
committerChristian Dywan <christian@twotoasts.de>
Sun, 19 Oct 2008 22:13:41 +0000 (00:13 +0200)
midori/main.c
midori/midori-browser.c
midori/midori-source.c
midori/midori-view.c

index 6f00cf3a36e83c31f9517fad2a71196d20fe5ce3..df4478cca804d77a138b6392f3b5be44741ee9df 100644 (file)
@@ -1109,6 +1109,9 @@ main (int    argc,
         return 1;
     }
 
+    #if HAVE_LIBSOUP
+    if (!g_thread_supported ()) g_thread_init (NULL);
+    #endif
     stock_items_init ();
     g_set_application_name (_("Midori"));
 
index 89bd556197d1c21adc61b87417a41b85749db0c5..c37bfcd2241ddc9a6e8e7a6669cf3930f45c54e1 100644 (file)
@@ -31,9 +31,6 @@
 #include "sokoke.h"
 #include "gjs.h"
 
-#if HAVE_GIO
-#include <gio/gio.h>
-#endif
 #include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
@@ -3739,10 +3736,6 @@ midori_browser_init (MidoriBrowser* browser)
 
     g_object_unref (ui_manager);
 
-    #if !HAVE_GIO
-    _action_set_sensitive (browser, "SourceView", FALSE);
-    #endif
-
     #ifndef WEBKIT_CHECK_VERSION
     _action_set_sensitive (browser, "ZoomIn", FALSE);
     _action_set_sensitive (browser, "ZoomOut", FALSE);
index bede4992d4896e89dceaa468c02bcfcb5870c949..f08975f423a183763f509e0b487f604563626c6a 100644 (file)
 #include "midori-source.h"
 
 #include <string.h>
-#if HAVE_GIO
-    #include <gio/gio.h>
-#endif
 #include <glib/gi18n.h>
 
+#if HAVE_LIBSOUP
+    #include <libsoup/soup.h>
+#endif
+
 struct _MidoriSource
 {
     GtkTextView parent_instance;
+
+    #if HAVE_LIBSOUP
+    SoupSession* session;
+    #endif
 };
 
 struct _MidoriSourceClass
@@ -53,11 +58,19 @@ midori_source_init (MidoriSource* source)
     buffer = gtk_text_buffer_new (NULL);
     gtk_text_view_set_buffer (GTK_TEXT_VIEW (source), buffer);
     gtk_text_view_set_editable (GTK_TEXT_VIEW (source), FALSE);
+
+    #if HAVE_LIBSOUP
+    source->session = soup_session_async_new ();
+    #endif
 }
 
 static void
 midori_source_finalize (GObject* object)
 {
+    #if HAVE_LIBSOUP
+    g_object_unref (MIDORI_SOURCE (object)->session);
+    #endif
+
     G_OBJECT_CLASS (midori_source_parent_class)->finalize (object);
 }
 
@@ -80,46 +93,97 @@ midori_source_new (const gchar* uri)
     return GTK_WIDGET (source);
 }
 
+#if HAVE_LIBSOUP
+static void
+midori_source_got_body_cb (SoupMessage*  msg,
+                           MidoriSource* source)
+{
+    const gchar* contents;
+    const gchar* mime;
+    gchar** mimev;
+    gchar* charset;
+    gchar* contents_utf8;
+    GtkTextBuffer* buffer;
+
+    if (msg->response_body->length > 0)
+    {
+        contents = msg->response_body->data;
+        if (contents && !g_utf8_validate (contents, -1, NULL))
+        {
+            charset = NULL;
+            if (msg->response_headers)
+            {
+                mime = soup_message_headers_get (msg->response_headers,
+                                                 "content-type");
+                if (mime)
+                {
+                    mimev = g_strsplit (mime, " ", 2);
+                    if (mimev[0] && mimev[1] &&
+                        g_str_has_prefix (mimev[1], "charset="))
+                        charset = g_strdup (&mimev[1][8]);
+                    g_strfreev (mimev);
+                }
+            }
+            contents_utf8 = g_convert (contents, -1, "UTF-8",
+                charset ? charset : "ISO-8859-1", NULL, NULL, NULL);
+        }
+        else
+            contents_utf8 = (gchar*)contents;
+        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source));
+        if (contents_utf8)
+            gtk_text_buffer_set_text (buffer, contents_utf8, -1);
+        g_object_unref (buffer);
+        if (contents != contents_utf8)
+            g_free (contents_utf8);
+    }
+}
+#endif
+
 void
 midori_source_set_uri (MidoriSource* source,
                        const gchar*  uri)
 {
-    #if HAVE_GIO
-    GFile* file;
-    gchar* tag;
-    #endif
     gchar* contents;
     gchar* contents_utf8;
     GtkTextBuffer* buffer;
+    #if HAVE_LIBSOUP
+    SoupMessage* msg;
+    #endif
+    gchar* filename;
 
     g_return_if_fail (MIDORI_IS_SOURCE (source));
+    g_return_if_fail (uri != NULL);
 
     contents = NULL;
 
-    #if HAVE_GIO
-    file = g_file_new_for_uri (uri);
-    tag = NULL;
-    if (g_file_load_contents (file, NULL, &contents, NULL, &tag, NULL))
+    #if HAVE_LIBSOUP
+    if (g_str_has_prefix (uri, "http://") || g_str_has_prefix (uri, "https://"))
     {
-        g_object_unref (file);
+        msg = soup_message_new ("GET", uri);
+        g_signal_connect (msg, "got-body",
+            G_CALLBACK (midori_source_got_body_cb), source);
+        soup_session_queue_message (source->session, msg, NULL, NULL);
+        return;
     }
-    if (contents && !g_utf8_validate (contents, -1, NULL))
+    #endif
+    if (g_str_has_prefix (uri, "file://"))
     {
-        contents_utf8 = g_convert (contents, -1, "UTF-8", "ISO-8859-1",
-                                   NULL, NULL, NULL);
-        g_free (contents);
+        contents = NULL;
+        filename = g_filename_from_uri (uri, NULL, NULL);
+        if (!filename || !g_file_get_contents (filename, &contents, NULL, NULL))
+            return;
+        if (contents && !g_utf8_validate (contents, -1, NULL))
+        {
+            contents_utf8 = g_convert (contents, -1, "UTF-8", "ISO-8859-1",
+                                       NULL, NULL, NULL);
+            g_free (contents);
+        }
+        else
+            contents_utf8 = contents;
+        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source));
+        if (contents_utf8)
+            gtk_text_buffer_set_text (buffer, contents_utf8, -1);
+        g_object_unref (buffer);
+        g_free (contents_utf8);
     }
-    else
-    #endif
-        contents_utf8 = contents;
-
-    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source));
-    if (contents_utf8)
-        gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), contents_utf8, -1);
-
-    g_object_unref (buffer);
-    g_free (contents_utf8);
-    #if HAVE_GIO
-    g_free (tag);
-    #endif
 }
index 345049cc60b7381378bab3acf264d3d0038f322c..2a978a08968b8fa5bd7d63469ae878f6f36b31ad 100644 (file)
@@ -1079,7 +1079,6 @@ midori_view_init (MidoriView* view)
     view->download_manager = NULL;
 
     #if HAVE_LIBSOUP
-    if (!g_thread_supported ()) g_thread_init (NULL);
     view->session = soup_session_async_new ();
     #endif
 
@@ -1911,6 +1910,20 @@ midori_view_can_zoom_out (MidoriView* view)
     #endif
 }
 
+gboolean
+midori_view_can_view_source (MidoriView* view)
+{
+    const gchar* uri = view->uri;
+
+    #if HAVE_LIBSOUP
+    if (g_str_has_prefix (uri, "http://") || g_str_has_prefix (uri, "https://"))
+        return TRUE;
+    #endif
+    if (g_str_has_prefix (uri, "file://"))
+        return TRUE;
+    return FALSE;
+}
+
 #define can_do(what) \
 gboolean \
 midori_view_can_##what (MidoriView* view) \
@@ -1922,14 +1935,6 @@ midori_view_can_##what (MidoriView* view) \
 
 can_do (reload)
 can_do (print)
-#if HAVE_GIO
-    can_do (view_source)
-#else
-    gboolean midori_view_can_view_source (MidoriView* view)
-    {
-        return view->web_view != NULL;
-    }
-#endif
 can_do (find)
 
 /**