Open an explorer.exe window at the location of the file when clicking
[pidgin-git.git] / libpurple / prefs.c
bloba24b4c4b6261a87b7c97e3a7eb128b3f927f7e75
1 /*
2 * purple
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <glib.h>
34 #include "internal.h"
35 #include "prefs.h"
36 #include "debug.h"
37 #include "util.h"
39 #ifdef _WIN32
40 #include "win32dep.h"
41 #endif
43 struct pref_cb {
44 PurplePrefCallback func;
45 gpointer data;
46 guint id;
47 void *handle;
50 /* TODO: This should use PurpleValues? */
51 struct purple_pref {
52 PurplePrefType type;
53 char *name;
54 union {
55 gpointer generic;
56 gboolean boolean;
57 int integer;
58 char *string;
59 GList *stringlist;
60 } value;
61 GSList *callbacks;
62 struct purple_pref *parent;
63 struct purple_pref *sibling;
64 struct purple_pref *first_child;
68 static struct purple_pref prefs = {
69 PURPLE_PREF_NONE,
70 NULL,
71 { NULL },
72 NULL,
73 NULL,
74 NULL,
75 NULL
78 static GHashTable *prefs_hash = NULL;
79 static guint save_timer = 0;
80 static gboolean prefs_loaded = FALSE;
83 /*********************************************************************
84 * Private utility functions *
85 *********************************************************************/
87 static struct
88 purple_pref *find_pref(const char *name)
90 g_return_val_if_fail(name != NULL && name[0] == '/', NULL);
92 if (name[1] == '\0')
93 return &prefs;
94 else
96 /* When we're initializing, the debug system is
97 * initialized before the prefs system, but debug
98 * calls will end up calling prefs functions, so we
99 * need to deal cleanly here. */
100 if (prefs_hash)
101 return g_hash_table_lookup(prefs_hash, name);
102 else
103 return NULL;
108 /*********************************************************************
109 * Writing to disk *
110 *********************************************************************/
113 * This function recursively creates the xmlnode tree from the prefs
114 * tree structure. Yay recursion!
116 static void
117 pref_to_xmlnode(xmlnode *parent, struct purple_pref *pref)
119 xmlnode *node, *childnode;
120 struct purple_pref *child;
121 char buf[21];
122 GList *cur;
124 /* Create a new node */
125 node = xmlnode_new_child(parent, "pref");
126 xmlnode_set_attrib(node, "name", pref->name);
128 /* Set the type of this node (if type == PURPLE_PREF_NONE then do nothing) */
129 if (pref->type == PURPLE_PREF_INT) {
130 xmlnode_set_attrib(node, "type", "int");
131 g_snprintf(buf, sizeof(buf), "%d", pref->value.integer);
132 xmlnode_set_attrib(node, "value", buf);
134 else if (pref->type == PURPLE_PREF_STRING) {
135 xmlnode_set_attrib(node, "type", "string");
136 xmlnode_set_attrib(node, "value", pref->value.string ? pref->value.string : "");
138 else if (pref->type == PURPLE_PREF_STRING_LIST) {
139 xmlnode_set_attrib(node, "type", "stringlist");
140 for (cur = pref->value.stringlist; cur != NULL; cur = cur->next)
142 childnode = xmlnode_new_child(node, "item");
143 xmlnode_set_attrib(childnode, "value", cur->data ? cur->data : "");
146 else if (pref->type == PURPLE_PREF_PATH) {
147 char *encoded = g_filename_to_utf8(pref->value.string ? pref->value.string : "", -1, NULL, NULL, NULL);
148 xmlnode_set_attrib(node, "type", "path");
149 xmlnode_set_attrib(node, "value", encoded);
150 g_free(encoded);
152 else if (pref->type == PURPLE_PREF_PATH_LIST) {
153 xmlnode_set_attrib(node, "type", "pathlist");
154 for (cur = pref->value.stringlist; cur != NULL; cur = cur->next)
156 char *encoded = g_filename_to_utf8(cur->data ? cur->data : "", -1, NULL, NULL, NULL);
157 childnode = xmlnode_new_child(node, "item");
158 xmlnode_set_attrib(childnode, "value", encoded);
159 g_free(encoded);
162 else if (pref->type == PURPLE_PREF_BOOLEAN) {
163 xmlnode_set_attrib(node, "type", "bool");
164 g_snprintf(buf, sizeof(buf), "%d", pref->value.boolean);
165 xmlnode_set_attrib(node, "value", buf);
168 /* All My Children */
169 for (child = pref->first_child; child != NULL; child = child->sibling)
170 pref_to_xmlnode(node, child);
173 static xmlnode *
174 prefs_to_xmlnode(void)
176 xmlnode *node;
177 struct purple_pref *pref, *child;
179 pref = &prefs;
181 /* Create the root preference node */
182 node = xmlnode_new("pref");
183 xmlnode_set_attrib(node, "version", "1");
184 xmlnode_set_attrib(node, "name", "/");
186 /* All My Children */
187 for (child = pref->first_child; child != NULL; child = child->sibling)
188 pref_to_xmlnode(node, child);
190 return node;
193 static void
194 sync_prefs(void)
196 xmlnode *node;
197 char *data;
199 if (!prefs_loaded)
202 * TODO: Call schedule_prefs_save()? Ideally we wouldn't need to.
203 * (prefs.xml should be loaded when purple_prefs_init is called)
205 purple_debug_error("prefs", "Attempted to save prefs before "
206 "they were read!\n");
207 return;
210 node = prefs_to_xmlnode();
211 data = xmlnode_to_formatted_str(node, NULL);
212 purple_util_write_data_to_file("prefs.xml", data, -1);
213 g_free(data);
214 xmlnode_free(node);
217 static gboolean
218 save_cb(gpointer data)
220 sync_prefs();
221 save_timer = 0;
222 return FALSE;
225 static void
226 schedule_prefs_save(void)
228 if (save_timer == 0)
229 save_timer = purple_timeout_add_seconds(5, save_cb, NULL);
233 /*********************************************************************
234 * Reading from disk *
235 *********************************************************************/
237 static GList *prefs_stack = NULL;
239 static void
240 prefs_start_element_handler (GMarkupParseContext *context,
241 const gchar *element_name,
242 const gchar **attribute_names,
243 const gchar **attribute_values,
244 gpointer user_data,
245 GError **error)
247 PurplePrefType pref_type = PURPLE_PREF_NONE;
248 int i;
249 const char *pref_name = NULL, *pref_value = NULL;
250 GString *pref_name_full;
251 GList *tmp;
253 if(!purple_strequal(element_name, "pref") &&
254 !purple_strequal(element_name, "item"))
255 return;
257 for(i = 0; attribute_names[i]; i++) {
258 if(purple_strequal(attribute_names[i], "name")) {
259 pref_name = attribute_values[i];
260 } else if(purple_strequal(attribute_names[i], "type")) {
261 if(purple_strequal(attribute_values[i], "bool"))
262 pref_type = PURPLE_PREF_BOOLEAN;
263 else if(purple_strequal(attribute_values[i], "int"))
264 pref_type = PURPLE_PREF_INT;
265 else if(purple_strequal(attribute_values[i], "string"))
266 pref_type = PURPLE_PREF_STRING;
267 else if(purple_strequal(attribute_values[i], "stringlist"))
268 pref_type = PURPLE_PREF_STRING_LIST;
269 else if(purple_strequal(attribute_values[i], "path"))
270 pref_type = PURPLE_PREF_PATH;
271 else if(purple_strequal(attribute_values[i], "pathlist"))
272 pref_type = PURPLE_PREF_PATH_LIST;
273 else
274 return;
275 } else if(purple_strequal(attribute_names[i], "value")) {
276 pref_value = attribute_values[i];
280 if ((pref_type == PURPLE_PREF_BOOLEAN || pref_type == PURPLE_PREF_INT) &&
281 pref_value == NULL) {
282 /* Missing a value attribute */
283 return;
286 if(purple_strequal(element_name, "item")) {
287 struct purple_pref *pref;
289 pref_name_full = g_string_new("");
291 for(tmp = prefs_stack; tmp; tmp = tmp->next) {
292 pref_name_full = g_string_prepend(pref_name_full, tmp->data);
293 pref_name_full = g_string_prepend_c(pref_name_full, '/');
296 pref = find_pref(pref_name_full->str);
298 if(pref) {
299 if(pref->type == PURPLE_PREF_STRING_LIST) {
300 pref->value.stringlist = g_list_append(pref->value.stringlist,
301 g_strdup(pref_value));
302 } else if(pref->type == PURPLE_PREF_PATH_LIST) {
303 pref->value.stringlist = g_list_append(pref->value.stringlist,
304 g_filename_from_utf8(pref_value, -1, NULL, NULL, NULL));
307 g_string_free(pref_name_full, TRUE);
308 } else {
309 char *decoded;
311 if(!pref_name || purple_strequal(pref_name, "/"))
312 return;
314 pref_name_full = g_string_new(pref_name);
316 for(tmp = prefs_stack; tmp; tmp = tmp->next) {
317 pref_name_full = g_string_prepend_c(pref_name_full, '/');
318 pref_name_full = g_string_prepend(pref_name_full, tmp->data);
321 pref_name_full = g_string_prepend_c(pref_name_full, '/');
323 switch(pref_type) {
324 case PURPLE_PREF_NONE:
325 purple_prefs_add_none(pref_name_full->str);
326 break;
327 case PURPLE_PREF_BOOLEAN:
328 purple_prefs_set_bool(pref_name_full->str, atoi(pref_value));
329 break;
330 case PURPLE_PREF_INT:
331 purple_prefs_set_int(pref_name_full->str, atoi(pref_value));
332 break;
333 case PURPLE_PREF_STRING:
334 purple_prefs_set_string(pref_name_full->str, pref_value);
335 break;
336 case PURPLE_PREF_STRING_LIST:
337 purple_prefs_set_string_list(pref_name_full->str, NULL);
338 break;
339 case PURPLE_PREF_PATH:
340 if (pref_value) {
341 decoded = g_filename_from_utf8(pref_value, -1, NULL, NULL, NULL);
342 purple_prefs_set_path(pref_name_full->str, decoded);
343 g_free(decoded);
344 } else {
345 purple_prefs_set_path(pref_name_full->str, NULL);
347 break;
348 case PURPLE_PREF_PATH_LIST:
349 purple_prefs_set_path_list(pref_name_full->str, NULL);
350 break;
352 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name));
353 g_string_free(pref_name_full, TRUE);
357 static void
358 prefs_end_element_handler(GMarkupParseContext *context,
359 const gchar *element_name,
360 gpointer user_data, GError **error)
362 if(prefs_stack && purple_strequal(element_name, "pref")) {
363 g_free(prefs_stack->data);
364 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack);
368 static GMarkupParser prefs_parser = {
369 prefs_start_element_handler,
370 prefs_end_element_handler,
371 NULL,
372 NULL,
373 NULL
376 gboolean
377 purple_prefs_load()
379 gchar *filename = g_build_filename(purple_user_dir(), "prefs.xml", NULL);
380 gchar *contents = NULL;
381 gsize length;
382 GMarkupParseContext *context;
383 GError *error = NULL;
385 if (!filename) {
386 prefs_loaded = TRUE;
387 return FALSE;
390 purple_debug_info("prefs", "Reading %s\n", filename);
392 if(!g_file_get_contents(filename, &contents, &length, &error)) {
393 #ifdef _WIN32
394 gchar *common_appdata = wpurple_get_special_folder(CSIDL_COMMON_APPDATA);
395 #endif
396 g_free(filename);
397 g_error_free(error);
399 error = NULL;
401 #ifdef _WIN32
402 filename = g_build_filename(common_appdata ? common_appdata : "", "purple", "prefs.xml", NULL);
403 g_free(common_appdata);
404 #else
405 filename = g_build_filename(SYSCONFDIR, "purple", "prefs.xml", NULL);
406 #endif
408 purple_debug_info("prefs", "Reading %s\n", filename);
410 if (!g_file_get_contents(filename, &contents, &length, &error)) {
411 purple_debug_error("prefs", "Error reading prefs: %s\n",
412 error->message);
413 g_error_free(error);
414 g_free(filename);
415 prefs_loaded = TRUE;
417 return FALSE;
421 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL);
423 if(!g_markup_parse_context_parse(context, contents, length, NULL)) {
424 g_markup_parse_context_free(context);
425 g_free(contents);
426 g_free(filename);
427 prefs_loaded = TRUE;
429 return FALSE;
432 if(!g_markup_parse_context_end_parse(context, NULL)) {
433 purple_debug_error("prefs", "Error parsing %s\n", filename);
434 g_markup_parse_context_free(context);
435 g_free(contents);
436 g_free(filename);
437 prefs_loaded = TRUE;
439 return FALSE;
442 purple_debug_info("prefs", "Finished reading %s\n", filename);
443 g_markup_parse_context_free(context);
444 g_free(contents);
445 g_free(filename);
446 prefs_loaded = TRUE;
448 return TRUE;
453 static void
454 prefs_save_cb(const char *name, PurplePrefType type, gconstpointer val,
455 gpointer user_data)
458 if(!prefs_loaded)
459 return;
461 purple_debug_misc("prefs", "%s changed, scheduling save.\n", name);
463 schedule_prefs_save();
466 static char *
467 get_path_dirname(const char *name)
469 char *c, *str;
471 str = g_strdup(name);
473 if ((c = strrchr(str, '/')) != NULL) {
474 *c = '\0';
476 if (*str == '\0') {
477 g_free(str);
479 str = g_strdup("/");
482 else {
483 g_free(str);
485 str = g_strdup(".");
488 return str;
491 static char *
492 get_path_basename(const char *name)
494 const char *c;
496 if ((c = strrchr(name, '/')) != NULL)
497 return g_strdup(c + 1);
499 return g_strdup(name);
502 static char *
503 pref_full_name(struct purple_pref *pref)
505 GString *name;
506 struct purple_pref *parent;
508 if(!pref)
509 return NULL;
511 if(pref == &prefs)
512 return g_strdup("/");
514 name = g_string_new(pref->name);
516 for(parent = pref->parent; parent && parent->name; parent = parent->parent) {
517 name = g_string_prepend_c(name, '/');
518 name = g_string_prepend(name, parent->name);
520 name = g_string_prepend_c(name, '/');
521 return g_string_free(name, FALSE);
524 static struct purple_pref *
525 find_pref_parent(const char *name)
527 char *parent_name = get_path_dirname(name);
528 struct purple_pref *ret = &prefs;
530 if(!purple_strequal(parent_name, "/")) {
531 ret = find_pref(parent_name);
534 g_free(parent_name);
535 return ret;
538 static void
539 free_pref_value(struct purple_pref *pref)
541 switch(pref->type) {
542 case PURPLE_PREF_BOOLEAN:
543 pref->value.boolean = FALSE;
544 break;
545 case PURPLE_PREF_INT:
546 pref->value.integer = 0;
547 break;
548 case PURPLE_PREF_STRING:
549 case PURPLE_PREF_PATH:
550 g_free(pref->value.string);
551 pref->value.string = NULL;
552 break;
553 case PURPLE_PREF_STRING_LIST:
554 case PURPLE_PREF_PATH_LIST:
556 g_list_foreach(pref->value.stringlist, (GFunc)g_free, NULL);
557 g_list_free(pref->value.stringlist);
558 } break;
559 case PURPLE_PREF_NONE:
560 break;
564 static struct purple_pref *
565 add_pref(PurplePrefType type, const char *name)
567 struct purple_pref *parent;
568 struct purple_pref *me;
569 struct purple_pref *sibling;
570 char *my_name;
572 parent = find_pref_parent(name);
574 if(!parent)
575 return NULL;
577 my_name = get_path_basename(name);
579 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) {
580 if(purple_strequal(sibling->name, my_name)) {
581 g_free(my_name);
582 return NULL;
586 me = g_new0(struct purple_pref, 1);
587 me->type = type;
588 me->name = my_name;
590 me->parent = parent;
591 if(parent->first_child) {
592 /* blatant abuse of a for loop */
593 for(sibling = parent->first_child; sibling->sibling;
594 sibling = sibling->sibling);
595 sibling->sibling = me;
596 } else {
597 parent->first_child = me;
600 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me);
602 return me;
605 void
606 purple_prefs_add_none(const char *name)
608 add_pref(PURPLE_PREF_NONE, name);
611 void
612 purple_prefs_add_bool(const char *name, gboolean value)
614 struct purple_pref *pref = add_pref(PURPLE_PREF_BOOLEAN, name);
616 if(!pref)
617 return;
619 pref->value.boolean = value;
622 void
623 purple_prefs_add_int(const char *name, int value)
625 struct purple_pref *pref = add_pref(PURPLE_PREF_INT, name);
627 if(!pref)
628 return;
630 pref->value.integer = value;
633 void
634 purple_prefs_add_string(const char *name, const char *value)
636 struct purple_pref *pref;
638 if(value != NULL && !g_utf8_validate(value, -1, NULL)) {
639 purple_debug_error("prefs", "purple_prefs_add_string: Cannot store invalid UTF8 for string pref %s\n", name);
640 return;
643 pref = add_pref(PURPLE_PREF_STRING, name);
645 if(!pref)
646 return;
648 pref->value.string = g_strdup(value);
651 void
652 purple_prefs_add_string_list(const char *name, GList *value)
654 struct purple_pref *pref = add_pref(PURPLE_PREF_STRING_LIST, name);
655 GList *tmp;
657 if(!pref)
658 return;
660 for(tmp = value; tmp; tmp = tmp->next) {
661 if(tmp->data != NULL && !g_utf8_validate(tmp->data, -1, NULL)) {
662 purple_debug_error("prefs", "purple_prefs_add_string_list: Skipping invalid UTF8 for string list pref %s\n", name);
663 continue;
665 pref->value.stringlist = g_list_append(pref->value.stringlist,
666 g_strdup(tmp->data));
670 void
671 purple_prefs_add_path(const char *name, const char *value)
673 struct purple_pref *pref = add_pref(PURPLE_PREF_PATH, name);
675 if(!pref)
676 return;
678 pref->value.string = g_strdup(value);
681 void
682 purple_prefs_add_path_list(const char *name, GList *value)
684 struct purple_pref *pref = add_pref(PURPLE_PREF_PATH_LIST, name);
685 GList *tmp;
687 if(!pref)
688 return;
690 for(tmp = value; tmp; tmp = tmp->next)
691 pref->value.stringlist = g_list_append(pref->value.stringlist,
692 g_strdup(tmp->data));
696 static void
697 remove_pref(struct purple_pref *pref)
699 char *name;
700 GSList *l;
702 if(!pref)
703 return;
705 while(pref->first_child)
706 remove_pref(pref->first_child);
708 if(pref == &prefs)
709 return;
711 if(pref->parent->first_child == pref) {
712 pref->parent->first_child = pref->sibling;
713 } else {
714 struct purple_pref *sib = pref->parent->first_child;
715 while(sib && sib->sibling != pref)
716 sib = sib->sibling;
717 if(sib)
718 sib->sibling = pref->sibling;
721 name = pref_full_name(pref);
723 if (prefs_loaded)
724 purple_debug_info("prefs", "removing pref %s\n", name);
726 g_hash_table_remove(prefs_hash, name);
727 g_free(name);
729 free_pref_value(pref);
731 while((l = pref->callbacks) != NULL) {
732 pref->callbacks = pref->callbacks->next;
733 g_free(l->data);
734 g_slist_free_1(l);
736 g_free(pref->name);
737 g_free(pref);
740 void
741 purple_prefs_remove(const char *name)
743 struct purple_pref *pref = find_pref(name);
745 if(!pref)
746 return;
748 remove_pref(pref);
751 void
752 purple_prefs_destroy()
754 purple_prefs_remove("/");
757 static void
758 do_callbacks(const char* name, struct purple_pref *pref)
760 GSList *cbs;
761 struct purple_pref *cb_pref;
762 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) {
763 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) {
764 struct pref_cb *cb = cbs->data;
765 cb->func(name, pref->type, pref->value.generic, cb->data);
770 void
771 purple_prefs_trigger_callback(const char *name)
773 struct purple_pref *pref = find_pref(name);
775 if(!pref) {
776 purple_debug_error("prefs",
777 "purple_prefs_trigger_callback: Unknown pref %s\n", name);
778 return;
781 do_callbacks(name, pref);
784 void
785 purple_prefs_set_generic(const char *name, gpointer value)
787 struct purple_pref *pref = find_pref(name);
789 if(!pref) {
790 purple_debug_error("prefs",
791 "purple_prefs_set_generic: Unknown pref %s\n", name);
792 return;
795 pref->value.generic = value;
796 do_callbacks(name, pref);
799 void
800 purple_prefs_set_bool(const char *name, gboolean value)
802 struct purple_pref *pref = find_pref(name);
804 if(pref) {
805 if(pref->type != PURPLE_PREF_BOOLEAN) {
806 purple_debug_error("prefs",
807 "purple_prefs_set_bool: %s not a boolean pref\n", name);
808 return;
811 if(pref->value.boolean != value) {
812 pref->value.boolean = value;
813 do_callbacks(name, pref);
815 } else {
816 purple_prefs_add_bool(name, value);
820 void
821 purple_prefs_set_int(const char *name, int value)
823 struct purple_pref *pref = find_pref(name);
825 if(pref) {
826 if(pref->type != PURPLE_PREF_INT) {
827 purple_debug_error("prefs",
828 "purple_prefs_set_int: %s not an integer pref\n", name);
829 return;
832 if(pref->value.integer != value) {
833 pref->value.integer = value;
834 do_callbacks(name, pref);
836 } else {
837 purple_prefs_add_int(name, value);
841 void
842 purple_prefs_set_string(const char *name, const char *value)
844 struct purple_pref *pref = find_pref(name);
846 if(value != NULL && !g_utf8_validate(value, -1, NULL)) {
847 purple_debug_error("prefs", "purple_prefs_set_string: Cannot store invalid UTF8 for string pref %s\n", name);
848 return;
851 if(pref) {
852 if(pref->type != PURPLE_PREF_STRING && pref->type != PURPLE_PREF_PATH) {
853 purple_debug_error("prefs",
854 "purple_prefs_set_string: %s not a string pref\n", name);
855 return;
858 if (!purple_strequal(pref->value.string, value)) {
859 g_free(pref->value.string);
860 pref->value.string = g_strdup(value);
861 do_callbacks(name, pref);
863 } else {
864 purple_prefs_add_string(name, value);
868 void
869 purple_prefs_set_string_list(const char *name, GList *value)
871 struct purple_pref *pref = find_pref(name);
872 if(pref) {
873 GList *tmp;
875 if(pref->type != PURPLE_PREF_STRING_LIST) {
876 purple_debug_error("prefs",
877 "purple_prefs_set_string_list: %s not a string list pref\n",
878 name);
879 return;
882 g_list_foreach(pref->value.stringlist, (GFunc)g_free, NULL);
883 g_list_free(pref->value.stringlist);
884 pref->value.stringlist = NULL;
886 for(tmp = value; tmp; tmp = tmp->next) {
887 if(tmp->data != NULL && !g_utf8_validate(tmp->data, -1, NULL)) {
888 purple_debug_error("prefs", "purple_prefs_set_string_list: Skipping invalid UTF8 for string list pref %s\n", name);
889 continue;
891 pref->value.stringlist = g_list_prepend(pref->value.stringlist,
892 g_strdup(tmp->data));
894 pref->value.stringlist = g_list_reverse(pref->value.stringlist);
896 do_callbacks(name, pref);
898 } else {
899 purple_prefs_add_string_list(name, value);
903 void
904 purple_prefs_set_path(const char *name, const char *value)
906 struct purple_pref *pref = find_pref(name);
908 if(pref) {
909 if(pref->type != PURPLE_PREF_PATH) {
910 purple_debug_error("prefs",
911 "purple_prefs_set_path: %s not a path pref\n", name);
912 return;
915 if (!purple_strequal(pref->value.string, value)) {
916 g_free(pref->value.string);
917 pref->value.string = g_strdup(value);
918 do_callbacks(name, pref);
920 } else {
921 purple_prefs_add_path(name, value);
925 void
926 purple_prefs_set_path_list(const char *name, GList *value)
928 struct purple_pref *pref = find_pref(name);
929 if(pref) {
930 GList *tmp;
932 if(pref->type != PURPLE_PREF_PATH_LIST) {
933 purple_debug_error("prefs",
934 "purple_prefs_set_path_list: %s not a path list pref\n",
935 name);
936 return;
939 g_list_foreach(pref->value.stringlist, (GFunc)g_free, NULL);
940 g_list_free(pref->value.stringlist);
941 pref->value.stringlist = NULL;
943 for(tmp = value; tmp; tmp = tmp->next)
944 pref->value.stringlist = g_list_prepend(pref->value.stringlist,
945 g_strdup(tmp->data));
946 pref->value.stringlist = g_list_reverse(pref->value.stringlist);
948 do_callbacks(name, pref);
950 } else {
951 purple_prefs_add_path_list(name, value);
956 gboolean
957 purple_prefs_exists(const char *name)
959 struct purple_pref *pref = find_pref(name);
961 if (pref != NULL)
962 return TRUE;
964 return FALSE;
967 PurplePrefType
968 purple_prefs_get_type(const char *name)
970 struct purple_pref *pref = find_pref(name);
972 if (pref == NULL)
973 return PURPLE_PREF_NONE;
975 return (pref->type);
978 gboolean
979 purple_prefs_get_bool(const char *name)
981 struct purple_pref *pref = find_pref(name);
983 if(!pref) {
984 purple_debug_error("prefs",
985 "purple_prefs_get_bool: Unknown pref %s\n", name);
986 return FALSE;
987 } else if(pref->type != PURPLE_PREF_BOOLEAN) {
988 purple_debug_error("prefs",
989 "purple_prefs_get_bool: %s not a boolean pref\n", name);
990 return FALSE;
993 return pref->value.boolean;
997 purple_prefs_get_int(const char *name)
999 struct purple_pref *pref = find_pref(name);
1001 if(!pref) {
1002 purple_debug_error("prefs",
1003 "purple_prefs_get_int: Unknown pref %s\n", name);
1004 return 0;
1005 } else if(pref->type != PURPLE_PREF_INT) {
1006 purple_debug_error("prefs",
1007 "purple_prefs_get_int: %s not an integer pref\n", name);
1008 return 0;
1011 return pref->value.integer;
1014 const char *
1015 purple_prefs_get_string(const char *name)
1017 struct purple_pref *pref = find_pref(name);
1019 if(!pref) {
1020 purple_debug_error("prefs",
1021 "purple_prefs_get_string: Unknown pref %s\n", name);
1022 return NULL;
1023 } else if(pref->type != PURPLE_PREF_STRING) {
1024 purple_debug_error("prefs",
1025 "purple_prefs_get_string: %s not a string pref\n", name);
1026 return NULL;
1029 return pref->value.string;
1032 GList *
1033 purple_prefs_get_string_list(const char *name)
1035 struct purple_pref *pref = find_pref(name);
1036 GList *ret = NULL, *tmp;
1038 if(!pref) {
1039 purple_debug_error("prefs",
1040 "purple_prefs_get_string_list: Unknown pref %s\n", name);
1041 return NULL;
1042 } else if(pref->type != PURPLE_PREF_STRING_LIST) {
1043 purple_debug_error("prefs",
1044 "purple_prefs_get_string_list: %s not a string list pref\n", name);
1045 return NULL;
1048 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next)
1049 ret = g_list_prepend(ret, g_strdup(tmp->data));
1050 ret = g_list_reverse(ret);
1052 return ret;
1055 const char *
1056 purple_prefs_get_path(const char *name)
1058 struct purple_pref *pref = find_pref(name);
1060 if(!pref) {
1061 purple_debug_error("prefs",
1062 "purple_prefs_get_path: Unknown pref %s\n", name);
1063 return NULL;
1064 } else if(pref->type != PURPLE_PREF_PATH) {
1065 purple_debug_error("prefs",
1066 "purple_prefs_get_path: %s not a path pref\n", name);
1067 return NULL;
1070 return pref->value.string;
1073 GList *
1074 purple_prefs_get_path_list(const char *name)
1076 struct purple_pref *pref = find_pref(name);
1077 GList *ret = NULL, *tmp;
1079 if(!pref) {
1080 purple_debug_error("prefs",
1081 "purple_prefs_get_path_list: Unknown pref %s\n", name);
1082 return NULL;
1083 } else if(pref->type != PURPLE_PREF_PATH_LIST) {
1084 purple_debug_error("prefs",
1085 "purple_prefs_get_path_list: %s not a path list pref\n", name);
1086 return NULL;
1089 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next)
1090 ret = g_list_prepend(ret, g_strdup(tmp->data));
1091 ret = g_list_reverse(ret);
1093 return ret;
1096 static void
1097 purple_prefs_rename_node(struct purple_pref *oldpref, struct purple_pref *newpref)
1099 struct purple_pref *child, *next;
1100 char *oldname, *newname;
1102 /* if we're a parent, rename the kids first */
1103 for(child = oldpref->first_child; child != NULL; child = next)
1105 struct purple_pref *newchild;
1106 next = child->sibling;
1107 for(newchild = newpref->first_child; newchild != NULL; newchild = newchild->sibling)
1109 if(purple_strequal(child->name, newchild->name))
1111 purple_prefs_rename_node(child, newchild);
1112 break;
1115 if(newchild == NULL) {
1116 /* no rename happened, we weren't able to find the new pref */
1117 char *tmpname = pref_full_name(child);
1118 purple_debug_error("prefs", "Unable to find rename pref for %s\n", tmpname);
1119 g_free(tmpname);
1123 oldname = pref_full_name(oldpref);
1124 newname = pref_full_name(newpref);
1126 if (oldpref->type != newpref->type)
1128 purple_debug_error("prefs", "Unable to rename %s to %s: differing types\n", oldname, newname);
1129 g_free(oldname);
1130 g_free(newname);
1131 return;
1134 purple_debug_info("prefs", "Renaming %s to %s\n", oldname, newname);
1135 g_free(oldname);
1137 switch(oldpref->type) {
1138 case PURPLE_PREF_NONE:
1139 break;
1140 case PURPLE_PREF_BOOLEAN:
1141 purple_prefs_set_bool(newname, oldpref->value.boolean);
1142 break;
1143 case PURPLE_PREF_INT:
1144 purple_prefs_set_int(newname, oldpref->value.integer);
1145 break;
1146 case PURPLE_PREF_STRING:
1147 purple_prefs_set_string(newname, oldpref->value.string);
1148 break;
1149 case PURPLE_PREF_STRING_LIST:
1150 purple_prefs_set_string_list(newname, oldpref->value.stringlist);
1151 break;
1152 case PURPLE_PREF_PATH:
1153 purple_prefs_set_path(newname, oldpref->value.string);
1154 break;
1155 case PURPLE_PREF_PATH_LIST:
1156 purple_prefs_set_path_list(newname, oldpref->value.stringlist);
1157 break;
1159 g_free(newname);
1161 remove_pref(oldpref);
1164 void
1165 purple_prefs_rename(const char *oldname, const char *newname)
1167 struct purple_pref *oldpref, *newpref;
1169 oldpref = find_pref(oldname);
1171 /* it's already been renamed, call off the dogs */
1172 if(!oldpref)
1173 return;
1175 newpref = find_pref(newname);
1177 if (newpref == NULL)
1179 purple_debug_error("prefs", "Unable to rename %s to %s: new pref not created\n", oldname, newname);
1180 return;
1183 purple_prefs_rename_node(oldpref, newpref);
1186 void
1187 purple_prefs_rename_boolean_toggle(const char *oldname, const char *newname)
1189 struct purple_pref *oldpref, *newpref;
1191 oldpref = find_pref(oldname);
1193 /* it's already been renamed, call off the cats */
1194 if(!oldpref)
1195 return;
1197 if (oldpref->type != PURPLE_PREF_BOOLEAN)
1199 purple_debug_error("prefs", "Unable to rename %s to %s: old pref not a boolean\n", oldname, newname);
1200 return;
1203 if (oldpref->first_child != NULL) /* can't rename parents */
1205 purple_debug_error("prefs", "Unable to rename %s to %s: can't rename parents\n", oldname, newname);
1206 return;
1210 newpref = find_pref(newname);
1212 if (newpref == NULL)
1214 purple_debug_error("prefs", "Unable to rename %s to %s: new pref not created\n", oldname, newname);
1215 return;
1218 if (oldpref->type != newpref->type)
1220 purple_debug_error("prefs", "Unable to rename %s to %s: differing types\n", oldname, newname);
1221 return;
1224 purple_debug_info("prefs", "Renaming and toggling %s to %s\n", oldname, newname);
1225 purple_prefs_set_bool(newname, !(oldpref->value.boolean));
1227 remove_pref(oldpref);
1230 guint
1231 purple_prefs_connect_callback(void *handle, const char *name, PurplePrefCallback func, gpointer data)
1233 struct purple_pref *pref;
1234 struct pref_cb *cb;
1235 static guint cb_id = 0;
1237 g_return_val_if_fail(name != NULL, 0);
1238 g_return_val_if_fail(func != NULL, 0);
1240 pref = find_pref(name);
1241 if (pref == NULL) {
1242 purple_debug_error("prefs", "purple_prefs_connect_callback: Unknown pref %s\n", name);
1243 return 0;
1246 cb = g_new0(struct pref_cb, 1);
1248 cb->func = func;
1249 cb->data = data;
1250 cb->id = ++cb_id;
1251 cb->handle = handle;
1253 pref->callbacks = g_slist_append(pref->callbacks, cb);
1255 return cb->id;
1258 static gboolean
1259 disco_callback_helper(struct purple_pref *pref, guint callback_id)
1261 GSList *cbs;
1262 struct purple_pref *child;
1264 if(!pref)
1265 return FALSE;
1267 for(cbs = pref->callbacks; cbs; cbs = cbs->next) {
1268 struct pref_cb *cb = cbs->data;
1269 if(cb->id == callback_id) {
1270 pref->callbacks = g_slist_delete_link(pref->callbacks, cbs);
1271 g_free(cb);
1272 return TRUE;
1276 for(child = pref->first_child; child; child = child->sibling) {
1277 if(disco_callback_helper(child, callback_id))
1278 return TRUE;
1281 return FALSE;
1284 void
1285 purple_prefs_disconnect_callback(guint callback_id)
1287 disco_callback_helper(&prefs, callback_id);
1290 static void
1291 disco_callback_helper_handle(struct purple_pref *pref, void *handle)
1293 GSList *cbs;
1294 struct purple_pref *child;
1296 if(!pref)
1297 return;
1299 cbs = pref->callbacks;
1300 while (cbs != NULL) {
1301 struct pref_cb *cb = cbs->data;
1302 if(cb->handle == handle) {
1303 pref->callbacks = g_slist_delete_link(pref->callbacks, cbs);
1304 g_free(cb);
1305 cbs = pref->callbacks;
1306 } else
1307 cbs = cbs->next;
1310 for(child = pref->first_child; child; child = child->sibling)
1311 disco_callback_helper_handle(child, handle);
1314 void
1315 purple_prefs_disconnect_by_handle(void *handle)
1317 g_return_if_fail(handle != NULL);
1319 disco_callback_helper_handle(&prefs, handle);
1322 GList *
1323 purple_prefs_get_children_names(const char *name)
1325 GList * list = NULL;
1326 struct purple_pref *pref = find_pref(name), *child;
1327 char sep[2] = "\0\0";;
1329 if (pref == NULL)
1330 return NULL;
1332 if (name[strlen(name) - 1] != '/')
1333 sep[0] = '/';
1334 for (child = pref->first_child; child; child = child->sibling) {
1335 list = g_list_append(list, g_strdup_printf("%s%s%s", name, sep, child->name));
1337 return list;
1340 void
1341 purple_prefs_update_old()
1343 purple_prefs_rename("/core", "/purple");
1345 /* Remove some no-longer-used prefs */
1346 purple_prefs_remove("/purple/away/auto_response/enabled");
1347 purple_prefs_remove("/purple/away/auto_response/idle_only");
1348 purple_prefs_remove("/purple/away/auto_response/in_active_conv");
1349 purple_prefs_remove("/purple/away/auto_response/sec_before_resend");
1350 purple_prefs_remove("/purple/away/auto_response");
1351 purple_prefs_remove("/purple/away/default_message");
1352 purple_prefs_remove("/purple/buddies/use_server_alias");
1353 purple_prefs_remove("/purple/conversations/away_back_on_send");
1354 purple_prefs_remove("/purple/conversations/send_urls_as_links");
1355 purple_prefs_remove("/purple/conversations/im/show_login");
1356 purple_prefs_remove("/purple/conversations/chat/show_join");
1357 purple_prefs_remove("/purple/conversations/chat/show_leave");
1358 purple_prefs_remove("/purple/conversations/combine_chat_im");
1359 purple_prefs_remove("/purple/conversations/use_alias_for_title");
1360 purple_prefs_remove("/purple/logging/log_signon_signoff");
1361 purple_prefs_remove("/purple/logging/log_idle_state");
1362 purple_prefs_remove("/purple/logging/log_away_state");
1363 purple_prefs_remove("/purple/logging/log_own_states");
1364 purple_prefs_remove("/purple/status/scores/hidden");
1365 purple_prefs_remove("/plugins/core/autorecon/hide_connected_error");
1366 purple_prefs_remove("/plugins/core/autorecon/hide_connecting_error");
1367 purple_prefs_remove("/plugins/core/autorecon/hide_reconnecting_dialog");
1368 purple_prefs_remove("/plugins/core/autorecon/restore_state");
1369 purple_prefs_remove("/plugins/core/autorecon");
1371 /* Convert old sounds while_away pref to new 3-way pref. */
1372 if (purple_prefs_exists("/purple/sound/while_away") &&
1373 purple_prefs_get_bool("/purple/sound/while_away"))
1375 purple_prefs_set_int("/purple/sound/while_status", 3);
1377 purple_prefs_remove("/purple/sound/while_away");
1380 void *
1381 purple_prefs_get_handle(void)
1383 static int handle;
1385 return &handle;
1388 void
1389 purple_prefs_init(void)
1391 void *handle = purple_prefs_get_handle();
1393 prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1395 purple_prefs_connect_callback(handle, "/", prefs_save_cb, NULL);
1397 purple_prefs_add_none("/purple");
1398 purple_prefs_add_none("/plugins");
1399 purple_prefs_add_none("/plugins/core");
1400 purple_prefs_add_none("/plugins/lopl");
1401 purple_prefs_add_none("/plugins/prpl");
1403 /* Away */
1404 purple_prefs_add_none("/purple/away");
1405 purple_prefs_add_string("/purple/away/idle_reporting", "system");
1406 purple_prefs_add_bool("/purple/away/away_when_idle", TRUE);
1407 purple_prefs_add_int("/purple/away/mins_before_away", 5);
1409 /* Away -> Auto-Reply */
1410 if (!purple_prefs_exists("/purple/away/auto_response/enabled") ||
1411 !purple_prefs_exists("/purple/away/auto_response/idle_only"))
1413 purple_prefs_add_string("/purple/away/auto_reply", "awayidle");
1415 else
1417 if (!purple_prefs_get_bool("/purple/away/auto_response/enabled"))
1419 purple_prefs_add_string("/purple/away/auto_reply", "never");
1421 else
1423 if (purple_prefs_get_bool("/purple/away/auto_response/idle_only"))
1425 purple_prefs_add_string("/purple/away/auto_reply", "awayidle");
1427 else
1429 purple_prefs_add_string("/purple/away/auto_reply", "away");
1434 /* Buddies */
1435 purple_prefs_add_none("/purple/buddies");
1437 /* Contact Priority Settings */
1438 purple_prefs_add_none("/purple/contact");
1439 purple_prefs_add_bool("/purple/contact/last_match", FALSE);
1440 purple_prefs_remove("/purple/contact/offline_score");
1441 purple_prefs_remove("/purple/contact/away_score");
1442 purple_prefs_remove("/purple/contact/idle_score");
1444 purple_prefs_load();
1445 purple_prefs_update_old();
1448 void
1449 purple_prefs_uninit()
1451 if (save_timer != 0)
1453 purple_timeout_remove(save_timer);
1454 save_timer = 0;
1455 sync_prefs();
1458 purple_prefs_disconnect_by_handle(purple_prefs_get_handle());
1460 prefs_loaded = FALSE;
1461 purple_prefs_destroy();
1462 g_hash_table_destroy(prefs_hash);
1463 prefs_hash = NULL;