From 72f39b0f82c150954faf05733094467d637951f1 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Thu, 20 Nov 2008 01:22:25 +0100 Subject: [PATCH] Change extension API to split initialization and activation We want to be able to keep extensions in a state where they don't actually do anything but merely provide context information. Therefore don't pass the MidoriApp. As soon as we want to use extensions, we activate them. Extensions are now saved in an array in MidoriApp. --- extensions/statusbar-features.c | 19 +++++-- extensions/tab-panel/main.c | 17 +++++-- midori/main.c | 36 +++++++++----- midori/midori-app.c | 88 +++++++++++---------------------- midori/midori-app.h | 10 ---- midori/midori-extension.c | 21 ++++++++ 6 files changed, 101 insertions(+), 90 deletions(-) diff --git a/extensions/statusbar-features.c b/extensions/statusbar-features.c index 26e2876e..c1a37e3a 100644 --- a/extensions/statusbar-features.c +++ b/extensions/statusbar-features.c @@ -12,8 +12,8 @@ #include void -realign_tabs_app_add_browser_cb (MidoriApp* app, - MidoriBrowser* browser) +statusbar_features_app_add_browser_cb (MidoriApp* app, + MidoriBrowser* browser) { GtkWidget* statusbar; GtkWidget* bbox; @@ -36,7 +36,16 @@ realign_tabs_app_add_browser_cb (MidoriApp* app, gtk_box_pack_start (GTK_BOX (statusbar), bbox, FALSE, FALSE, 3); } -MidoriExtension* extension_main (MidoriApp* app) +static void +statusbar_features_activate_cb (MidoriExtension* extension, + MidoriApp* app) +{ + g_signal_connect (app, "add-browser", + G_CALLBACK (statusbar_features_app_add_browser_cb), NULL); +} + +MidoriExtension* +extension_init (void) { MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION, "name", "Statusbar Features", @@ -45,8 +54,8 @@ MidoriExtension* extension_main (MidoriApp* app) "authors", "Christian Dywan ", NULL); - g_signal_connect (app, "add-browser", - G_CALLBACK (realign_tabs_app_add_browser_cb), NULL); + g_signal_connect (extension, "activate", + G_CALLBACK (statusbar_features_activate_cb), NULL); return extension; } diff --git a/extensions/tab-panel/main.c b/extensions/tab-panel/main.c index 83664ba8..60cb316b 100644 --- a/extensions/tab-panel/main.c +++ b/extensions/tab-panel/main.c @@ -13,7 +13,7 @@ #include -void +static void tab_panel_app_add_browser_cb (MidoriApp* app, MidoriBrowser* browser) { @@ -29,7 +29,16 @@ tab_panel_app_add_browser_cb (MidoriApp* app, NULL, GTK_STOCK_INDEX, "Tab Panel"); } -MidoriExtension* extension_main (MidoriApp* app) +static void +tab_panel_activate_cb (MidoriExtension* extension, + MidoriApp* app) +{ + g_signal_connect (app, "add-browser", + G_CALLBACK (tab_panel_app_add_browser_cb), NULL); +} + +MidoriExtension* +extension_init (void) { MidoriExtension* extension = g_object_new (TAB_PANEL_TYPE_EXTENSION, "name", "Tab Panel", @@ -38,8 +47,8 @@ MidoriExtension* extension_main (MidoriApp* app) "authors", "Christian Dywan ", NULL); - g_signal_connect (app, "add-browser", - G_CALLBACK (tab_panel_app_add_browser_cb), extension); + g_signal_connect (extension, "activate", + G_CALLBACK (tab_panel_activate_cb), NULL); return extension; } diff --git a/midori/main.c b/midori/main.c index 2b6b2cf2..c4bd900c 100644 --- a/midori/main.c +++ b/midori/main.c @@ -1291,18 +1291,14 @@ main (int argc, G_CALLBACK (midori_history_clear_cb), db); #endif - g_object_set (app, "settings", settings, - "bookmarks", bookmarks, - "trash", trash, - "search-engines", search_engines, - "history", history, - NULL); - /* Load extensions */ + KatzeArray* extensions; gchar* extension_path; GDir* extension_dir; const gchar* filename; + MidoriExtension* extension; + extensions = katze_array_new (MIDORI_TYPE_EXTENSION); extension_path = g_build_filename (LIBDIR, PACKAGE_NAME, NULL); if (g_module_supported ()) extension_dir = g_dir_open (extension_path, 0, NULL); @@ -1314,8 +1310,8 @@ main (int argc, { gchar* fullname; GModule* module; - typedef MidoriExtension* (*extension_main_func)(MidoriApp* app); - extension_main_func extension_main; + typedef MidoriExtension* (*extension_init_func)(void); + extension_init_func extension_init; fullname = g_build_filename (extension_path, filename, NULL); module = g_module_open (fullname, G_MODULE_BIND_LOCAL); @@ -1326,17 +1322,33 @@ main (int argc, continue; } ; - if (!g_module_symbol (module, "extension_main", - (gpointer) &extension_main)) + if (!g_module_symbol (module, "extension_init", + (gpointer) &extension_init)) { g_warning ("%s", g_module_error ()); continue; } - extension_main (app); + extension = extension_init (); + katze_array_add_item (extensions, extension); } g_dir_close (extension_dir); } + g_object_set (app, "settings", settings, + "bookmarks", bookmarks, + "trash", trash, + "search-engines", search_engines, + "history", history, + "extensions", extensions, + NULL); + + n = katze_array_get_length (extensions); + for (i = 0; i < n; i++) + { + extension = katze_array_get_nth_item (extensions, i); + g_signal_emit_by_name (extension, "activate", app); + } + browser = g_object_new (MIDORI_TYPE_BROWSER, "settings", settings, "bookmarks", bookmarks, diff --git a/midori/midori-app.c b/midori/midori-app.c index b7966835..5e754e1e 100644 --- a/midori/midori-app.c +++ b/midori/midori-app.c @@ -36,6 +36,7 @@ struct _MidoriApp KatzeArray* trash; KatzeArray* search_engines; KatzeArray* history; + KatzeArray* extensions; gpointer instance; }; @@ -52,8 +53,9 @@ enum PROP_BOOKMARKS, PROP_TRASH, PROP_SEARCH_ENGINES, - PROP_BROWSER, PROP_HISTORY, + PROP_EXTENSIONS, + PROP_BROWSER, PROP_BROWSER_COUNT }; @@ -235,6 +237,24 @@ midori_app_class_init (MidoriAppClass* class) KATZE_TYPE_ARRAY, G_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, + PROP_HISTORY, + g_param_spec_object ( + "history", + "History", + "The list of history items", + KATZE_TYPE_ARRAY, + G_PARAM_READWRITE)); + + g_object_class_install_property (gobject_class, + PROP_EXTENSIONS, + g_param_spec_object ( + "extensions", + "Extensions", + "The list of extensions", + KATZE_TYPE_ARRAY, + G_PARAM_READWRITE)); + g_object_class_install_property (gobject_class, PROP_BROWSER, g_param_spec_object ( @@ -252,15 +272,6 @@ midori_app_class_init (MidoriAppClass* class) "The current number of browsers", 0, G_MAXUINT, 0, G_PARAM_READABLE)); - - g_object_class_install_property (gobject_class, - PROP_HISTORY, - g_param_spec_object ( - "history", - "History", - "The list of history items", - KATZE_TYPE_ARRAY, - G_PARAM_READWRITE)); } static GObject* @@ -387,6 +398,7 @@ midori_app_init (MidoriApp* app) app->trash = NULL; app->search_engines = NULL; app->history = NULL; + app->extensions = NULL; #if HAVE_UNIQUE display_name = g_strdup (gdk_display_get_name (gdk_display_get_default ())); @@ -418,6 +430,7 @@ midori_app_finalize (GObject* object) katze_object_assign (app->trash, NULL); katze_object_assign (app->search_engines, NULL); katze_object_assign (app->history, NULL); + katze_object_assign (app->extensions, NULL); katze_object_assign (app->instance, NULL); @@ -454,6 +467,9 @@ midori_app_set_property (GObject* object, katze_object_assign (app->history, g_value_dup_object (value)); /* FIXME: Propagate history to all browsers */ break; + case PROP_EXTENSIONS: + katze_object_assign (app->extensions, g_value_dup_object (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -485,6 +501,9 @@ midori_app_get_property (GObject* object, case PROP_HISTORY: g_value_set_object (value, app->history); break; + case PROP_EXTENSIONS: + g_value_set_object (value, app->extensions); + break; case PROP_BROWSER: g_value_set_object (value, app->browser); break; @@ -654,55 +673,6 @@ midori_app_add_browser (MidoriApp* app, g_signal_emit (app, signals[ADD_BROWSER], 0, browser); } -/** - * midori_app_get_settings: - * @app: a #MidoriApp - * - * Retrieves the #MidoriWebSettings of the app. - * - * Return value: the assigned #MidoriWebSettings - **/ -MidoriWebSettings* -midori_app_get_settings (MidoriApp* app) -{ - g_return_val_if_fail (MIDORI_IS_APP (app), NULL); - - return app->settings; -} - -/** - * midori_app_set_settings: - * @app: a #MidoriApp - * - * Assigns the #MidoriWebSettings to the app. - * - * Return value: the assigned #MidoriWebSettings - **/ -void -midori_app_set_settings (MidoriApp* app, - MidoriWebSettings* settings) -{ - g_return_if_fail (MIDORI_IS_APP (app)); - - g_object_set (app, "settings", settings, NULL); -} - -/** - * midori_app_get_trash: - * @app: a #MidoriApp - * - * Retrieves the trash of the app. - * - * Return value: the assigned #MidoriTrash - **/ -KatzeArray* -midori_app_get_trash (MidoriApp* app) -{ - g_return_val_if_fail (MIDORI_IS_APP (app), NULL); - - return app->trash; -} - /** * midori_app_quit: * @app: a #MidoriApp diff --git a/midori/midori-app.h b/midori/midori-app.h index 361b9d66..98aff04c 100644 --- a/midori/midori-app.h +++ b/midori/midori-app.h @@ -70,16 +70,6 @@ void midori_app_add_browser (MidoriApp* app, MidoriBrowser* browser); -MidoriWebSettings* -midori_app_get_settings (MidoriApp* app); - -void -midori_app_set_settings (MidoriApp* app, - MidoriWebSettings* settings); - -KatzeArray* -midori_app_get_trash (MidoriApp* app); - void midori_app_quit (MidoriApp* app); diff --git a/midori/midori-extension.c b/midori/midori-extension.c index b97f8eda..553292bd 100644 --- a/midori/midori-extension.c +++ b/midori/midori-extension.c @@ -11,6 +11,8 @@ #include "midori-extension.h" +#include "midori-app.h" + #include G_DEFINE_TYPE (MidoriExtension, midori_extension, G_TYPE_OBJECT); @@ -33,6 +35,14 @@ enum PROP_AUTHORS }; +enum { + ACTIVATE, + + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL]; + static void midori_extension_finalize (GObject* object); @@ -54,6 +64,17 @@ midori_extension_class_init (MidoriExtensionClass* class) GObjectClass* gobject_class; GParamFlags flags; + signals[ACTIVATE] = g_signal_new ( + "activate", + G_TYPE_FROM_CLASS (class), + (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION), + 0, + 0, + NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + MIDORI_TYPE_APP); + gobject_class = G_OBJECT_CLASS (class); gobject_class->finalize = midori_extension_finalize; gobject_class->set_property = midori_extension_set_property; -- 2.39.5