Mercurial > pidgin
annotate src/prefs.c @ 5582:e2f2d8857f9f
[gaim-migrate @ 5986]
(00:54:06) Robot101: fixes:
(00:54:21) Robot101: "copy link location" on URL context menu
(00:54:37) Robot101: leak of a GtkWindow and annoying dialog hinting of the signon meter window
(00:55:06) Robot101: and the appending of even when you cancel the dialog in the convo window
(00:55:07) Robot101: that's the lot
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Sat, 31 May 2003 04:55:24 +0000 |
parents | 3a9b54f260e3 |
children | 57165429ed73 |
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/show_logoff", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
139 gaim_prefs_add_bool("/core/conversations/im/send_typing", TRUE); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
140 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
141 /* Proxy */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
142 gaim_prefs_add_none("/core/proxy"); |
5551
51699de873af
[gaim-migrate @ 5952]
Christian Hammond <chipx86@chipx86.com>
parents:
5550
diff
changeset
|
143 gaim_prefs_add_string("/core/proxy/type", "none"); |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
144 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
145 /* Sound */ |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
146 gaim_prefs_add_none("/core/sound"); |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
147 gaim_prefs_add_bool("/core/sound/while_away", FALSE); |
5440 | 148 } |
3366 | 149 |
5440 | 150 static char *pref_full_name(struct gaim_pref *pref) { |
151 GString *name; | |
152 struct gaim_pref *parent; | |
153 if(!pref) | |
154 return NULL; | |
155 | |
156 if(pref == &prefs) | |
157 return g_strdup("/"); | |
158 | |
159 name = g_string_new(pref->name); | |
160 parent = pref->parent; | |
3366 | 161 |
5440 | 162 for(parent = pref->parent; parent && parent->name; parent = parent->parent) { |
163 name = g_string_prepend_c(name, '/'); | |
164 name = g_string_prepend(name, parent->name); | |
165 } | |
166 g_string_free(name, FALSE); | |
167 return name->str; | |
168 } | |
169 | |
170 static struct gaim_pref *find_pref(const char *name) | |
171 { | |
172 if(!name || name[0] != '/') { | |
173 return NULL; | |
174 } else if(name[1] == '\0') { | |
175 return &prefs; | |
176 } else { | |
177 return g_hash_table_lookup(prefs_hash, name); | |
178 } | |
179 } | |
180 | |
181 static struct gaim_pref *find_pref_parent(const char *name) | |
182 { | |
183 char *parent_name = g_path_get_dirname(name); | |
184 struct gaim_pref *ret = &prefs; | |
185 | |
186 if(strcmp(parent_name, "/")) { | |
187 ret = find_pref(parent_name); | |
188 } | |
1026 | 189 |
5440 | 190 g_free(parent_name); |
191 return ret; | |
192 } | |
193 | |
194 static void free_pref_value(struct gaim_pref *pref) { | |
195 switch(pref->type) { | |
196 case GAIM_PREF_BOOLEAN: | |
197 pref->value.boolean = FALSE; | |
198 case GAIM_PREF_INT: | |
199 pref->value.integer = 0; | |
200 break; | |
201 case GAIM_PREF_STRING: | |
202 g_free(pref->value.string); | |
203 pref->value.string = NULL; | |
204 break; | |
5561 | 205 case GAIM_PREF_STRING_LIST: |
206 { | |
207 GList *tmp; | |
208 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
209 g_free(tmp->data); | |
210 | |
211 g_list_free(pref->value.stringlist); | |
212 } break; | |
5440 | 213 case GAIM_PREF_NONE: |
214 break; | |
215 } | |
216 } | |
217 | |
218 static struct gaim_pref *add_pref(GaimPrefType type, const char *name) { | |
219 struct gaim_pref *parent; | |
220 struct gaim_pref *me; | |
221 struct gaim_pref *sibling; | |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
222 char *my_name; |
5440 | 223 |
224 parent = find_pref_parent(name); | |
225 | |
226 if(!parent) | |
227 return NULL; | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
228 |
5458
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
229 my_name = g_path_get_basename(name); |
156e65ca910f
[gaim-migrate @ 5846]
Christian Hammond <chipx86@chipx86.com>
parents:
5451
diff
changeset
|
230 |
5440 | 231 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { |
232 if(!strcmp(sibling->name, my_name)) { | |
233 g_free(my_name); | |
234 return NULL; | |
235 } | |
236 } | |
237 | |
238 me = g_new0(struct gaim_pref, 1); | |
239 me->type = type; | |
240 me->name = my_name; | |
241 | |
242 me->parent = parent; | |
243 if(parent->first_child) { | |
244 /* blatant abuse of a for loop */ | |
245 for(sibling = parent->first_child; sibling->sibling; | |
246 sibling = sibling->sibling); | |
247 sibling->sibling = me; | |
248 } else { | |
249 parent->first_child = me; | |
250 } | |
251 | |
252 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
253 | |
254 return me; | |
255 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
256 |
5440 | 257 void gaim_prefs_add_none(const char *name) { |
258 add_pref(GAIM_PREF_NONE, name); | |
259 } | |
260 | |
261 void gaim_prefs_add_bool(const char *name, gboolean value) { | |
262 struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
263 | |
264 if(!pref) | |
265 return; | |
266 | |
267 pref->value.boolean = value; | |
268 } | |
3630 | 269 |
5440 | 270 void gaim_prefs_add_int(const char *name, int value) { |
271 struct gaim_pref *pref = add_pref(GAIM_PREF_INT, 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.integer = value; |
277 } | |
278 | |
279 void gaim_prefs_add_string(const char *name, const char *value) { | |
280 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
281 | |
282 if(!pref) | |
283 return; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
284 |
5440 | 285 pref->value.string = g_strdup(value); |
286 } | |
287 | |
5561 | 288 void gaim_prefs_add_string_list(const char *name, GList *value) { |
289 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING_LIST, name); | |
290 GList *tmp; | |
291 | |
292 if(!pref) | |
293 return; | |
294 | |
295 for(tmp = value; tmp; tmp = tmp->next) | |
296 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
297 g_strdup(tmp->data)); | |
298 } | |
299 | |
5440 | 300 void remove_pref(struct gaim_pref *pref) { |
301 char *name; | |
302 | |
303 if(!pref || pref == &prefs) | |
304 return; | |
305 | |
306 if(pref->parent->first_child == pref) { | |
307 pref->parent->first_child = pref->sibling; | |
308 } else { | |
309 struct gaim_pref *sib = pref->parent->first_child; | |
310 while(sib->sibling != pref) | |
311 sib = sib->sibling; | |
312 sib->sibling = pref->sibling; | |
313 } | |
314 | |
315 name = pref_full_name(pref); | |
316 | |
317 g_hash_table_remove(prefs_hash, name); | |
318 g_free(name); | |
319 | |
320 free_pref_value(pref); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
321 |
5440 | 322 g_slist_free(pref->callbacks); |
323 g_free(pref->name); | |
324 g_free(pref); | |
325 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
326 |
5440 | 327 void gaim_prefs_remove(const char *name) { |
328 struct gaim_pref *pref = find_pref(name); | |
329 struct gaim_pref *child, *child2; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
330 |
5440 | 331 if(!pref) |
332 return; | |
333 child = pref->first_child; | |
334 while(child) { | |
335 child2 = child; | |
336 child = child->sibling; | |
337 remove_pref(child2); | |
338 } | |
339 | |
340 remove_pref(pref); | |
341 } | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
342 |
5440 | 343 void gaim_prefs_destroy() { |
344 gaim_prefs_remove("/"); | |
345 } | |
346 | |
347 static void do_callbacks(const char* name, struct gaim_pref *pref) { | |
348 GSList *cbs; | |
349 struct gaim_pref *cb_pref; | |
350 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
351 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
352 struct pref_cb *cb = cbs->data; | |
353 cb->func(name, pref->type, pref->value.generic, cb->data); | |
4215 | 354 } |
355 } | |
1525
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
356 } |
ba8e6e211af5
[gaim-migrate @ 1535]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1455
diff
changeset
|
357 |
5440 | 358 void gaim_prefs_set_generic(const char *name, gpointer value) { |
359 struct gaim_pref *pref = find_pref(name); | |
3366 | 360 |
5440 | 361 g_return_if_fail(pref != NULL); |
3500 | 362 |
5440 | 363 pref->value.generic = value; |
364 do_callbacks(name, pref); | |
3366 | 365 } |
366 | |
5440 | 367 void gaim_prefs_set_bool(const char *name, gboolean value) { |
368 struct gaim_pref *pref = find_pref(name); | |
4288 | 369 |
5533 | 370 if(pref) { |
371 g_return_if_fail(pref->type == GAIM_PREF_BOOLEAN); | |
4325 | 372 |
5533 | 373 if(pref->value.boolean != value) { |
374 pref->value.boolean = value; | |
375 do_callbacks(name, pref); | |
376 } | |
377 } else { | |
378 gaim_prefs_add_bool(name, value); | |
4288 | 379 } |
4324 | 380 } |
4325 | 381 |
5440 | 382 void gaim_prefs_set_int(const char *name, int value) { |
383 struct gaim_pref *pref = find_pref(name); | |
4325 | 384 |
5533 | 385 if(pref) { |
386 g_return_if_fail(pref->type == GAIM_PREF_INT); | |
4325 | 387 |
5533 | 388 if(pref->value.integer != value) { |
389 pref->value.integer = value; | |
390 do_callbacks(name, pref); | |
391 } | |
392 } else { | |
393 gaim_prefs_add_int(name, value); | |
5440 | 394 } |
4326 | 395 } |
396 | |
5451 | 397 void gaim_prefs_set_string(const char *name, const char *value) { |
5440 | 398 struct gaim_pref *pref = find_pref(name); |
4325 | 399 |
5533 | 400 if(pref) { |
401 g_return_if_fail(pref->type == GAIM_PREF_STRING); | |
4325 | 402 |
5533 | 403 if(strcmp(pref->value.string, value)) { |
404 g_free(pref->value.string); | |
405 pref->value.string = g_strdup(value); | |
406 do_callbacks(name, pref); | |
407 } | |
408 } else { | |
409 gaim_prefs_add_string(name, value); | |
4325 | 410 } |
411 } | |
412 | |
5561 | 413 void gaim_prefs_set_string_list(const char *name, GList *value) { |
414 struct gaim_pref *pref = find_pref(name); | |
415 if(pref) { | |
416 GList *tmp; | |
417 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
418 g_free(tmp->data); | |
419 | |
420 g_list_free(pref->value.stringlist); | |
421 pref->value.stringlist = NULL; | |
422 | |
423 for(tmp = value; tmp; tmp = tmp->next) | |
424 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
425 g_strdup(tmp->data)); | |
426 | |
427 } else { | |
428 gaim_prefs_add_string_list(name, value); | |
429 } | |
430 } | |
431 | |
5440 | 432 gpointer gaim_prefs_get_generic(const char *name) { |
433 struct gaim_pref *pref = find_pref(name); | |
4325 | 434 |
5440 | 435 g_return_val_if_fail(pref != NULL, NULL); |
4288 | 436 |
5440 | 437 return pref->value.generic; |
4288 | 438 } |
439 | |
5440 | 440 gboolean gaim_prefs_get_bool(const char *name) { |
441 struct gaim_pref *pref = find_pref(name); | |
3427 | 442 |
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5569
diff
changeset
|
443 gaim_debug(GAIM_DEBUG_MISC, "prefs", "key = %s\n", name); |
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5569
diff
changeset
|
444 |
5440 | 445 g_return_val_if_fail(pref != NULL, FALSE); |
446 g_return_val_if_fail(pref->type == GAIM_PREF_BOOLEAN, FALSE); | |
3472 | 447 |
5440 | 448 return pref->value.boolean; |
3366 | 449 } |
450 | |
5440 | 451 int gaim_prefs_get_int(const char *name) { |
452 struct gaim_pref *pref = find_pref(name); | |
3500 | 453 |
5440 | 454 g_return_val_if_fail(pref != NULL, 0); |
455 g_return_val_if_fail(pref->type == GAIM_PREF_INT, 0); | |
3366 | 456 |
5440 | 457 return pref->value.integer; |
3366 | 458 } |
459 | |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
460 const char *gaim_prefs_get_string(const char *name) { |
5440 | 461 struct gaim_pref *pref = find_pref(name); |
3366 | 462 |
5440 | 463 g_return_val_if_fail(pref != NULL, NULL); |
464 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
|
465 |
5440 | 466 return pref->value.string; |
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
467 } |
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
468 |
5561 | 469 GList *gaim_prefs_get_string_list(const char *name) { |
470 struct gaim_pref *pref = find_pref(name); | |
471 GList *ret = NULL, *tmp; | |
472 | |
473 g_return_val_if_fail(pref != NULL, NULL); | |
474 g_return_val_if_fail(pref->type == GAIM_PREF_STRING_LIST, NULL); | |
475 | |
476 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
477 ret = g_list_append(ret, g_strdup(tmp->data)); | |
478 | |
479 return ret; | |
480 } | |
481 | |
5440 | 482 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
483 { | |
484 struct gaim_pref *pref = find_pref(name); | |
485 struct pref_cb *cb; | |
486 static guint cb_id = 0; | |
3366 | 487 |
5440 | 488 if(!pref) |
489 return 0; | |
3366 | 490 |
5440 | 491 cb = g_new0(struct pref_cb, 1); |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
492 |
5440 | 493 cb->func = func; |
494 cb->data = data; | |
495 cb->id = ++cb_id; | |
4991 | 496 |
5440 | 497 pref->callbacks = g_slist_append(pref->callbacks, cb); |
3366 | 498 |
5440 | 499 return cb->id; |
3366 | 500 } |
501 | |
5440 | 502 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
503 GSList *cbs; | |
504 struct gaim_pref *child; | |
2254 | 505 |
5440 | 506 if(!pref) |
507 return FALSE; | |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
508 |
5440 | 509 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
510 struct pref_cb *cb = cbs->data; | |
511 if(cb->id == callback_id) { | |
512 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
513 g_free(cb); | |
514 return TRUE; | |
4428 | 515 } |
516 } | |
517 | |
5440 | 518 for(child = pref->first_child; child; child = child->sibling) { |
519 if(disco_callback_helper(child, callback_id)) | |
520 return TRUE; | |
4428 | 521 } |
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
522 |
5440 | 523 return FALSE; |
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
524 } |
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
525 |
5440 | 526 void gaim_prefs_disconnect_callback(guint callback_id) { |
527 disco_callback_helper(&prefs, callback_id); | |
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
528 } |
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
529 |
5440 | 530 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
531 struct gaim_pref *tmp; | |
532 char *esc; | |
533 int i; | |
3500 | 534 |
5440 | 535 if(!pref) { |
536 pref = &prefs; | |
3551 | 537 |
5440 | 538 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
539 fprintf(f, "<pref name='/'"); | |
540 } else { | |
541 for(i=0; i<depth; i++) | |
542 fprintf(f, "\t"); | |
543 esc = g_markup_escape_text(pref->name, -1); | |
544 fprintf(f, "<pref name='%s'", esc); | |
545 g_free(esc); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
546 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
547 |
5440 | 548 switch(pref->type) { |
549 case GAIM_PREF_NONE: | |
550 break; | |
551 case GAIM_PREF_BOOLEAN: | |
552 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
553 break; | |
554 case GAIM_PREF_INT: | |
555 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
556 break; | |
557 case GAIM_PREF_STRING: | |
558 esc = g_markup_escape_text(pref->value.string, -1); | |
559 fprintf(f, " type='string' value='%s'", esc); | |
560 g_free(esc); | |
561 break; | |
5561 | 562 case GAIM_PREF_STRING_LIST: |
563 fprintf(f, " type='stringlist'"); | |
564 break; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
565 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
566 |
5561 | 567 if(pref->first_child || pref->type == GAIM_PREF_STRING_LIST) { |
5440 | 568 fprintf(f, ">\n"); |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
569 |
5440 | 570 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
571 gaim_prefs_write(f, tmp, depth+1); | |
5561 | 572 |
573 if(pref->type == GAIM_PREF_STRING_LIST) { | |
574 GList *tmp2; | |
575 for(tmp2 = pref->value.stringlist; tmp2; tmp2 = tmp2->next) { | |
576 for(i=0; i<depth+1; i++) | |
577 fprintf(f, "\t"); | |
578 esc = g_markup_escape_text(tmp2->data, -1); | |
579 fprintf(f, "<item value='%s' />\n", esc); | |
580 g_free(esc); | |
581 } | |
582 } | |
583 | |
5440 | 584 for(i=0; i<depth; i++) |
585 fprintf(f, "\t"); | |
586 fprintf(f, "</pref>\n"); | |
587 } else { | |
588 fprintf(f, " />\n"); | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
589 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
590 } |
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
591 |
5440 | 592 void gaim_prefs_sync() { |
593 FILE *file; | |
594 const char *user_dir = gaim_user_dir(); | |
595 char *filename; | |
596 char *filename_real; | |
3551 | 597 |
5534 | 598 if(!prefs_is_loaded) { |
599 gaim_debug(GAIM_DEBUG_WARNING, "prefs", "prefs saved before loading! scheduling save.\n"); | |
600 schedule_prefs_save(); /* schedule a save for after we read in */ | |
601 return; | |
602 } | |
603 | |
5440 | 604 if(!user_dir) |
605 return; | |
3551 | 606 |
5534 | 607 gaim_debug(GAIM_DEBUG_INFO, "prefs", "writing prefs out to disk.\n"); |
608 | |
5440 | 609 file = fopen(user_dir, "r"); |
610 if(!file) | |
611 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
612 else | |
613 fclose(file); | |
3551 | 614 |
5440 | 615 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
3551 | 616 |
5440 | 617 if((file = fopen(filename, "w"))) { |
618 gaim_prefs_write(file, NULL, 0); | |
619 fclose(file); | |
620 chmod(filename, S_IRUSR | S_IWUSR); | |
621 } else { | |
622 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
623 filename); | |
624 } | |
3551 | 625 |
5440 | 626 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
627 if(rename(filename, filename_real) < 0) | |
628 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
629 filename, filename_real); | |
3551 | 630 |
5440 | 631 g_free(filename); |
632 g_free(filename_real); | |
3551 | 633 } |
634 | |
5440 | 635 static GList *prefs_stack = NULL; |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
636 |
5440 | 637 static void prefs_start_element_handler (GMarkupParseContext *context, |
638 const gchar *element_name, | |
639 const gchar **attribute_names, | |
640 const gchar **attribute_values, | |
641 gpointer user_data, | |
642 GError **error) { | |
643 GaimPrefType pref_type = GAIM_PREF_NONE; | |
644 int i; | |
645 const char *pref_name = NULL, *pref_value = NULL; | |
646 GString *pref_name_full; | |
647 GList *tmp; | |
3366 | 648 |
5561 | 649 if(strcmp(element_name, "pref") && strcmp(element_name, "item")) |
5440 | 650 return; |
3500 | 651 |
5440 | 652 for(i = 0; attribute_names[i]; i++) { |
653 if(!strcmp(attribute_names[i], "name")) { | |
654 pref_name = attribute_values[i]; | |
655 } else if(!strcmp(attribute_names[i], "type")) { | |
656 if(!strcmp(attribute_values[i], "bool")) | |
657 pref_type = GAIM_PREF_BOOLEAN; | |
658 else if(!strcmp(attribute_values[i], "int")) | |
659 pref_type = GAIM_PREF_INT; | |
660 else if(!strcmp(attribute_values[i], "string")) | |
661 pref_type = GAIM_PREF_STRING; | |
5561 | 662 else if(!strcmp(attribute_values[i], "stringlist")) |
663 pref_type = GAIM_PREF_STRING_LIST; | |
5440 | 664 else |
665 return; | |
666 } else if(!strcmp(attribute_names[i], "value")) { | |
667 pref_value = attribute_values[i]; | |
668 } | |
669 } | |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
670 |
5561 | 671 if(!strcmp(element_name, "item")) { |
672 struct gaim_pref *pref = find_pref(prefs_stack->data); | |
673 if(pref) { | |
674 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
675 g_strdup(pref_value)); | |
676 } | |
5440 | 677 return; |
5561 | 678 } else if(!pref_name || !strcmp(pref_name, "/")) { |
679 return; | |
680 } | |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
681 |
5440 | 682 pref_name_full = g_string_new(pref_name); |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
683 |
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
684 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
5440 | 685 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
686 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
687 } | |
1170 | 688 |
5440 | 689 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
690 |
5533 | 691 switch(pref_type) { |
692 case GAIM_PREF_NONE: | |
693 break; | |
694 case GAIM_PREF_BOOLEAN: | |
695 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
696 break; | |
697 case GAIM_PREF_INT: | |
698 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
699 break; | |
700 case GAIM_PREF_STRING: | |
701 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
702 break; | |
5561 | 703 case GAIM_PREF_STRING_LIST: |
704 break; | |
5440 | 705 } |
1170 | 706 |
5440 | 707 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
708 g_string_free(pref_name_full, TRUE); | |
1170 | 709 } |
710 | |
5440 | 711 static void prefs_end_element_handler(GMarkupParseContext *context, |
712 const gchar *element_name, gpointer user_data, GError **error) { | |
713 if(!strcmp(element_name, "pref")) { | |
714 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
715 } | |
1170 | 716 } |
717 | |
5440 | 718 static GMarkupParser prefs_parser = { |
719 prefs_start_element_handler, | |
720 prefs_end_element_handler, | |
721 NULL, | |
722 NULL, | |
723 NULL | |
724 }; | |
1170 | 725 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
726 gboolean gaim_prefs_load() { |
5440 | 727 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); |
728 gchar *contents = NULL; | |
729 gsize length; | |
730 GMarkupParseContext *context; | |
731 GError *error = NULL; | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
732 |
5534 | 733 |
734 if(!filename) { | |
735 prefs_is_loaded = TRUE; | |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
736 return FALSE; |
5534 | 737 } |
5440 | 738 |
739 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
740 |
5440 | 741 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
742 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
743 error->message); | |
744 g_error_free(error); | |
5534 | 745 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
746 return FALSE; |
1170 | 747 } |
748 | |
5440 | 749 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
750 | |
751 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
752 g_markup_parse_context_free(context); | |
753 g_free(contents); | |
5534 | 754 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
755 return FALSE; |
5440 | 756 } |
757 | |
758 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
759 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
760 g_markup_parse_context_free(context); | |
761 g_free(contents); | |
5534 | 762 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
763 return FALSE; |
5440 | 764 } |
765 | |
766 g_markup_parse_context_free(context); | |
767 g_free(contents); | |
768 | |
769 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
770 g_free(filename); | |
5534 | 771 prefs_is_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
772 |
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
773 return TRUE; |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
774 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
775 |