Mercurial > pidgin.yaz
comparison src/blist.c @ 10430:f5508fbf6764
[gaim-migrate @ 11682]
Use GaimValue instead of struct gaim_blist_node_setting for buddy list
node settings, and get rid of gaim_blist_node_setting. Yay for code
reuse.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 27 Dec 2004 06:47:04 +0000 |
parents | e41f0668a648 |
children | 843ed1f2bf3f |
comparison
equal
deleted
inserted
replaced
10429:e41f0668a648 | 10430:f5508fbf6764 |
---|---|
33 #include "util.h" | 33 #include "util.h" |
34 #include "xmlnode.h" | 34 #include "xmlnode.h" |
35 | 35 |
36 #define PATHSIZE 1024 | 36 #define PATHSIZE 1024 |
37 | 37 |
38 /* TODO: Should use GaimValue instead of this. */ | |
39 struct gaim_blist_node_setting { | |
40 enum { | |
41 GAIM_BLIST_NODE_SETTING_BOOL, | |
42 GAIM_BLIST_NODE_SETTING_INT, | |
43 GAIM_BLIST_NODE_SETTING_STRING | |
44 } type; | |
45 union { | |
46 gboolean boolean; | |
47 int integer; | |
48 char *string; | |
49 } value; | |
50 }; | |
51 | |
52 static GaimBlistUiOps *blist_ui_ops = NULL; | 38 static GaimBlistUiOps *blist_ui_ops = NULL; |
53 | 39 |
54 static GaimBuddyList *gaimbuddylist = NULL; | 40 static GaimBuddyList *gaimbuddylist = NULL; |
55 static guint save_timer = 0; | 41 static guint save_timer = 0; |
56 static gboolean blist_loaded = FALSE; | 42 static gboolean blist_loaded = FALSE; |
103 /********************************************************************* | 89 /********************************************************************* |
104 * Writing to disk * | 90 * Writing to disk * |
105 *********************************************************************/ | 91 *********************************************************************/ |
106 | 92 |
107 static void | 93 static void |
108 setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data) | 94 value_to_xmlnode(gpointer key, gpointer hvalue, gpointer user_data) |
109 { | 95 { |
110 const char *name; | 96 const char *name; |
111 struct gaim_blist_node_setting *setting; | 97 GaimValue *value; |
112 xmlnode *node, *child; | 98 xmlnode *node, *child; |
113 char buf[20]; | 99 char buf[20]; |
114 | 100 |
115 name = (const char *)key; | 101 name = (const char *)key; |
116 setting = (struct gaim_blist_node_setting *)value; | 102 value = (GaimValue *)hvalue; |
117 node = (xmlnode *)user_data; | 103 node = (xmlnode *)user_data; |
118 | 104 |
119 child = xmlnode_new_child(node, "setting"); | 105 child = xmlnode_new_child(node, "setting"); |
120 xmlnode_set_attrib(child, "name", name); | 106 xmlnode_set_attrib(child, "name", name); |
121 | 107 |
122 if (setting->type == GAIM_BLIST_NODE_SETTING_INT) { | 108 if (gaim_value_get_type(value) == GAIM_TYPE_INT) { |
123 xmlnode_set_attrib(child, "type", "int"); | 109 xmlnode_set_attrib(child, "type", "int"); |
124 snprintf(buf, sizeof(buf), "%d", setting->value.integer); | 110 snprintf(buf, sizeof(buf), "%d", gaim_value_get_int(value)); |
125 xmlnode_insert_data(child, buf, -1); | 111 xmlnode_insert_data(child, buf, -1); |
126 } | 112 } |
127 else if (setting->type == GAIM_BLIST_NODE_SETTING_STRING) { | 113 else if (gaim_value_get_type(value) == GAIM_TYPE_STRING) { |
128 xmlnode_set_attrib(child, "type", "string"); | 114 xmlnode_set_attrib(child, "type", "string"); |
129 xmlnode_insert_data(child, setting->value.string, -1); | 115 xmlnode_insert_data(child, gaim_value_get_string(value), -1); |
130 } | 116 } |
131 else if (setting->type == GAIM_BLIST_NODE_SETTING_BOOL) { | 117 else if (gaim_value_get_type(value) == GAIM_TYPE_BOOLEAN) { |
132 xmlnode_set_attrib(child, "type", "bool"); | 118 xmlnode_set_attrib(child, "type", "bool"); |
133 snprintf(buf, sizeof(buf), "%d", setting->value.boolean); | 119 snprintf(buf, sizeof(buf), "%d", gaim_value_get_boolean(value)); |
134 xmlnode_insert_data(child, buf, -1); | 120 xmlnode_insert_data(child, buf, -1); |
135 } | 121 } |
136 } | 122 } |
137 | 123 |
138 static void | 124 static void |
171 child = xmlnode_new_child(node, "alias"); | 157 child = xmlnode_new_child(node, "alias"); |
172 xmlnode_insert_data(child, buddy->alias, -1); | 158 xmlnode_insert_data(child, buddy->alias, -1); |
173 } | 159 } |
174 | 160 |
175 /* Write buddy settings */ | 161 /* Write buddy settings */ |
176 g_hash_table_foreach(buddy->node.settings, setting_to_xmlnode, node); | 162 g_hash_table_foreach(buddy->node.settings, value_to_xmlnode, node); |
177 | 163 |
178 return node; | 164 return node; |
179 } | 165 } |
180 | 166 |
181 xmlnode * | 167 xmlnode * |
205 xmlnode_insert_child(node, child); | 191 xmlnode_insert_child(node, child); |
206 } | 192 } |
207 } | 193 } |
208 | 194 |
209 /* Write contact settings */ | 195 /* Write contact settings */ |
210 g_hash_table_foreach(cnode->settings, setting_to_xmlnode, node); | 196 g_hash_table_foreach(cnode->settings, value_to_xmlnode, node); |
211 | 197 |
212 return node; | 198 return node; |
213 } | 199 } |
214 | 200 |
215 xmlnode * | 201 xmlnode * |
232 | 218 |
233 /* Write chat components */ | 219 /* Write chat components */ |
234 g_hash_table_foreach(chat->components, chat_component_to_xmlnode, node); | 220 g_hash_table_foreach(chat->components, chat_component_to_xmlnode, node); |
235 | 221 |
236 /* Write chat settings */ | 222 /* Write chat settings */ |
237 g_hash_table_foreach(chat->node.settings, setting_to_xmlnode, node); | 223 g_hash_table_foreach(chat->node.settings, value_to_xmlnode, node); |
238 | 224 |
239 return node; | 225 return node; |
240 } | 226 } |
241 | 227 |
242 xmlnode * | 228 xmlnode * |
250 | 236 |
251 node = xmlnode_new("group"); | 237 node = xmlnode_new("group"); |
252 xmlnode_set_attrib(node, "name", group->name); | 238 xmlnode_set_attrib(node, "name", group->name); |
253 | 239 |
254 /* Write settings */ | 240 /* Write settings */ |
255 g_hash_table_foreach(group->node.settings, setting_to_xmlnode, node); | 241 g_hash_table_foreach(group->node.settings, value_to_xmlnode, node); |
256 | 242 |
257 /* Write contacts and chats */ | 243 /* Write contacts and chats */ |
258 for (cnode = gnode->child; cnode != NULL; cnode = cnode->next) | 244 for (cnode = gnode->child; cnode != NULL; cnode = cnode->next) |
259 { | 245 { |
260 if (!GAIM_BLIST_NODE_SHOULD_SAVE(cnode)) | 246 if (!GAIM_BLIST_NODE_SHOULD_SAVE(cnode)) |
2317 | 2303 |
2318 if (ui_ops != NULL && ui_ops->request_add_group != NULL) | 2304 if (ui_ops != NULL && ui_ops->request_add_group != NULL) |
2319 ui_ops->request_add_group(); | 2305 ui_ops->request_add_group(); |
2320 } | 2306 } |
2321 | 2307 |
2322 static void gaim_blist_node_setting_free(struct gaim_blist_node_setting *setting) | 2308 static void |
2323 { | 2309 gaim_blist_node_setting_free(gpointer data) |
2324 switch(setting->type) { | 2310 { |
2325 case GAIM_BLIST_NODE_SETTING_BOOL: | 2311 GaimValue *value; |
2326 case GAIM_BLIST_NODE_SETTING_INT: | 2312 |
2327 break; | 2313 value = (GaimValue *)data; |
2328 case GAIM_BLIST_NODE_SETTING_STRING: | 2314 |
2329 g_free(setting->value.string); | 2315 gaim_value_destroy(value); |
2330 break; | |
2331 } | |
2332 g_free(setting); | |
2333 } | 2316 } |
2334 | 2317 |
2335 static void gaim_blist_node_initialize_settings(GaimBlistNode *node) | 2318 static void gaim_blist_node_initialize_settings(GaimBlistNode *node) |
2336 { | 2319 { |
2337 if (node->settings) | 2320 if (node->settings) |
2350 g_hash_table_remove(node->settings, key); | 2333 g_hash_table_remove(node->settings, key); |
2351 | 2334 |
2352 schedule_blist_save(); | 2335 schedule_blist_save(); |
2353 } | 2336 } |
2354 | 2337 |
2355 void gaim_blist_node_set_bool(GaimBlistNode* node, const char *key, gboolean value) | 2338 void |
2356 { | 2339 gaim_blist_node_set_bool(GaimBlistNode* node, const char *key, gboolean data) |
2357 struct gaim_blist_node_setting *setting; | 2340 { |
2341 GaimValue *value; | |
2358 | 2342 |
2359 g_return_if_fail(node != NULL); | 2343 g_return_if_fail(node != NULL); |
2360 g_return_if_fail(node->settings != NULL); | 2344 g_return_if_fail(node->settings != NULL); |
2361 g_return_if_fail(key != NULL); | 2345 g_return_if_fail(key != NULL); |
2362 | 2346 |
2363 setting = g_new0(struct gaim_blist_node_setting, 1); | 2347 value = gaim_value_new(GAIM_TYPE_BOOLEAN); |
2364 setting->type = GAIM_BLIST_NODE_SETTING_BOOL; | 2348 gaim_value_set_boolean(value, data); |
2365 setting->value.boolean = value; | 2349 |
2366 | 2350 g_hash_table_replace(node->settings, g_strdup(key), value); |
2367 g_hash_table_replace(node->settings, g_strdup(key), setting); | |
2368 | 2351 |
2369 schedule_blist_save(); | 2352 schedule_blist_save(); |
2370 } | 2353 } |
2371 | 2354 |
2372 gboolean gaim_blist_node_get_bool(GaimBlistNode* node, const char *key) | 2355 gboolean |
2373 { | 2356 gaim_blist_node_get_bool(GaimBlistNode* node, const char *key) |
2374 struct gaim_blist_node_setting *setting; | 2357 { |
2358 GaimValue *value; | |
2375 | 2359 |
2376 g_return_val_if_fail(node != NULL, FALSE); | 2360 g_return_val_if_fail(node != NULL, FALSE); |
2377 g_return_val_if_fail(node->settings != NULL, FALSE); | 2361 g_return_val_if_fail(node->settings != NULL, FALSE); |
2378 g_return_val_if_fail(key != NULL, FALSE); | 2362 g_return_val_if_fail(key != NULL, FALSE); |
2379 | 2363 |
2380 setting = g_hash_table_lookup(node->settings, key); | 2364 value = g_hash_table_lookup(node->settings, key); |
2381 | 2365 |
2382 if (!setting) | 2366 if (value == NULL) |
2383 return FALSE; | 2367 return FALSE; |
2384 | 2368 |
2385 g_return_val_if_fail(setting->type == GAIM_BLIST_NODE_SETTING_BOOL, FALSE); | 2369 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_BOOLEAN, FALSE); |
2386 | 2370 |
2387 return setting->value.boolean; | 2371 return gaim_value_get_boolean(value); |
2388 } | 2372 } |
2389 | 2373 |
2390 void gaim_blist_node_set_int(GaimBlistNode* node, const char *key, int value) | 2374 void |
2391 { | 2375 gaim_blist_node_set_int(GaimBlistNode* node, const char *key, int data) |
2392 struct gaim_blist_node_setting *setting; | 2376 { |
2377 GaimValue *value; | |
2393 | 2378 |
2394 g_return_if_fail(node != NULL); | 2379 g_return_if_fail(node != NULL); |
2395 g_return_if_fail(node->settings != NULL); | 2380 g_return_if_fail(node->settings != NULL); |
2396 g_return_if_fail(key != NULL); | 2381 g_return_if_fail(key != NULL); |
2397 | 2382 |
2398 setting = g_new0(struct gaim_blist_node_setting, 1); | 2383 value = gaim_value_new(GAIM_TYPE_INT); |
2399 setting->type = GAIM_BLIST_NODE_SETTING_INT; | 2384 gaim_value_set_int(value, data); |
2400 setting->value.integer = value; | 2385 |
2401 | 2386 g_hash_table_replace(node->settings, g_strdup(key), value); |
2402 g_hash_table_replace(node->settings, g_strdup(key), setting); | |
2403 | 2387 |
2404 schedule_blist_save(); | 2388 schedule_blist_save(); |
2405 } | 2389 } |
2406 | 2390 |
2407 int gaim_blist_node_get_int(GaimBlistNode* node, const char *key) | 2391 int |
2408 { | 2392 gaim_blist_node_get_int(GaimBlistNode* node, const char *key) |
2409 struct gaim_blist_node_setting *setting; | 2393 { |
2394 GaimValue *value; | |
2410 | 2395 |
2411 g_return_val_if_fail(node != NULL, 0); | 2396 g_return_val_if_fail(node != NULL, 0); |
2412 g_return_val_if_fail(node->settings != NULL, 0); | 2397 g_return_val_if_fail(node->settings != NULL, 0); |
2413 g_return_val_if_fail(key != NULL, 0); | 2398 g_return_val_if_fail(key != NULL, 0); |
2414 | 2399 |
2415 setting = g_hash_table_lookup(node->settings, key); | 2400 value = g_hash_table_lookup(node->settings, key); |
2416 | 2401 |
2417 if (!setting) | 2402 if (value == NULL) |
2418 return 0; | 2403 return 0; |
2419 | 2404 |
2420 g_return_val_if_fail(setting->type == GAIM_BLIST_NODE_SETTING_INT, 0); | 2405 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_INT, 0); |
2421 | 2406 |
2422 return setting->value.integer; | 2407 return gaim_value_get_int(value); |
2423 } | 2408 } |
2424 | 2409 |
2425 void gaim_blist_node_set_string(GaimBlistNode* node, const char *key, | 2410 void |
2426 const char *value) | 2411 gaim_blist_node_set_string(GaimBlistNode* node, const char *key, const char *data) |
2427 { | 2412 { |
2428 struct gaim_blist_node_setting *setting; | 2413 GaimValue *value; |
2429 | 2414 |
2430 g_return_if_fail(node != NULL); | 2415 g_return_if_fail(node != NULL); |
2431 g_return_if_fail(node->settings != NULL); | 2416 g_return_if_fail(node->settings != NULL); |
2432 g_return_if_fail(key != NULL); | 2417 g_return_if_fail(key != NULL); |
2433 | 2418 |
2434 setting = g_new0(struct gaim_blist_node_setting, 1); | 2419 value = gaim_value_new(GAIM_TYPE_STRING); |
2435 setting->type = GAIM_BLIST_NODE_SETTING_STRING; | 2420 gaim_value_set_string(value, data); |
2436 setting->value.string = g_strdup(value); | 2421 |
2437 | 2422 g_hash_table_replace(node->settings, g_strdup(key), value); |
2438 g_hash_table_replace(node->settings, g_strdup(key), setting); | |
2439 | 2423 |
2440 schedule_blist_save(); | 2424 schedule_blist_save(); |
2441 } | 2425 } |
2442 | 2426 |
2443 const char *gaim_blist_node_get_string(GaimBlistNode* node, const char *key) | 2427 const char * |
2444 { | 2428 gaim_blist_node_get_string(GaimBlistNode* node, const char *key) |
2445 struct gaim_blist_node_setting *setting; | 2429 { |
2430 GaimValue *value; | |
2446 | 2431 |
2447 g_return_val_if_fail(node != NULL, NULL); | 2432 g_return_val_if_fail(node != NULL, NULL); |
2448 g_return_val_if_fail(node->settings != NULL, NULL); | 2433 g_return_val_if_fail(node->settings != NULL, NULL); |
2449 g_return_val_if_fail(key != NULL, NULL); | 2434 g_return_val_if_fail(key != NULL, NULL); |
2450 | 2435 |
2451 setting = g_hash_table_lookup(node->settings, key); | 2436 value = g_hash_table_lookup(node->settings, key); |
2452 | 2437 |
2453 if (!setting) | 2438 if (value == NULL) |
2454 return NULL; | 2439 return NULL; |
2455 | 2440 |
2456 g_return_val_if_fail(setting->type == GAIM_BLIST_NODE_SETTING_STRING, NULL); | 2441 g_return_val_if_fail(gaim_value_get_type(value) == GAIM_TYPE_STRING, NULL); |
2457 | 2442 |
2458 return setting->value.string; | 2443 return gaim_value_get_string(value); |
2459 } | 2444 } |
2460 | 2445 |
2461 GList *gaim_blist_node_get_extended_menu(GaimBlistNode *n) | 2446 GList *gaim_blist_node_get_extended_menu(GaimBlistNode *n) |
2462 { | 2447 { |
2463 GList *menu = NULL; | 2448 GList *menu = NULL; |