Add new signal for extension preferences

If the "preferences" property is TRUE and the signal
is implemented, a button is shown next to the extension
and the callback is invoked on click.
This commit is contained in:
André Stösel 2011-06-07 21:02:09 +02:00 committed by Christian Dywan
parent 7da0e14b2a
commit 6120fa1e49
6 changed files with 162 additions and 1 deletions

View file

@ -29,6 +29,7 @@ struct _MidoriExtensionPrivate
gchar* version; gchar* version;
gchar* authors; gchar* authors;
gchar* website; gchar* website;
gboolean preferences;
MidoriApp* app; MidoriApp* app;
gint active; gint active;
@ -131,12 +132,14 @@ enum
PROP_DESCRIPTION, PROP_DESCRIPTION,
PROP_VERSION, PROP_VERSION,
PROP_AUTHORS, PROP_AUTHORS,
PROP_WEBSITE PROP_WEBSITE,
PROP_PREFERENCES
}; };
enum { enum {
ACTIVATE, ACTIVATE,
DEACTIVATE, DEACTIVATE,
OPEN_PREFERENCES,
LAST_SIGNAL LAST_SIGNAL
}; };
@ -186,6 +189,24 @@ midori_extension_class_init (MidoriExtensionClass* class)
G_TYPE_NONE, 0, G_TYPE_NONE, 0,
G_TYPE_NONE); G_TYPE_NONE);
/**
* MidoriExtension::open-preferences:
*
* The preferences of the extension should be opened.
*
* Since: 0.4.0
*/
signals[OPEN_PREFERENCES] = g_signal_new (
"open-preferences",
G_TYPE_FROM_CLASS (class),
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
0,
0,
NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0,
G_TYPE_NONE);
gobject_class = G_OBJECT_CLASS (class); gobject_class = G_OBJECT_CLASS (class);
gobject_class->finalize = midori_extension_finalize; gobject_class->finalize = midori_extension_finalize;
gobject_class->set_property = midori_extension_set_property; gobject_class->set_property = midori_extension_set_property;
@ -245,6 +266,22 @@ midori_extension_class_init (MidoriExtensionClass* class)
NULL, NULL,
flags)); flags));
/**
* MidoriExtension:preferences:
*
* True if the extension can handle the preferences signal.
*
* Since: 0.4.0
*/
g_object_class_install_property (gobject_class,
PROP_PREFERENCES,
g_param_spec_boolean (
"preferences",
"Preferences",
"True if the extension can handle the preferences signal.",
FALSE,
flags));
g_type_class_add_private (class, sizeof (MidoriExtensionPrivate)); g_type_class_add_private (class, sizeof (MidoriExtensionPrivate));
} }
@ -409,6 +446,9 @@ midori_extension_set_property (GObject* object,
case PROP_WEBSITE: case PROP_WEBSITE:
katze_assign (extension->priv->website, g_value_dup_string (value)); katze_assign (extension->priv->website, g_value_dup_string (value));
break; break;
case PROP_PREFERENCES:
extension->priv->preferences = g_value_get_boolean (value);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -440,6 +480,9 @@ midori_extension_get_property (GObject* object,
case PROP_WEBSITE: case PROP_WEBSITE:
g_value_set_string (value, extension->priv->website); g_value_set_string (value, extension->priv->website);
break; break;
case PROP_PREFERENCES:
g_value_set_boolean (value, extension->priv->preferences);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -468,6 +511,25 @@ midori_extension_is_prepared (MidoriExtension* extension)
return FALSE; return FALSE;
} }
/**
* midori_extension_has_preferences:
* @extension: a #MidoriExtension
*
* Determines if @extension has preferences.
*
* Return value: %TRUE if @extension has preferences
**/
gboolean
midori_extension_has_preferences (MidoriExtension* extension)
{
g_return_val_if_fail (MIDORI_IS_EXTENSION (extension), FALSE);
if (extension->priv->preferences
&& g_signal_has_handler_pending (extension, signals[OPEN_PREFERENCES], 0, FALSE))
return TRUE;
return FALSE;
}
/** /**
* midori_extension_is_active: * midori_extension_is_active:
* @extension: a #MidoriExtension * @extension: a #MidoriExtension

View file

@ -51,6 +51,9 @@ midori_extension_get_type (void) G_GNUC_CONST;
gboolean gboolean
midori_extension_is_prepared (MidoriExtension* extension); midori_extension_is_prepared (MidoriExtension* extension);
gboolean
midori_extension_has_preferences (MidoriExtension* extension);
gboolean gboolean
midori_extension_is_active (MidoriExtension* extension); midori_extension_is_active (MidoriExtension* extension);

View file

@ -0,0 +1,4 @@
public class Midori.ExtensionsCoumn : Gtk.TreeViewColumn {
public signal void row_clicked (Gtk.TreeView view, Gtk.TreePath path);
}

View file

@ -101,9 +101,12 @@ namespace Midori {
public string version { get; set; } public string version { get; set; }
[NoAccessorMethod] [NoAccessorMethod]
public string authors { get; set; } public string authors { get; set; }
[NoAccessorMethod]
public bool preferences { get; set; }
public signal void activate (Midori.App app); public signal void activate (Midori.App app);
public signal void deactivate (); public signal void deactivate ();
public signal void open_preferences ();
} }
public class View : Gtk.VBox { public class View : Gtk.VBox {

View file

@ -17,6 +17,7 @@ if progressive or Options.commands['check']:
obj.uselib = libs obj.uselib = libs
obj.add_marshal_file ('marshal.list', 'midori_cclosure_marshal') obj.add_marshal_file ('marshal.list', 'midori_cclosure_marshal')
obj.install_path = None obj.install_path = None
obj.packages = 'glib-2.0 gio-2.0 gtk+-2.0 libsoup-2.4 webkit-1.0'
bld.add_group () bld.add_group ()
if progressive: if progressive:

View file

@ -16,6 +16,8 @@
#include "midori-stock.h" #include "midori-stock.h"
#include "midori-viewable.h" #include "midori-viewable.h"
#include "midori-extensions-column.c"
#include "sokoke.h" #include "sokoke.h"
#include <glib/gi18n.h> #include <glib/gi18n.h>
@ -244,6 +246,23 @@ midori_extensions_treeview_render_icon_cb (GtkTreeViewColumn* column,
g_object_unref (extension); g_object_unref (extension);
} }
static void
midori_extensions_treeview_render_preferences_cb (GtkTreeViewColumn* column,
GtkCellRenderer* renderer,
GtkTreeModel* model,
GtkTreeIter* iter,
GtkWidget* treeview)
{
MidoriExtension* extension;
gtk_tree_model_get (model, iter, 0, &extension, -1);
g_object_set (renderer, "stock-id", GTK_STOCK_PREFERENCES,
"stock-size", GTK_ICON_SIZE_BUTTON,
"visible", midori_extension_has_preferences (extension),
"xpad", 4, NULL);
g_object_unref (extension);
}
static void static void
midori_extensions_treeview_render_text_cb (GtkTreeViewColumn* column, midori_extensions_treeview_render_text_cb (GtkTreeViewColumn* column,
GtkCellRenderer* renderer, GtkCellRenderer* renderer,
@ -351,6 +370,56 @@ midori_extensions_tree_sort_func (GtkTreeModel* model,
return result; return result;
} }
static void
midori_extensions_treeview_column_preference_clicked_cb (GtkWidget* widget,
GtkTreeView* treeview,
GtkTreePath* path)
{
GtkTreeModel* model;
GtkTreeIter iter;
model = gtk_tree_view_get_model (treeview);
if (gtk_tree_model_get_iter (model, &iter, path))
{
MidoriExtension* extension;
gtk_tree_model_get (model, &iter, 0, &extension, -1);
g_signal_emit_by_name (extension, "open-preferences");
g_object_unref (extension);
}
}
static gboolean
midori_extensions_treeview_button_pressed_cb (GtkWidget* view,
GdkEventButton* bevent,
gpointer data)
{
gboolean ret = FALSE;
GtkTreePath* path;
GtkTreeViewColumn* column;
guint signal_id;
if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (view),
bevent->x, bevent->y, &path, &column, NULL, NULL))
{
if (path != NULL)
{
if (MIDORI_IS_EXTENSIONS_COUMN (column))
{
signal_id = g_signal_lookup ("row-clicked", G_OBJECT_TYPE (column));
if (signal_id && g_signal_has_handler_pending (column, signal_id, 0, FALSE)) {
g_signal_emit (column, signal_id, 0, GTK_TREE_VIEW (view), path);
ret = TRUE;
}
}
gtk_tree_path_free (path);
}
}
return ret;
}
static void static void
midori_extensions_init (MidoriExtensions* extensions) midori_extensions_init (MidoriExtensions* extensions)
{ {
@ -359,8 +428,13 @@ midori_extensions_init (MidoriExtensions* extensions)
GtkCellRenderer* renderer_icon; GtkCellRenderer* renderer_icon;
GtkCellRenderer* renderer_text; GtkCellRenderer* renderer_text;
GtkCellRenderer* renderer_toggle; GtkCellRenderer* renderer_toggle;
GtkCellRenderer* renderer_preferences;
GtkListStore* liststore = gtk_list_store_new (1, G_TYPE_OBJECT); GtkListStore* liststore = gtk_list_store_new (1, G_TYPE_OBJECT);
extensions->treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore)); extensions->treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (liststore));
g_object_connect (extensions->treeview,
"signal::button-press-event",
midori_extensions_treeview_button_pressed_cb, NULL,
NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (liststore), gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (liststore),
0, GTK_SORT_ASCENDING); 0, GTK_SORT_ASCENDING);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (liststore), gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (liststore),
@ -386,10 +460,24 @@ midori_extensions_init (MidoriExtensions* extensions)
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
renderer_text = gtk_cell_renderer_text_new (); renderer_text = gtk_cell_renderer_text_new ();
gtk_tree_view_column_pack_start (column, renderer_text, FALSE); gtk_tree_view_column_pack_start (column, renderer_text, FALSE);
gtk_tree_view_column_set_expand (column, TRUE);
gtk_tree_view_column_set_cell_data_func (column, renderer_text, gtk_tree_view_column_set_cell_data_func (column, renderer_text,
(GtkTreeCellDataFunc)midori_extensions_treeview_render_text_cb, (GtkTreeCellDataFunc)midori_extensions_treeview_render_text_cb,
extensions->treeview, NULL); extensions->treeview, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (extensions->treeview), column); gtk_tree_view_append_column (GTK_TREE_VIEW (extensions->treeview), column);
column = GTK_TREE_VIEW_COLUMN (midori_extensions_coumn_new ());
g_signal_connect (column,
"row-clicked",
G_CALLBACK (midori_extensions_treeview_column_preference_clicked_cb),
NULL);
renderer_preferences = gtk_cell_renderer_pixbuf_new ();
gtk_tree_view_column_pack_start (column, renderer_preferences, FALSE);
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_fixed_width (column, 30);
gtk_tree_view_column_set_cell_data_func (column, renderer_preferences,
(GtkTreeCellDataFunc)midori_extensions_treeview_render_preferences_cb,
extensions->treeview, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (extensions->treeview), column);
g_object_unref (liststore); g_object_unref (liststore);
g_object_connect (extensions->treeview, g_object_connect (extensions->treeview,
"signal::row-activated", "signal::row-activated",