Implement source view with GIO and drop internal source view

We use GIO to choose a text editor unless a text editor was
specified in the Preferences. We drop our internal source view
now which used to serve as a fallback.
This commit is contained in:
Christian Dywan 2009-03-17 21:35:05 +01:00
parent 7f207ce2c9
commit bbaa0fb9e6
6 changed files with 54 additions and 241 deletions

View file

@ -17,7 +17,6 @@
#include "midori-browser.h"
#include "midori-view.h"
#include "midori-source.h"
#include "midori-preferences.h"
#include "midori-panel.h"
#include "midori-locationaction.h"
@ -2185,6 +2184,8 @@ midori_browser_source_transfer_cb (KatzeNetRequest* request,
{
if ((fp = fdopen (fd, "w")))
{
gboolean success;
ret = fwrite (request->data, 1, request->length, fp);
fclose (fp);
if ((ret - request->length) != 0)
@ -2195,7 +2196,15 @@ midori_browser_source_transfer_cb (KatzeNetRequest* request,
}
g_object_get (browser->settings,
"text-editor", &text_editor, NULL);
sokoke_spawn_program (text_editor, unique_filename);
if (text_editor && *text_editor)
success = sokoke_spawn_program (text_editor, unique_filename);
else
{
gchar* command = g_strconcat ("exo-open ", unique_filename, NULL);
success = g_spawn_command_line_async (command, NULL);
g_free (command);
}
g_free (unique_filename);
g_free (text_editor);
}
@ -2209,11 +2218,9 @@ static void
_action_source_view_activate (GtkAction* action,
MidoriBrowser* browser)
{
gchar* text_editor;
GtkWidget* view;
GtkWidget* source_view;
gchar* uri;
gint n;
gchar* text_editor;
const gchar* uri;
if (!(view = midori_browser_get_current_tab (browser)))
return;
@ -2222,28 +2229,46 @@ _action_source_view_activate (GtkAction* action,
g_object_get (browser->settings, "text-editor", &text_editor, NULL);
else
text_editor = NULL;
uri = midori_view_get_display_uri (MIDORI_VIEW (view));
if (text_editor && *text_editor)
if (!g_strcmp0 (text_editor, ""))
{
katze_net_load_uri (browser->net,
midori_view_get_display_uri (MIDORI_VIEW (view)), NULL,
(KatzeNetTransferCb)midori_browser_source_transfer_cb, browser);
g_free (text_editor);
return;
}
else
{
uri = g_strdup_printf ("view-source:%s",
midori_view_get_display_uri (MIDORI_VIEW (view)));
source_view = midori_view_new (browser->net);
midori_view_set_settings (MIDORI_VIEW (source_view), browser->settings);
midori_view_set_uri (MIDORI_VIEW (source_view), uri);
midori_view_notify_icon_cb (MIDORI_VIEW (source_view), NULL, browser);
g_free (uri);
gtk_widget_show (source_view);
n = midori_browser_add_tab (browser, source_view);
midori_browser_set_current_page (browser, n);
GFile* file = g_file_new_for_uri (uri);
gchar* content_type;
GAppInfo* app_info;
GList* files;
gpointer context;
#if GLIB_CHECK_VERSION (2, 18, 0)
content_type = g_content_type_from_mime_type ("text/plain");
#else
content_type = g_strdup ("text/plain");
#endif
app_info = g_app_info_get_default_for_type (content_type,
!g_str_has_prefix (uri, "file://"));
g_free (content_type);
files = g_list_prepend (NULL, file);
#if GTK_CHECK_VERSION (2, 14, 0)
context = gdk_app_launch_context_new ();
gdk_app_launch_context_set_screen (context, gtk_widget_get_screen (view));
gdk_app_launch_context_set_timestamp (context, gtk_get_current_event_time ());
#else
context = g_app_launch_context_new ();
#endif
if (g_app_info_launch (app_info, files, context, NULL))
{
g_object_unref (app_info);
g_list_free (files);
g_object_unref (file);
g_free (text_editor);
return;
}
}
katze_net_load_uri (browser->net, uri, NULL,
(KatzeNetTransferCb)midori_browser_source_transfer_cb, browser);
g_free (text_editor);
}

View file

@ -1,136 +0,0 @@
/*
Copyright (C) 2007-2008 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See the file COPYING for the full license text.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include "midori-source.h"
#include <katze/katze.h>
#include <string.h>
#include <glib/gi18n.h>
struct _MidoriSource
{
GtkTextView parent_instance;
KatzeNet* net;
};
struct _MidoriSourceClass
{
GtkTextViewClass parent_class;
};
G_DEFINE_TYPE (MidoriSource, midori_source, GTK_TYPE_TEXT_VIEW);
static void
midori_source_finalize (GObject* object);
static void
midori_source_class_init (MidoriSourceClass* class)
{
GObjectClass* gobject_class;
gobject_class = G_OBJECT_CLASS (class);
gobject_class->finalize = midori_source_finalize;
}
static void
midori_source_init (MidoriSource* source)
{
gtk_text_view_set_editable (GTK_TEXT_VIEW (source), FALSE);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (source), TRUE);
source->net = katze_net_new ();
}
static void
midori_source_finalize (GObject* object)
{
katze_object_assign (MIDORI_SOURCE (object)->net, NULL);
G_OBJECT_CLASS (midori_source_parent_class)->finalize (object);
}
/**
* midori_source_new:
* @uri: a view-source: URI
*
* Creates a new source widget.
*
* Return value: a new #MidoriSource
**/
GtkWidget*
midori_source_new (const gchar* uri)
{
MidoriSource* source = g_object_new (MIDORI_TYPE_SOURCE,
/*"uri", uri,*/
NULL);
midori_source_set_uri (source, uri);
return GTK_WIDGET (source);
}
static void
midori_source_transfer_cb (KatzeNetRequest* request,
MidoriSource* source)
{
gchar** mimev;
gchar* charset;
const gchar* default_charset;
gchar* contents_utf8;
GtkTextBuffer* buffer;
if (request->data)
{
if (!g_utf8_validate (request->data, request->length, NULL))
{
charset = NULL;
if (request->mime_type)
{
mimev = g_strsplit (request->mime_type, " ", 2);
if (mimev[0] && mimev[1] &&
g_str_has_prefix (mimev[1], "charset="))
charset = g_strdup (&mimev[1][8]);
g_strfreev (mimev);
}
g_get_charset (&default_charset);
contents_utf8 = g_convert (request->data, -1, "UTF-8",
charset ? charset : default_charset, NULL, NULL, NULL);
/* If conversion from the user's locale also failed,
try ISO-8859-1 as a last resort */
if (!contents_utf8)
contents_utf8 = g_convert (request->data, -1, "UTF-8",
"ISO-8859-1", NULL, NULL, NULL);
}
else
contents_utf8 = (gchar*)request->data;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (source));
if (contents_utf8)
gtk_text_buffer_set_text (buffer, contents_utf8, -1);
if (contents_utf8 != request->data)
g_free (contents_utf8);
}
}
void
midori_source_set_uri (MidoriSource* source,
const gchar* uri)
{
g_return_if_fail (MIDORI_IS_SOURCE (source));
g_return_if_fail (uri != NULL);
katze_net_load_uri (source->net, uri,
NULL, (KatzeNetTransferCb)midori_source_transfer_cb, source);
}

View file

@ -1,47 +0,0 @@
/*
Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See the file COPYING for the full license text.
*/
#ifndef __MIDORI_SOURCE_H__
#define __MIDORI_SOURCE_H__
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define MIDORI_TYPE_SOURCE \
(midori_source_get_type ())
#define MIDORI_SOURCE(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), MIDORI_TYPE_SOURCE, MidoriSource))
#define MIDORI_SOURCE_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST ((klass), MIDORI_TYPE_SOURCE, MidoriSourceClass))
#define MIDORI_IS_SOURCE(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), MIDORI_TYPE_SOURCE))
#define MIDORI_IS_SOURCE_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE ((klass), MIDORI_TYPE_SOURCE))
#define MIDORI_SOURCE_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), MIDORI_TYPE_SOURCE, MidoriSourceClass))
typedef struct _MidoriSource MidoriSource;
typedef struct _MidoriSourceClass MidoriSourceClass;
GType
midori_source_get_type (void);
GtkWidget*
midori_source_new (const gchar* uri);
void
midori_source_set_uri (MidoriSource* source,
const gchar* uri);
G_END_DECLS
#endif /* __MIDORI_SOURCE_H__ */

View file

@ -14,7 +14,6 @@
#endif
#include "midori-view.h"
#include "midori-source.h"
#include "midori-stock.h"
#include "compat.h"
@ -1603,43 +1602,18 @@ midori_view_construct_web_view (MidoriView* view)
* @view: a #MidoriView
*
* Opens the specified URI in the view.
*
* Pass an URI prefixed with "view-source:" in
* order to create a source view.
**/
void
midori_view_set_uri (MidoriView* view,
const gchar* uri)
{
GtkWidget* widget;
gchar* data;
g_return_if_fail (MIDORI_IS_VIEW (view));
if (!uri) uri = "";
if (!view->web_view && view->uri
&& g_str_has_prefix (view->uri, "view-source:"))
{
g_signal_emit (view, signals[NEW_TAB], 0, uri);
}
else if (!view->web_view && g_str_has_prefix (uri, "view-source:"))
{
katze_assign (view->uri, g_strdup (uri));
g_object_notify (G_OBJECT (view), "uri");
if (view->item)
katze_item_set_uri (view->item, uri);
data = g_strdup_printf ("%s - %s", _("Source"), &uri[12]);
g_object_set (view, "title", data, NULL);
g_free (data);
katze_object_assign (view->icon,
gtk_widget_render_icon (GTK_WIDGET (view),
GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU, NULL));
widget = midori_source_new (&uri[12]);
gtk_container_add (GTK_CONTAINER (view), widget);
gtk_widget_show (widget);
}
else
if (1)
{
if (!view->web_view)
midori_view_construct_web_view (view);
@ -2260,10 +2234,10 @@ midori_view_can_zoom_out (MidoriView* view)
gboolean
midori_view_can_view_source (MidoriView* view)
{
g_return_val_if_fail (MIDORI_IS_VIEW (view), FALSE);
const gchar* uri = view->uri;
g_return_val_if_fail (MIDORI_IS_VIEW (view), FALSE);
/* FIXME: Consider other types that are also text */
if (!g_str_has_prefix (view->mime_type, "text/")
&& !g_strrstr (view->mime_type, "xml"))

View file

@ -1,5 +1,5 @@
/*
Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@ -20,7 +20,6 @@
#include "midori-panel.h"
#include "midori-preferences.h"
#include "midori-searchaction.h"
#include "midori-source.h"
#include "midori-stock.h"
#include "midori-view.h"
#include "midori-viewable.h"

View file

@ -172,8 +172,6 @@ main (int argc,
(gconstpointer)MIDORI_TYPE_PREFERENCES, properties_type_test);
g_test_add_data_func ("/properties/search-action",
(gconstpointer)MIDORI_TYPE_SEARCH_ACTION, properties_type_test);
g_test_add_data_func ("/properties/source",
(gconstpointer)MIDORI_TYPE_SOURCE, properties_type_test);
g_test_add_data_func ("/properties/view",
(gconstpointer)MIDORI_TYPE_VIEW, properties_type_test);
g_test_add_data_func ("/properties/viewable",