Mercurial > pidgin.yaz
annotate src/prefs.c @ 5739:edf4d443e671
[gaim-migrate @ 6163]
Fixed a leakbug in trepia.c.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Wed, 04 Jun 2003 07:26:04 +0000 |
parents | b61520e71679 |
children | 2adc29c88a45 |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
5440 | 4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> |
1 | 5 * |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
340
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2074
diff
changeset
|
23 #include <config.h> |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
340
diff
changeset
|
24 #endif |
5440 | 25 |
1 | 26 #include <string.h> |
27 #include <stdio.h> | |
28 #include <stdlib.h> | |
5440 | 29 #include <sys/stat.h> |
30 #include <sys/types.h> | |
31 #include <glib.h> | |
32 #include "prefs.h" | |
33 #include "debug.h" | |
34 #include "util.h" | |
3366 | 35 |
4026
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
36 #ifdef _WIN32 |
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
37 #include "win32dep.h" |
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
38 #endif |
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
39 |
5440 | 40 struct pref_cb { |
41 GaimPrefCallback func; | |
42 gpointer data; | |
43 guint id; | |
44 }; | |
45 | |
46 struct gaim_pref { | |
47 GaimPrefType type; | |
48 char *name; | |
49 union { | |
50 gpointer generic; | |
51 gboolean boolean; | |
52 int integer; | |
53 char *string; | |
5561 | 54 GList *stringlist; |
5440 | 55 } value; |
56 GSList *callbacks; | |
57 struct gaim_pref *parent; | |
58 struct gaim_pref *sibling; | |
59 struct gaim_pref *first_child; | |
60 }; | |
3366 | 61 |
5440 | 62 static GHashTable *prefs_hash = NULL; |
63 | |
64 static struct gaim_pref prefs = { GAIM_PREF_NONE, NULL, {NULL}, NULL, | |
65 NULL, NULL, NULL }; | |
66 | |
5534 | 67 static guint prefs_save_timer = 0; |
68 static gboolean prefs_is_loaded = FALSE; | |
69 | |
70 | |
71 static gboolean prefs_save_callback(gpointer who_cares) { | |
72 gaim_prefs_sync(); | |
73 prefs_save_timer = 0; | |
74 return FALSE; | |
75 } | |
76 | |
77 static void schedule_prefs_save() { | |
78 if(!prefs_save_timer) | |
79 prefs_save_timer = g_timeout_add(5000, prefs_save_callback, NULL); | |
80 } | |
81 | |
82 static void prefs_save_cb(const char *name, GaimPrefType type, gpointer val, | |
83 gpointer user_data) { | |
84 | |
85 if(!prefs_is_loaded) | |
86 return; | |
87 | |
88 gaim_debug(GAIM_DEBUG_MISC, "prefs", "%s changed, scheduling save.\n", name); | |
89 | |
90 schedule_prefs_save(); | |
91 } | |
92 | |
5440 | 93 void gaim_prefs_init() { |
94 prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
95 | |
5534 | 96 gaim_prefs_connect_callback("/", prefs_save_cb, NULL); |
97 | |
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
98 gaim_prefs_add_none("/core"); |
5550
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
99 gaim_prefs_add_none("/plugins"); |
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
100 gaim_prefs_add_none("/plugins/core"); |
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
101 gaim_prefs_add_none("/plugins/lopl"); |
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
102 gaim_prefs_add_none("/plugins/prpl"); |
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
103 |
5440 | 104 /* XXX: this is where you would want to put prefs declarations */ |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
105 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
106 /* Away */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
107 gaim_prefs_add_none("/core/away"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
108 gaim_prefs_add_bool("/core/away/away_when_idle", TRUE); |
5550
b18c2a37cc96
[gaim-migrate @ 5951]
Christian Hammond <chipx86@chipx86.com>
parents:
5547
diff
changeset
|
109 gaim_prefs_add_int("/core/away/mins_before_away", 1); |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
110 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
111 /* Away -> Auto Response */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
112 gaim_prefs_add_none("/core/away/auto_response"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
113 gaim_prefs_add_bool("/core/away/auto_response/enabled", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
118 /* Buddies */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
119 gaim_prefs_add_none("/core/buddies"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
120 gaim_prefs_add_bool("/core/buddies/use_server_alias", TRUE); |
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 /* Conversations */ |
5539
de09863bd4b5
[gaim-migrate @ 5939]
Christian Hammond <chipx86@chipx86.com>
parents:
5534
diff
changeset
|
123 gaim_prefs_add_none("/core/conversations"); |
de09863bd4b5
[gaim-migrate @ 5939]
Christian Hammond <chipx86@chipx86.com>
parents:
5534
diff
changeset
|
124 gaim_prefs_add_bool("/core/conversations/send_urls_as_links", TRUE); |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
125 gaim_prefs_add_bool("/core/conversations/away_back_on_send", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
126 gaim_prefs_add_bool("/core/conversations/use_alias_for_title", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
127 gaim_prefs_add_bool("/core/conversations/combine_chat_im", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
128 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
129 /* Conversations -> Chat */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
130 gaim_prefs_add_none("/core/conversations/chat"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
131 gaim_prefs_add_bool("/core/conversations/chat/show_join", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
132 gaim_prefs_add_bool("/core/conversations/chat/show_leave", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
133 gaim_prefs_add_bool("/core/conversations/chat/show_nick_change", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
134 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
135 /* Conversations -> IM */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
136 gaim_prefs_add_none("/core/conversations/im"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
137 gaim_prefs_add_bool("/core/conversations/im/show_login", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
138 gaim_prefs_add_bool("/core/conversations/im/send_typing", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
139 |
5440 | 140 } |
3366 | 141 |
5440 | 142 static char *pref_full_name(struct gaim_pref *pref) { |
143 GString *name; | |
144 struct gaim_pref *parent; | |
145 if(!pref) | |
146 return NULL; | |
147 | |
148 if(pref == &prefs) | |
149 return g_strdup("/"); | |
150 | |
151 name = g_string_new(pref->name); | |
152 parent = pref->parent; | |
3366 | 153 |
5440 | 154 for(parent = pref->parent; parent && parent->name; parent = parent->parent) { |
155 name = g_string_prepend_c(name, '/'); | |
156 name = g_string_prepend(name, parent->name); | |
157 } | |
158 g_string_free(name, FALSE); | |
159 return name->str; | |
160 } | |
161 | |
162 static struct gaim_pref *find_pref(const char *name) | |
163 { | |
164 if(!name || name[0] != '/') { | |
165 return NULL; | |
166 } else if(name[1] == '\0') { | |
167 return &prefs; | |
168 } else { | |
169 return g_hash_table_lookup(prefs_hash, name); | |
170 } | |
171 } | |
172 | |
173 static struct gaim_pref *find_pref_parent(const char *name) | |
174 { | |
175 char *parent_name = g_path_get_dirname(name); | |
176 struct gaim_pref *ret = &prefs; | |
177 | |
178 if(strcmp(parent_name, "/")) { | |
179 ret = find_pref(parent_name); | |
180 } | |
1026 | 181 |
5440 | 182 g_free(parent_name); |
183 return ret; | |
184 } | |
185 | |
186 static void free_pref_value(struct gaim_pref *pref) { | |
187 switch(pref->type) { | |
188 case GAIM_PREF_BOOLEAN: | |
189 pref->value.boolean = FALSE; | |
190 case GAIM_PREF_INT: | |
191 pref->value.integer = 0; | |
192 break; | |
193 case GAIM_PREF_STRING: | |
194 g_free(pref->value.string); | |
195 pref->value.string = NULL; | |
196 break; | |
5561 | 197 case GAIM_PREF_STRING_LIST: |
198 { | |
199 GList *tmp; | |
200 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
201 g_free(tmp->data); | |
202 | |
203 g_list_free(pref->value.stringlist); | |
204 } break; | |
5440 | 205 case GAIM_PREF_NONE: |
206 break; | |
207 } | |
208 } | |
209 | |
210 static struct gaim_pref *add_pref(GaimPrefType type, const char *name) { | |
211 struct gaim_pref *parent; | |
212 struct gaim_pref *me; | |
213 struct gaim_pref *sibling; | |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
214 char *my_name; |
5440 | 215 |
216 parent = find_pref_parent(name); | |
217 | |
218 if(!parent) | |
219 return NULL; | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
220 |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
221 my_name = g_path_get_basename(name); |
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
222 |
5440 | 223 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { |
224 if(!strcmp(sibling->name, my_name)) { | |
225 g_free(my_name); | |
226 return NULL; | |
227 } | |
228 } | |
229 | |
230 me = g_new0(struct gaim_pref, 1); | |
231 me->type = type; | |
232 me->name = my_name; | |
233 | |
234 me->parent = parent; | |
235 if(parent->first_child) { | |
236 /* blatant abuse of a for loop */ | |
237 for(sibling = parent->first_child; sibling->sibling; | |
238 sibling = sibling->sibling); | |
239 sibling->sibling = me; | |
240 } else { | |
241 parent->first_child = me; | |
242 } | |
243 | |
244 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
245 | |
246 return me; | |
247 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
248 |
5440 | 249 void gaim_prefs_add_none(const char *name) { |
250 add_pref(GAIM_PREF_NONE, name); | |
251 } | |
252 | |
253 void gaim_prefs_add_bool(const char *name, gboolean value) { | |
254 struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
255 | |
256 if(!pref) | |
257 return; | |
258 | |
259 pref->value.boolean = value; | |
260 } | |
3630 | 261 |
5440 | 262 void gaim_prefs_add_int(const char *name, int value) { |
263 struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); | |
264 | |
265 if(!pref) | |
266 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
267 |
5440 | 268 pref->value.integer = value; |
269 } | |
270 | |
271 void gaim_prefs_add_string(const char *name, const char *value) { | |
272 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
273 | |
274 if(!pref) | |
275 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
276 |
5440 | 277 pref->value.string = g_strdup(value); |
278 } | |
279 | |
5561 | 280 void gaim_prefs_add_string_list(const char *name, GList *value) { |
281 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING_LIST, name); | |
282 GList *tmp; | |
283 | |
284 if(!pref) | |
285 return; | |
286 | |
287 for(tmp = value; tmp; tmp = tmp->next) | |
288 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
289 g_strdup(tmp->data)); | |
290 } | |
291 | |
5440 | 292 void remove_pref(struct gaim_pref *pref) { |
293 char *name; | |
294 | |
295 if(!pref || pref == &prefs) | |
296 return; | |
297 | |
298 if(pref->parent->first_child == pref) { | |
299 pref->parent->first_child = pref->sibling; | |
300 } else { | |
301 struct gaim_pref *sib = pref->parent->first_child; | |
302 while(sib->sibling != pref) | |
303 sib = sib->sibling; | |
304 sib->sibling = pref->sibling; | |
305 } | |
306 | |
307 name = pref_full_name(pref); | |
308 | |
309 g_hash_table_remove(prefs_hash, name); | |
310 g_free(name); | |
311 | |
312 free_pref_value(pref); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
313 |
5440 | 314 g_slist_free(pref->callbacks); |
315 g_free(pref->name); | |
316 g_free(pref); | |
317 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
318 |
5440 | 319 void gaim_prefs_remove(const char *name) { |
320 struct gaim_pref *pref = find_pref(name); | |
321 struct gaim_pref *child, *child2; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
322 |
5440 | 323 if(!pref) |
324 return; | |
325 child = pref->first_child; | |
326 while(child) { | |
327 child2 = child; | |
328 child = child->sibling; | |
329 remove_pref(child2); | |
330 } | |
331 | |
332 remove_pref(pref); | |
333 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
334 |
5440 | 335 void gaim_prefs_destroy() { |
336 gaim_prefs_remove("/"); | |
337 } | |
338 | |
339 static void do_callbacks(const char* name, struct gaim_pref *pref) { | |
340 GSList *cbs; | |
341 struct gaim_pref *cb_pref; | |
342 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
343 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
344 struct pref_cb *cb = cbs->data; | |
345 cb->func(name, pref->type, pref->value.generic, cb->data); | |
4215 | 346 } |
347 } | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
348 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
349 |
5684 | 350 void gaim_prefs_trigger_callback(const char *name) { |
351 struct gaim_pref *pref = find_pref(name); | |
352 | |
353 g_return_if_fail(pref != NULL); | |
354 do_callbacks(name, pref); | |
355 } | |
356 | |
5440 | 357 void gaim_prefs_set_generic(const char *name, gpointer value) { |
358 struct gaim_pref *pref = find_pref(name); | |
3366 | 359 |
5440 | 360 g_return_if_fail(pref != NULL); |
3500 | 361 |
5440 | 362 pref->value.generic = value; |
363 do_callbacks(name, pref); | |
3366 | 364 } |
365 | |
5440 | 366 void gaim_prefs_set_bool(const char *name, gboolean value) { |
367 struct gaim_pref *pref = find_pref(name); | |
4288 | 368 |
5533 | 369 if(pref) { |
370 g_return_if_fail(pref->type == GAIM_PREF_BOOLEAN); | |
4325 | 371 |
5533 | 372 if(pref->value.boolean != value) { |
373 pref->value.boolean = value; | |
374 do_callbacks(name, pref); | |
375 } | |
376 } else { | |
377 gaim_prefs_add_bool(name, value); | |
4288 | 378 } |
4324 | 379 } |
4325 | 380 |
5440 | 381 void gaim_prefs_set_int(const char *name, int value) { |
382 struct gaim_pref *pref = find_pref(name); | |
4325 | 383 |
5533 | 384 if(pref) { |
385 g_return_if_fail(pref->type == GAIM_PREF_INT); | |
4325 | 386 |
5533 | 387 if(pref->value.integer != value) { |
388 pref->value.integer = value; | |
389 do_callbacks(name, pref); | |
390 } | |
391 } else { | |
392 gaim_prefs_add_int(name, value); | |
5440 | 393 } |
4326 | 394 } |
395 | |
5451 | 396 void gaim_prefs_set_string(const char *name, const char *value) { |
5440 | 397 struct gaim_pref *pref = find_pref(name); |
4325 | 398 |
5533 | 399 if(pref) { |
400 g_return_if_fail(pref->type == GAIM_PREF_STRING); | |
4325 | 401 |
5533 | 402 if(strcmp(pref->value.string, value)) { |
403 g_free(pref->value.string); | |
404 pref->value.string = g_strdup(value); | |
405 do_callbacks(name, pref); | |
406 } | |
407 } else { | |
408 gaim_prefs_add_string(name, value); | |
4325 | 409 } |
410 } | |
411 | |
5561 | 412 void gaim_prefs_set_string_list(const char *name, GList *value) { |
413 struct gaim_pref *pref = find_pref(name); | |
414 if(pref) { | |
415 GList *tmp; | |
416 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
417 g_free(tmp->data); | |
418 | |
419 g_list_free(pref->value.stringlist); | |
420 pref->value.stringlist = NULL; | |
421 | |
422 for(tmp = value; tmp; tmp = tmp->next) | |
423 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
424 g_strdup(tmp->data)); | |
425 | |
426 } else { | |
427 gaim_prefs_add_string_list(name, value); | |
428 } | |
429 } | |
430 | |
5440 | 431 gpointer gaim_prefs_get_generic(const char *name) { |
432 struct gaim_pref *pref = find_pref(name); | |
4325 | 433 |
5440 | 434 g_return_val_if_fail(pref != NULL, NULL); |
4288 | 435 |
5440 | 436 return pref->value.generic; |
4288 | 437 } |
438 | |
5440 | 439 gboolean gaim_prefs_get_bool(const char *name) { |
440 struct gaim_pref *pref = find_pref(name); | |
3427 | 441 |
5440 | 442 g_return_val_if_fail(pref != NULL, FALSE); |
443 g_return_val_if_fail(pref->type == GAIM_PREF_BOOLEAN, FALSE); | |
3472 | 444 |
5440 | 445 return pref->value.boolean; |
3366 | 446 } |
447 | |
5440 | 448 int gaim_prefs_get_int(const char *name) { |
449 struct gaim_pref *pref = find_pref(name); | |
3500 | 450 |
5440 | 451 g_return_val_if_fail(pref != NULL, 0); |
452 g_return_val_if_fail(pref->type == GAIM_PREF_INT, 0); | |
3366 | 453 |
5440 | 454 return pref->value.integer; |
3366 | 455 } |
456 | |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
457 const char *gaim_prefs_get_string(const char *name) { |
5440 | 458 struct gaim_pref *pref = find_pref(name); |
3366 | 459 |
5440 | 460 g_return_val_if_fail(pref != NULL, NULL); |
461 g_return_val_if_fail(pref->type == GAIM_PREF_STRING, NULL); | |
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
462 |
5440 | 463 return pref->value.string; |
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
464 } |
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
465 |
5561 | 466 GList *gaim_prefs_get_string_list(const char *name) { |
467 struct gaim_pref *pref = find_pref(name); | |
468 GList *ret = NULL, *tmp; | |
469 | |
470 g_return_val_if_fail(pref != NULL, NULL); | |
471 g_return_val_if_fail(pref->type == GAIM_PREF_STRING_LIST, NULL); | |
472 | |
473 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
474 ret = g_list_append(ret, g_strdup(tmp->data)); | |
475 | |
476 return ret; | |
477 } | |
478 | |
5440 | 479 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
480 { | |
481 struct gaim_pref *pref = find_pref(name); | |
482 struct pref_cb *cb; | |
483 static guint cb_id = 0; | |
3366 | 484 |
5440 | 485 if(!pref) |
486 return 0; | |
3366 | 487 |
5440 | 488 cb = g_new0(struct pref_cb, 1); |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
489 |
5440 | 490 cb->func = func; |
491 cb->data = data; | |
492 cb->id = ++cb_id; | |
4991 | 493 |
5440 | 494 pref->callbacks = g_slist_append(pref->callbacks, cb); |
3366 | 495 |
5440 | 496 return cb->id; |
3366 | 497 } |
498 | |
5440 | 499 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
500 GSList *cbs; | |
501 struct gaim_pref *child; | |
2254 | 502 |
5440 | 503 if(!pref) |
504 return FALSE; | |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
505 |
5440 | 506 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
507 struct pref_cb *cb = cbs->data; | |
508 if(cb->id == callback_id) { | |
509 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
510 g_free(cb); | |
511 return TRUE; | |
4428 | 512 } |
513 } | |
514 | |
5440 | 515 for(child = pref->first_child; child; child = child->sibling) { |
516 if(disco_callback_helper(child, callback_id)) | |
517 return TRUE; | |
4428 | 518 } |
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
519 |
5440 | 520 return FALSE; |
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
521 } |
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
522 |
5440 | 523 void gaim_prefs_disconnect_callback(guint callback_id) { |
524 disco_callback_helper(&prefs, callback_id); | |
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
525 } |
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
526 |
5440 | 527 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
528 struct gaim_pref *tmp; | |
529 char *esc; | |
530 int i; | |
3500 | 531 |
5440 | 532 if(!pref) { |
533 pref = &prefs; | |
3551 | 534 |
5440 | 535 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
536 fprintf(f, "<pref name='/'"); | |
537 } else { | |
538 for(i=0; i<depth; i++) | |
539 fprintf(f, "\t"); | |
540 esc = g_markup_escape_text(pref->name, -1); | |
541 fprintf(f, "<pref name='%s'", esc); | |
542 g_free(esc); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
543 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
544 |
5440 | 545 switch(pref->type) { |
546 case GAIM_PREF_NONE: | |
547 break; | |
548 case GAIM_PREF_BOOLEAN: | |
549 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
550 break; | |
551 case GAIM_PREF_INT: | |
552 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
553 break; | |
554 case GAIM_PREF_STRING: | |
555 esc = g_markup_escape_text(pref->value.string, -1); | |
556 fprintf(f, " type='string' value='%s'", esc); | |
557 g_free(esc); | |
558 break; | |
5561 | 559 case GAIM_PREF_STRING_LIST: |
560 fprintf(f, " type='stringlist'"); | |
561 break; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
562 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
563 |
5561 | 564 if(pref->first_child || pref->type == GAIM_PREF_STRING_LIST) { |
5440 | 565 fprintf(f, ">\n"); |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
566 |
5440 | 567 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
568 gaim_prefs_write(f, tmp, depth+1); | |
5561 | 569 |
570 if(pref->type == GAIM_PREF_STRING_LIST) { | |
571 GList *tmp2; | |
572 for(tmp2 = pref->value.stringlist; tmp2; tmp2 = tmp2->next) { | |
573 for(i=0; i<depth+1; i++) | |
574 fprintf(f, "\t"); | |
575 esc = g_markup_escape_text(tmp2->data, -1); | |
576 fprintf(f, "<item value='%s' />\n", esc); | |
577 g_free(esc); | |
578 } | |
579 } | |
580 | |
5440 | 581 for(i=0; i<depth; i++) |
582 fprintf(f, "\t"); | |
583 fprintf(f, "</pref>\n"); | |
584 } else { | |
585 fprintf(f, " />\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
586 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
587 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
588 |
5440 | 589 void gaim_prefs_sync() { |
590 FILE *file; | |
591 const char *user_dir = gaim_user_dir(); | |
592 char *filename; | |
593 char *filename_real; | |
3551 | 594 |
5534 | 595 if(!prefs_is_loaded) { |
596 gaim_debug(GAIM_DEBUG_WARNING, "prefs", "prefs saved before loading! scheduling save.\n"); | |
597 schedule_prefs_save(); /* schedule a save for after we read in */ | |
598 return; | |
599 } | |
600 | |
5440 | 601 if(!user_dir) |
602 return; | |
3551 | 603 |
5534 | 604 gaim_debug(GAIM_DEBUG_INFO, "prefs", "writing prefs out to disk.\n"); |
605 | |
5440 | 606 file = fopen(user_dir, "r"); |
607 if(!file) | |
608 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
609 else | |
610 fclose(file); | |
3551 | 611 |
5440 | 612 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
3551 | 613 |
5440 | 614 if((file = fopen(filename, "w"))) { |
615 gaim_prefs_write(file, NULL, 0); | |
616 fclose(file); | |
617 chmod(filename, S_IRUSR | S_IWUSR); | |
618 } else { | |
619 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
620 filename); | |
621 } | |
3551 | 622 |
5440 | 623 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
624 if(rename(filename, filename_real) < 0) | |
625 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
626 filename, filename_real); | |
3551 | 627 |
5440 | 628 g_free(filename); |
629 g_free(filename_real); | |
3551 | 630 } |
631 | |
5440 | 632 static GList *prefs_stack = NULL; |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
633 |
5440 | 634 static void prefs_start_element_handler (GMarkupParseContext *context, |
635 const gchar *element_name, | |
636 const gchar **attribute_names, | |
637 const gchar **attribute_values, | |
638 gpointer user_data, | |
639 GError **error) { | |
640 GaimPrefType pref_type = GAIM_PREF_NONE; | |
641 int i; | |
642 const char *pref_name = NULL, *pref_value = NULL; | |
643 GString *pref_name_full; | |
644 GList *tmp; | |
3366 | 645 |
5561 | 646 if(strcmp(element_name, "pref") && strcmp(element_name, "item")) |
5440 | 647 return; |
3500 | 648 |
5440 | 649 for(i = 0; attribute_names[i]; i++) { |
650 if(!strcmp(attribute_names[i], "name")) { | |
651 pref_name = attribute_values[i]; | |
652 } else if(!strcmp(attribute_names[i], "type")) { | |
653 if(!strcmp(attribute_values[i], "bool")) | |
654 pref_type = GAIM_PREF_BOOLEAN; | |
655 else if(!strcmp(attribute_values[i], "int")) | |
656 pref_type = GAIM_PREF_INT; | |
657 else if(!strcmp(attribute_values[i], "string")) | |
658 pref_type = GAIM_PREF_STRING; | |
5561 | 659 else if(!strcmp(attribute_values[i], "stringlist")) |
660 pref_type = GAIM_PREF_STRING_LIST; | |
5440 | 661 else |
662 return; | |
663 } else if(!strcmp(attribute_names[i], "value")) { | |
664 pref_value = attribute_values[i]; | |
665 } | |
666 } | |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
667 |
5561 | 668 if(!strcmp(element_name, "item")) { |
669 struct gaim_pref *pref = find_pref(prefs_stack->data); | |
670 if(pref) { | |
671 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
672 g_strdup(pref_value)); | |
673 } | |
5440 | 674 return; |
5561 | 675 } else if(!pref_name || !strcmp(pref_name, "/")) { |
676 return; | |
677 } | |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
678 |
5440 | 679 pref_name_full = g_string_new(pref_name); |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
680 |
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
681 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
5440 | 682 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
683 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
684 } | |
1170 | 685 |
5440 | 686 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
687 |
5533 | 688 switch(pref_type) { |
689 case GAIM_PREF_NONE: | |
690 break; | |
691 case GAIM_PREF_BOOLEAN: | |
692 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
693 break; | |
694 case GAIM_PREF_INT: | |
695 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
696 break; | |
697 case GAIM_PREF_STRING: | |
698 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
699 break; | |
5561 | 700 case GAIM_PREF_STRING_LIST: |
701 break; | |
5440 | 702 } |
1170 | 703 |
5440 | 704 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
705 g_string_free(pref_name_full, TRUE); | |
1170 | 706 } |
707 | |
5440 | 708 static void prefs_end_element_handler(GMarkupParseContext *context, |
709 const gchar *element_name, gpointer user_data, GError **error) { | |
710 if(!strcmp(element_name, "pref")) { | |
711 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
712 } | |
1170 | 713 } |
714 | |
5440 | 715 static GMarkupParser prefs_parser = { |
716 prefs_start_element_handler, | |
717 prefs_end_element_handler, | |
718 NULL, | |
719 NULL, | |
720 NULL | |
721 }; | |
1170 | 722 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
723 gboolean gaim_prefs_load() { |
5440 | 724 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); |
725 gchar *contents = NULL; | |
726 gsize length; | |
727 GMarkupParseContext *context; | |
728 GError *error = NULL; | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
729 |
5534 | 730 |
731 if(!filename) { | |
732 prefs_is_loaded = TRUE; | |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
733 return FALSE; |
5534 | 734 } |
5440 | 735 |
736 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
737 |
5440 | 738 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
739 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
740 error->message); | |
741 g_error_free(error); | |
5534 | 742 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
743 return FALSE; |
1170 | 744 } |
745 | |
5440 | 746 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
747 | |
748 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
749 g_markup_parse_context_free(context); | |
750 g_free(contents); | |
5534 | 751 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
752 return FALSE; |
5440 | 753 } |
754 | |
755 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
756 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
757 g_markup_parse_context_free(context); | |
758 g_free(contents); | |
5534 | 759 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
760 return FALSE; |
5440 | 761 } |
762 | |
763 g_markup_parse_context_free(context); | |
764 g_free(contents); | |
765 | |
766 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
767 g_free(filename); | |
5534 | 768 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
769 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
770 return TRUE; |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
771 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
772 |