Implement string_list setting in MidoriExtension

This commit is contained in:
Dale Whittaker 2009-04-25 16:02:29 +02:00 committed by Christian Dywan
parent ffd58a06a8
commit 9c9f34180d
2 changed files with 161 additions and 1 deletions

View file

@ -1,5 +1,6 @@
/*
Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
Copyright (C) 2009 Dale Whittaker <dayul@users.sf.net>
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);
}
}
}

View file

@ -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__ */