Mercurial > pidgin.yaz
annotate src/prefs.c @ 8589:72a0759a37a2
[gaim-migrate @ 9340]
Paco-Paco got his 8space tab on earlier, and it makes me feel dirty.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Tue, 06 Apr 2004 01:06:45 +0000 |
parents | 1a016d979ce0 |
children | d99d2572d1a9 |
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 | |
629 void gaim_prefs_rename_old() { | |
7555 | 630 gaim_prefs_rename("/gaim/gtk/logging/log_ims", "/core/logging/log_ims"); |
631 gaim_prefs_rename("/gaim/gtk/logging/log_chats", "/core/logging/log_chats"); | |
7561
cdfdbabd3266
[gaim-migrate @ 8175]
Christian Hammond <chipx86@chipx86.com>
parents:
7555
diff
changeset
|
632 gaim_prefs_rename("/core/conversations/placement", |
cdfdbabd3266
[gaim-migrate @ 8175]
Christian Hammond <chipx86@chipx86.com>
parents:
7555
diff
changeset
|
633 "/gaim/gtk/conversations/placement"); |
6693 | 634 } |
635 | |
5440 | 636 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
637 { | |
638 struct gaim_pref *pref = find_pref(name); | |
639 struct pref_cb *cb; | |
640 static guint cb_id = 0; | |
3366 | 641 |
5440 | 642 if(!pref) |
643 return 0; | |
3366 | 644 |
5440 | 645 cb = g_new0(struct pref_cb, 1); |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
646 |
5440 | 647 cb->func = func; |
648 cb->data = data; | |
649 cb->id = ++cb_id; | |
4991 | 650 |
5440 | 651 pref->callbacks = g_slist_append(pref->callbacks, cb); |
3366 | 652 |
5440 | 653 return cb->id; |
3366 | 654 } |
655 | |
5440 | 656 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
657 GSList *cbs; | |
658 struct gaim_pref *child; | |
2254 | 659 |
5440 | 660 if(!pref) |
661 return FALSE; | |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
662 |
5440 | 663 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
664 struct pref_cb *cb = cbs->data; | |
665 if(cb->id == callback_id) { | |
666 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
667 g_free(cb); | |
668 return TRUE; | |
4428 | 669 } |
670 } | |
671 | |
5440 | 672 for(child = pref->first_child; child; child = child->sibling) { |
673 if(disco_callback_helper(child, callback_id)) | |
674 return TRUE; | |
4428 | 675 } |
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
676 |
5440 | 677 return FALSE; |
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
678 } |
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
679 |
5440 | 680 void gaim_prefs_disconnect_callback(guint callback_id) { |
681 disco_callback_helper(&prefs, callback_id); | |
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
682 } |
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
683 |
5440 | 684 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
685 struct gaim_pref *tmp; | |
686 char *esc; | |
687 int i; | |
3500 | 688 |
5440 | 689 if(!pref) { |
690 pref = &prefs; | |
3551 | 691 |
5440 | 692 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
693 fprintf(f, "<pref name='/'"); | |
694 } else { | |
695 for(i=0; i<depth; i++) | |
696 fprintf(f, "\t"); | |
697 esc = g_markup_escape_text(pref->name, -1); | |
698 fprintf(f, "<pref name='%s'", esc); | |
699 g_free(esc); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
700 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
701 |
5440 | 702 switch(pref->type) { |
703 case GAIM_PREF_NONE: | |
704 break; | |
705 case GAIM_PREF_BOOLEAN: | |
706 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
707 break; | |
708 case GAIM_PREF_INT: | |
709 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
710 break; | |
711 case GAIM_PREF_STRING: | |
712 esc = g_markup_escape_text(pref->value.string, -1); | |
713 fprintf(f, " type='string' value='%s'", esc); | |
714 g_free(esc); | |
715 break; | |
5561 | 716 case GAIM_PREF_STRING_LIST: |
717 fprintf(f, " type='stringlist'"); | |
718 break; | |
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 |
5561 | 721 if(pref->first_child || pref->type == GAIM_PREF_STRING_LIST) { |
5440 | 722 fprintf(f, ">\n"); |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
723 |
5440 | 724 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
725 gaim_prefs_write(f, tmp, depth+1); | |
5561 | 726 |
727 if(pref->type == GAIM_PREF_STRING_LIST) { | |
728 GList *tmp2; | |
729 for(tmp2 = pref->value.stringlist; tmp2; tmp2 = tmp2->next) { | |
730 for(i=0; i<depth+1; i++) | |
731 fprintf(f, "\t"); | |
732 esc = g_markup_escape_text(tmp2->data, -1); | |
733 fprintf(f, "<item value='%s' />\n", esc); | |
734 g_free(esc); | |
735 } | |
736 } | |
737 | |
5440 | 738 for(i=0; i<depth; i++) |
739 fprintf(f, "\t"); | |
740 fprintf(f, "</pref>\n"); | |
741 } else { | |
742 fprintf(f, " />\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
743 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
744 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
745 |
5440 | 746 void gaim_prefs_sync() { |
747 FILE *file; | |
748 const char *user_dir = gaim_user_dir(); | |
749 char *filename; | |
750 char *filename_real; | |
3551 | 751 |
5534 | 752 if(!prefs_is_loaded) { |
753 gaim_debug(GAIM_DEBUG_WARNING, "prefs", "prefs saved before loading! scheduling save.\n"); | |
754 schedule_prefs_save(); /* schedule a save for after we read in */ | |
755 return; | |
756 } | |
757 | |
5440 | 758 if(!user_dir) |
759 return; | |
3551 | 760 |
5534 | 761 gaim_debug(GAIM_DEBUG_INFO, "prefs", "writing prefs out to disk.\n"); |
762 | |
5440 | 763 file = fopen(user_dir, "r"); |
764 if(!file) | |
765 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
766 else | |
767 fclose(file); | |
3551 | 768 |
5440 | 769 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
3551 | 770 |
5440 | 771 if((file = fopen(filename, "w"))) { |
772 gaim_prefs_write(file, NULL, 0); | |
773 fclose(file); | |
774 chmod(filename, S_IRUSR | S_IWUSR); | |
775 } else { | |
776 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
777 filename); | |
8549 | 778 g_free(filename); |
779 return; | |
5440 | 780 } |
3551 | 781 |
5440 | 782 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
783 if(rename(filename, filename_real) < 0) | |
784 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
785 filename, filename_real); | |
3551 | 786 |
5440 | 787 g_free(filename); |
788 g_free(filename_real); | |
3551 | 789 } |
790 | |
5440 | 791 static GList *prefs_stack = NULL; |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
792 |
5440 | 793 static void prefs_start_element_handler (GMarkupParseContext *context, |
794 const gchar *element_name, | |
795 const gchar **attribute_names, | |
796 const gchar **attribute_values, | |
797 gpointer user_data, | |
798 GError **error) { | |
799 GaimPrefType pref_type = GAIM_PREF_NONE; | |
800 int i; | |
801 const char *pref_name = NULL, *pref_value = NULL; | |
802 GString *pref_name_full; | |
803 GList *tmp; | |
3366 | 804 |
5561 | 805 if(strcmp(element_name, "pref") && strcmp(element_name, "item")) |
5440 | 806 return; |
3500 | 807 |
5440 | 808 for(i = 0; attribute_names[i]; i++) { |
809 if(!strcmp(attribute_names[i], "name")) { | |
810 pref_name = attribute_values[i]; | |
811 } else if(!strcmp(attribute_names[i], "type")) { | |
812 if(!strcmp(attribute_values[i], "bool")) | |
813 pref_type = GAIM_PREF_BOOLEAN; | |
814 else if(!strcmp(attribute_values[i], "int")) | |
815 pref_type = GAIM_PREF_INT; | |
816 else if(!strcmp(attribute_values[i], "string")) | |
817 pref_type = GAIM_PREF_STRING; | |
5561 | 818 else if(!strcmp(attribute_values[i], "stringlist")) |
819 pref_type = GAIM_PREF_STRING_LIST; | |
5440 | 820 else |
821 return; | |
822 } else if(!strcmp(attribute_names[i], "value")) { | |
823 pref_value = attribute_values[i]; | |
824 } | |
825 } | |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
826 |
5561 | 827 if(!strcmp(element_name, "item")) { |
5838 | 828 struct gaim_pref *pref; |
829 | |
830 pref_name_full = g_string_new(""); | |
831 | |
832 for(tmp = prefs_stack; tmp; tmp = tmp->next) { | |
833 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
834 pref_name_full = g_string_prepend_c(pref_name_full, '/'); | |
835 } | |
836 | |
837 pref = find_pref(pref_name_full->str); | |
838 | |
5561 | 839 if(pref) { |
840 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
841 g_strdup(pref_value)); | |
842 } | |
5838 | 843 } else { |
844 if(!pref_name || !strcmp(pref_name, "/")) | |
845 return; | |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
846 |
5838 | 847 pref_name_full = g_string_new(pref_name); |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
848 |
5838 | 849 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
850 pref_name_full = g_string_prepend_c(pref_name_full, '/'); | |
851 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
852 } | |
853 | |
5440 | 854 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
855 |
5838 | 856 switch(pref_type) { |
857 case GAIM_PREF_NONE: | |
7785 | 858 gaim_prefs_add_none(pref_name_full->str); |
5838 | 859 break; |
860 case GAIM_PREF_BOOLEAN: | |
861 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
862 break; | |
863 case GAIM_PREF_INT: | |
864 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
865 break; | |
866 case GAIM_PREF_STRING: | |
867 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
868 break; | |
869 case GAIM_PREF_STRING_LIST: | |
870 gaim_prefs_set_string_list(pref_name_full->str, NULL); | |
871 break; | |
872 } | |
873 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); | |
874 g_string_free(pref_name_full, TRUE); | |
5440 | 875 } |
1170 | 876 } |
877 | |
5440 | 878 static void prefs_end_element_handler(GMarkupParseContext *context, |
879 const gchar *element_name, gpointer user_data, GError **error) { | |
5940 | 880 if(prefs_stack && !strcmp(element_name, "pref")) { |
881 g_free(prefs_stack->data); | |
5440 | 882 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); |
883 } | |
1170 | 884 } |
885 | |
5440 | 886 static GMarkupParser prefs_parser = { |
887 prefs_start_element_handler, | |
888 prefs_end_element_handler, | |
889 NULL, | |
890 NULL, | |
891 NULL | |
892 }; | |
1170 | 893 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
894 gboolean gaim_prefs_load() { |
5440 | 895 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); |
896 gchar *contents = NULL; | |
897 gsize length; | |
898 GMarkupParseContext *context; | |
899 GError *error = NULL; | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
900 |
7561
cdfdbabd3266
[gaim-migrate @ 8175]
Christian Hammond <chipx86@chipx86.com>
parents:
7555
diff
changeset
|
901 if (!filename) { |
5534 | 902 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
903 return FALSE; |
5534 | 904 } |
5440 | 905 |
906 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
907 |
5440 | 908 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
909 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
910 error->message); | |
911 g_error_free(error); | |
6040 | 912 g_free(filename); |
5534 | 913 prefs_is_loaded = TRUE; |
6040 | 914 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
915 return FALSE; |
1170 | 916 } |
917 | |
5440 | 918 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
919 | |
920 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
921 g_markup_parse_context_free(context); | |
922 g_free(contents); | |
6040 | 923 g_free(filename); |
5534 | 924 prefs_is_loaded = TRUE; |
6040 | 925 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
926 return FALSE; |
5440 | 927 } |
928 | |
929 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
930 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
931 g_markup_parse_context_free(context); | |
932 g_free(contents); | |
6040 | 933 g_free(filename); |
5534 | 934 prefs_is_loaded = TRUE; |
6040 | 935 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
936 return FALSE; |
5440 | 937 } |
938 | |
6040 | 939 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); |
5440 | 940 g_markup_parse_context_free(context); |
941 g_free(contents); | |
942 g_free(filename); | |
5534 | 943 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
944 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
945 return TRUE; |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
946 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
947 |