Implement viewing source with libsoup

This commit is contained in:
Christian Dywan 2008-10-20 00:13:41 +02:00
parent cf854d9617
commit 7f8fc2b31d
4 changed files with 110 additions and 45 deletions

View file

@ -1109,6 +1109,9 @@ main (int argc,
return 1; return 1;
} }
#if HAVE_LIBSOUP
if (!g_thread_supported ()) g_thread_init (NULL);
#endif
stock_items_init (); stock_items_init ();
g_set_application_name (_("Midori")); g_set_application_name (_("Midori"));

View file

@ -31,9 +31,6 @@
#include "sokoke.h" #include "sokoke.h"
#include "gjs.h" #include "gjs.h"
#if HAVE_GIO
#include <gio/gio.h>
#endif
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
@ -3739,10 +3736,6 @@ midori_browser_init (MidoriBrowser* browser)
g_object_unref (ui_manager); g_object_unref (ui_manager);
#if !HAVE_GIO
_action_set_sensitive (browser, "SourceView", FALSE);
#endif
#ifndef WEBKIT_CHECK_VERSION #ifndef WEBKIT_CHECK_VERSION
_action_set_sensitive (browser, "ZoomIn", FALSE); _action_set_sensitive (browser, "ZoomIn", FALSE);
_action_set_sensitive (browser, "ZoomOut", FALSE); _action_set_sensitive (browser, "ZoomOut", FALSE);

View file

@ -16,14 +16,19 @@
#include "midori-source.h" #include "midori-source.h"
#include <string.h> #include <string.h>
#if HAVE_GIO
#include <gio/gio.h>
#endif
#include <glib/gi18n.h> #include <glib/gi18n.h>
#if HAVE_LIBSOUP
#include <libsoup/soup.h>
#endif
struct _MidoriSource struct _MidoriSource
{ {
GtkTextView parent_instance; GtkTextView parent_instance;
#if HAVE_LIBSOUP
SoupSession* session;
#endif
}; };
struct _MidoriSourceClass struct _MidoriSourceClass
@ -53,11 +58,19 @@ midori_source_init (MidoriSource* source)
buffer = gtk_text_buffer_new (NULL); buffer = gtk_text_buffer_new (NULL);
gtk_text_view_set_buffer (GTK_TEXT_VIEW (source), buffer); gtk_text_view_set_buffer (GTK_TEXT_VIEW (source), buffer);
gtk_text_view_set_editable (GTK_TEXT_VIEW (source), FALSE); gtk_text_view_set_editable (GTK_TEXT_VIEW (source), FALSE);
#if HAVE_LIBSOUP
source->session = soup_session_async_new ();
#endif
} }
static void static void
midori_source_finalize (GObject* object) 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); G_OBJECT_CLASS (midori_source_parent_class)->finalize (object);
} }
@ -80,46 +93,97 @@ midori_source_new (const gchar* uri)
return GTK_WIDGET (source); 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 void
midori_source_set_uri (MidoriSource* source, midori_source_set_uri (MidoriSource* source,
const gchar* uri) const gchar* uri)
{ {
#if HAVE_GIO
GFile* file;
gchar* tag;
#endif
gchar* contents; gchar* contents;
gchar* contents_utf8; gchar* contents_utf8;
GtkTextBuffer* buffer; GtkTextBuffer* buffer;
#if HAVE_LIBSOUP
SoupMessage* msg;
#endif
gchar* filename;
g_return_if_fail (MIDORI_IS_SOURCE (source)); g_return_if_fail (MIDORI_IS_SOURCE (source));
g_return_if_fail (uri != NULL);
contents = NULL; contents = NULL;
#if HAVE_GIO #if HAVE_LIBSOUP
file = g_file_new_for_uri (uri); if (g_str_has_prefix (uri, "http://") || g_str_has_prefix (uri, "https://"))
tag = NULL;
if (g_file_load_contents (file, NULL, &contents, NULL, &tag, NULL))
{ {
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", contents = NULL;
NULL, NULL, NULL); filename = g_filename_from_uri (uri, NULL, NULL);
g_free (contents); 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
} }

View file

@ -1079,7 +1079,6 @@ midori_view_init (MidoriView* view)
view->download_manager = NULL; view->download_manager = NULL;
#if HAVE_LIBSOUP #if HAVE_LIBSOUP
if (!g_thread_supported ()) g_thread_init (NULL);
view->session = soup_session_async_new (); view->session = soup_session_async_new ();
#endif #endif
@ -1911,6 +1910,20 @@ midori_view_can_zoom_out (MidoriView* view)
#endif #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) \ #define can_do(what) \
gboolean \ gboolean \
midori_view_can_##what (MidoriView* view) \ midori_view_can_##what (MidoriView* view) \
@ -1922,14 +1935,6 @@ midori_view_can_##what (MidoriView* view) \
can_do (reload) can_do (reload)
can_do (print) 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) can_do (find)
/** /**