* Gaim is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /* TODO: This should use GaimValues? */ struct gaim_pref *parent; struct gaim_pref *sibling; struct gaim_pref *first_child; static struct gaim_pref prefs = { static GHashTable *prefs_hash = NULL; static guint save_timer = 0; static gboolean prefs_loaded = FALSE; /********************************************************************* * Private utility functions * *********************************************************************/ gaim_pref *find_pref(const char *name) if (!name || name[0] != '/') else if (name[1] == '\0') return g_hash_table_lookup(prefs_hash, name); /********************************************************************* *********************************************************************/ * This function recursively creates the xmlnode tree from the prefs * tree structure. Yay recursion! pref_to_xmlnode(xmlnode *parent, struct gaim_pref *pref) xmlnode *node, *childnode; node = xmlnode_new_child(parent, "pref"); xmlnode_set_attrib(node, "name", pref->name); /* Set the type of this node (if type == GAIM_PREF_NONE then do nothing) */ if (pref->type == GAIM_PREF_INT) { xmlnode_set_attrib(node, "type", "int"); snprintf(buf, sizeof(buf), "%d", pref->value.integer); xmlnode_set_attrib(node, "value", buf); else if (pref->type == GAIM_PREF_STRING) { xmlnode_set_attrib(node, "type", "string"); xmlnode_set_attrib(node, "value", pref->value.string); else if (pref->type == GAIM_PREF_STRING_LIST) { xmlnode_set_attrib(node, "type", "stringlist"); for (cur = pref->value.stringlist; cur != NULL; cur = cur->next) childnode = xmlnode_new_child(node, "item"); xmlnode_set_attrib(childnode, "value", cur->data); else if (pref->type == GAIM_PREF_BOOLEAN) { xmlnode_set_attrib(node, "type", "bool"); snprintf(buf, sizeof(buf), "%d", pref->value.boolean); xmlnode_set_attrib(node, "value", buf); for (child = pref->first_child; child != NULL; child = child->sibling) pref_to_xmlnode(node, child); struct gaim_pref *pref, *child; /* Create the root preference node */ node = xmlnode_new("pref"); xmlnode_set_attrib(node, "version", "1"); xmlnode_set_attrib(node, "name", "/"); for (child = pref->first_child; child != NULL; child = child->sibling) pref_to_xmlnode(node, child); * TODO: Call schedule_prefs_save()? Ideally we wouldn't need to. * (prefs.xml should be loaded when gaim_prefs_init is called) gaim_debug_error("prefs", "Attempted to save prefs before " node = prefs_to_xmlnode(); data = xmlnode_to_formatted_str(node, NULL); gaim_util_write_data_to_file("prefs.xml", data, -1); schedule_prefs_save(void) save_timer = gaim_timeout_add(5000, save_cb, NULL); /********************************************************************* *********************************************************************/ static GList *prefs_stack = NULL; prefs_start_element_handler (GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, GaimPrefType pref_type = GAIM_PREF_NONE; const char *pref_name = NULL, *pref_value = NULL; if(strcmp(element_name, "pref") && strcmp(element_name, "item")) for(i = 0; attribute_names[i]; i++) { if(!strcmp(attribute_names[i], "name")) { pref_name = attribute_values[i]; } else if(!strcmp(attribute_names[i], "type")) { if(!strcmp(attribute_values[i], "bool")) pref_type = GAIM_PREF_BOOLEAN; else if(!strcmp(attribute_values[i], "int")) pref_type = GAIM_PREF_INT; else if(!strcmp(attribute_values[i], "string")) pref_type = GAIM_PREF_STRING; else if(!strcmp(attribute_values[i], "stringlist")) pref_type = GAIM_PREF_STRING_LIST; } else if(!strcmp(attribute_names[i], "value")) { pref_value = attribute_values[i]; if(!strcmp(element_name, "item")) { pref_name_full = g_string_new(""); for(tmp = prefs_stack; tmp; tmp = tmp->next) { pref_name_full = g_string_prepend(pref_name_full, tmp->data); pref_name_full = g_string_prepend_c(pref_name_full, '/'); pref = find_pref(pref_name_full->str); pref->value.stringlist = g_list_append(pref->value.stringlist, if(!pref_name || !strcmp(pref_name, "/")) pref_name_full = g_string_new(pref_name); for(tmp = prefs_stack; tmp; tmp = tmp->next) { pref_name_full = g_string_prepend_c(pref_name_full, '/'); pref_name_full = g_string_prepend(pref_name_full, tmp->data); pref_name_full = g_string_prepend_c(pref_name_full, '/'); gaim_prefs_add_none(pref_name_full->str); gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); gaim_prefs_set_string(pref_name_full->str, pref_value); case GAIM_PREF_STRING_LIST: gaim_prefs_set_string_list(pref_name_full->str, NULL); prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); g_string_free(pref_name_full, TRUE); prefs_end_element_handler(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) if(prefs_stack && !strcmp(element_name, "pref")) { g_free(prefs_stack->data); prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); static GMarkupParser prefs_parser = { prefs_start_element_handler, prefs_end_element_handler, gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); GMarkupParseContext *context; gaim_debug_info("prefs", "Reading %s\n", filename); if(!g_file_get_contents(filename, &contents, &length, &error)) { filename = g_build_filename(SYSCONFDIR, "gaim", "prefs.xml", NULL); gaim_debug_info("prefs", "Reading %s\n", filename); if (!g_file_get_contents(filename, &contents, &length, &error)) { gaim_debug_error("prefs", "Error reading prefs: %s\n", gaim_debug_error("prefs", "Error reading prefs: %s\n", context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); if(!g_markup_parse_context_parse(context, contents, length, NULL)) { g_markup_parse_context_free(context); if(!g_markup_parse_context_end_parse(context, NULL)) { gaim_debug_error("prefs", "Error parsing %s\n", filename); g_markup_parse_context_free(context); gaim_debug_info("prefs", "Finished reading %s\n", filename); g_markup_parse_context_free(context); /* I introduced a bug in 2.0.0beta2. This fixes the broken * scores on upgrade. This can be removed sometime shortly * after 2.0.0 final is released. -- rlaager */ if (gaim_prefs_get_int("/core/status/scores/offline") == -500 && gaim_prefs_get_int("/core/status/scores/available") == 100 && gaim_prefs_get_int("/core/status/scores/invisible") == -50 && gaim_prefs_get_int("/core/status/scores/away") == -100 && gaim_prefs_get_int("/core/status/scores/extended_away") == -200 && gaim_prefs_get_int("/core/status/scores/idle") == -400) gaim_prefs_set_int("/core/status/scores/idle", -10); prefs_save_cb(const char *name, GaimPrefType type, gconstpointer val, gaim_debug_misc("prefs", "%s changed, scheduling save.\n", name); get_path_dirname(const char *name) if ((c = strrchr(str, '/')) != NULL) { get_path_basename(const char *name) if ((c = strrchr(name, '/')) != NULL) pref_full_name(struct gaim_pref *pref) struct gaim_pref *parent; name = g_string_new(pref->name); for(parent = pref->parent; parent && parent->name; parent = parent->parent) { name = g_string_prepend_c(name, '/'); name = g_string_prepend(name, parent->name); name = g_string_prepend_c(name, '/'); g_string_free(name, FALSE); static struct gaim_pref * find_pref_parent(const char *name) char *parent_name = get_path_dirname(name); struct gaim_pref *ret = &prefs; if(strcmp(parent_name, "/")) { ret = find_pref(parent_name); free_pref_value(struct gaim_pref *pref) pref->value.boolean = FALSE; g_free(pref->value.string); pref->value.string = NULL; case GAIM_PREF_STRING_LIST: for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) g_list_free(pref->value.stringlist); static struct gaim_pref * add_pref(GaimPrefType type, const char *name) struct gaim_pref *parent; struct gaim_pref *sibling; parent = find_pref_parent(name); my_name = get_path_basename(name); for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { if(!strcmp(sibling->name, my_name)) { me = g_new0(struct gaim_pref, 1); if(parent->first_child) { /* blatant abuse of a for loop */ for(sibling = parent->first_child; sibling->sibling; sibling = sibling->sibling); parent->first_child = me; g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); gaim_prefs_add_none(const char *name) add_pref(GAIM_PREF_NONE, name); gaim_prefs_add_bool(const char *name, gboolean value) struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); pref->value.boolean = value; gaim_prefs_add_int(const char *name, int value) struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); pref->value.integer = value; gaim_prefs_add_string(const char *name, const char *value) struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); pref->value.string = g_strdup(value); gaim_prefs_add_string_list(const char *name, GList *value) struct gaim_pref *pref = add_pref(GAIM_PREF_STRING_LIST, name); for(tmp = value; tmp; tmp = tmp->next) pref->value.stringlist = g_list_append(pref->value.stringlist, remove_pref(struct gaim_pref *pref) if(!pref || pref == &prefs) remove_pref(pref->first_child); if(pref->parent->first_child == pref) { pref->parent->first_child = pref->sibling; struct gaim_pref *sib = pref->parent->first_child; while(sib && sib->sibling != pref) sib->sibling = pref->sibling; name = pref_full_name(pref); gaim_debug_info("prefs", "removing pref %s\n", name); g_hash_table_remove(prefs_hash, name); g_slist_free(pref->callbacks); gaim_prefs_remove(const char *name) struct gaim_pref *pref = find_pref(name); do_callbacks(const char* name, struct gaim_pref *pref) struct gaim_pref *cb_pref; for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { struct pref_cb *cb = cbs->data; cb->func(name, pref->type, pref->value.generic, cb->data); gaim_prefs_trigger_callback(const char *name) struct gaim_pref *pref = find_pref(name); gaim_debug_error("prefs", "gaim_prefs_trigger_callback: Unknown pref %s\n", name); do_callbacks(name, pref); gaim_prefs_set_generic(const char *name, gpointer value) struct gaim_pref *pref = find_pref(name); gaim_debug_error("prefs", "gaim_prefs_set_generic: Unknown pref %s\n", name); pref->value.generic = value; do_callbacks(name, pref); gaim_prefs_set_bool(const char *name, gboolean value) struct gaim_pref *pref = find_pref(name); if(pref->type != GAIM_PREF_BOOLEAN) { gaim_debug_error("prefs", "gaim_prefs_set_bool: %s not a boolean pref\n", name); if(pref->value.boolean != value) { pref->value.boolean = value; do_callbacks(name, pref); gaim_prefs_add_bool(name, value); gaim_prefs_set_int(const char *name, int value) struct gaim_pref *pref = find_pref(name); if(pref->type != GAIM_PREF_INT) { gaim_debug_error("prefs", "gaim_prefs_set_int: %s not an integer pref\n", name); if(pref->value.integer != value) { pref->value.integer = value; do_callbacks(name, pref); gaim_prefs_add_int(name, value); gaim_prefs_set_string(const char *name, const char *value) struct gaim_pref *pref = find_pref(name); if(pref->type != GAIM_PREF_STRING) { gaim_debug_error("prefs", "gaim_prefs_set_string: %s not a string pref\n", name); if((value && !pref->value.string) || (!value && pref->value.string) || strcmp(pref->value.string, value)) { g_free(pref->value.string); pref->value.string = g_strdup(value); do_callbacks(name, pref); gaim_prefs_add_string(name, value); gaim_prefs_set_string_list(const char *name, GList *value) struct gaim_pref *pref = find_pref(name); if(pref->type != GAIM_PREF_STRING_LIST) { gaim_debug_error("prefs", "gaim_prefs_set_string_list: %s not a string list pref\n", for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) g_list_free(pref->value.stringlist); pref->value.stringlist = NULL; for(tmp = value; tmp; tmp = tmp->next) pref->value.stringlist = g_list_append(pref->value.stringlist, do_callbacks(name, pref); gaim_prefs_add_string_list(name, value); gaim_prefs_exists(const char *name) struct gaim_pref *pref = find_pref(name); gaim_prefs_get_type(const char *name) struct gaim_pref *pref = find_pref(name); gaim_prefs_get_bool(const char *name) struct gaim_pref *pref = find_pref(name); gaim_debug_error("prefs", "gaim_prefs_get_bool: Unknown pref %s\n", name); } else if(pref->type != GAIM_PREF_BOOLEAN) { gaim_debug_error("prefs", "gaim_prefs_get_bool: %s not a boolean pref\n", name); return pref->value.boolean; gaim_prefs_get_int(const char *name) struct gaim_pref *pref = find_pref(name); gaim_debug_error("prefs", "gaim_prefs_get_int: Unknown pref %s\n", name); } else if(pref->type != GAIM_PREF_INT) { gaim_debug_error("prefs", "gaim_prefs_get_int: %s not an integer pref\n", name); return pref->value.integer; gaim_prefs_get_string(const char *name) struct gaim_pref *pref = find_pref(name); gaim_debug_error("prefs", "gaim_prefs_get_string: Unknown pref %s\n", name); } else if(pref->type != GAIM_PREF_STRING) { gaim_debug_error("prefs", "gaim_prefs_get_string: %s not a string pref\n", name); return pref->value.string; gaim_prefs_get_string_list(const char *name) struct gaim_pref *pref = find_pref(name); gaim_debug_error("prefs", "gaim_prefs_get_string_list: Unknown pref %s\n", name); } else if(pref->type != GAIM_PREF_STRING_LIST) { gaim_debug_error("prefs", "gaim_prefs_get_string_list: %s not a string list pref\n", name); for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) ret = g_list_append(ret, g_strdup(tmp->data)); gaim_prefs_rename(const char *oldname, const char *newname) struct gaim_pref *oldpref, *newpref; oldpref = find_pref(oldname); newpref = find_pref(newname); /* it's already been renamed, call off the dogs */ gaim_debug_info("prefs", "Renaming %s to %s\n", oldname, newname); g_return_if_fail(newpref != NULL); /* the new one needs to be created first */ g_return_if_fail(oldpref->type == newpref->type); g_return_if_fail(oldpref->first_child == NULL); /* can't rename parents */ gaim_prefs_set_bool(newname, oldpref->value.boolean); gaim_prefs_set_int(newname, oldpref->value.integer); gaim_prefs_set_string(newname, oldpref->value.string); case GAIM_PREF_STRING_LIST: gaim_prefs_set_string_list(newname, oldpref->value.stringlist); gaim_prefs_rename_boolean_toggle(const char *oldname, const char *newname) struct gaim_pref *oldpref, *newpref; oldpref = find_pref(oldname); newpref = find_pref(newname); /* it's already been renamed, call off the cats */ gaim_debug_info("prefs", "Renaming and toggling %s to %s\n", oldname, newname); g_return_if_fail(newpref != NULL); /* the new one needs to be created */ g_return_if_fail(oldpref->type == newpref->type); g_return_if_fail(oldpref->type == GAIM_PREF_BOOLEAN); g_return_if_fail(oldpref->first_child == NULL); /* can't rename parents */ gaim_prefs_set_bool(newname, !(oldpref->value.boolean)); gaim_prefs_connect_callback(void *handle, const char *name, GaimPrefCallback func, gpointer data) cb = g_new0(struct pref_cb, 1); pref->callbacks = g_slist_append(pref->callbacks, cb); disco_callback_helper(struct gaim_pref *pref, guint callback_id) for(cbs = pref->callbacks; cbs; cbs = cbs->next) { struct pref_cb *cb = cbs->data; if(cb->id == callback_id) { pref->callbacks = g_slist_delete_link(pref->callbacks, cbs); for(child = pref->first_child; child; child = child->sibling) { if(disco_callback_helper(child, callback_id)) gaim_prefs_disconnect_callback(guint callback_id) disco_callback_helper(&prefs, callback_id); disco_callback_helper_handle(struct gaim_pref *pref, void *handle) struct pref_cb *cb = cbs->data; if(cb->handle == handle) { pref->callbacks = g_slist_delete_link(pref->callbacks, cbs); for(child = pref->first_child; child; child = child->sibling) disco_callback_helper_handle(child, handle); gaim_prefs_disconnect_by_handle(void *handle) g_return_if_fail(handle != NULL); disco_callback_helper_handle(&prefs, handle); /* Remove some no-longer-used prefs */ gaim_prefs_remove("/core/away/auto_response/enabled"); gaim_prefs_remove("/core/away/auto_response/idle_only"); gaim_prefs_remove("/core/away/auto_response/in_active_conv"); gaim_prefs_remove("/core/away/auto_response/sec_before_resend"); gaim_prefs_remove("/core/away/auto_response"); gaim_prefs_remove("/core/away/default_message"); gaim_prefs_remove("/core/buddies/use_server_alias"); gaim_prefs_remove("/core/conversations/away_back_on_send"); gaim_prefs_remove("/core/conversations/send_urls_as_links"); gaim_prefs_remove("/core/conversations/im/show_login"); gaim_prefs_remove("/core/conversations/chat/show_join"); gaim_prefs_remove("/core/conversations/chat/show_leave"); gaim_prefs_remove("/core/conversations/combine_chat_im"); gaim_prefs_remove("/core/conversations/use_alias_for_title"); gaim_prefs_remove("/core/logging/log_signon_signoff"); gaim_prefs_remove("/core/logging/log_idle_state"); gaim_prefs_remove("/core/logging/log_away_state"); gaim_prefs_remove("/core/logging/log_own_states"); gaim_prefs_remove("/core/status/scores/hidden"); gaim_prefs_remove("/plugins/core/autorecon/hide_connected_error"); gaim_prefs_remove("/plugins/core/autorecon/hide_connecting_error"); gaim_prefs_remove("/plugins/core/autorecon/hide_reconnecting_dialog"); gaim_prefs_remove("/plugins/core/autorecon/restore_state"); gaim_prefs_remove("/plugins/core/autorecon"); gaim_prefs_get_handle(void) void *handle = gaim_prefs_get_handle(); prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); gaim_prefs_connect_callback(handle, "/", prefs_save_cb, NULL); gaim_prefs_add_none("/core"); gaim_prefs_add_none("/plugins"); gaim_prefs_add_none("/plugins/core"); gaim_prefs_add_none("/plugins/lopl"); gaim_prefs_add_none("/plugins/prpl"); gaim_prefs_add_none("/core/away"); gaim_prefs_add_string("/core/away/idle_reporting", "system"); gaim_prefs_add_bool("/core/away/away_when_idle", TRUE); gaim_prefs_add_int("/core/away/mins_before_away", 5); if (!gaim_prefs_exists("/core/away/auto_response/enabled") || !gaim_prefs_exists("/core/away/auto_response/idle_only")) gaim_prefs_add_string("/core/away/auto_reply", "awayidle"); if (!gaim_prefs_get_bool("/core/away/auto_response/enabled")) gaim_prefs_add_string("/core/away/auto_reply", "never"); if (gaim_prefs_get_bool("/core/away/auto_response/idle_only")) gaim_prefs_add_string("/core/away/auto_reply", "awayidle"); gaim_prefs_add_string("/core/away/auto_reply", "away"); gaim_prefs_add_none("/core/buddies"); /* Contact Priority Settings */ gaim_prefs_add_none("/core/contact"); gaim_prefs_add_bool("/core/contact/last_match", FALSE); gaim_prefs_remove("/core/contact/offline_score"); gaim_prefs_remove("/core/contact/away_score"); gaim_prefs_remove("/core/contact/idle_score"); gaim_timeout_remove(save_timer); gaim_prefs_disconnect_by_handle(gaim_prefs_get_handle());