Mercurial > pidgin
annotate src/prefs.c @ 5814:fb9209877f37
[gaim-migrate @ 6244]
more useful errors when we try to use an unknown pref, or use a pref as a
different type than it was defined
committer: Tailor Script <tailor@pidgin.im>
| author | Nathan Walp <nwalp@pidgin.im> |
|---|---|
| date | Sun, 08 Jun 2003 15:35:28 +0000 |
| parents | 2adc29c88a45 |
| children | 6aa7651c7c15 |
| rev | line source |
|---|---|
| 1 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 5440 | 4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> |
| 1 | 5 * |
| 6 * This program is free software; you can redistribute it and/or modify | |
| 7 * it under the terms of the GNU General Public License as published by | |
| 8 * the Free Software Foundation; either version 2 of the License, or | |
| 9 * (at your option) any later version. | |
| 10 * | |
| 11 * This program is distributed in the hope that it will be useful, | |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 * GNU General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU General Public License | |
| 17 * along with this program; if not, write to the Free Software | |
| 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 * | |
| 20 */ | |
| 21 | |
|
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
340
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
|
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2074
diff
changeset
|
23 #include <config.h> |
|
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
340
diff
changeset
|
24 #endif |
| 5440 | 25 |
| 1 | 26 #include <string.h> |
| 27 #include <stdio.h> | |
| 28 #include <stdlib.h> | |
| 5440 | 29 #include <sys/stat.h> |
| 30 #include <sys/types.h> | |
| 31 #include <glib.h> | |
| 32 #include "prefs.h" | |
| 33 #include "debug.h" | |
| 34 #include "util.h" | |
| 3366 | 35 |
|
4026
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
36 #ifdef _WIN32 |
|
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
37 #include "win32dep.h" |
|
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
38 #endif |
|
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
39 |
| 5440 | 40 struct pref_cb { |
| 41 GaimPrefCallback func; | |
| 42 gpointer data; | |
| 43 guint id; | |
| 44 }; | |
| 45 | |
| 46 struct gaim_pref { | |
| 47 GaimPrefType type; | |
| 48 char *name; | |
| 49 union { | |
| 50 gpointer generic; | |
| 51 gboolean boolean; | |
| 52 int integer; | |
| 53 char *string; | |
| 5561 | 54 GList *stringlist; |
| 5440 | 55 } value; |
| 56 GSList *callbacks; | |
| 57 struct gaim_pref *parent; | |
| 58 struct gaim_pref *sibling; | |
| 59 struct gaim_pref *first_child; | |
| 60 }; | |
| 3366 | 61 |
| 5440 | 62 static GHashTable *prefs_hash = NULL; |
| 63 | |
| 64 static struct gaim_pref prefs = { GAIM_PREF_NONE, NULL, {NULL}, NULL, | |
| 65 NULL, NULL, NULL }; | |
| 66 | |
| 5534 | 67 static guint prefs_save_timer = 0; |
| 68 static gboolean prefs_is_loaded = FALSE; | |
| 69 | |
| 70 | |
| 71 static gboolean prefs_save_callback(gpointer who_cares) { | |
| 72 gaim_prefs_sync(); | |
| 73 prefs_save_timer = 0; | |
| 74 return FALSE; | |
| 75 } | |
| 76 | |
| 77 static void schedule_prefs_save() { | |
| 78 if(!prefs_save_timer) | |
| 79 prefs_save_timer = g_timeout_add(5000, prefs_save_callback, NULL); | |
| 80 } | |
| 81 | |
| 82 static void prefs_save_cb(const char *name, GaimPrefType type, gpointer val, | |
| 83 gpointer user_data) { | |
| 84 | |
| 85 if(!prefs_is_loaded) | |
| 86 return; | |
| 87 | |
| 88 gaim_debug(GAIM_DEBUG_MISC, "prefs", "%s changed, scheduling save.\n", name); | |
| 89 | |
| 90 schedule_prefs_save(); | |
| 91 } | |
| 92 | |
| 5440 | 93 void gaim_prefs_init() { |
| 94 prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
| 95 | |
| 5534 | 96 gaim_prefs_connect_callback("/", prefs_save_cb, NULL); |
| 97 | |
|
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
98 gaim_prefs_add_none("/core"); |
|
5550
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
99 gaim_prefs_add_none("/plugins"); |
|
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
100 gaim_prefs_add_none("/plugins/core"); |
|
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
101 gaim_prefs_add_none("/plugins/lopl"); |
|
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
102 gaim_prefs_add_none("/plugins/prpl"); |
|
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
103 |
| 5440 | 104 /* XXX: this is where you would want to put prefs declarations */ |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
105 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
106 /* Away */ |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
107 gaim_prefs_add_none("/core/away"); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
108 gaim_prefs_add_bool("/core/away/away_when_idle", TRUE); |
|
5550
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
109 gaim_prefs_add_int("/core/away/mins_before_away", 1); |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
110 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
111 /* Away -> Auto Response */ |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
112 gaim_prefs_add_none("/core/away/auto_response"); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
113 gaim_prefs_add_bool("/core/away/auto_response/enabled", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
114 gaim_prefs_add_bool("/core/away/auto_response/in_active_conv", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
115 gaim_prefs_add_bool("/core/away/auto_response/idle_only", FALSE); |
|
5550
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
116 gaim_prefs_add_int("/core/away/auto_response/sec_before_resend", 60); |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
117 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
118 /* Buddies */ |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
119 gaim_prefs_add_none("/core/buddies"); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
120 gaim_prefs_add_bool("/core/buddies/use_server_alias", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
121 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
122 /* Conversations */ |
|
5539
de09863bd4b5
[gaim-migrate @ 5939]
Christian Hammond <chipx86@chipx86.com>
parents:
5534
diff
changeset
|
123 gaim_prefs_add_none("/core/conversations"); |
|
de09863bd4b5
[gaim-migrate @ 5939]
Christian Hammond <chipx86@chipx86.com>
parents:
5534
diff
changeset
|
124 gaim_prefs_add_bool("/core/conversations/send_urls_as_links", TRUE); |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
125 gaim_prefs_add_bool("/core/conversations/away_back_on_send", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
126 gaim_prefs_add_bool("/core/conversations/use_alias_for_title", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
127 gaim_prefs_add_bool("/core/conversations/combine_chat_im", FALSE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
128 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
129 /* Conversations -> Chat */ |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
130 gaim_prefs_add_none("/core/conversations/chat"); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
131 gaim_prefs_add_bool("/core/conversations/chat/show_join", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
132 gaim_prefs_add_bool("/core/conversations/chat/show_leave", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
133 gaim_prefs_add_bool("/core/conversations/chat/show_nick_change", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
134 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
135 /* Conversations -> IM */ |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
136 gaim_prefs_add_none("/core/conversations/im"); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
137 gaim_prefs_add_bool("/core/conversations/im/show_login", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
138 gaim_prefs_add_bool("/core/conversations/im/send_typing", TRUE); |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
139 |
| 5440 | 140 } |
| 3366 | 141 |
|
5787
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
142 static char * |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
143 get_path_dirname(const char *name) |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
144 { |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
145 char *c, *str; |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
146 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
147 str = g_strdup(name); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
148 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
149 if ((c = strrchr(str, '/')) != NULL) { |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
150 *c = '\0'; |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
151 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
152 if (*str == '\0') { |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
153 g_free(str); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
154 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
155 str = g_strdup("/"); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
156 } |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
157 } |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
158 else { |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
159 g_free(str); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
160 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
161 str = g_strdup("."); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
162 } |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
163 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
164 return str; |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
165 } |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
166 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
167 static char * |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
168 get_path_basename(const char *name) |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
169 { |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
170 const char *c; |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
171 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
172 if ((c = strrchr(name, '/')) != NULL) |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
173 return g_strdup(c + 1); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
174 |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
175 return g_strdup(name); |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
176 } |
|
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
177 |
| 5440 | 178 static char *pref_full_name(struct gaim_pref *pref) { |
| 179 GString *name; | |
| 180 struct gaim_pref *parent; | |
| 181 if(!pref) | |
| 182 return NULL; | |
| 183 | |
| 184 if(pref == &prefs) | |
| 185 return g_strdup("/"); | |
| 186 | |
| 187 name = g_string_new(pref->name); | |
| 188 parent = pref->parent; | |
| 3366 | 189 |
| 5440 | 190 for(parent = pref->parent; parent && parent->name; parent = parent->parent) { |
| 191 name = g_string_prepend_c(name, '/'); | |
| 192 name = g_string_prepend(name, parent->name); | |
| 193 } | |
| 194 g_string_free(name, FALSE); | |
| 195 return name->str; | |
| 196 } | |
| 197 | |
| 198 static struct gaim_pref *find_pref(const char *name) | |
| 199 { | |
| 200 if(!name || name[0] != '/') { | |
| 201 return NULL; | |
| 202 } else if(name[1] == '\0') { | |
| 203 return &prefs; | |
| 204 } else { | |
| 205 return g_hash_table_lookup(prefs_hash, name); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 static struct gaim_pref *find_pref_parent(const char *name) | |
| 210 { | |
|
5787
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
211 char *parent_name = get_path_dirname(name); |
| 5440 | 212 struct gaim_pref *ret = &prefs; |
| 213 | |
| 214 if(strcmp(parent_name, "/")) { | |
| 215 ret = find_pref(parent_name); | |
| 216 } | |
| 1026 | 217 |
| 5440 | 218 g_free(parent_name); |
| 219 return ret; | |
| 220 } | |
| 221 | |
| 222 static void free_pref_value(struct gaim_pref *pref) { | |
| 223 switch(pref->type) { | |
| 224 case GAIM_PREF_BOOLEAN: | |
| 225 pref->value.boolean = FALSE; | |
| 226 case GAIM_PREF_INT: | |
| 227 pref->value.integer = 0; | |
| 228 break; | |
| 229 case GAIM_PREF_STRING: | |
| 230 g_free(pref->value.string); | |
| 231 pref->value.string = NULL; | |
| 232 break; | |
| 5561 | 233 case GAIM_PREF_STRING_LIST: |
| 234 { | |
| 235 GList *tmp; | |
| 236 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
| 237 g_free(tmp->data); | |
| 238 | |
| 239 g_list_free(pref->value.stringlist); | |
| 240 } break; | |
| 5440 | 241 case GAIM_PREF_NONE: |
| 242 break; | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 static struct gaim_pref *add_pref(GaimPrefType type, const char *name) { | |
| 247 struct gaim_pref *parent; | |
| 248 struct gaim_pref *me; | |
| 249 struct gaim_pref *sibling; | |
|
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
250 char *my_name; |
| 5440 | 251 |
| 252 parent = find_pref_parent(name); | |
| 253 | |
| 254 if(!parent) | |
| 255 return NULL; | |
|
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
256 |
|
5787
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
257 my_name = get_path_basename(name); |
|
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
258 |
| 5440 | 259 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { |
| 260 if(!strcmp(sibling->name, my_name)) { | |
| 261 g_free(my_name); | |
| 262 return NULL; | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 me = g_new0(struct gaim_pref, 1); | |
| 267 me->type = type; | |
| 268 me->name = my_name; | |
| 269 | |
| 270 me->parent = parent; | |
| 271 if(parent->first_child) { | |
| 272 /* blatant abuse of a for loop */ | |
| 273 for(sibling = parent->first_child; sibling->sibling; | |
| 274 sibling = sibling->sibling); | |
| 275 sibling->sibling = me; | |
| 276 } else { | |
| 277 parent->first_child = me; | |
| 278 } | |
| 279 | |
| 280 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
| 281 | |
| 282 return me; | |
| 283 } | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
284 |
| 5440 | 285 void gaim_prefs_add_none(const char *name) { |
| 286 add_pref(GAIM_PREF_NONE, name); | |
| 287 } | |
| 288 | |
| 289 void gaim_prefs_add_bool(const char *name, gboolean value) { | |
| 290 struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
| 291 | |
| 292 if(!pref) | |
| 293 return; | |
| 294 | |
| 295 pref->value.boolean = value; | |
| 296 } | |
| 3630 | 297 |
| 5440 | 298 void gaim_prefs_add_int(const char *name, int value) { |
| 299 struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); | |
| 300 | |
| 301 if(!pref) | |
| 302 return; | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
303 |
| 5440 | 304 pref->value.integer = value; |
| 305 } | |
| 306 | |
| 307 void gaim_prefs_add_string(const char *name, const char *value) { | |
| 308 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
| 309 | |
| 310 if(!pref) | |
| 311 return; | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
312 |
| 5440 | 313 pref->value.string = g_strdup(value); |
| 314 } | |
| 315 | |
| 5561 | 316 void gaim_prefs_add_string_list(const char *name, GList *value) { |
| 317 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING_LIST, name); | |
| 318 GList *tmp; | |
| 319 | |
| 320 if(!pref) | |
| 321 return; | |
| 322 | |
| 323 for(tmp = value; tmp; tmp = tmp->next) | |
| 324 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
| 325 g_strdup(tmp->data)); | |
| 326 } | |
| 327 | |
| 5440 | 328 void remove_pref(struct gaim_pref *pref) { |
| 329 char *name; | |
| 330 | |
| 331 if(!pref || pref == &prefs) | |
| 332 return; | |
| 333 | |
| 334 if(pref->parent->first_child == pref) { | |
| 335 pref->parent->first_child = pref->sibling; | |
| 336 } else { | |
| 337 struct gaim_pref *sib = pref->parent->first_child; | |
| 338 while(sib->sibling != pref) | |
| 339 sib = sib->sibling; | |
| 340 sib->sibling = pref->sibling; | |
| 341 } | |
| 342 | |
| 343 name = pref_full_name(pref); | |
| 344 | |
| 345 g_hash_table_remove(prefs_hash, name); | |
| 346 g_free(name); | |
| 347 | |
| 348 free_pref_value(pref); | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
349 |
| 5440 | 350 g_slist_free(pref->callbacks); |
| 351 g_free(pref->name); | |
| 352 g_free(pref); | |
| 353 } | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
354 |
| 5440 | 355 void gaim_prefs_remove(const char *name) { |
| 356 struct gaim_pref *pref = find_pref(name); | |
| 357 struct gaim_pref *child, *child2; | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
358 |
| 5440 | 359 if(!pref) |
| 360 return; | |
| 361 child = pref->first_child; | |
| 362 while(child) { | |
| 363 child2 = child; | |
| 364 child = child->sibling; | |
| 365 remove_pref(child2); | |
| 366 } | |
| 367 | |
| 368 remove_pref(pref); | |
| 369 } | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
370 |
| 5440 | 371 void gaim_prefs_destroy() { |
| 372 gaim_prefs_remove("/"); | |
| 373 } | |
| 374 | |
| 375 static void do_callbacks(const char* name, struct gaim_pref *pref) { | |
| 376 GSList *cbs; | |
| 377 struct gaim_pref *cb_pref; | |
| 378 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
| 379 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
| 380 struct pref_cb *cb = cbs->data; | |
| 381 cb->func(name, pref->type, pref->value.generic, cb->data); | |
| 4215 | 382 } |
| 383 } | |
|
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
384 } |
|
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
385 |
| 5684 | 386 void gaim_prefs_trigger_callback(const char *name) { |
| 387 struct gaim_pref *pref = find_pref(name); | |
| 388 | |
| 5814 | 389 if(!pref) { |
| 390 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 391 "gaim_prefs_trigger_callback: Unknown pref %s\n", name); | |
| 392 return; | |
| 393 } | |
| 394 | |
| 5684 | 395 do_callbacks(name, pref); |
| 396 } | |
| 397 | |
| 5440 | 398 void gaim_prefs_set_generic(const char *name, gpointer value) { |
| 399 struct gaim_pref *pref = find_pref(name); | |
| 3366 | 400 |
| 5814 | 401 if(!pref) { |
| 402 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 403 "gaim_prefs_set_generic: Unknown pref %s\n", name); | |
| 404 return; | |
| 405 } | |
| 3500 | 406 |
| 5440 | 407 pref->value.generic = value; |
| 408 do_callbacks(name, pref); | |
| 3366 | 409 } |
| 410 | |
| 5440 | 411 void gaim_prefs_set_bool(const char *name, gboolean value) { |
| 412 struct gaim_pref *pref = find_pref(name); | |
| 4288 | 413 |
| 5533 | 414 if(pref) { |
| 5814 | 415 if(pref->type != GAIM_PREF_BOOLEAN) { |
| 416 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 417 "gaim_prefs_set_bool: %s not a boolean pref\n", name); | |
| 418 return; | |
| 419 } | |
| 4325 | 420 |
| 5533 | 421 if(pref->value.boolean != value) { |
| 422 pref->value.boolean = value; | |
| 423 do_callbacks(name, pref); | |
| 424 } | |
| 425 } else { | |
| 426 gaim_prefs_add_bool(name, value); | |
| 4288 | 427 } |
| 4324 | 428 } |
| 4325 | 429 |
| 5440 | 430 void gaim_prefs_set_int(const char *name, int value) { |
| 431 struct gaim_pref *pref = find_pref(name); | |
| 4325 | 432 |
| 5533 | 433 if(pref) { |
| 5814 | 434 if(pref->type != GAIM_PREF_INT) { |
| 435 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 436 "gaim_prefs_set_int: %s not an integer pref\n", name); | |
| 437 return; | |
| 438 } | |
| 4325 | 439 |
| 5533 | 440 if(pref->value.integer != value) { |
| 441 pref->value.integer = value; | |
| 442 do_callbacks(name, pref); | |
| 443 } | |
| 444 } else { | |
| 445 gaim_prefs_add_int(name, value); | |
| 5440 | 446 } |
| 4326 | 447 } |
| 448 | |
| 5451 | 449 void gaim_prefs_set_string(const char *name, const char *value) { |
| 5440 | 450 struct gaim_pref *pref = find_pref(name); |
| 4325 | 451 |
| 5533 | 452 if(pref) { |
| 5814 | 453 if(pref->type != GAIM_PREF_STRING) { |
| 454 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 455 "gaim_prefs_set_string: %s not a string pref\n", name); | |
| 456 return; | |
| 457 } | |
| 4325 | 458 |
| 5533 | 459 if(strcmp(pref->value.string, value)) { |
| 460 g_free(pref->value.string); | |
| 461 pref->value.string = g_strdup(value); | |
| 462 do_callbacks(name, pref); | |
| 463 } | |
| 464 } else { | |
| 465 gaim_prefs_add_string(name, value); | |
| 4325 | 466 } |
| 467 } | |
| 468 | |
| 5561 | 469 void gaim_prefs_set_string_list(const char *name, GList *value) { |
| 470 struct gaim_pref *pref = find_pref(name); | |
| 471 if(pref) { | |
| 472 GList *tmp; | |
| 5814 | 473 |
| 474 if(pref->type != GAIM_PREF_STRING_LIST) { | |
| 475 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 476 "gaim_prefs_set_string_list: %s not a string list pref\n", | |
| 477 name); | |
| 478 return; | |
| 479 } | |
| 480 | |
| 5561 | 481 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) |
| 482 g_free(tmp->data); | |
| 483 | |
| 484 g_list_free(pref->value.stringlist); | |
| 485 pref->value.stringlist = NULL; | |
| 486 | |
| 487 for(tmp = value; tmp; tmp = tmp->next) | |
| 488 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
| 489 g_strdup(tmp->data)); | |
| 490 | |
| 491 } else { | |
| 492 gaim_prefs_add_string_list(name, value); | |
| 493 } | |
| 494 } | |
| 495 | |
| 5440 | 496 gpointer gaim_prefs_get_generic(const char *name) { |
| 497 struct gaim_pref *pref = find_pref(name); | |
| 4325 | 498 |
| 5814 | 499 if(!pref) { |
| 500 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 501 "gaim_prefs_get_generic: Unknown pref %s\n", name); | |
| 502 return NULL; | |
| 503 } | |
| 4288 | 504 |
| 5440 | 505 return pref->value.generic; |
| 4288 | 506 } |
| 507 | |
| 5440 | 508 gboolean gaim_prefs_get_bool(const char *name) { |
| 509 struct gaim_pref *pref = find_pref(name); | |
| 3427 | 510 |
| 5814 | 511 if(!pref) { |
| 512 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 513 "gaim_prefs_get_bool: Unknown pref %s\n", name); | |
| 514 return FALSE; | |
| 515 } else if(pref->type != GAIM_PREF_BOOLEAN) { | |
| 516 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 517 "gaim_prefs_get_bool: %s not a boolean pref\n", name); | |
| 518 return FALSE; | |
| 519 } | |
| 3472 | 520 |
| 5440 | 521 return pref->value.boolean; |
| 3366 | 522 } |
| 523 | |
| 5440 | 524 int gaim_prefs_get_int(const char *name) { |
| 525 struct gaim_pref *pref = find_pref(name); | |
| 3500 | 526 |
| 5814 | 527 if(!pref) { |
| 528 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 529 "gaim_prefs_get_int: Unknown pref %s\n", name); | |
| 530 return 0; | |
| 531 } else if(pref->type != GAIM_PREF_INT) { | |
| 532 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 533 "gaim_prefs_get_int: %s not an integer pref\n", name); | |
| 534 return 0; | |
| 535 } | |
| 3366 | 536 |
| 5440 | 537 return pref->value.integer; |
| 3366 | 538 } |
| 539 | |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
540 const char *gaim_prefs_get_string(const char *name) { |
| 5440 | 541 struct gaim_pref *pref = find_pref(name); |
| 3366 | 542 |
| 5814 | 543 if(!pref) { |
| 544 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 545 "gaim_prefs_get_string: Unknown pref %s\n", name); | |
| 546 return NULL; | |
| 547 } else if(pref->type != GAIM_PREF_STRING) { | |
| 548 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 549 "gaim_prefs_get_string: %s not a string pref\n", name); | |
| 550 return NULL; | |
| 551 } | |
|
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
552 |
| 5440 | 553 return pref->value.string; |
|
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
554 } |
|
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
555 |
| 5561 | 556 GList *gaim_prefs_get_string_list(const char *name) { |
| 557 struct gaim_pref *pref = find_pref(name); | |
| 558 GList *ret = NULL, *tmp; | |
| 559 | |
| 5814 | 560 if(!pref) { |
| 561 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 562 "gaim_prefs_get_string_list: Unknown pref %s\n", name); | |
| 563 return NULL; | |
| 564 } else if(pref->type != GAIM_PREF_STRING_LIST) { | |
| 565 gaim_debug(GAIM_DEBUG_ERROR, "prefs", | |
| 566 "gaim_prefs_get_string_list: %s not a string list pref\n", name); | |
| 567 return NULL; | |
| 568 } | |
| 5561 | 569 |
| 570 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
| 571 ret = g_list_append(ret, g_strdup(tmp->data)); | |
| 572 | |
| 573 return ret; | |
| 574 } | |
| 575 | |
| 5440 | 576 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
| 577 { | |
| 578 struct gaim_pref *pref = find_pref(name); | |
| 579 struct pref_cb *cb; | |
| 580 static guint cb_id = 0; | |
| 3366 | 581 |
| 5440 | 582 if(!pref) |
| 583 return 0; | |
| 3366 | 584 |
| 5440 | 585 cb = g_new0(struct pref_cb, 1); |
|
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
586 |
| 5440 | 587 cb->func = func; |
| 588 cb->data = data; | |
| 589 cb->id = ++cb_id; | |
| 4991 | 590 |
| 5440 | 591 pref->callbacks = g_slist_append(pref->callbacks, cb); |
| 3366 | 592 |
| 5440 | 593 return cb->id; |
| 3366 | 594 } |
| 595 | |
| 5440 | 596 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
| 597 GSList *cbs; | |
| 598 struct gaim_pref *child; | |
| 2254 | 599 |
| 5440 | 600 if(!pref) |
| 601 return FALSE; | |
|
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
602 |
| 5440 | 603 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
| 604 struct pref_cb *cb = cbs->data; | |
| 605 if(cb->id == callback_id) { | |
| 606 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
| 607 g_free(cb); | |
| 608 return TRUE; | |
| 4428 | 609 } |
| 610 } | |
| 611 | |
| 5440 | 612 for(child = pref->first_child; child; child = child->sibling) { |
| 613 if(disco_callback_helper(child, callback_id)) | |
| 614 return TRUE; | |
| 4428 | 615 } |
|
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
616 |
| 5440 | 617 return FALSE; |
|
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
618 } |
|
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
619 |
| 5440 | 620 void gaim_prefs_disconnect_callback(guint callback_id) { |
| 621 disco_callback_helper(&prefs, callback_id); | |
|
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
622 } |
|
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
623 |
| 5440 | 624 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
| 625 struct gaim_pref *tmp; | |
| 626 char *esc; | |
| 627 int i; | |
| 3500 | 628 |
| 5440 | 629 if(!pref) { |
| 630 pref = &prefs; | |
| 3551 | 631 |
| 5440 | 632 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
| 633 fprintf(f, "<pref name='/'"); | |
| 634 } else { | |
| 635 for(i=0; i<depth; i++) | |
| 636 fprintf(f, "\t"); | |
| 637 esc = g_markup_escape_text(pref->name, -1); | |
| 638 fprintf(f, "<pref name='%s'", esc); | |
| 639 g_free(esc); | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
640 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
641 |
| 5440 | 642 switch(pref->type) { |
| 643 case GAIM_PREF_NONE: | |
| 644 break; | |
| 645 case GAIM_PREF_BOOLEAN: | |
| 646 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
| 647 break; | |
| 648 case GAIM_PREF_INT: | |
| 649 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
| 650 break; | |
| 651 case GAIM_PREF_STRING: | |
| 652 esc = g_markup_escape_text(pref->value.string, -1); | |
| 653 fprintf(f, " type='string' value='%s'", esc); | |
| 654 g_free(esc); | |
| 655 break; | |
| 5561 | 656 case GAIM_PREF_STRING_LIST: |
| 657 fprintf(f, " type='stringlist'"); | |
| 658 break; | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
659 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
660 |
| 5561 | 661 if(pref->first_child || pref->type == GAIM_PREF_STRING_LIST) { |
| 5440 | 662 fprintf(f, ">\n"); |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
663 |
| 5440 | 664 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
| 665 gaim_prefs_write(f, tmp, depth+1); | |
| 5561 | 666 |
| 667 if(pref->type == GAIM_PREF_STRING_LIST) { | |
| 668 GList *tmp2; | |
| 669 for(tmp2 = pref->value.stringlist; tmp2; tmp2 = tmp2->next) { | |
| 670 for(i=0; i<depth+1; i++) | |
| 671 fprintf(f, "\t"); | |
| 672 esc = g_markup_escape_text(tmp2->data, -1); | |
| 673 fprintf(f, "<item value='%s' />\n", esc); | |
| 674 g_free(esc); | |
| 675 } | |
| 676 } | |
| 677 | |
| 5440 | 678 for(i=0; i<depth; i++) |
| 679 fprintf(f, "\t"); | |
| 680 fprintf(f, "</pref>\n"); | |
| 681 } else { | |
| 682 fprintf(f, " />\n"); | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
683 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
684 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
685 |
| 5440 | 686 void gaim_prefs_sync() { |
| 687 FILE *file; | |
| 688 const char *user_dir = gaim_user_dir(); | |
| 689 char *filename; | |
| 690 char *filename_real; | |
| 3551 | 691 |
| 5534 | 692 if(!prefs_is_loaded) { |
| 693 gaim_debug(GAIM_DEBUG_WARNING, "prefs", "prefs saved before loading! scheduling save.\n"); | |
| 694 schedule_prefs_save(); /* schedule a save for after we read in */ | |
| 695 return; | |
| 696 } | |
| 697 | |
| 5440 | 698 if(!user_dir) |
| 699 return; | |
| 3551 | 700 |
| 5534 | 701 gaim_debug(GAIM_DEBUG_INFO, "prefs", "writing prefs out to disk.\n"); |
| 702 | |
| 5440 | 703 file = fopen(user_dir, "r"); |
| 704 if(!file) | |
| 705 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 706 else | |
| 707 fclose(file); | |
| 3551 | 708 |
| 5440 | 709 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
| 3551 | 710 |
| 5440 | 711 if((file = fopen(filename, "w"))) { |
| 712 gaim_prefs_write(file, NULL, 0); | |
| 713 fclose(file); | |
| 714 chmod(filename, S_IRUSR | S_IWUSR); | |
| 715 } else { | |
| 716 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
| 717 filename); | |
| 718 } | |
| 3551 | 719 |
| 5440 | 720 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
| 721 if(rename(filename, filename_real) < 0) | |
| 722 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
| 723 filename, filename_real); | |
| 3551 | 724 |
| 5440 | 725 g_free(filename); |
| 726 g_free(filename_real); | |
| 3551 | 727 } |
| 728 | |
| 5440 | 729 static GList *prefs_stack = NULL; |
|
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
730 |
| 5440 | 731 static void prefs_start_element_handler (GMarkupParseContext *context, |
| 732 const gchar *element_name, | |
| 733 const gchar **attribute_names, | |
| 734 const gchar **attribute_values, | |
| 735 gpointer user_data, | |
| 736 GError **error) { | |
| 737 GaimPrefType pref_type = GAIM_PREF_NONE; | |
| 738 int i; | |
| 739 const char *pref_name = NULL, *pref_value = NULL; | |
| 740 GString *pref_name_full; | |
| 741 GList *tmp; | |
| 3366 | 742 |
| 5561 | 743 if(strcmp(element_name, "pref") && strcmp(element_name, "item")) |
| 5440 | 744 return; |
| 3500 | 745 |
| 5440 | 746 for(i = 0; attribute_names[i]; i++) { |
| 747 if(!strcmp(attribute_names[i], "name")) { | |
| 748 pref_name = attribute_values[i]; | |
| 749 } else if(!strcmp(attribute_names[i], "type")) { | |
| 750 if(!strcmp(attribute_values[i], "bool")) | |
| 751 pref_type = GAIM_PREF_BOOLEAN; | |
| 752 else if(!strcmp(attribute_values[i], "int")) | |
| 753 pref_type = GAIM_PREF_INT; | |
| 754 else if(!strcmp(attribute_values[i], "string")) | |
| 755 pref_type = GAIM_PREF_STRING; | |
| 5561 | 756 else if(!strcmp(attribute_values[i], "stringlist")) |
| 757 pref_type = GAIM_PREF_STRING_LIST; | |
| 5440 | 758 else |
| 759 return; | |
| 760 } else if(!strcmp(attribute_names[i], "value")) { | |
| 761 pref_value = attribute_values[i]; | |
| 762 } | |
| 763 } | |
|
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
764 |
| 5561 | 765 if(!strcmp(element_name, "item")) { |
| 766 struct gaim_pref *pref = find_pref(prefs_stack->data); | |
| 767 if(pref) { | |
| 768 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
| 769 g_strdup(pref_value)); | |
| 770 } | |
| 5440 | 771 return; |
| 5561 | 772 } else if(!pref_name || !strcmp(pref_name, "/")) { |
| 773 return; | |
| 774 } | |
|
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
775 |
| 5440 | 776 pref_name_full = g_string_new(pref_name); |
|
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
777 |
|
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
778 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
| 5440 | 779 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
| 780 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
| 781 } | |
| 1170 | 782 |
| 5440 | 783 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
|
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
784 |
| 5533 | 785 switch(pref_type) { |
| 786 case GAIM_PREF_NONE: | |
| 787 break; | |
| 788 case GAIM_PREF_BOOLEAN: | |
| 789 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
| 790 break; | |
| 791 case GAIM_PREF_INT: | |
| 792 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
| 793 break; | |
| 794 case GAIM_PREF_STRING: | |
| 795 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
| 796 break; | |
| 5561 | 797 case GAIM_PREF_STRING_LIST: |
| 798 break; | |
| 5440 | 799 } |
| 1170 | 800 |
| 5440 | 801 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
| 802 g_string_free(pref_name_full, TRUE); | |
| 1170 | 803 } |
| 804 | |
| 5440 | 805 static void prefs_end_element_handler(GMarkupParseContext *context, |
| 806 const gchar *element_name, gpointer user_data, GError **error) { | |
| 807 if(!strcmp(element_name, "pref")) { | |
| 808 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
| 809 } | |
| 1170 | 810 } |
| 811 | |
| 5440 | 812 static GMarkupParser prefs_parser = { |
| 813 prefs_start_element_handler, | |
| 814 prefs_end_element_handler, | |
| 815 NULL, | |
| 816 NULL, | |
| 817 NULL | |
| 818 }; | |
| 1170 | 819 |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
820 gboolean gaim_prefs_load() { |
| 5440 | 821 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); |
| 822 gchar *contents = NULL; | |
| 823 gsize length; | |
| 824 GMarkupParseContext *context; | |
| 825 GError *error = NULL; | |
|
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
826 |
| 5534 | 827 |
| 828 if(!filename) { | |
| 829 prefs_is_loaded = TRUE; | |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
830 return FALSE; |
| 5534 | 831 } |
| 5440 | 832 |
| 833 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
|
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
834 |
| 5440 | 835 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
| 836 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
| 837 error->message); | |
| 838 g_error_free(error); | |
| 5534 | 839 prefs_is_loaded = TRUE; |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
840 return FALSE; |
| 1170 | 841 } |
| 842 | |
| 5440 | 843 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
| 844 | |
| 845 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
| 846 g_markup_parse_context_free(context); | |
| 847 g_free(contents); | |
| 5534 | 848 prefs_is_loaded = TRUE; |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
849 return FALSE; |
| 5440 | 850 } |
| 851 | |
| 852 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
| 853 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
| 854 g_markup_parse_context_free(context); | |
| 855 g_free(contents); | |
| 5534 | 856 prefs_is_loaded = TRUE; |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
857 return FALSE; |
| 5440 | 858 } |
| 859 | |
| 860 g_markup_parse_context_free(context); | |
| 861 g_free(contents); | |
| 862 | |
| 863 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
| 864 g_free(filename); | |
| 5534 | 865 prefs_is_loaded = TRUE; |
|
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
866 |
|
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
867 return TRUE; |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
868 } |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
869 |
