Mercurial > pidgin.yaz
annotate src/prefs.c @ 5545:7a64114641c3
[gaim-migrate @ 5946]
I've been rewriting the prefs to move from gaimrc to prefs.xml. This will
NOT compile! I don't want to see complaints about this, as it's CVS, and
you should only be using it if you can put up with things like this. Also,
don't ask how long it'll take until it compiles again. It may be several
days. This is a big migration. Even when it works, it may not run right.
However, it's made a lot of progress, and I plan to actively work on it
today and tomorrow.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Thu, 29 May 2003 19:10:24 +0000 |
parents | de09863bd4b5 |
children | 90a67ea697f7 |
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; | |
54 } value; | |
55 GSList *callbacks; | |
56 struct gaim_pref *parent; | |
57 struct gaim_pref *sibling; | |
58 struct gaim_pref *first_child; | |
59 }; | |
3366 | 60 |
5440 | 61 static GHashTable *prefs_hash = NULL; |
62 | |
63 static struct gaim_pref prefs = { GAIM_PREF_NONE, NULL, {NULL}, NULL, | |
64 NULL, NULL, NULL }; | |
65 | |
5534 | 66 static guint prefs_save_timer = 0; |
67 static gboolean prefs_is_loaded = FALSE; | |
68 | |
69 | |
70 static gboolean prefs_save_callback(gpointer who_cares) { | |
71 gaim_prefs_sync(); | |
72 prefs_save_timer = 0; | |
73 return FALSE; | |
74 } | |
75 | |
76 static void schedule_prefs_save() { | |
77 if(!prefs_save_timer) | |
78 prefs_save_timer = g_timeout_add(5000, prefs_save_callback, NULL); | |
79 } | |
80 | |
81 static void prefs_save_cb(const char *name, GaimPrefType type, gpointer val, | |
82 gpointer user_data) { | |
83 | |
84 if(!prefs_is_loaded) | |
85 return; | |
86 | |
87 gaim_debug(GAIM_DEBUG_MISC, "prefs", "%s changed, scheduling save.\n", name); | |
88 | |
89 schedule_prefs_save(); | |
90 } | |
91 | |
5440 | 92 void gaim_prefs_init() { |
93 prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
94 | |
5534 | 95 gaim_prefs_connect_callback("/", prefs_save_cb, NULL); |
96 | |
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
97 gaim_prefs_add_none("/core"); |
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
98 |
5440 | 99 /* 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
|
100 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
101 /* Away */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
102 gaim_prefs_add_none("/core/away"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
103 gaim_prefs_add_bool("/core/away/queue_messages", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
104 gaim_prefs_add_bool("/core/away/away_when_idle", TRUE); |
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 -> Auto Response */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
107 gaim_prefs_add_none("/core/away/auto_response"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
108 gaim_prefs_add_bool("/core/away/auto_response/enabled", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
109 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
|
110 gaim_prefs_add_bool("/core/away/auto_response/idle_only", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
111 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
112 /* Buddies */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
113 gaim_prefs_add_none("/core/buddies"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
114 gaim_prefs_add_bool("/core/buddies/use_server_alias", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
115 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
116 /* Conversations */ |
5539
de09863bd4b5
[gaim-migrate @ 5939]
Christian Hammond <chipx86@chipx86.com>
parents:
5534
diff
changeset
|
117 gaim_prefs_add_none("/core/conversations"); |
de09863bd4b5
[gaim-migrate @ 5939]
Christian Hammond <chipx86@chipx86.com>
parents:
5534
diff
changeset
|
118 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
|
119 gaim_prefs_add_bool("/core/conversations/away_back_on_send", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
120 gaim_prefs_add_bool("/core/conversations/use_alias_for_title", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
121 gaim_prefs_add_bool("/core/conversations/combine_chat_im", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
122 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
123 /* Conversations -> Chat */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
124 gaim_prefs_add_none("/core/conversations/chat"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
125 gaim_prefs_add_bool("/core/conversations/chat/show_join", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
126 gaim_prefs_add_bool("/core/conversations/chat/show_leave", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
127 gaim_prefs_add_bool("/core/conversations/chat/show_nick_change", TRUE); |
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 -> IM */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
130 gaim_prefs_add_none("/core/conversations/im"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
131 gaim_prefs_add_bool("/core/conversations/im/show_login", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
132 gaim_prefs_add_bool("/core/conversations/im/show_logoff", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
133 gaim_prefs_add_bool("/core/conversations/im/send_typing", 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 /* Proxy */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
136 gaim_prefs_add_none("/core/proxy"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
137 gaim_prefs_add_int("/core/proxy/type"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
138 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
139 /* Sound */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
140 gaim_prefs_add_none("/core/sound"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
141 gaim_prefs_add_bool("/core/sound/command", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
142 gaim_prefs_add_bool("/core/sound/use_arts", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
143 gaim_prefs_add_bool("/core/sound/use_beep", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
144 gaim_prefs_add_bool("/core/sound/use_custom", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
145 gaim_prefs_add_bool("/core/sound/use_esd", FALSE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
146 gaim_prefs_add_bool("/core/sound/use_nas", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
147 gaim_prefs_add_bool("/core/sound/use_sys_default", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
148 gaim_prefs_add_bool("/core/sound/while_away", FALSE); |
5440 | 149 } |
3366 | 150 |
5440 | 151 static char *pref_full_name(struct gaim_pref *pref) { |
152 GString *name; | |
153 struct gaim_pref *parent; | |
154 if(!pref) | |
155 return NULL; | |
156 | |
157 if(pref == &prefs) | |
158 return g_strdup("/"); | |
159 | |
160 name = g_string_new(pref->name); | |
161 parent = pref->parent; | |
3366 | 162 |
5440 | 163 for(parent = pref->parent; parent && parent->name; parent = parent->parent) { |
164 name = g_string_prepend_c(name, '/'); | |
165 name = g_string_prepend(name, parent->name); | |
166 } | |
167 g_string_free(name, FALSE); | |
168 return name->str; | |
169 } | |
170 | |
171 static struct gaim_pref *find_pref(const char *name) | |
172 { | |
173 if(!name || name[0] != '/') { | |
174 return NULL; | |
175 } else if(name[1] == '\0') { | |
176 return &prefs; | |
177 } else { | |
178 return g_hash_table_lookup(prefs_hash, name); | |
179 } | |
180 } | |
181 | |
182 static struct gaim_pref *find_pref_parent(const char *name) | |
183 { | |
184 char *parent_name = g_path_get_dirname(name); | |
185 struct gaim_pref *ret = &prefs; | |
186 | |
187 if(strcmp(parent_name, "/")) { | |
188 ret = find_pref(parent_name); | |
189 } | |
1026 | 190 |
5440 | 191 g_free(parent_name); |
192 return ret; | |
193 } | |
194 | |
195 static void free_pref_value(struct gaim_pref *pref) { | |
196 switch(pref->type) { | |
197 case GAIM_PREF_BOOLEAN: | |
198 pref->value.boolean = FALSE; | |
199 case GAIM_PREF_INT: | |
200 pref->value.integer = 0; | |
201 break; | |
202 case GAIM_PREF_STRING: | |
203 g_free(pref->value.string); | |
204 pref->value.string = NULL; | |
205 break; | |
206 case GAIM_PREF_NONE: | |
207 break; | |
208 } | |
209 } | |
210 | |
211 static struct gaim_pref *add_pref(GaimPrefType type, const char *name) { | |
212 struct gaim_pref *parent; | |
213 struct gaim_pref *me; | |
214 struct gaim_pref *sibling; | |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
215 char *my_name; |
5440 | 216 |
217 parent = find_pref_parent(name); | |
218 | |
219 if(!parent) | |
220 return NULL; | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
221 |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
222 my_name = g_path_get_basename(name); |
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
223 |
5440 | 224 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { |
225 if(!strcmp(sibling->name, my_name)) { | |
226 g_free(my_name); | |
227 return NULL; | |
228 } | |
229 } | |
230 | |
231 me = g_new0(struct gaim_pref, 1); | |
232 me->type = type; | |
233 me->name = my_name; | |
234 | |
235 me->parent = parent; | |
236 if(parent->first_child) { | |
237 /* blatant abuse of a for loop */ | |
238 for(sibling = parent->first_child; sibling->sibling; | |
239 sibling = sibling->sibling); | |
240 sibling->sibling = me; | |
241 } else { | |
242 parent->first_child = me; | |
243 } | |
244 | |
245 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
246 | |
247 return me; | |
248 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
249 |
5440 | 250 void gaim_prefs_add_none(const char *name) { |
251 add_pref(GAIM_PREF_NONE, name); | |
252 } | |
253 | |
254 void gaim_prefs_add_bool(const char *name, gboolean value) { | |
255 struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
256 | |
257 if(!pref) | |
258 return; | |
259 | |
260 pref->value.boolean = value; | |
261 } | |
3630 | 262 |
5440 | 263 void gaim_prefs_add_int(const char *name, int value) { |
264 struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); | |
265 | |
266 if(!pref) | |
267 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
268 |
5440 | 269 pref->value.integer = value; |
270 } | |
271 | |
272 void gaim_prefs_add_string(const char *name, const char *value) { | |
273 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
274 | |
275 if(!pref) | |
276 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
277 |
5440 | 278 pref->value.string = g_strdup(value); |
279 } | |
280 | |
281 void remove_pref(struct gaim_pref *pref) { | |
282 char *name; | |
283 | |
284 if(!pref || pref == &prefs) | |
285 return; | |
286 | |
287 if(pref->parent->first_child == pref) { | |
288 pref->parent->first_child = pref->sibling; | |
289 } else { | |
290 struct gaim_pref *sib = pref->parent->first_child; | |
291 while(sib->sibling != pref) | |
292 sib = sib->sibling; | |
293 sib->sibling = pref->sibling; | |
294 } | |
295 | |
296 name = pref_full_name(pref); | |
297 | |
298 g_hash_table_remove(prefs_hash, name); | |
299 g_free(name); | |
300 | |
301 free_pref_value(pref); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
302 |
5440 | 303 g_slist_free(pref->callbacks); |
304 g_free(pref->name); | |
305 g_free(pref); | |
306 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
307 |
5440 | 308 void gaim_prefs_remove(const char *name) { |
309 struct gaim_pref *pref = find_pref(name); | |
310 struct gaim_pref *child, *child2; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
311 |
5440 | 312 if(!pref) |
313 return; | |
314 child = pref->first_child; | |
315 while(child) { | |
316 child2 = child; | |
317 child = child->sibling; | |
318 remove_pref(child2); | |
319 } | |
320 | |
321 remove_pref(pref); | |
322 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
323 |
5440 | 324 void gaim_prefs_destroy() { |
325 gaim_prefs_remove("/"); | |
326 } | |
327 | |
328 static void do_callbacks(const char* name, struct gaim_pref *pref) { | |
329 GSList *cbs; | |
330 struct gaim_pref *cb_pref; | |
331 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
332 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
333 struct pref_cb *cb = cbs->data; | |
334 cb->func(name, pref->type, pref->value.generic, cb->data); | |
4215 | 335 } |
336 } | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
337 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
338 |
5440 | 339 void gaim_prefs_set_generic(const char *name, gpointer value) { |
340 struct gaim_pref *pref = find_pref(name); | |
3366 | 341 |
5440 | 342 g_return_if_fail(pref != NULL); |
3500 | 343 |
5440 | 344 pref->value.generic = value; |
345 do_callbacks(name, pref); | |
3366 | 346 } |
347 | |
5440 | 348 void gaim_prefs_set_bool(const char *name, gboolean value) { |
349 struct gaim_pref *pref = find_pref(name); | |
4288 | 350 |
5533 | 351 if(pref) { |
352 g_return_if_fail(pref->type == GAIM_PREF_BOOLEAN); | |
4325 | 353 |
5533 | 354 if(pref->value.boolean != value) { |
355 pref->value.boolean = value; | |
356 do_callbacks(name, pref); | |
357 } | |
358 } else { | |
359 gaim_prefs_add_bool(name, value); | |
4288 | 360 } |
4324 | 361 } |
4325 | 362 |
5440 | 363 void gaim_prefs_set_int(const char *name, int value) { |
364 struct gaim_pref *pref = find_pref(name); | |
4325 | 365 |
5533 | 366 if(pref) { |
367 g_return_if_fail(pref->type == GAIM_PREF_INT); | |
4325 | 368 |
5533 | 369 if(pref->value.integer != value) { |
370 pref->value.integer = value; | |
371 do_callbacks(name, pref); | |
372 } | |
373 } else { | |
374 gaim_prefs_add_int(name, value); | |
5440 | 375 } |
4326 | 376 } |
377 | |
5451 | 378 void gaim_prefs_set_string(const char *name, const char *value) { |
5440 | 379 struct gaim_pref *pref = find_pref(name); |
4325 | 380 |
5533 | 381 if(pref) { |
382 g_return_if_fail(pref->type == GAIM_PREF_STRING); | |
4325 | 383 |
5533 | 384 if(strcmp(pref->value.string, value)) { |
385 g_free(pref->value.string); | |
386 pref->value.string = g_strdup(value); | |
387 do_callbacks(name, pref); | |
388 } | |
389 } else { | |
390 gaim_prefs_add_string(name, value); | |
4325 | 391 } |
392 } | |
393 | |
5440 | 394 gpointer gaim_prefs_get_generic(const char *name) { |
395 struct gaim_pref *pref = find_pref(name); | |
4325 | 396 |
5440 | 397 g_return_val_if_fail(pref != NULL, NULL); |
4288 | 398 |
5440 | 399 return pref->value.generic; |
4288 | 400 } |
401 | |
5440 | 402 gboolean gaim_prefs_get_bool(const char *name) { |
403 struct gaim_pref *pref = find_pref(name); | |
3427 | 404 |
5440 | 405 g_return_val_if_fail(pref != NULL, FALSE); |
406 g_return_val_if_fail(pref->type == GAIM_PREF_BOOLEAN, FALSE); | |
3472 | 407 |
5440 | 408 return pref->value.boolean; |
3366 | 409 } |
410 | |
5440 | 411 int gaim_prefs_get_int(const char *name) { |
412 struct gaim_pref *pref = find_pref(name); | |
3500 | 413 |
5440 | 414 g_return_val_if_fail(pref != NULL, 0); |
415 g_return_val_if_fail(pref->type == GAIM_PREF_INT, 0); | |
3366 | 416 |
5440 | 417 return pref->value.integer; |
3366 | 418 } |
419 | |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
420 const char *gaim_prefs_get_string(const char *name) { |
5440 | 421 struct gaim_pref *pref = find_pref(name); |
3366 | 422 |
5440 | 423 g_return_val_if_fail(pref != NULL, NULL); |
424 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
|
425 |
5440 | 426 return pref->value.string; |
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
427 } |
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
428 |
5440 | 429 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
430 { | |
431 struct gaim_pref *pref = find_pref(name); | |
432 struct pref_cb *cb; | |
433 static guint cb_id = 0; | |
3366 | 434 |
5440 | 435 if(!pref) |
436 return 0; | |
3366 | 437 |
5440 | 438 cb = g_new0(struct pref_cb, 1); |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
439 |
5440 | 440 cb->func = func; |
441 cb->data = data; | |
442 cb->id = ++cb_id; | |
4991 | 443 |
5440 | 444 pref->callbacks = g_slist_append(pref->callbacks, cb); |
3366 | 445 |
5440 | 446 return cb->id; |
3366 | 447 } |
448 | |
5440 | 449 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
450 GSList *cbs; | |
451 struct gaim_pref *child; | |
2254 | 452 |
5440 | 453 if(!pref) |
454 return FALSE; | |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
455 |
5440 | 456 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
457 struct pref_cb *cb = cbs->data; | |
458 if(cb->id == callback_id) { | |
459 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
460 g_free(cb); | |
461 return TRUE; | |
4428 | 462 } |
463 } | |
464 | |
5440 | 465 for(child = pref->first_child; child; child = child->sibling) { |
466 if(disco_callback_helper(child, callback_id)) | |
467 return TRUE; | |
4428 | 468 } |
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
469 |
5440 | 470 return FALSE; |
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
471 } |
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
472 |
5440 | 473 void gaim_prefs_disconnect_callback(guint callback_id) { |
474 disco_callback_helper(&prefs, callback_id); | |
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
475 } |
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
476 |
5440 | 477 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
478 struct gaim_pref *tmp; | |
479 char *esc; | |
480 int i; | |
3500 | 481 |
5440 | 482 if(!pref) { |
483 pref = &prefs; | |
3551 | 484 |
5440 | 485 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
486 fprintf(f, "<pref name='/'"); | |
487 } else { | |
488 for(i=0; i<depth; i++) | |
489 fprintf(f, "\t"); | |
490 esc = g_markup_escape_text(pref->name, -1); | |
491 fprintf(f, "<pref name='%s'", esc); | |
492 g_free(esc); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
493 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
494 |
5440 | 495 switch(pref->type) { |
496 case GAIM_PREF_NONE: | |
497 break; | |
498 case GAIM_PREF_BOOLEAN: | |
499 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
500 break; | |
501 case GAIM_PREF_INT: | |
502 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
503 break; | |
504 case GAIM_PREF_STRING: | |
505 esc = g_markup_escape_text(pref->value.string, -1); | |
506 fprintf(f, " type='string' value='%s'", esc); | |
507 g_free(esc); | |
508 break; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
509 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
510 |
5440 | 511 if(pref->first_child) { |
512 fprintf(f, ">\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
513 |
5440 | 514 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
515 gaim_prefs_write(f, tmp, depth+1); | |
516 for(i=0; i<depth; i++) | |
517 fprintf(f, "\t"); | |
518 fprintf(f, "</pref>\n"); | |
519 } else { | |
520 fprintf(f, " />\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
521 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
522 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
523 |
5440 | 524 void gaim_prefs_sync() { |
525 FILE *file; | |
526 const char *user_dir = gaim_user_dir(); | |
527 char *filename; | |
528 char *filename_real; | |
3551 | 529 |
5534 | 530 if(!prefs_is_loaded) { |
531 gaim_debug(GAIM_DEBUG_WARNING, "prefs", "prefs saved before loading! scheduling save.\n"); | |
532 schedule_prefs_save(); /* schedule a save for after we read in */ | |
533 return; | |
534 } | |
535 | |
5440 | 536 if(!user_dir) |
537 return; | |
3551 | 538 |
5534 | 539 gaim_debug(GAIM_DEBUG_INFO, "prefs", "writing prefs out to disk.\n"); |
540 | |
5440 | 541 file = fopen(user_dir, "r"); |
542 if(!file) | |
543 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
544 else | |
545 fclose(file); | |
3551 | 546 |
5440 | 547 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
3551 | 548 |
5440 | 549 if((file = fopen(filename, "w"))) { |
550 gaim_prefs_write(file, NULL, 0); | |
551 fclose(file); | |
552 chmod(filename, S_IRUSR | S_IWUSR); | |
553 } else { | |
554 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
555 filename); | |
556 } | |
3551 | 557 |
5440 | 558 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
559 if(rename(filename, filename_real) < 0) | |
560 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
561 filename, filename_real); | |
3551 | 562 |
5440 | 563 g_free(filename); |
564 g_free(filename_real); | |
3551 | 565 } |
566 | |
5440 | 567 static GList *prefs_stack = NULL; |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
568 |
5440 | 569 static void prefs_start_element_handler (GMarkupParseContext *context, |
570 const gchar *element_name, | |
571 const gchar **attribute_names, | |
572 const gchar **attribute_values, | |
573 gpointer user_data, | |
574 GError **error) { | |
575 GaimPrefType pref_type = GAIM_PREF_NONE; | |
576 int i; | |
577 const char *pref_name = NULL, *pref_value = NULL; | |
578 GString *pref_name_full; | |
579 GList *tmp; | |
3366 | 580 |
5440 | 581 if(strcmp(element_name, "pref")) |
582 return; | |
3500 | 583 |
5440 | 584 for(i = 0; attribute_names[i]; i++) { |
585 if(!strcmp(attribute_names[i], "name")) { | |
586 pref_name = attribute_values[i]; | |
587 } else if(!strcmp(attribute_names[i], "type")) { | |
588 if(!strcmp(attribute_values[i], "bool")) | |
589 pref_type = GAIM_PREF_BOOLEAN; | |
590 else if(!strcmp(attribute_values[i], "int")) | |
591 pref_type = GAIM_PREF_INT; | |
592 else if(!strcmp(attribute_values[i], "string")) | |
593 pref_type = GAIM_PREF_STRING; | |
594 else | |
595 return; | |
596 } else if(!strcmp(attribute_names[i], "value")) { | |
597 pref_value = attribute_values[i]; | |
598 } | |
599 } | |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
600 |
5440 | 601 if(!pref_name || !strcmp(pref_name, "/")) |
602 return; | |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
603 |
5440 | 604 pref_name_full = g_string_new(pref_name); |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
605 |
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
606 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
5440 | 607 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
608 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
609 } | |
1170 | 610 |
5440 | 611 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
612 |
5533 | 613 switch(pref_type) { |
614 case GAIM_PREF_NONE: | |
615 break; | |
616 case GAIM_PREF_BOOLEAN: | |
617 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
618 break; | |
619 case GAIM_PREF_INT: | |
620 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
621 break; | |
622 case GAIM_PREF_STRING: | |
623 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
624 break; | |
5440 | 625 } |
1170 | 626 |
5440 | 627 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
628 g_string_free(pref_name_full, TRUE); | |
1170 | 629 } |
630 | |
5440 | 631 static void prefs_end_element_handler(GMarkupParseContext *context, |
632 const gchar *element_name, gpointer user_data, GError **error) { | |
633 if(!strcmp(element_name, "pref")) { | |
634 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
635 } | |
1170 | 636 } |
637 | |
5440 | 638 static GMarkupParser prefs_parser = { |
639 prefs_start_element_handler, | |
640 prefs_end_element_handler, | |
641 NULL, | |
642 NULL, | |
643 NULL | |
644 }; | |
1170 | 645 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
646 gboolean gaim_prefs_load() { |
5440 | 647 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); |
648 gchar *contents = NULL; | |
649 gsize length; | |
650 GMarkupParseContext *context; | |
651 GError *error = NULL; | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
652 |
5534 | 653 |
654 if(!filename) { | |
655 prefs_is_loaded = TRUE; | |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
656 return FALSE; |
5534 | 657 } |
5440 | 658 |
659 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
660 |
5440 | 661 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
662 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
663 error->message); | |
664 g_error_free(error); | |
5534 | 665 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
666 return FALSE; |
1170 | 667 } |
668 | |
5440 | 669 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
670 | |
671 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
672 g_markup_parse_context_free(context); | |
673 g_free(contents); | |
5534 | 674 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
675 return FALSE; |
5440 | 676 } |
677 | |
678 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
679 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
680 g_markup_parse_context_free(context); | |
681 g_free(contents); | |
5534 | 682 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
683 return FALSE; |
5440 | 684 } |
685 | |
686 g_markup_parse_context_free(context); | |
687 g_free(contents); | |
688 | |
689 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
690 g_free(filename); | |
5534 | 691 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
692 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
693 return TRUE; |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
694 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
695 |