comparison mcabber/mcabber/settings.c @ 1673:552da310b83e

Add option guards
author Myhailo Danylenko <isbear@ukrpost.net>
date Mon, 18 Jan 2010 16:32:02 +0200
parents 41c26b7d2890
children e6e89b1d7831
comparison
equal deleted inserted replaced
1672:3f8a44af3695 1673:552da310b83e
37 #define CONFLINE_LENGTH 1024 37 #define CONFLINE_LENGTH 1024
38 38
39 static GHashTable *option; 39 static GHashTable *option;
40 static GHashTable *alias; 40 static GHashTable *alias;
41 static GHashTable *binding; 41 static GHashTable *binding;
42 static GHashTable *guards;
42 43
43 #ifdef HAVE_GPGME /* PGP settings */ 44 #ifdef HAVE_GPGME /* PGP settings */
44 static GHashTable *pgpopt; 45 static GHashTable *pgpopt;
45 46
46 typedef struct { 47 typedef struct {
71 void settings_init(void) 72 void settings_init(void)
72 { 73 {
73 option = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); 74 option = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free);
74 alias = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); 75 alias = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free);
75 binding = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); 76 binding = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free);
77 guards = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, NULL);
76 #ifdef HAVE_GPGME 78 #ifdef HAVE_GPGME
77 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal); 79 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal);
78 #endif 80 #endif
79 #ifdef HAVE_LIBOTR 81 #ifdef HAVE_LIBOTR
80 otrpolicy = g_hash_table_new(&g_str_hash, &g_str_equal); 82 otrpolicy = g_hash_table_new(&g_str_hash, &g_str_equal);
298 } 300 }
299 *pval = g_strndup(val, t+1-val); 301 *pval = g_strndup(val, t+1-val);
300 return TRUE; 302 return TRUE;
301 } 303 }
302 304
305 void settings_set_guard(const gchar *key, settings_guard_t guard)
306 {
307 if (!guard)
308 g_hash_table_remove(guards, key);
309 else
310 g_hash_table_insert(guards, g_strdup(key), (gpointer)guard);
311 }
312
313 void settings_del_guard(const gchar *key)
314 {
315 settings_set_guard(key, NULL);
316 }
317
318 void settings_opt_set_raw(const gchar *key, const gchar *value)
319 {
320 if (!value)
321 g_hash_table_remove(option, key);
322 else
323 g_hash_table_insert(option, g_strdup(key), g_strdup(value));
324 }
325
303 void settings_set(guint type, const gchar *key, const gchar *value) 326 void settings_set(guint type, const gchar *key, const gchar *value)
304 { 327 {
305 GHashTable *hash; 328 GHashTable *hash;
329 gchar *dup_value = NULL;
330 settings_guard_t guard = NULL;
331
332 if (type == SETTINGS_TYPE_OPTION) {
333 guard = (settings_guard_t)g_hash_table_lookup(guards, key);
334 if (guard)
335 dup_value = guard(key, value);
336 }
306 337
307 hash = get_hash(type); 338 hash = get_hash(type);
308 if (!hash) 339 if (!hash)
309 return; 340 return;
310 341
311 if (!value) { 342 if (!value && !dup_value)
312 g_hash_table_remove(hash, key); 343 g_hash_table_remove(hash, key);
313 } else { 344 else if (!guard)
314 g_hash_table_insert(hash, g_strdup(key), g_strdup(value)); 345 g_hash_table_insert(hash, g_strdup(key), g_strdup(value));
315 } 346 else if (dup_value)
347 g_hash_table_insert(hash, g_strdup(key), dup_value);
348 else
349 g_hash_table_remove(option, key);
316 } 350 }
317 351
318 void settings_del(guint type, const gchar *key) 352 void settings_del(guint type, const gchar *key)
319 { 353 {
320 settings_set(type, key, NULL); 354 settings_set(type, key, NULL);