Add boolean and integer settings to MidoriExtension, with tests
This commit is contained in:
parent
01c9ed152c
commit
2cd86150e4
3 changed files with 273 additions and 20 deletions
|
@ -34,6 +34,22 @@ struct _MidoriExtensionPrivate
|
||||||
GHashTable* settings;
|
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
|
typedef struct
|
||||||
{
|
{
|
||||||
gchar* name;
|
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) \
|
#define me_setting_install(stype, _name, gtype, _default_value, _value) \
|
||||||
setting = g_new (stype, 1); \
|
setting = g_new (stype, 1); \
|
||||||
setting->name = _name; \
|
setting->name = _name; \
|
||||||
|
@ -186,7 +216,17 @@ midori_extension_activate_cb (MidoriExtension* extension,
|
||||||
{
|
{
|
||||||
MESettingString* setting = (MESettingString*)lsettings->data;
|
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);
|
setting->value = g_strdup (setting->default_value);
|
||||||
else
|
else
|
||||||
g_assert_not_reached ();
|
g_assert_not_reached ();
|
||||||
|
@ -369,6 +409,166 @@ midori_extension_get_config_dir (MidoriExtension* extension)
|
||||||
return extension->priv->config_dir;
|
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:
|
* midori_extension_install_string:
|
||||||
* @extension: a #MidoriExtension
|
* @extension: a #MidoriExtension
|
||||||
|
@ -391,22 +591,7 @@ midori_extension_install_string (MidoriExtension* extension,
|
||||||
MESettingString* setting;
|
MESettingString* setting;
|
||||||
|
|
||||||
g_return_if_fail (midori_extension_is_prepared (extension));
|
g_return_if_fail (midori_extension_is_prepared (extension));
|
||||||
|
midori_extension_can_install_setting (extension, name);
|
||||||
/* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
me_setting_install (MESettingString, g_strdup (name), G_TYPE_STRING,
|
me_setting_install (MESettingString, g_strdup (name), G_TYPE_STRING,
|
||||||
g_strdup (default_value), NULL);
|
g_strdup (default_value), NULL);
|
||||||
|
|
|
@ -60,6 +60,34 @@ midori_extension_deactivate (MidoriExtension* extension);
|
||||||
const gchar*
|
const gchar*
|
||||||
midori_extension_get_config_dir (MidoriExtension* extension);
|
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
|
void
|
||||||
midori_extension_install_string (MidoriExtension* extension,
|
midori_extension_install_string (MidoriExtension* extension,
|
||||||
const gchar* name,
|
const gchar* name,
|
||||||
|
|
|
@ -60,11 +60,10 @@ extension_create (void)
|
||||||
g_assert (g_object_get_data (G_OBJECT (extension), "deactivated") == magic);
|
g_assert (g_object_get_data (G_OBJECT (extension), "deactivated") == magic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static MidoriExtension*
|
||||||
extension_settings (void)
|
extension_mock_object (void)
|
||||||
{
|
{
|
||||||
MidoriExtension* extension;
|
MidoriExtension* extension;
|
||||||
const gchar* lastname;
|
|
||||||
|
|
||||||
extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
||||||
"name", "TestExtension",
|
"name", "TestExtension",
|
||||||
|
@ -72,6 +71,46 @@ extension_settings (void)
|
||||||
"description", "Nothing but a test.",
|
"description", "Nothing but a test.",
|
||||||
"authors", "John Doe",
|
"authors", "John Doe",
|
||||||
NULL);
|
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");
|
midori_extension_install_string (extension, "lastname", "Thomas Mann");
|
||||||
lastname = midori_extension_get_string (extension, "lastname");
|
lastname = midori_extension_get_string (extension, "lastname");
|
||||||
g_assert_cmpstr (lastname, ==, NULL);
|
g_assert_cmpstr (lastname, ==, NULL);
|
||||||
|
@ -83,6 +122,7 @@ extension_settings (void)
|
||||||
midori_extension_set_string (extension, "lastname", "Theodor Fontane");
|
midori_extension_set_string (extension, "lastname", "Theodor Fontane");
|
||||||
lastname = midori_extension_get_string (extension, "lastname");
|
lastname = midori_extension_get_string (extension, "lastname");
|
||||||
g_assert_cmpstr (lastname, ==, "Theodor Fontane");
|
g_assert_cmpstr (lastname, ==, "Theodor Fontane");
|
||||||
|
midori_extension_deactivate (extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Reference in a new issue