Mercurial > pidgin.yaz
annotate src/prefs.c @ 5496:b7c0be69c749
[gaim-migrate @ 5892]
do_ask_dialog() will soon go the way of the something that goes away a lot.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Fri, 23 May 2003 00:48:20 +0000 |
parents | 156e65ca910f |
children | e7747cae9710 |
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; | |
54 } value; | |
55 GSList *callbacks; | |
56 struct gaim_pref *parent; | |
57 struct gaim_pref *sibling; | |
58 struct gaim_pref *first_child; | |
59 }; | |
3366 | 60 |
5440 | 61 static GHashTable *prefs_hash = NULL; |
62 | |
63 static struct gaim_pref prefs = { GAIM_PREF_NONE, NULL, {NULL}, NULL, | |
64 NULL, NULL, NULL }; | |
65 | |
66 void gaim_prefs_init() { | |
67 prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
68 | |
69 /* XXX: this is where you would want to put prefs declarations */ | |
70 } | |
3366 | 71 |
5440 | 72 static char *pref_full_name(struct gaim_pref *pref) { |
73 GString *name; | |
74 struct gaim_pref *parent; | |
75 if(!pref) | |
76 return NULL; | |
77 | |
78 if(pref == &prefs) | |
79 return g_strdup("/"); | |
80 | |
81 name = g_string_new(pref->name); | |
82 parent = pref->parent; | |
3366 | 83 |
5440 | 84 for(parent = pref->parent; parent && parent->name; parent = parent->parent) { |
85 name = g_string_prepend_c(name, '/'); | |
86 name = g_string_prepend(name, parent->name); | |
87 } | |
88 g_string_free(name, FALSE); | |
89 return name->str; | |
90 } | |
91 | |
92 static struct gaim_pref *find_pref(const char *name) | |
93 { | |
94 if(!name || name[0] != '/') { | |
95 return NULL; | |
96 } else if(name[1] == '\0') { | |
97 return &prefs; | |
98 } else { | |
99 return g_hash_table_lookup(prefs_hash, name); | |
100 } | |
101 } | |
102 | |
103 static struct gaim_pref *find_pref_parent(const char *name) | |
104 { | |
105 char *parent_name = g_path_get_dirname(name); | |
106 struct gaim_pref *ret = &prefs; | |
107 | |
108 if(strcmp(parent_name, "/")) { | |
109 ret = find_pref(parent_name); | |
110 } | |
1026 | 111 |
5440 | 112 g_free(parent_name); |
113 return ret; | |
114 } | |
115 | |
116 static void free_pref_value(struct gaim_pref *pref) { | |
117 switch(pref->type) { | |
118 case GAIM_PREF_BOOLEAN: | |
119 pref->value.boolean = FALSE; | |
120 case GAIM_PREF_INT: | |
121 pref->value.integer = 0; | |
122 break; | |
123 case GAIM_PREF_STRING: | |
124 g_free(pref->value.string); | |
125 pref->value.string = NULL; | |
126 break; | |
127 case GAIM_PREF_NONE: | |
128 break; | |
129 } | |
130 } | |
131 | |
132 static struct gaim_pref *add_pref(GaimPrefType type, const char *name) { | |
133 struct gaim_pref *parent; | |
134 struct gaim_pref *me; | |
135 struct gaim_pref *sibling; | |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
136 char *my_name; |
5440 | 137 |
138 parent = find_pref_parent(name); | |
139 | |
140 if(!parent) | |
141 return NULL; | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
142 |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
143 my_name = g_path_get_basename(name); |
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
144 |
5440 | 145 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { |
146 if(!strcmp(sibling->name, my_name)) { | |
147 g_free(my_name); | |
148 return NULL; | |
149 } | |
150 } | |
151 | |
152 me = g_new0(struct gaim_pref, 1); | |
153 me->type = type; | |
154 me->name = my_name; | |
155 | |
156 me->parent = parent; | |
157 if(parent->first_child) { | |
158 /* blatant abuse of a for loop */ | |
159 for(sibling = parent->first_child; sibling->sibling; | |
160 sibling = sibling->sibling); | |
161 sibling->sibling = me; | |
162 } else { | |
163 parent->first_child = me; | |
164 } | |
165 | |
166 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
167 | |
168 return me; | |
169 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
170 |
5440 | 171 void gaim_prefs_add_none(const char *name) { |
172 add_pref(GAIM_PREF_NONE, name); | |
173 } | |
174 | |
175 void gaim_prefs_add_bool(const char *name, gboolean value) { | |
176 struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
177 | |
178 if(!pref) | |
179 return; | |
180 | |
181 pref->value.boolean = value; | |
182 } | |
3630 | 183 |
5440 | 184 void gaim_prefs_add_int(const char *name, int value) { |
185 struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); | |
186 | |
187 if(!pref) | |
188 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
189 |
5440 | 190 pref->value.integer = value; |
191 } | |
192 | |
193 void gaim_prefs_add_string(const char *name, const char *value) { | |
194 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
195 | |
196 if(!pref) | |
197 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
198 |
5440 | 199 pref->value.string = g_strdup(value); |
200 } | |
201 | |
202 void remove_pref(struct gaim_pref *pref) { | |
203 char *name; | |
204 | |
205 if(!pref || pref == &prefs) | |
206 return; | |
207 | |
208 if(pref->parent->first_child == pref) { | |
209 pref->parent->first_child = pref->sibling; | |
210 } else { | |
211 struct gaim_pref *sib = pref->parent->first_child; | |
212 while(sib->sibling != pref) | |
213 sib = sib->sibling; | |
214 sib->sibling = pref->sibling; | |
215 } | |
216 | |
217 name = pref_full_name(pref); | |
218 | |
219 g_hash_table_remove(prefs_hash, name); | |
220 g_free(name); | |
221 | |
222 free_pref_value(pref); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
223 |
5440 | 224 g_slist_free(pref->callbacks); |
225 g_free(pref->name); | |
226 g_free(pref); | |
227 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
228 |
5440 | 229 void gaim_prefs_remove(const char *name) { |
230 struct gaim_pref *pref = find_pref(name); | |
231 struct gaim_pref *child, *child2; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
232 |
5440 | 233 if(!pref) |
234 return; | |
235 child = pref->first_child; | |
236 while(child) { | |
237 child2 = child; | |
238 child = child->sibling; | |
239 remove_pref(child2); | |
240 } | |
241 | |
242 remove_pref(pref); | |
243 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
244 |
5440 | 245 void gaim_prefs_destroy() { |
246 gaim_prefs_remove("/"); | |
247 } | |
248 | |
249 static void do_callbacks(const char* name, struct gaim_pref *pref) { | |
250 GSList *cbs; | |
251 struct gaim_pref *cb_pref; | |
252 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
253 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
254 struct pref_cb *cb = cbs->data; | |
255 cb->func(name, pref->type, pref->value.generic, cb->data); | |
4215 | 256 } |
257 } | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
258 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
259 |
5440 | 260 void gaim_prefs_set_generic(const char *name, gpointer value) { |
261 struct gaim_pref *pref = find_pref(name); | |
3366 | 262 |
5440 | 263 g_return_if_fail(pref != NULL); |
3500 | 264 |
5440 | 265 pref->value.generic = value; |
266 do_callbacks(name, pref); | |
3366 | 267 } |
268 | |
5440 | 269 void gaim_prefs_set_bool(const char *name, gboolean value) { |
270 struct gaim_pref *pref = find_pref(name); | |
4288 | 271 |
5440 | 272 g_return_if_fail(pref != NULL); |
273 g_return_if_fail(pref->type == GAIM_PREF_BOOLEAN); | |
4325 | 274 |
5440 | 275 if(pref->value.boolean != value) { |
276 pref->value.boolean = value; | |
277 do_callbacks(name, pref); | |
4288 | 278 } |
4324 | 279 } |
4325 | 280 |
5440 | 281 void gaim_prefs_set_int(const char *name, int value) { |
282 struct gaim_pref *pref = find_pref(name); | |
4325 | 283 |
5440 | 284 g_return_if_fail(pref != NULL); |
285 g_return_if_fail(pref->type == GAIM_PREF_INT); | |
4325 | 286 |
5440 | 287 if(pref->value.integer != value) { |
288 pref->value.integer = value; | |
289 do_callbacks(name, pref); | |
290 } | |
4326 | 291 } |
292 | |
5451 | 293 void gaim_prefs_set_string(const char *name, const char *value) { |
5440 | 294 struct gaim_pref *pref = find_pref(name); |
4325 | 295 |
5440 | 296 g_return_if_fail(pref != NULL); |
297 g_return_if_fail(pref->type == GAIM_PREF_STRING); | |
4325 | 298 |
5440 | 299 if(strcmp(pref->value.string, value)) { |
300 g_free(pref->value.string); | |
301 pref->value.string = g_strdup(value); | |
302 do_callbacks(name, pref); | |
4325 | 303 } |
304 } | |
305 | |
5440 | 306 gpointer gaim_prefs_get_generic(const char *name) { |
307 struct gaim_pref *pref = find_pref(name); | |
4325 | 308 |
5440 | 309 g_return_val_if_fail(pref != NULL, NULL); |
4288 | 310 |
5440 | 311 return pref->value.generic; |
4288 | 312 } |
313 | |
5440 | 314 gboolean gaim_prefs_get_bool(const char *name) { |
315 struct gaim_pref *pref = find_pref(name); | |
3427 | 316 |
5440 | 317 g_return_val_if_fail(pref != NULL, FALSE); |
318 g_return_val_if_fail(pref->type == GAIM_PREF_BOOLEAN, FALSE); | |
3472 | 319 |
5440 | 320 return pref->value.boolean; |
3366 | 321 } |
322 | |
5440 | 323 int gaim_prefs_get_int(const char *name) { |
324 struct gaim_pref *pref = find_pref(name); | |
3500 | 325 |
5440 | 326 g_return_val_if_fail(pref != NULL, 0); |
327 g_return_val_if_fail(pref->type == GAIM_PREF_INT, 0); | |
3366 | 328 |
5440 | 329 return pref->value.integer; |
3366 | 330 } |
331 | |
5440 | 332 char *gaim_prefs_get_string(const char *name) { |
333 struct gaim_pref *pref = find_pref(name); | |
3366 | 334 |
5440 | 335 g_return_val_if_fail(pref != NULL, NULL); |
336 g_return_val_if_fail(pref->type == GAIM_PREF_STRING, NULL); | |
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
337 |
5440 | 338 return pref->value.string; |
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
339 } |
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
340 |
5440 | 341 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
342 { | |
343 struct gaim_pref *pref = find_pref(name); | |
344 struct pref_cb *cb; | |
345 static guint cb_id = 0; | |
3366 | 346 |
5440 | 347 if(!pref) |
348 return 0; | |
3366 | 349 |
5440 | 350 cb = g_new0(struct pref_cb, 1); |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
351 |
5440 | 352 cb->func = func; |
353 cb->data = data; | |
354 cb->id = ++cb_id; | |
4991 | 355 |
5440 | 356 pref->callbacks = g_slist_append(pref->callbacks, cb); |
3366 | 357 |
5440 | 358 return cb->id; |
3366 | 359 } |
360 | |
5440 | 361 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
362 GSList *cbs; | |
363 struct gaim_pref *child; | |
2254 | 364 |
5440 | 365 if(!pref) |
366 return FALSE; | |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
367 |
5440 | 368 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
369 struct pref_cb *cb = cbs->data; | |
370 if(cb->id == callback_id) { | |
371 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
372 g_free(cb); | |
373 return TRUE; | |
4428 | 374 } |
375 } | |
376 | |
5440 | 377 for(child = pref->first_child; child; child = child->sibling) { |
378 if(disco_callback_helper(child, callback_id)) | |
379 return TRUE; | |
4428 | 380 } |
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
381 |
5440 | 382 return FALSE; |
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
383 } |
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
384 |
5440 | 385 void gaim_prefs_disconnect_callback(guint callback_id) { |
386 disco_callback_helper(&prefs, callback_id); | |
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
387 } |
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
388 |
5440 | 389 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
390 struct gaim_pref *tmp; | |
391 char *esc; | |
392 int i; | |
3500 | 393 |
5440 | 394 if(!pref) { |
395 pref = &prefs; | |
3551 | 396 |
5440 | 397 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
398 fprintf(f, "<pref name='/'"); | |
399 } else { | |
400 for(i=0; i<depth; i++) | |
401 fprintf(f, "\t"); | |
402 esc = g_markup_escape_text(pref->name, -1); | |
403 fprintf(f, "<pref name='%s'", esc); | |
404 g_free(esc); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
405 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
406 |
5440 | 407 switch(pref->type) { |
408 case GAIM_PREF_NONE: | |
409 break; | |
410 case GAIM_PREF_BOOLEAN: | |
411 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
412 break; | |
413 case GAIM_PREF_INT: | |
414 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
415 break; | |
416 case GAIM_PREF_STRING: | |
417 esc = g_markup_escape_text(pref->value.string, -1); | |
418 fprintf(f, " type='string' value='%s'", esc); | |
419 g_free(esc); | |
420 break; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
421 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
422 |
5440 | 423 if(pref->first_child) { |
424 fprintf(f, ">\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
425 |
5440 | 426 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
427 gaim_prefs_write(f, tmp, depth+1); | |
428 for(i=0; i<depth; i++) | |
429 fprintf(f, "\t"); | |
430 fprintf(f, "</pref>\n"); | |
431 } else { | |
432 fprintf(f, " />\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
433 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
434 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
435 |
5440 | 436 void gaim_prefs_save() { |
437 /* FIXME: do this with timers so we don't save so damn often */ | |
438 gaim_prefs_sync(); | |
439 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
440 |
5440 | 441 void gaim_prefs_sync() { |
442 FILE *file; | |
443 const char *user_dir = gaim_user_dir(); | |
444 char *filename; | |
445 char *filename_real; | |
3551 | 446 |
5440 | 447 if(!user_dir) |
448 return; | |
3551 | 449 |
5440 | 450 file = fopen(user_dir, "r"); |
451 if(!file) | |
452 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
453 else | |
454 fclose(file); | |
3551 | 455 |
5440 | 456 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
3551 | 457 |
5440 | 458 if((file = fopen(filename, "w"))) { |
459 gaim_prefs_write(file, NULL, 0); | |
460 fclose(file); | |
461 chmod(filename, S_IRUSR | S_IWUSR); | |
462 } else { | |
463 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
464 filename); | |
465 } | |
3551 | 466 |
5440 | 467 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
468 if(rename(filename, filename_real) < 0) | |
469 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
470 filename, filename_real); | |
3551 | 471 |
5440 | 472 g_free(filename); |
473 g_free(filename_real); | |
3551 | 474 } |
475 | |
5440 | 476 static GList *prefs_stack = NULL; |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
477 |
5440 | 478 static void prefs_start_element_handler (GMarkupParseContext *context, |
479 const gchar *element_name, | |
480 const gchar **attribute_names, | |
481 const gchar **attribute_values, | |
482 gpointer user_data, | |
483 GError **error) { | |
484 GaimPrefType pref_type = GAIM_PREF_NONE; | |
485 int i; | |
486 const char *pref_name = NULL, *pref_value = NULL; | |
487 GString *pref_name_full; | |
488 GList *tmp; | |
3366 | 489 |
5440 | 490 if(strcmp(element_name, "pref")) |
491 return; | |
3500 | 492 |
5440 | 493 for(i = 0; attribute_names[i]; i++) { |
494 if(!strcmp(attribute_names[i], "name")) { | |
495 pref_name = attribute_values[i]; | |
496 } else if(!strcmp(attribute_names[i], "type")) { | |
497 if(!strcmp(attribute_values[i], "bool")) | |
498 pref_type = GAIM_PREF_BOOLEAN; | |
499 else if(!strcmp(attribute_values[i], "int")) | |
500 pref_type = GAIM_PREF_INT; | |
501 else if(!strcmp(attribute_values[i], "string")) | |
502 pref_type = GAIM_PREF_STRING; | |
503 else | |
504 return; | |
505 } else if(!strcmp(attribute_names[i], "value")) { | |
506 pref_value = attribute_values[i]; | |
507 } | |
508 } | |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
509 |
5440 | 510 if(!pref_name || !strcmp(pref_name, "/")) |
511 return; | |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
512 |
5440 | 513 pref_name_full = g_string_new(pref_name); |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
514 |
5440 | 515 for(tmp = prefs_stack; tmp; tmp = tmp->prev) { |
516 pref_name_full = g_string_prepend_c(pref_name_full, '/'); | |
517 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
518 } | |
1170 | 519 |
5440 | 520 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
521 |
5444 | 522 if(find_pref(pref_name_full->str)) { |
523 switch(pref_type) { | |
524 case GAIM_PREF_NONE: | |
525 gaim_prefs_add_none(pref_name_full->str); | |
526 break; | |
527 case GAIM_PREF_BOOLEAN: | |
528 gaim_prefs_add_bool(pref_name_full->str, atoi(pref_value)); | |
529 break; | |
530 case GAIM_PREF_INT: | |
531 gaim_prefs_add_int(pref_name_full->str, atoi(pref_value)); | |
532 break; | |
533 case GAIM_PREF_STRING: | |
534 gaim_prefs_add_string(pref_name_full->str, pref_value); | |
535 break; | |
536 } | |
537 } else { | |
538 switch(pref_type) { | |
539 case GAIM_PREF_NONE: | |
540 break; | |
541 case GAIM_PREF_BOOLEAN: | |
542 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
543 break; | |
544 case GAIM_PREF_INT: | |
545 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
546 break; | |
547 case GAIM_PREF_STRING: | |
548 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
549 break; | |
550 } | |
5440 | 551 } |
1170 | 552 |
5440 | 553 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
554 g_string_free(pref_name_full, TRUE); | |
1170 | 555 } |
556 | |
5440 | 557 static void prefs_end_element_handler(GMarkupParseContext *context, |
558 const gchar *element_name, gpointer user_data, GError **error) { | |
559 if(!strcmp(element_name, "pref")) { | |
560 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
561 } | |
1170 | 562 } |
563 | |
5440 | 564 static GMarkupParser prefs_parser = { |
565 prefs_start_element_handler, | |
566 prefs_end_element_handler, | |
567 NULL, | |
568 NULL, | |
569 NULL | |
570 }; | |
1170 | 571 |
5440 | 572 void gaim_prefs_load() { |
573 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); | |
574 gchar *contents = NULL; | |
575 gsize length; | |
576 GMarkupParseContext *context; | |
577 GError *error = NULL; | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
578 |
5440 | 579 if(!filename) |
580 return; | |
581 | |
582 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
583 |
5440 | 584 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
585 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
586 error->message); | |
587 g_error_free(error); | |
588 return; | |
1170 | 589 } |
590 | |
5440 | 591 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
592 | |
593 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
594 g_markup_parse_context_free(context); | |
595 g_free(contents); | |
596 return; | |
597 } | |
598 | |
599 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
600 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
601 g_markup_parse_context_free(context); | |
602 g_free(contents); | |
603 return; | |
604 } | |
605 | |
606 g_markup_parse_context_free(context); | |
607 g_free(contents); | |
608 | |
609 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
610 g_free(filename); | |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
611 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
612 |
3366 | 613 |