Mercurial > hg
comparison mcabber/src/settings.c @ 1070:9eaacc0ad3a2
Use a hash for settings
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 03 Dec 2006 11:59:42 +0100 |
parents | 6646d2ed7f74 |
children | b2fc694a8228 |
comparison
equal
deleted
inserted
replaced
1069:641e6c618e5d | 1070:9eaacc0ad3a2 |
---|---|
26 #include "settings.h" | 26 #include "settings.h" |
27 #include "commands.h" | 27 #include "commands.h" |
28 #include "utils.h" | 28 #include "utils.h" |
29 #include "logprint.h" | 29 #include "logprint.h" |
30 | 30 |
31 static GSList *option; | 31 static GHashTable *option; |
32 static GSList *alias; | 32 static GHashTable *alias; |
33 static GSList *binding; | 33 static GHashTable *binding; |
34 | |
35 typedef struct { | |
36 gchar *name; | |
37 gchar *value; | |
38 } T_setting; | |
39 | 34 |
40 #ifdef HAVE_GPGME /* PGP settings */ | 35 #ifdef HAVE_GPGME /* PGP settings */ |
41 static GHashTable *pgpopt; | 36 static GHashTable *pgpopt; |
42 | 37 |
43 typedef struct { | 38 typedef struct { |
44 gchar *pgp_keyid; /* KeyId the contact is supposed to use */ | 39 gchar *pgp_keyid; /* KeyId the contact is supposed to use */ |
45 guint pgp_disabled; /* If TRUE, PGP is disabled for outgoing messages */ | 40 guint pgp_disabled; /* If TRUE, PGP is disabled for outgoing messages */ |
46 } T_pgpopt; | 41 } T_pgpopt; |
47 #endif | 42 #endif |
48 | 43 |
49 static inline GSList **get_list_ptr(guint type) | 44 static inline GHashTable *get_hash(guint type) |
50 { | 45 { |
51 if (type == SETTINGS_TYPE_OPTION) return &option; | 46 if (type == SETTINGS_TYPE_OPTION) return option; |
52 else if (type == SETTINGS_TYPE_ALIAS) return &alias; | 47 else if (type == SETTINGS_TYPE_ALIAS) return alias; |
53 else if (type == SETTINGS_TYPE_BINDING) return &binding; | 48 else if (type == SETTINGS_TYPE_BINDING) return binding; |
54 return NULL; | 49 return NULL; |
55 } | 50 } |
56 | 51 |
57 // Return a pointer to the node with the requested key, or NULL if none found | |
58 static GSList *settings_find(GSList *list, const gchar *key) | |
59 { | |
60 GSList *ptr; | |
61 | |
62 if (!list) return NULL; | |
63 | |
64 for (ptr = list ; ptr; ptr = g_slist_next(ptr)) | |
65 if (!strcasecmp(key, ((T_setting*)ptr->data)->name)) | |
66 break; | |
67 | |
68 return ptr; | |
69 } | |
70 | |
71 /* -- */ | 52 /* -- */ |
72 | 53 |
73 void settings_init(void) | 54 void settings_init(void) |
74 { | 55 { |
56 option = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); | |
57 alias = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); | |
58 binding = g_hash_table_new_full(&g_str_hash, &g_str_equal, &g_free, &g_free); | |
75 #ifdef HAVE_GPGME | 59 #ifdef HAVE_GPGME |
76 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal); | 60 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal); |
77 #endif | 61 #endif |
78 } | 62 } |
79 | 63 |
245 return TRUE; | 229 return TRUE; |
246 } | 230 } |
247 | 231 |
248 void settings_set(guint type, const gchar *key, const gchar *value) | 232 void settings_set(guint type, const gchar *key, const gchar *value) |
249 { | 233 { |
250 GSList **plist; | 234 GHashTable *hash; |
251 GSList *sptr; | 235 |
252 T_setting *setting; | 236 hash = get_hash(type); |
253 | 237 if (!hash) |
254 plist = get_list_ptr(type); | 238 return; |
255 if (!plist) return; | 239 |
256 | 240 if (!value) { |
257 sptr = settings_find(*plist, key); | 241 g_hash_table_remove(hash, key); |
258 if (sptr) { | 242 } else { |
259 // The setting has been found. We will update it or delete it. | 243 g_hash_table_insert(hash, g_strdup(key), g_strdup(value)); |
260 setting = (T_setting*)sptr->data; | |
261 g_free(setting->value); | |
262 if (!value) { | |
263 // Let's remove the setting | |
264 g_free(setting->name); | |
265 *plist = g_slist_delete_link(*plist, sptr); | |
266 } else { | |
267 // Let's update the setting | |
268 setting->value = g_strdup(value); | |
269 } | |
270 } else if (value) { | |
271 setting = g_new(T_setting, 1); | |
272 setting->name = g_strdup(key); | |
273 setting->value = g_strdup(value); | |
274 *plist = g_slist_append(*plist, setting); | |
275 } | 244 } |
276 } | 245 } |
277 | 246 |
278 void settings_del(guint type, const gchar *key) | 247 void settings_del(guint type, const gchar *key) |
279 { | 248 { |
280 settings_set(type, key, NULL); | 249 settings_set(type, key, NULL); |
281 } | 250 } |
282 | 251 |
283 const gchar *settings_get(guint type, const gchar *key) | 252 const gchar *settings_get(guint type, const gchar *key) |
284 { | 253 { |
285 GSList **plist; | 254 GHashTable *hash; |
286 GSList *sptr; | 255 |
287 T_setting *setting; | 256 hash = get_hash(type); |
288 | 257 if (!hash) |
289 plist = get_list_ptr(type); | 258 return NULL; |
290 sptr = settings_find(*plist, key); | 259 |
291 if (!sptr) return NULL; | 260 return g_hash_table_lookup(hash, key); |
292 | |
293 setting = (T_setting*)sptr->data; | |
294 return setting->value; | |
295 } | 261 } |
296 | 262 |
297 int settings_get_int(guint type, const gchar *key) | 263 int settings_get_int(guint type, const gchar *key) |
298 { | 264 { |
299 const gchar *setval = settings_get(type, key); | 265 const gchar *setval = settings_get(type, key); |
342 return rstatus; | 308 return rstatus; |
343 } | 309 } |
344 | 310 |
345 // settings_foreach(type, pfunction, param) | 311 // settings_foreach(type, pfunction, param) |
346 // Call pfunction(param, key, value) for each setting with requested type. | 312 // Call pfunction(param, key, value) for each setting with requested type. |
347 void settings_foreach(guint type, void (*pfunc)(void *param, char *k, char *v), | 313 void settings_foreach(guint type, void (*pfunc)(char *k, char *v, void *param), |
348 void *param) | 314 void *param) |
349 { | 315 { |
350 GSList **plist; | 316 GHashTable *hash; |
351 GSList *ptr; | 317 |
352 T_setting *setting; | 318 hash = get_hash(type); |
353 | 319 if (!hash) |
354 plist = get_list_ptr(type); | 320 return; |
355 | 321 |
356 if (!*plist) return; | 322 g_hash_table_foreach(hash, (GHFunc)pfunc, param); |
357 | |
358 for (ptr = *plist ; ptr; ptr = g_slist_next(ptr)) { | |
359 setting = ptr->data; | |
360 pfunc(param, setting->name, setting->value); | |
361 } | |
362 } | 323 } |
363 | 324 |
364 | 325 |
365 // default_muc_nickname() | 326 // default_muc_nickname() |
366 // Return the user's default nickname | 327 // Return the user's default nickname |