From 9c9f34180d0555f89c7052f0770bd038208e3313 Mon Sep 17 00:00:00 2001 From: Dale Whittaker Date: Sat, 25 Apr 2009 16:02:29 +0200 Subject: [PATCH] Implement string_list setting in MidoriExtension --- midori/midori-extension.c | 145 +++++++++++++++++++++++++++++++++++++- midori/midori-extension.h | 17 +++++ 2 files changed, 161 insertions(+), 1 deletion(-) diff --git a/midori/midori-extension.c b/midori/midori-extension.c index e882172c..9950a0bb 100644 --- a/midori/midori-extension.c +++ b/midori/midori-extension.c @@ -1,5 +1,6 @@ /* Copyright (C) 2008-2009 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 @@ -60,16 +61,33 @@ typedef struct gchar* value; } MESettingString; +typedef struct +{ + gchar* name; + GType type; + gchar** default_value; + gchar** value; + gsize default_length; + gsize length; +} MESettingStringList; + void me_setting_free (gpointer setting) { MESettingString* string_setting = (MESettingString*)setting; + MESettingStringList* strlist_setting = (MESettingStringList*)setting; - g_free (string_setting->name); if (string_setting->type == G_TYPE_STRING) { + g_free (string_setting->name); g_free (string_setting->default_value); g_free (string_setting->value); } + if (strlist_setting->type == G_TYPE_STRV) + { + g_free (strlist_setting->name); + g_strfreev (strlist_setting->default_value); + g_strfreev (strlist_setting->value); + } } #define midori_extension_can_install_setting(extension, name) \ @@ -276,6 +294,19 @@ midori_extension_activate_cb (MidoriExtension* extension, else setting->value = g_strdup (setting->default_value); } + else if (setting->type == G_TYPE_STRV) + { + MESettingStringList* setting_ = (MESettingStringList*)setting; + if (extension->priv->key_file) + { + setting_->value = sokoke_key_file_get_string_list_default ( + extension->priv->key_file, + "settings", setting->name, &setting_->length, + setting_->default_value, &setting_->default_length, NULL); + } + else + setting_->value = g_strdupv (setting_->default_value); + } else g_assert_not_reached (); @@ -797,3 +828,115 @@ midori_extension_set_string (MidoriExtension* extension, } } } + +/** + * 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) +{ + MESettingStringList* setting; + + g_return_if_fail (midori_extension_is_prepared (extension)); + midori_extension_can_install_setting (extension, name); + + me_setting_install (MESettingStringList, g_strdup (name), G_TYPE_STRV, + g_strdupv (default_value), NULL); + + setting->default_length = 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) +{ + MESettingStringList* setting; + + g_return_val_if_fail (midori_extension_is_prepared (extension), NULL); + g_return_val_if_fail (name != NULL, NULL); + + setting = g_hash_table_lookup (extension->priv->settings, name); + + me_setting_type (setting, G_TYPE_STRV, return NULL); + + if (length) + *length = setting->length; + + return g_strdupv (setting->value); +} + +/** + * 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) +{ + MESettingStringList* 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_STRV, return); + + katze_strv_assign (setting->value, g_strdupv (value)); + setting->length = length; + + if (extension->priv->key_file) + { + GError* error = NULL; + /* FIXME: Handle readonly folder/ file */ + gchar* config_file = g_build_filename (extension->priv->config_dir, + "config", NULL); + g_mkdir_with_parents (extension->priv->config_dir, 0700); + g_key_file_set_string_list (extension->priv->key_file, + "settings", name, (const gchar**)value, length); + sokoke_key_file_save_to_file (extension->priv->key_file, config_file, &error); + if (error) + { + printf (_("The configuration of the extension '%s' couldn't be saved: %s\n"), + extension->priv->name, error->message); + g_error_free (error); + } + } +} diff --git a/midori/midori-extension.h b/midori/midori-extension.h index 54e96b94..60b7d017 100644 --- a/midori/midori-extension.h +++ b/midori/midori-extension.h @@ -108,6 +108,23 @@ midori_extension_set_string (MidoriExtension* extension, const gchar* name, const gchar* value); +void +midori_extension_install_string_list (MidoriExtension* extension, + const gchar* name, + gchar** default_value, + gsize default_length); + +gchar** +midori_extension_get_string_list (MidoriExtension* extension, + const gchar* name, + gsize* length); + +void +midori_extension_set_string_list (MidoriExtension* extension, + const gchar* name, + gchar** value, + gsize length); + G_END_DECLS #endif /* __MIDORI_EXTENSION_H__ */