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