From: Christian Dywan Date: Fri, 20 Feb 2009 19:24:13 +0000 (+0100) Subject: Add boolean and integer settings to MidoriExtension, with tests X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2cd86150e4564acaa96cf1c1c4293477ea137ae5;p=midori Add boolean and integer settings to MidoriExtension, with tests --- diff --git a/midori/midori-extension.c b/midori/midori-extension.c index 9a576003..7f8f01b1 100644 --- a/midori/midori-extension.c +++ b/midori/midori-extension.c @@ -34,6 +34,22 @@ struct _MidoriExtensionPrivate GHashTable* settings; }; +typedef struct +{ + gchar* name; + GType type; + gboolean default_value; + gboolean value; +} MESettingBoolean; + +typedef struct +{ + gchar* name; + GType type; + gint default_value; + gint value; +} MESettingInteger; + typedef struct { gchar* name; @@ -54,6 +70,20 @@ void me_setting_free (gpointer setting) } } +#define midori_extension_can_install_setting(extension, name) \ + if (extension->priv->active) \ + { \ + g_critical ("%s: Settings have to be installed before " \ + "the extension is activated.", G_STRFUNC); \ + return; \ + } \ + if (g_hash_table_lookup (extension->priv->settings, name)) \ + { \ + g_critical ("%s: A setting with the name '%s' is already installed.", \ + G_STRFUNC, name); \ + return; \ + } + #define me_setting_install(stype, _name, gtype, _default_value, _value) \ setting = g_new (stype, 1); \ setting->name = _name; \ @@ -186,7 +216,17 @@ midori_extension_activate_cb (MidoriExtension* extension, { MESettingString* setting = (MESettingString*)lsettings->data; - if (setting->type == G_TYPE_STRING) + if (setting->type == G_TYPE_BOOLEAN) + { + MESettingBoolean* setting_ = (MESettingBoolean*)setting; + setting_->value = setting_->default_value; + } + else if (setting->type == G_TYPE_INT) + { + MESettingInteger* setting_ = (MESettingInteger*)setting; + setting_->value = setting_->default_value; + } + else if (setting->type == G_TYPE_STRING) setting->value = g_strdup (setting->default_value); else g_assert_not_reached (); @@ -369,6 +409,166 @@ midori_extension_get_config_dir (MidoriExtension* extension) return extension->priv->config_dir; } +/** + * 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) +{ + MESettingBoolean* setting; + + g_return_if_fail (midori_extension_is_prepared (extension)); + midori_extension_can_install_setting (extension, name); + + me_setting_install (MESettingBoolean, g_strdup (name), G_TYPE_BOOLEAN, + default_value, FALSE); +} + +/** + * 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) +{ + MESettingBoolean* setting; + + g_return_val_if_fail (midori_extension_is_prepared (extension), FALSE); + g_return_val_if_fail (name != NULL, FALSE); + + setting = g_hash_table_lookup (extension->priv->settings, name); + + me_setting_type (setting, G_TYPE_BOOLEAN, return FALSE); + + return setting->value; +} + +/** + * 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) +{ + MESettingBoolean* setting; + + g_return_if_fail (midori_extension_is_active (extension)); + g_return_if_fail (name != NULL); + + setting = g_hash_table_lookup (extension->priv->settings, name); + + me_setting_type (setting, G_TYPE_BOOLEAN, return); + + setting->value = 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) +{ + MESettingInteger* setting; + + g_return_if_fail (midori_extension_is_prepared (extension)); + midori_extension_can_install_setting (extension, name); + + me_setting_install (MESettingInteger, g_strdup (name), G_TYPE_INT, + default_value, 0); +} + +/** + * 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) +{ + MESettingInteger* setting; + + g_return_val_if_fail (midori_extension_is_prepared (extension), 0); + g_return_val_if_fail (name != NULL, 0); + + setting = g_hash_table_lookup (extension->priv->settings, name); + + me_setting_type (setting, G_TYPE_INT, return 0); + + return setting->value; +} + +/** + * 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) +{ + MESettingInteger* setting; + + g_return_if_fail (midori_extension_is_active (extension)); + g_return_if_fail (name != NULL); + + setting = g_hash_table_lookup (extension->priv->settings, name); + + me_setting_type (setting, G_TYPE_INT, return); + + setting->value = value; +} + /** * midori_extension_install_string: * @extension: a #MidoriExtension @@ -391,22 +591,7 @@ midori_extension_install_string (MidoriExtension* extension, MESettingString* setting; g_return_if_fail (midori_extension_is_prepared (extension)); - - /* This is not strictly a technical requirement but we want - to ensure that a running extension is in a reliable state. */ - if (extension->priv->active) - { - g_critical ("%s: Settings have to be installed before " - "the extension is activated.", G_STRFUNC); - return; - } - - if (g_hash_table_lookup (extension->priv->settings, name)) - { - g_critical ("%s: A setting with the name '%s' is already installed.", - G_STRFUNC, name); - return; - } + midori_extension_can_install_setting (extension, name); me_setting_install (MESettingString, g_strdup (name), G_TYPE_STRING, g_strdup (default_value), NULL); diff --git a/midori/midori-extension.h b/midori/midori-extension.h index 00cc808d..3cab6ba6 100644 --- a/midori/midori-extension.h +++ b/midori/midori-extension.h @@ -60,6 +60,34 @@ midori_extension_deactivate (MidoriExtension* extension); const gchar* midori_extension_get_config_dir (MidoriExtension* extension); +void +midori_extension_install_boolean (MidoriExtension* extension, + const gchar* name, + gboolean default_value); + +gboolean +midori_extension_get_boolean (MidoriExtension* extension, + const gchar* name); + +void +midori_extension_set_boolean (MidoriExtension* extension, + const gchar* name, + gboolean value); + +void +midori_extension_install_integer (MidoriExtension* extension, + const gchar* name, + gint default_value); + +gint +midori_extension_get_integer (MidoriExtension* extension, + const gchar* name); + +void +midori_extension_set_integer (MidoriExtension* extension, + const gchar* name, + gint value); + void midori_extension_install_string (MidoriExtension* extension, const gchar* name, diff --git a/tests/extensions.c b/tests/extensions.c index 33b81e2d..69638fbd 100644 --- a/tests/extensions.c +++ b/tests/extensions.c @@ -60,11 +60,10 @@ extension_create (void) g_assert (g_object_get_data (G_OBJECT (extension), "deactivated") == magic); } -static void -extension_settings (void) +static MidoriExtension* +extension_mock_object (void) { MidoriExtension* extension; - const gchar* lastname; extension = g_object_new (MIDORI_TYPE_EXTENSION, "name", "TestExtension", @@ -72,6 +71,46 @@ extension_settings (void) "description", "Nothing but a test.", "authors", "John Doe", NULL); + return extension; +} + +static void +extension_settings (void) +{ + MidoriExtension* extension; + gboolean nihilist; + gint age; + const gchar* lastname; + + extension = extension_mock_object (); + midori_extension_install_boolean (extension, "nihilist", TRUE); + nihilist = midori_extension_get_boolean (extension, "nihilist"); + g_assert (!nihilist); + g_signal_connect (extension, "activate", + G_CALLBACK (extension_activate_cb), NULL); + g_signal_emit_by_name (extension, "activate", NULL); + nihilist = midori_extension_get_boolean (extension, "nihilist"); + g_assert (nihilist); + midori_extension_set_boolean (extension, "nihilist", FALSE); + nihilist = midori_extension_get_boolean (extension, "nihilist"); + g_assert (!nihilist); + midori_extension_deactivate (extension); + + extension = extension_mock_object (); + midori_extension_install_integer (extension, "age", 88); + age = midori_extension_get_integer (extension, "age"); + g_assert_cmpint (age, ==, 0); + g_signal_connect (extension, "activate", + G_CALLBACK (extension_activate_cb), NULL); + g_signal_emit_by_name (extension, "activate", NULL); + age = midori_extension_get_integer (extension, "age"); + g_assert_cmpint (age, ==, 88); + midori_extension_set_integer (extension, "age", 66); + age = midori_extension_get_integer (extension, "age"); + g_assert_cmpint (age, ==, 66); + midori_extension_deactivate (extension); + + extension = extension_mock_object (); midori_extension_install_string (extension, "lastname", "Thomas Mann"); lastname = midori_extension_get_string (extension, "lastname"); g_assert_cmpstr (lastname, ==, NULL); @@ -83,6 +122,7 @@ extension_settings (void) midori_extension_set_string (extension, "lastname", "Theodor Fontane"); lastname = midori_extension_get_string (extension, "lastname"); g_assert_cmpstr (lastname, ==, "Theodor Fontane"); + midori_extension_deactivate (extension); } int