2008-04-28 20:38:57 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "midori-addons.h"
|
|
|
|
|
|
|
|
#include "sokoke.h"
|
2008-05-22 12:32:54 +00:00
|
|
|
#include "gjs.h"
|
2008-04-28 20:38:57 +00:00
|
|
|
#include <webkit/webkit.h>
|
|
|
|
#include <JavaScriptCore/JavaScript.h>
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
struct _MidoriAddons
|
2008-04-28 20:38:57 +00:00
|
|
|
{
|
2008-06-12 21:58:08 +00:00
|
|
|
GtkVBox parent_instance;
|
|
|
|
|
2008-04-28 20:38:57 +00:00
|
|
|
MidoriAddonKind kind;
|
|
|
|
GtkWidget* toolbar;
|
|
|
|
GtkWidget* treeview;
|
|
|
|
};
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
G_DEFINE_TYPE (MidoriAddons, midori_addons, GTK_TYPE_VBOX)
|
2008-04-28 20:38:57 +00:00
|
|
|
|
|
|
|
GType
|
|
|
|
midori_addon_kind_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
if (!type)
|
|
|
|
{
|
|
|
|
static const GEnumValue values[] = {
|
|
|
|
{ MIDORI_ADDON_EXTENSIONS, "MIDORI_ADDON_EXTENSIONS", N_("Extensions") },
|
|
|
|
{ MIDORI_ADDON_USER_SCRIPTS, "MIDORI_USER_SCRIPTS", N_("Userscripts") },
|
|
|
|
{ MIDORI_ADDON_USER_STYLES, "MIDORI_USER_STYLES", N_("Userstyles") },
|
|
|
|
{ 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
type = g_enum_register_static ("MidoriAddonKind", values);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
midori_addons_class_init (MidoriAddonsClass* class)
|
|
|
|
{
|
2008-06-12 21:58:08 +00:00
|
|
|
/* Nothing to do */
|
2008-04-28 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2008-05-02 20:30:26 +00:00
|
|
|
static const
|
|
|
|
gchar* _folder_for_kind (MidoriAddonKind kind)
|
|
|
|
{
|
|
|
|
switch (kind)
|
|
|
|
{
|
|
|
|
case MIDORI_ADDON_EXTENSIONS:
|
|
|
|
return "extensions";
|
|
|
|
case MIDORI_ADDON_USER_SCRIPTS:
|
|
|
|
return "scripts";
|
|
|
|
case MIDORI_ADDON_USER_STYLES:
|
|
|
|
return "styles";
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-28 20:38:57 +00:00
|
|
|
static void
|
|
|
|
midori_addons_button_add_clicked_cb (GtkToolItem* toolitem,
|
|
|
|
MidoriAddons* addons)
|
|
|
|
{
|
|
|
|
GtkWidget* dialog = gtk_message_dialog_new (
|
2008-04-28 22:12:23 +00:00
|
|
|
GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (addons))),
|
2008-04-28 20:38:57 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
|
2008-05-02 20:30:26 +00:00
|
|
|
"Put scripts in the folder ~/.local/share/midori/%s",
|
2008-06-12 21:58:08 +00:00
|
|
|
_folder_for_kind (addons->kind));
|
2008-04-28 20:38:57 +00:00
|
|
|
gtk_dialog_run (GTK_DIALOG (dialog));
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
midori_addons_treeview_render_icon_cb (GtkTreeViewColumn* column,
|
|
|
|
GtkCellRenderer* renderer,
|
|
|
|
GtkTreeModel* model,
|
|
|
|
GtkTreeIter* iter,
|
|
|
|
GtkWidget* treeview)
|
|
|
|
{
|
2008-06-12 21:58:08 +00:00
|
|
|
/* gchar* source_id;
|
|
|
|
gtk_tree_model_get (model, iter, 2, &source_id, -1); */
|
2008-04-28 20:38:57 +00:00
|
|
|
|
|
|
|
g_object_set (renderer, "stock-id", GTK_STOCK_FILE, NULL);
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
/* g_free (source_id); */
|
2008-04-28 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
midori_addons_treeview_render_text_cb (GtkTreeViewColumn* column,
|
|
|
|
GtkCellRenderer* renderer,
|
|
|
|
GtkTreeModel* model,
|
|
|
|
GtkTreeIter* iter,
|
|
|
|
GtkWidget* treeview)
|
|
|
|
{
|
|
|
|
gchar* filename;
|
|
|
|
gint a;
|
|
|
|
gchar* b;
|
|
|
|
gtk_tree_model_get (model, iter, 0, &filename, 1, &a, 2, &b, -1);
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
/* FIXME: Convert filename to UTF8 */
|
2008-04-28 20:38:57 +00:00
|
|
|
gchar* text = g_strdup_printf ("%s", filename);
|
|
|
|
g_object_set (renderer, "text", text, NULL);
|
|
|
|
g_free (text);
|
|
|
|
|
|
|
|
g_free (filename);
|
|
|
|
g_free (b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
midori_addons_treeview_row_activated_cb (GtkTreeView* treeview,
|
|
|
|
GtkTreePath* path,
|
|
|
|
GtkTreeViewColumn* column,
|
|
|
|
MidoriAddons* addons)
|
|
|
|
{
|
|
|
|
/*GtkTreeModel* model = gtk_tree_view_get_model (treeview);
|
|
|
|
GtkTreeIter iter;
|
|
|
|
if (gtk_tree_model_get_iter (model, &iter, path))
|
|
|
|
{
|
|
|
|
gchar* b;
|
|
|
|
gtk_tree_model_get (model, &iter, 2, &b, -1);
|
|
|
|
g_free (b);
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
2008-05-02 20:30:26 +00:00
|
|
|
static void
|
|
|
|
midori_addons_init (MidoriAddons* addons)
|
2008-04-28 20:38:57 +00:00
|
|
|
{
|
2008-05-02 20:30:26 +00:00
|
|
|
GtkTreeViewColumn* column;
|
|
|
|
GtkCellRenderer* renderer_text;
|
|
|
|
GtkCellRenderer* renderer_pixbuf;
|
2008-06-12 21:58:08 +00:00
|
|
|
|
|
|
|
addons->treeview = gtk_tree_view_new ();
|
|
|
|
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (addons->treeview), FALSE);
|
2008-05-02 20:30:26 +00:00
|
|
|
column = gtk_tree_view_column_new ();
|
|
|
|
renderer_pixbuf = gtk_cell_renderer_pixbuf_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, renderer_pixbuf, FALSE);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (column, renderer_pixbuf,
|
|
|
|
(GtkTreeCellDataFunc)midori_addons_treeview_render_icon_cb,
|
2008-06-12 21:58:08 +00:00
|
|
|
addons->treeview, NULL);
|
2008-05-02 20:30:26 +00:00
|
|
|
renderer_text = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, renderer_text, FALSE);
|
|
|
|
gtk_tree_view_column_set_cell_data_func (column, renderer_text,
|
|
|
|
(GtkTreeCellDataFunc)midori_addons_treeview_render_text_cb,
|
2008-06-12 21:58:08 +00:00
|
|
|
addons->treeview, NULL);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (addons->treeview), column);
|
|
|
|
g_signal_connect (addons->treeview, "row-activated",
|
2008-05-02 20:30:26 +00:00
|
|
|
G_CALLBACK (midori_addons_treeview_row_activated_cb),
|
|
|
|
addons);
|
2008-06-12 21:58:08 +00:00
|
|
|
gtk_widget_show (addons->treeview);
|
|
|
|
gtk_box_pack_start (GTK_BOX (addons), addons->treeview, TRUE, TRUE, 0);
|
2008-05-02 20:30:26 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 22:51:04 +00:00
|
|
|
static gboolean
|
|
|
|
_js_script_from_file (JSContextRef js_context,
|
|
|
|
const gchar* filename,
|
|
|
|
gchar** exception)
|
2008-05-02 20:30:26 +00:00
|
|
|
{
|
2008-05-05 22:51:04 +00:00
|
|
|
gboolean result = FALSE;
|
|
|
|
gchar* script;
|
|
|
|
GError* error = NULL;
|
2008-06-12 21:58:08 +00:00
|
|
|
|
2008-05-05 22:51:04 +00:00
|
|
|
if (g_file_get_contents (filename, &script, NULL, &error))
|
2008-04-28 20:38:57 +00:00
|
|
|
{
|
2008-06-12 21:58:08 +00:00
|
|
|
/* Wrap the script to prevent global variables */
|
2008-05-05 22:51:04 +00:00
|
|
|
gchar* wrapped_script = g_strdup_printf (
|
|
|
|
"var wrapped = function () { %s }; wrapped ();", script);
|
|
|
|
if (gjs_script_eval (js_context, wrapped_script, exception))
|
|
|
|
result = TRUE;
|
|
|
|
g_free (wrapped_script);
|
|
|
|
g_free (script);
|
2008-04-28 20:38:57 +00:00
|
|
|
}
|
2008-05-05 22:51:04 +00:00
|
|
|
else
|
2008-05-02 20:30:26 +00:00
|
|
|
{
|
2008-05-05 22:51:04 +00:00
|
|
|
*exception = g_strdup (error->message);
|
|
|
|
g_error_free (error);
|
2008-05-02 20:30:26 +00:00
|
|
|
}
|
2008-05-05 22:51:04 +00:00
|
|
|
return result;
|
2008-04-28 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
midori_web_widget_window_object_cleared_cb (GtkWidget* web_widget,
|
|
|
|
WebKitWebFrame* web_frame,
|
|
|
|
JSGlobalContextRef js_context,
|
|
|
|
JSObjectRef js_window,
|
|
|
|
MidoriAddons* addons)
|
|
|
|
{
|
2008-06-12 21:58:08 +00:00
|
|
|
/* FIXME: We want to honor system installed addons as well */
|
2008-04-28 20:38:57 +00:00
|
|
|
gchar* addon_path = g_build_filename (g_get_user_data_dir (), PACKAGE_NAME,
|
2008-06-12 21:58:08 +00:00
|
|
|
_folder_for_kind (addons->kind), NULL);
|
2008-04-28 20:38:57 +00:00
|
|
|
GDir* addon_dir = g_dir_open (addon_path, 0, NULL);
|
|
|
|
if (addon_dir)
|
|
|
|
{
|
|
|
|
const gchar* filename;
|
|
|
|
while ((filename = g_dir_read_name (addon_dir)))
|
|
|
|
{
|
|
|
|
gchar* fullname = g_build_filename (addon_path, filename, NULL);
|
|
|
|
gchar* exception;
|
2008-05-05 22:51:04 +00:00
|
|
|
if (!_js_script_from_file (js_context, fullname, &exception))
|
2008-04-28 20:38:57 +00:00
|
|
|
{
|
|
|
|
gchar* message = g_strdup_printf ("console.error ('%s');",
|
|
|
|
exception);
|
2008-05-05 22:51:04 +00:00
|
|
|
gjs_script_eval (js_context, message, NULL);
|
2008-04-28 20:38:57 +00:00
|
|
|
g_free (message);
|
|
|
|
g_free (exception);
|
|
|
|
}
|
|
|
|
g_free (fullname);
|
|
|
|
}
|
|
|
|
g_dir_close (addon_dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* midori_addons_new:
|
|
|
|
* @web_widget: a web widget
|
|
|
|
* @kind: the kind of addon
|
|
|
|
* @extension: a file extension mask
|
|
|
|
*
|
|
|
|
* Creates a new addons widget.
|
|
|
|
*
|
|
|
|
* @web_widget can be one of the following:
|
|
|
|
* %MidoriBrowser, %MidoriWebView, %WebKitWebView
|
|
|
|
*
|
|
|
|
* Note: Currently @extension has no effect.
|
|
|
|
*
|
|
|
|
* Return value: a new #MidoriAddons
|
|
|
|
**/
|
|
|
|
GtkWidget*
|
|
|
|
midori_addons_new (GtkWidget* web_widget,
|
|
|
|
MidoriAddonKind kind)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_WIDGET (web_widget), NULL);
|
|
|
|
|
|
|
|
MidoriAddons* addons = g_object_new (MIDORI_TYPE_ADDONS,
|
2008-06-12 21:58:08 +00:00
|
|
|
/* "kind", kind, */
|
2008-04-28 20:38:57 +00:00
|
|
|
NULL);
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
addons->kind = kind;
|
2008-05-05 22:51:04 +00:00
|
|
|
if (kind == MIDORI_ADDON_USER_SCRIPTS)
|
2008-04-28 20:38:57 +00:00
|
|
|
g_signal_connect (web_widget, "window-object-cleared",
|
|
|
|
G_CALLBACK (midori_web_widget_window_object_cleared_cb), addons);
|
|
|
|
|
|
|
|
GtkListStore* liststore = gtk_list_store_new (3, G_TYPE_STRING,
|
|
|
|
G_TYPE_INT,
|
|
|
|
G_TYPE_STRING);
|
2008-06-12 21:58:08 +00:00
|
|
|
/* FIXME: We want to honor system installed addons as well */
|
2008-04-28 20:38:57 +00:00
|
|
|
gchar* addon_path = g_build_filename (g_get_user_data_dir (), PACKAGE_NAME,
|
2008-06-12 21:58:08 +00:00
|
|
|
_folder_for_kind (addons->kind), NULL);
|
2008-04-28 20:38:57 +00:00
|
|
|
GDir* addon_dir = g_dir_open (addon_path, 0, NULL);
|
|
|
|
if (addon_dir)
|
|
|
|
{
|
|
|
|
const gchar* filename;
|
|
|
|
while ((filename = g_dir_read_name (addon_dir)))
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
gtk_list_store_append (liststore, &iter);
|
|
|
|
gtk_list_store_set (liststore, &iter,
|
|
|
|
0, filename, 1, 0, 2, "", -1);
|
|
|
|
}
|
|
|
|
g_dir_close (addon_dir);
|
|
|
|
}
|
2008-06-12 21:58:08 +00:00
|
|
|
gtk_tree_view_set_model (GTK_TREE_VIEW (addons->treeview),
|
2008-04-28 20:38:57 +00:00
|
|
|
GTK_TREE_MODEL (liststore));
|
|
|
|
|
|
|
|
return GTK_WIDGET (addons);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* midori_addons_get_toolbar:
|
|
|
|
*
|
|
|
|
* Retrieves the toolbar of the addons. A new widget is created on
|
|
|
|
* the first call of this function.
|
|
|
|
*
|
|
|
|
* Return value: a new #MidoriAddons
|
|
|
|
**/
|
|
|
|
GtkWidget*
|
|
|
|
midori_addons_get_toolbar (MidoriAddons* addons)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (MIDORI_IS_ADDONS (addons), NULL);
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
if (!addons->toolbar)
|
2008-04-28 20:38:57 +00:00
|
|
|
{
|
|
|
|
GtkWidget* toolbar = gtk_toolbar_new ();
|
|
|
|
gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
|
|
|
|
gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_BUTTON);
|
|
|
|
GtkToolItem* toolitem = gtk_tool_item_new ();
|
|
|
|
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
|
|
|
|
gtk_widget_show (GTK_WIDGET (toolitem));
|
|
|
|
toolitem = gtk_separator_tool_item_new ();
|
|
|
|
gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (toolitem),
|
|
|
|
FALSE);
|
|
|
|
gtk_tool_item_set_expand (toolitem, TRUE);
|
|
|
|
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
|
|
|
|
gtk_widget_show (GTK_WIDGET (toolitem));
|
|
|
|
toolitem = gtk_tool_button_new_from_stock (GTK_STOCK_ADD);
|
|
|
|
gtk_tool_item_set_is_important (toolitem, TRUE);
|
|
|
|
g_signal_connect (toolitem, "clicked",
|
|
|
|
G_CALLBACK (midori_addons_button_add_clicked_cb), addons);
|
|
|
|
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
|
|
|
|
gtk_widget_show (GTK_WIDGET (toolitem));
|
2008-06-12 21:58:08 +00:00
|
|
|
addons->toolbar = toolbar;
|
2008-04-28 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 21:58:08 +00:00
|
|
|
return addons->toolbar;
|
2008-04-28 20:38:57 +00:00
|
|
|
}
|