/* Copyright (C) 2008-2014 Christian Dywan Copyright (C) 2009 Dale Whittaker 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 "midori-extension.h" #include #include "midori-platform.h" #include "midori-core.h" #include G_DEFINE_TYPE (MidoriExtension, midori_extension, MIDORI_TYPE_CLASSIC_EXTENSION); static void midori_extension_class_init (MidoriExtensionClass* class) { } static void midori_extension_init (MidoriExtension* extension) { } gboolean midori_extension_is_prepared (MidoriExtension* extension) { return midori_classic_extension_is_prepared (MIDORI_CLASSIC_EXTENSION (extension)); } gboolean midori_extension_has_preferences (MidoriExtension* extension) { return midori_classic_extension_has_preferences (MIDORI_CLASSIC_EXTENSION (extension)); } const gchar* midori_extension_get_key (MidoriExtension* extension) { return midori_classic_extension_get_key (MIDORI_CLASSIC_EXTENSION (extension)); } gboolean midori_extension_is_active (MidoriExtension* extension) { return midori_classic_extension_is_active (MIDORI_CLASSIC_EXTENSION (extension)); } void midori_extension_load_from_folder (MidoriApp* app, gchar** keys, gboolean activate) { gchar* extension_path = NULL; if (activate) { gint i = 0; const gchar* filename; while (keys && (filename = keys[i++])) midori_extension_activate_gracefully (app, extension_path, filename, activate); } } GObject* midori_extension_load_from_file (const gchar* extension_path, const gchar* filename, gboolean activate, gboolean test) { g_return_val_if_fail (filename != NULL, NULL); return midori_addons_load_by_filename (midori_addons_get_default (), filename); } GObject* midori_extension_activate_gracefully (MidoriApp* app, const gchar* extension_path, const gchar* filename, gboolean activate) { GObject* extension = midori_extension_load_from_file (extension_path, filename, activate, FALSE); midori_extension_activate (extension, filename, activate, app); if (!extension && g_module_error () != NULL) { KatzeArray* extensions = katze_object_get_object (app, "extensions"); extension = g_object_new (MIDORI_TYPE_EXTENSION, "name", filename, "description", g_module_error (), NULL); g_warning ("%s", g_module_error ()); katze_array_add_item (extensions, extension); g_object_unref (extensions); g_object_unref (extension); return NULL; } else return extension; } static void midori_extension_add_to_list (MidoriApp* app, MidoriExtension* extension, const gchar* filename) { if (!app) return; g_return_if_fail (MIDORI_IS_APP (app)); g_return_if_fail (filename != NULL); KatzeArray* extensions = katze_object_get_object (app, "extensions"); g_return_if_fail (KATZE_IS_ARRAY (extensions)); if (katze_array_get_item_index (extensions, extension) >= 0) return; if (!midori_addon_get_intern (MIDORI_ADDON (extension))) katze_array_add_item (extensions, extension); g_object_unref (extensions); } void midori_extension_activate (GObject* extension, const gchar* filename, gboolean activate, MidoriApp* app) { if (MIDORI_IS_EXTENSION (extension)) { if (filename != NULL) midori_extension_add_to_list (app, MIDORI_EXTENSION (extension), filename); if (activate && !midori_extension_is_active (MIDORI_EXTENSION (extension))) g_signal_emit_by_name (extension, "activate", app); } else if (KATZE_IS_ARRAY (extension)) { gboolean success = FALSE; MidoriExtension* extension_item; KATZE_ARRAY_FOREACH_ITEM (extension_item, KATZE_ARRAY (extension)) if (MIDORI_IS_EXTENSION (extension_item)) { const gchar* key = midori_extension_get_key (extension_item); g_return_if_fail (key != NULL); if (filename != NULL && strchr (filename, '/')) { gchar* clean = g_strndup (filename, strchr (filename, '/') - filename); g_object_set_data_full (G_OBJECT (extension_item), "filename", clean, g_free); midori_extension_add_to_list (app, extension_item, clean); } else if (filename != NULL) { midori_extension_add_to_list (app, extension_item, filename); g_object_set_data_full (G_OBJECT (extension_item), "filename", g_strdup (filename), g_free); } if (activate && !midori_extension_is_active (MIDORI_EXTENSION (extension_item)) && filename && strstr (filename, key)) { g_signal_emit_by_name (extension_item, "activate", app); success = TRUE; } } /* Passed a multi extension w/o key or non-existing key */ g_warn_if_fail (!activate || success); } } /** * midori_extension_deactivate: * @extension: a #MidoriExtension * * Attempts to deactivate @extension. **/ void midori_extension_deactivate (MidoriExtension* extension) { g_return_if_fail (midori_extension_is_active (extension)); g_signal_emit_by_name (extension, "deactivate"); } /** * midori_extension_get_app: * @extension: a #MidoriExtension * * Retrieves the #MidoriApp the extension belongs to. The * extension has to be active. * * Return value: the #MidoriApp instance * * Since 0.1.6 **/ MidoriApp* midori_extension_get_app (MidoriExtension* extension) { return MIDORI_APP (midori_classic_extension_get_app (MIDORI_CLASSIC_EXTENSION (extension))); } /** * midori_extension_get_config_dir: * @extension: a #MidoriExtension * * Retrieves the path to a directory reserved for configuration * files specific to the extension. * * If settings are installed on the extension, they will be * loaded from and saved to a file "config" in this path. * * Return value: a path, such as ~/.config/midori/extensions/name **/ const gchar* midori_extension_get_config_dir (MidoriExtension* extension) { return midori_classic_extension_get_config_dir(MIDORI_CLASSIC_EXTENSION (extension)); } /** * midori_extension_install_boolean: * @extension: a #MidoriExtension * @name: the name of the setting * @default_value: the default value * * Installs a boolean that can be used to conveniently * store user configuration. * * Note that all settings have to be installed before * the extension is activated. * * Since: 0.1.3 **/ void midori_extension_install_boolean (MidoriExtension* extension, const gchar* name, gboolean default_value) { midori_classic_extension_install_boolean (MIDORI_CLASSIC_EXTENSION (extension), name, default_value); } /** * midori_extension_get_boolean: * @extension: a #MidoriExtension * @name: the name of the setting * * Retrieves the value of the specified setting. * * Since: 0.1.3 **/ gboolean midori_extension_get_boolean (MidoriExtension* extension, const gchar* name) { return midori_classic_extension_get_boolean (MIDORI_CLASSIC_EXTENSION (extension), name); } /** * midori_extension_set_boolean: * @extension: a #MidoriExtension * @name: the name of the setting * @value: the new value * * Assigns a new value to the specified setting. * * Since: 0.1.3 **/ void midori_extension_set_boolean (MidoriExtension* extension, const gchar* name, gboolean value) { midori_classic_extension_set_boolean (MIDORI_CLASSIC_EXTENSION (extension), name, value); } /** * midori_extension_install_integer: * @extension: a #MidoriExtension * @name: the name of the setting * @default_value: the default value * * Installs an integer that can be used to conveniently * store user configuration. * * Note that all settings have to be installed before * the extension is activated. * * Since: 0.1.3 **/ void midori_extension_install_integer (MidoriExtension* extension, const gchar* name, gint default_value) { midori_classic_extension_install_integer (MIDORI_CLASSIC_EXTENSION (extension), name, default_value); } /** * midori_extension_get_integer: * @extension: a #MidoriExtension * @name: the name of the setting * * Retrieves the value of the specified setting. * * Since: 0.1.3 **/ gint midori_extension_get_integer (MidoriExtension* extension, const gchar* name) { return midori_classic_extension_get_integer (MIDORI_CLASSIC_EXTENSION (extension), name); } /** * midori_extension_set_integer: * @extension: a #MidoriExtension * @name: the name of the setting * @value: the new value * * Assigns a new value to the specified setting. * * Since: 0.1.3 **/ void midori_extension_set_integer (MidoriExtension* extension, const gchar* name, gint value) { midori_classic_extension_set_integer (MIDORI_CLASSIC_EXTENSION (extension), name, value); } /** * midori_extension_install_string: * @extension: a #MidoriExtension * @name: the name of the setting * @default_value: the default value * * Installs a string that can be used to conveniently * store user configuration. * * Note that all settings have to be installed before * the extension is activated. * * Since: 0.1.3 **/ void midori_extension_install_string (MidoriExtension* extension, const gchar* name, const gchar* default_value) { midori_classic_extension_install_string (MIDORI_CLASSIC_EXTENSION (extension), name, default_value); } /** * midori_extension_get_string: * @extension: a #MidoriExtension * @name: the name of the setting * * Retrieves the value of the specified setting. * * Since: 0.1.3 **/ const gchar* midori_extension_get_string (MidoriExtension* extension, const gchar* name) { return midori_classic_extension_get_string (MIDORI_CLASSIC_EXTENSION (extension), name); } /** * midori_extension_set_string: * @extension: a #MidoriExtension * @name: the name of the setting * @value: the new value * * Assigns a new value to the specified setting. * * Since: 0.1.3 **/ void midori_extension_set_string (MidoriExtension* extension, const gchar* name, const gchar* value) { midori_classic_extension_set_string (MIDORI_CLASSIC_EXTENSION (extension), name, value); } /** * midori_extension_install_string_list: * @extension: a #MidoriExtension * @name: the name of the setting * @default_value: the default value * * Installs a string list that can be used to conveniently * store user configuration. * * Note that all settings have to be installed before * the extension is activated. * * Since: 0.1.7 **/ void midori_extension_install_string_list (MidoriExtension* extension, const gchar* name, gchar** default_value, gsize default_length) { midori_classic_extension_install_string_list (MIDORI_CLASSIC_EXTENSION (extension), name, default_value, default_length); } /** * midori_extension_get_string_list: * @extension: a #MidoriExtension * @name: the name of the setting * @length: return location to store number of strings, or %NULL * * Retrieves the value of the specified setting. * * Return value: a newly allocated NULL-terminated list of strings, * should be freed with g_strfreev() * * Since: 0.1.7 **/ gchar** midori_extension_get_string_list (MidoriExtension* extension, const gchar* name, gsize* length) { return midori_classic_extension_get_string_list (MIDORI_CLASSIC_EXTENSION (extension), name, (int*)length); } /** * midori_extension_set_string_list: * @extension: a #MidoriExtension * @name: the name of the setting * @value: the new value * @length: number of strings in @value, or G_MAXSIZE * * Assigns a new value to the specified setting. * * Since: 0.1.7 **/ void midori_extension_set_string_list (MidoriExtension* extension, const gchar* name, gchar** value, gsize length) { midori_classic_extension_set_string_list (MIDORI_CLASSIC_EXTENSION (extension), name, value, length); }