# HG changeset patch # User Myhailo Danylenko # Date 1263825122 -7200 # Node ID 552da310b83e441e17ef7d19789b7a2ba4f8ebdf # Parent 3f8a44af36954a3c904e49e784aefa55b08f8bb5 Add option guards diff -r 3f8a44af3695 -r 552da310b83e mcabber/mcabber/settings.c --- a/mcabber/mcabber/settings.c Tue Feb 02 22:11:44 2010 +0100 +++ b/mcabber/mcabber/settings.c Mon Jan 18 16:32:02 2010 +0200 @@ -39,6 +39,7 @@ static GHashTable *option; static GHashTable *alias; static GHashTable *binding; +static GHashTable *guards; #ifdef HAVE_GPGME /* PGP settings */ static GHashTable *pgpopt; @@ -73,6 +74,7 @@ option = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); alias = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); binding = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); + guards = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, NULL); #ifdef HAVE_GPGME pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal); #endif @@ -300,19 +302,51 @@ return TRUE; } +void settings_set_guard(const gchar *key, settings_guard_t guard) +{ + if (!guard) + g_hash_table_remove(guards, key); + else + g_hash_table_insert(guards, g_strdup(key), (gpointer)guard); +} + +void settings_del_guard(const gchar *key) +{ + settings_set_guard(key, NULL); +} + +void settings_opt_set_raw(const gchar *key, const gchar *value) +{ + if (!value) + g_hash_table_remove(option, key); + else + g_hash_table_insert(option, g_strdup(key), g_strdup(value)); +} + void settings_set(guint type, const gchar *key, const gchar *value) { GHashTable *hash; + gchar *dup_value = NULL; + settings_guard_t guard = NULL; + + if (type == SETTINGS_TYPE_OPTION) { + guard = (settings_guard_t)g_hash_table_lookup(guards, key); + if (guard) + dup_value = guard(key, value); + } hash = get_hash(type); if (!hash) return; - if (!value) { + if (!value && !dup_value) g_hash_table_remove(hash, key); - } else { + else if (!guard) g_hash_table_insert(hash, g_strdup(key), g_strdup(value)); - } + else if (dup_value) + g_hash_table_insert(hash, g_strdup(key), dup_value); + else + g_hash_table_remove(option, key); } void settings_del(guint type, const gchar *key) diff -r 3f8a44af3695 -r 552da310b83e mcabber/mcabber/settings.h --- a/mcabber/mcabber/settings.h Tue Feb 02 22:11:44 2010 +0100 +++ b/mcabber/mcabber/settings.h Mon Jan 18 16:32:02 2010 +0200 @@ -27,9 +27,14 @@ #define mkcmdstr(cmd) COMMAND_CHARSTR cmd +typedef gchar *(*settings_guard_t)(const gchar *key, const gchar *new_value); + void settings_init(void); int cfg_read_file(char *filename, guint mainfile); guint parse_assigment(gchar *assignment, gchar **pkey, gchar **pval); +void settings_set_guard(const gchar *key, settings_guard_t guard); +void settings_del_guard(const gchar *key); +void settings_opt_set_raw(const gchar *key, const gchar *value); void settings_set(guint type, const gchar *key, const gchar *value); void settings_del(guint type, const gchar *key); const gchar *settings_get(guint type, const gchar *key);