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