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