changeset 1673:552da310b83e

Add option guards
author Myhailo Danylenko <isbear@ukrpost.net>
date Mon, 18 Jan 2010 16:32:02 +0200
parents 3f8a44af3695
children f02e7076ccec
files mcabber/mcabber/settings.c mcabber/mcabber/settings.h
diffstat 2 files changed, 42 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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);