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