Mercurial > pidgin
annotate src/prefs.c @ 13710:848c9685ecc7
[gaim-migrate @ 16114]
CID 153
Same idea as CID 154: Do the sanity checks directly, in case g_return_if_fail()s are being compiled out.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 01 May 2006 20:31:25 +0000 |
parents | ff25f6d2b484 |
children | 8bda65b88e49 |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
8046 | 4 * Gaim is the legal property of its developers, whose names are too numerous |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
1 | 7 * |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
340
diff
changeset
|
24 #ifdef HAVE_CONFIG_H |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2074
diff
changeset
|
25 #include <config.h> |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
340
diff
changeset
|
26 #endif |
5440 | 27 |
1 | 28 #include <string.h> |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
5440 | 31 #include <sys/stat.h> |
32 #include <sys/types.h> | |
33 #include <glib.h> | |
6216 | 34 #include "internal.h" |
5440 | 35 #include "prefs.h" |
36 #include "debug.h" | |
37 #include "util.h" | |
3366 | 38 |
4026
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
39 #ifdef _WIN32 |
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
40 #include "win32dep.h" |
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
41 #endif |
a997156437b6
[gaim-migrate @ 4230]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4010
diff
changeset
|
42 |
5440 | 43 struct pref_cb { |
44 GaimPrefCallback func; | |
45 gpointer data; | |
46 guint id; | |
10087 | 47 void *handle; |
5440 | 48 }; |
49 | |
10443 | 50 /* TODO: This should use GaimValues? */ |
5440 | 51 struct gaim_pref { |
52 GaimPrefType type; | |
53 char *name; | |
54 union { | |
55 gpointer generic; | |
56 gboolean boolean; | |
57 int integer; | |
58 char *string; | |
5561 | 59 GList *stringlist; |
5440 | 60 } value; |
61 GSList *callbacks; | |
62 struct gaim_pref *parent; | |
63 struct gaim_pref *sibling; | |
64 struct gaim_pref *first_child; | |
65 }; | |
3366 | 66 |
5440 | 67 |
10443 | 68 static struct gaim_pref prefs = { |
69 GAIM_PREF_NONE, | |
70 NULL, | |
71 { NULL }, | |
72 NULL, | |
73 NULL, | |
74 NULL, | |
75 NULL | |
76 }; | |
5440 | 77 |
10443 | 78 static GHashTable *prefs_hash = NULL; |
79 static guint save_timer = 0; | |
80 static gboolean prefs_loaded = FALSE; | |
5534 | 81 |
82 | |
10443 | 83 /********************************************************************* |
84 * Private utility functions * | |
85 *********************************************************************/ | |
8235 | 86 |
10443 | 87 static struct |
88 gaim_pref *find_pref(const char *name) | |
5787
2adc29c88a45
[gaim-migrate @ 6212]
Christian Hammond <chipx86@chipx86.com>
parents:
5684
diff
changeset
|
89 { |
10443 | 90 if (!name || name[0] != '/') |
5440 | 91 return NULL; |
10443 | 92 else if (name[1] == '\0') |
5440 | 93 return &prefs; |
10443 | 94 else |
5440 | 95 return g_hash_table_lookup(prefs_hash, name); |
96 } | |
97 | |
10443 | 98 |
99 /********************************************************************* | |
100 * Writing to disk * | |
101 *********************************************************************/ | |
102 | |
103 /* | |
104 * This function recursively creates the xmlnode tree from the prefs | |
105 * tree structure. Yay recursion! | |
106 */ | |
10850 | 107 static void |
10443 | 108 pref_to_xmlnode(xmlnode *parent, struct gaim_pref *pref) |
109 { | |
110 xmlnode *node, *childnode; | |
111 struct gaim_pref *child; | |
112 char buf[20]; | |
113 GList *cur; | |
5561 | 114 |
10443 | 115 /* Create a new node */ |
116 node = xmlnode_new_child(parent, "pref"); | |
117 xmlnode_set_attrib(node, "name", pref->name); | |
5440 | 118 |
10443 | 119 /* Set the type of this node (if type == GAIM_PREF_NONE then do nothing) */ |
120 if (pref->type == GAIM_PREF_INT) { | |
121 xmlnode_set_attrib(node, "type", "int"); | |
122 snprintf(buf, sizeof(buf), "%d", pref->value.integer); | |
123 xmlnode_set_attrib(node, "value", buf); | |
124 } | |
125 else if (pref->type == GAIM_PREF_STRING) { | |
126 xmlnode_set_attrib(node, "type", "string"); | |
127 xmlnode_set_attrib(node, "value", pref->value.string); | |
128 } | |
129 else if (pref->type == GAIM_PREF_STRING_LIST) { | |
130 xmlnode_set_attrib(node, "type", "stringlist"); | |
131 for (cur = pref->value.stringlist; cur != NULL; cur = cur->next) | |
132 { | |
133 childnode = xmlnode_new_child(node, "item"); | |
134 xmlnode_set_attrib(childnode, "value", cur->data); | |
5440 | 135 } |
136 } | |
10443 | 137 else if (pref->type == GAIM_PREF_BOOLEAN) { |
138 xmlnode_set_attrib(node, "type", "bool"); | |
139 snprintf(buf, sizeof(buf), "%d", pref->value.boolean); | |
140 xmlnode_set_attrib(node, "value", buf); | |
5440 | 141 } |
142 | |
10443 | 143 /* All My Children */ |
144 for (child = pref->first_child; child != NULL; child = child->sibling) | |
145 pref_to_xmlnode(node, child); | |
5440 | 146 } |
147 | |
10443 | 148 static xmlnode * |
149 prefs_to_xmlnode(void) | |
150 { | |
151 xmlnode *node; | |
152 struct gaim_pref *pref, *child; | |
5440 | 153 |
10443 | 154 pref = &prefs; |
5440 | 155 |
10443 | 156 /* Create the root preference node */ |
157 node = xmlnode_new("pref"); | |
158 xmlnode_set_attrib(node, "version", "1"); | |
159 xmlnode_set_attrib(node, "name", "/"); | |
5561 | 160 |
10443 | 161 /* All My Children */ |
162 for (child = pref->first_child; child != NULL; child = child->sibling) | |
163 pref_to_xmlnode(node, child); | |
5561 | 164 |
10443 | 165 return node; |
5561 | 166 } |
167 | |
10443 | 168 static void |
169 sync_prefs(void) | |
170 { | |
171 xmlnode *node; | |
172 char *data; | |
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
173 |
10443 | 174 if (!prefs_loaded) |
175 { | |
176 /* | |
177 * TODO: Call schedule_prefs_save()? Ideally we wouldn't need to. | |
178 * (prefs.xml should be loaded when gaim_prefs_init is called) | |
179 */ | |
180 gaim_debug_error("prefs", "Attempted to save prefs before " | |
181 "they were read!\n"); | |
5814 | 182 return; |
183 } | |
3500 | 184 |
10443 | 185 node = prefs_to_xmlnode(); |
186 data = xmlnode_to_formatted_str(node, NULL); | |
187 gaim_util_write_data_to_file("prefs.xml", data, -1); | |
188 g_free(data); | |
189 xmlnode_free(node); | |
4326 | 190 } |
191 | |
10443 | 192 static gboolean |
193 save_cb(gpointer data) | |
194 { | |
195 sync_prefs(); | |
196 save_timer = 0; | |
9594 | 197 return FALSE; |
4288 | 198 } |
199 | |
10443 | 200 static void |
201 schedule_prefs_save(void) | |
5440 | 202 { |
10443 | 203 if (save_timer == 0) |
204 save_timer = gaim_timeout_add(5000, save_cb, NULL); | |
3366 | 205 } |
206 | |
2254 | 207 |
10443 | 208 /********************************************************************* |
209 * Reading from disk * | |
210 *********************************************************************/ | |
3551 | 211 |
5440 | 212 static GList *prefs_stack = NULL; |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
213 |
10443 | 214 static void |
215 prefs_start_element_handler (GMarkupParseContext *context, | |
5440 | 216 const gchar *element_name, |
217 const gchar **attribute_names, | |
218 const gchar **attribute_values, | |
219 gpointer user_data, | |
10443 | 220 GError **error) |
221 { | |
5440 | 222 GaimPrefType pref_type = GAIM_PREF_NONE; |
223 int i; | |
224 const char *pref_name = NULL, *pref_value = NULL; | |
225 GString *pref_name_full; | |
226 GList *tmp; | |
3366 | 227 |
5561 | 228 if(strcmp(element_name, "pref") && strcmp(element_name, "item")) |
5440 | 229 return; |
3500 | 230 |
5440 | 231 for(i = 0; attribute_names[i]; i++) { |
232 if(!strcmp(attribute_names[i], "name")) { | |
233 pref_name = attribute_values[i]; | |
234 } else if(!strcmp(attribute_names[i], "type")) { | |
235 if(!strcmp(attribute_values[i], "bool")) | |
236 pref_type = GAIM_PREF_BOOLEAN; | |
237 else if(!strcmp(attribute_values[i], "int")) | |
238 pref_type = GAIM_PREF_INT; | |
239 else if(!strcmp(attribute_values[i], "string")) | |
240 pref_type = GAIM_PREF_STRING; | |
5561 | 241 else if(!strcmp(attribute_values[i], "stringlist")) |
242 pref_type = GAIM_PREF_STRING_LIST; | |
5440 | 243 else |
244 return; | |
245 } else if(!strcmp(attribute_names[i], "value")) { | |
246 pref_value = attribute_values[i]; | |
247 } | |
248 } | |
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
249 |
5561 | 250 if(!strcmp(element_name, "item")) { |
5838 | 251 struct gaim_pref *pref; |
252 | |
253 pref_name_full = g_string_new(""); | |
254 | |
255 for(tmp = prefs_stack; tmp; tmp = tmp->next) { | |
256 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
257 pref_name_full = g_string_prepend_c(pref_name_full, '/'); | |
258 } | |
259 | |
260 pref = find_pref(pref_name_full->str); | |
261 | |
5561 | 262 if(pref) { |
263 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
264 g_strdup(pref_value)); | |
265 } | |
5838 | 266 } else { |
267 if(!pref_name || !strcmp(pref_name, "/")) | |
268 return; | |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
269 |
5838 | 270 pref_name_full = g_string_new(pref_name); |
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
271 |
5838 | 272 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
273 pref_name_full = g_string_prepend_c(pref_name_full, '/'); | |
274 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
275 } | |
276 | |
5440 | 277 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
278 |
5838 | 279 switch(pref_type) { |
280 case GAIM_PREF_NONE: | |
7785 | 281 gaim_prefs_add_none(pref_name_full->str); |
5838 | 282 break; |
283 case GAIM_PREF_BOOLEAN: | |
284 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
285 break; | |
286 case GAIM_PREF_INT: | |
287 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
288 break; | |
289 case GAIM_PREF_STRING: | |
290 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
291 break; | |
292 case GAIM_PREF_STRING_LIST: | |
293 gaim_prefs_set_string_list(pref_name_full->str, NULL); | |
294 break; | |
295 } | |
296 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); | |
297 g_string_free(pref_name_full, TRUE); | |
5440 | 298 } |
1170 | 299 } |
300 | |
10443 | 301 static void |
302 prefs_end_element_handler(GMarkupParseContext *context, | |
303 const gchar *element_name, | |
304 gpointer user_data, GError **error) | |
305 { | |
5940 | 306 if(prefs_stack && !strcmp(element_name, "pref")) { |
307 g_free(prefs_stack->data); | |
5440 | 308 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); |
309 } | |
1170 | 310 } |
311 | |
5440 | 312 static GMarkupParser prefs_parser = { |
313 prefs_start_element_handler, | |
314 prefs_end_element_handler, | |
315 NULL, | |
316 NULL, | |
317 NULL | |
318 }; | |
1170 | 319 |
10443 | 320 gboolean |
321 gaim_prefs_load() | |
322 { | |
5440 | 323 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); |
324 gchar *contents = NULL; | |
325 gsize length; | |
326 GMarkupParseContext *context; | |
327 GError *error = NULL; | |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
328 |
7561
cdfdbabd3266
[gaim-migrate @ 8175]
Christian Hammond <chipx86@chipx86.com>
parents:
7555
diff
changeset
|
329 if (!filename) { |
10443 | 330 prefs_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
331 return FALSE; |
5534 | 332 } |
5440 | 333 |
10443 | 334 gaim_debug_info("prefs", "Reading %s\n", filename); |
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
335 |
5440 | 336 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
8671
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
337 #ifndef _WIN32 |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
338 g_free(filename); |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
339 g_error_free(error); |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
340 |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
341 error = NULL; |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
342 |
8702 | 343 filename = g_build_filename(SYSCONFDIR, "gaim", "prefs.xml", NULL); |
8671
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
344 |
10443 | 345 gaim_debug_info("prefs", "Reading %s\n", filename); |
8671
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
346 |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
347 if (!g_file_get_contents(filename, &contents, &length, &error)) { |
10443 | 348 gaim_debug_error("prefs", "Error reading prefs: %s\n", |
8671
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
349 error->message); |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
350 g_error_free(error); |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
351 g_free(filename); |
10443 | 352 prefs_loaded = TRUE; |
8671
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
353 |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
354 return FALSE; |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
355 } |
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
356 #else /* _WIN32 */ |
10443 | 357 gaim_debug_error("prefs", "Error reading prefs: %s\n", |
5440 | 358 error->message); |
359 g_error_free(error); | |
6040 | 360 g_free(filename); |
10443 | 361 prefs_loaded = TRUE; |
6040 | 362 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
363 return FALSE; |
8671
d99d2572d1a9
[gaim-migrate @ 9423]
Christian Hammond <chipx86@chipx86.com>
parents:
8549
diff
changeset
|
364 #endif /* _WIN32 */ |
1170 | 365 } |
366 | |
5440 | 367 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
368 | |
369 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
370 g_markup_parse_context_free(context); | |
371 g_free(contents); | |
6040 | 372 g_free(filename); |
10443 | 373 prefs_loaded = TRUE; |
6040 | 374 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
375 return FALSE; |
5440 | 376 } |
377 | |
378 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
10443 | 379 gaim_debug_error("prefs", "Error parsing %s\n", filename); |
5440 | 380 g_markup_parse_context_free(context); |
381 g_free(contents); | |
6040 | 382 g_free(filename); |
10443 | 383 prefs_loaded = TRUE; |
6040 | 384 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
385 return FALSE; |
5440 | 386 } |
387 | |
10443 | 388 gaim_debug_info("prefs", "Finished reading %s\n", filename); |
5440 | 389 g_markup_parse_context_free(context); |
390 g_free(contents); | |
391 g_free(filename); | |
10443 | 392 prefs_loaded = TRUE; |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
393 |
13053
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
394 /* I introduced a bug in 2.0.0beta2. This fixes the broken |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
395 * scores on upgrade. This can be removed sometime shortly |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
396 * after 2.0.0 final is released. -- rlaager */ |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
397 if (gaim_prefs_get_int("/core/status/scores/offline") == -500 && |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
398 gaim_prefs_get_int("/core/status/scores/available") == 100 && |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
399 gaim_prefs_get_int("/core/status/scores/invisible") == -50 && |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
400 gaim_prefs_get_int("/core/status/scores/away") == -100 && |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
401 gaim_prefs_get_int("/core/status/scores/extended_away") == -200 && |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
402 gaim_prefs_get_int("/core/status/scores/idle") == -400) |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
403 { |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
404 gaim_prefs_set_int("/core/status/scores/idle", -10); |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
405 } |
d50c330e8089
[gaim-migrate @ 15414]
Richard Laager <rlaager@wiktel.com>
parents:
13040
diff
changeset
|
406 |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5539
diff
changeset
|
407 return TRUE; |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
408 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
409 |
10443 | 410 |
411 | |
412 static void | |
12822
cecc9706c11f
[gaim-migrate @ 15170]
Richard Laager <rlaager@wiktel.com>
parents:
12762
diff
changeset
|
413 prefs_save_cb(const char *name, GaimPrefType type, gconstpointer val, |
10443 | 414 gpointer user_data) |
415 { | |
416 | |
417 if(!prefs_loaded) | |
418 return; | |
419 | |
420 gaim_debug_misc("prefs", "%s changed, scheduling save.\n", name); | |
421 | |
422 schedule_prefs_save(); | |
423 } | |
424 | |
425 static char * | |
426 get_path_dirname(const char *name) | |
427 { | |
428 char *c, *str; | |
429 | |
430 str = g_strdup(name); | |
431 | |
432 if ((c = strrchr(str, '/')) != NULL) { | |
433 *c = '\0'; | |
434 | |
435 if (*str == '\0') { | |
436 g_free(str); | |
437 | |
438 str = g_strdup("/"); | |
439 } | |
440 } | |
441 else { | |
442 g_free(str); | |
443 | |
444 str = g_strdup("."); | |
445 } | |
446 | |
447 return str; | |
448 } | |
449 | |
450 static char * | |
451 get_path_basename(const char *name) | |
452 { | |
453 const char *c; | |
454 | |
455 if ((c = strrchr(name, '/')) != NULL) | |
456 return g_strdup(c + 1); | |
457 | |
458 return g_strdup(name); | |
459 } | |
460 | |
461 static char * | |
462 pref_full_name(struct gaim_pref *pref) | |
463 { | |
464 GString *name; | |
465 struct gaim_pref *parent; | |
466 char *ret; | |
467 | |
468 if(!pref) | |
469 return NULL; | |
470 | |
471 if(pref == &prefs) | |
472 return g_strdup("/"); | |
473 | |
474 name = g_string_new(pref->name); | |
475 parent = pref->parent; | |
476 | |
477 for(parent = pref->parent; parent && parent->name; parent = parent->parent) { | |
478 name = g_string_prepend_c(name, '/'); | |
479 name = g_string_prepend(name, parent->name); | |
480 } | |
12759
019d0e4d8d65
[gaim-migrate @ 15106]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12615
diff
changeset
|
481 name = g_string_prepend_c(name, '/'); |
10443 | 482 ret = name->str; |
483 g_string_free(name, FALSE); | |
484 return ret; | |
485 } | |
486 | |
487 static struct gaim_pref * | |
488 find_pref_parent(const char *name) | |
489 { | |
490 char *parent_name = get_path_dirname(name); | |
491 struct gaim_pref *ret = &prefs; | |
492 | |
493 if(strcmp(parent_name, "/")) { | |
494 ret = find_pref(parent_name); | |
495 } | |
496 | |
497 g_free(parent_name); | |
498 return ret; | |
499 } | |
500 | |
501 static void | |
502 free_pref_value(struct gaim_pref *pref) | |
503 { | |
504 switch(pref->type) { | |
505 case GAIM_PREF_BOOLEAN: | |
506 pref->value.boolean = FALSE; | |
507 break; | |
508 case GAIM_PREF_INT: | |
509 pref->value.integer = 0; | |
510 break; | |
511 case GAIM_PREF_STRING: | |
512 g_free(pref->value.string); | |
513 pref->value.string = NULL; | |
514 break; | |
515 case GAIM_PREF_STRING_LIST: | |
516 { | |
517 GList *tmp; | |
518 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
519 g_free(tmp->data); | |
520 | |
521 g_list_free(pref->value.stringlist); | |
522 } break; | |
523 case GAIM_PREF_NONE: | |
524 break; | |
525 } | |
526 } | |
527 | |
528 static struct gaim_pref * | |
529 add_pref(GaimPrefType type, const char *name) | |
530 { | |
531 struct gaim_pref *parent; | |
532 struct gaim_pref *me; | |
533 struct gaim_pref *sibling; | |
534 char *my_name; | |
535 | |
536 parent = find_pref_parent(name); | |
537 | |
538 if(!parent) | |
539 return NULL; | |
540 | |
541 my_name = get_path_basename(name); | |
542 | |
543 for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { | |
544 if(!strcmp(sibling->name, my_name)) { | |
545 g_free(my_name); | |
546 return NULL; | |
547 } | |
548 } | |
549 | |
550 me = g_new0(struct gaim_pref, 1); | |
551 me->type = type; | |
552 me->name = my_name; | |
553 | |
554 me->parent = parent; | |
555 if(parent->first_child) { | |
556 /* blatant abuse of a for loop */ | |
557 for(sibling = parent->first_child; sibling->sibling; | |
558 sibling = sibling->sibling); | |
559 sibling->sibling = me; | |
560 } else { | |
561 parent->first_child = me; | |
562 } | |
563 | |
564 g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
565 | |
566 return me; | |
567 } | |
568 | |
569 void | |
570 gaim_prefs_add_none(const char *name) | |
571 { | |
572 add_pref(GAIM_PREF_NONE, name); | |
573 } | |
574 | |
575 void | |
576 gaim_prefs_add_bool(const char *name, gboolean value) | |
577 { | |
578 struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
579 | |
580 if(!pref) | |
581 return; | |
582 | |
583 pref->value.boolean = value; | |
584 } | |
585 | |
586 void | |
587 gaim_prefs_add_int(const char *name, int value) | |
588 { | |
589 struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); | |
590 | |
591 if(!pref) | |
592 return; | |
593 | |
594 pref->value.integer = value; | |
595 } | |
596 | |
597 void | |
598 gaim_prefs_add_string(const char *name, const char *value) | |
599 { | |
600 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
601 | |
602 if(!pref) | |
603 return; | |
604 | |
605 pref->value.string = g_strdup(value); | |
606 } | |
607 | |
608 void | |
609 gaim_prefs_add_string_list(const char *name, GList *value) | |
610 { | |
611 struct gaim_pref *pref = add_pref(GAIM_PREF_STRING_LIST, name); | |
612 GList *tmp; | |
613 | |
614 if(!pref) | |
615 return; | |
616 | |
617 for(tmp = value; tmp; tmp = tmp->next) | |
618 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
619 g_strdup(tmp->data)); | |
620 } | |
621 | |
10871 | 622 static void |
10443 | 623 remove_pref(struct gaim_pref *pref) |
624 { | |
625 char *name; | |
626 | |
627 if(!pref || pref == &prefs) | |
628 return; | |
629 | |
630 while(pref->first_child) | |
631 remove_pref(pref->first_child); | |
632 | |
633 if(pref->parent->first_child == pref) { | |
634 pref->parent->first_child = pref->sibling; | |
635 } else { | |
636 struct gaim_pref *sib = pref->parent->first_child; | |
12599 | 637 while(sib && sib->sibling != pref) |
10443 | 638 sib = sib->sibling; |
12599 | 639 if(sib) |
640 sib->sibling = pref->sibling; | |
10443 | 641 } |
642 | |
643 name = pref_full_name(pref); | |
644 | |
12762
40584fbf8c6e
[gaim-migrate @ 15109]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12759
diff
changeset
|
645 gaim_debug_info("prefs", "removing pref %s\n", name); |
10443 | 646 |
647 g_hash_table_remove(prefs_hash, name); | |
648 g_free(name); | |
649 | |
650 free_pref_value(pref); | |
651 | |
652 g_slist_free(pref->callbacks); | |
653 g_free(pref->name); | |
654 g_free(pref); | |
655 } | |
656 | |
657 void | |
658 gaim_prefs_remove(const char *name) | |
659 { | |
660 struct gaim_pref *pref = find_pref(name); | |
661 | |
662 if(!pref) | |
663 return; | |
664 | |
665 remove_pref(pref); | |
666 } | |
667 | |
668 void | |
669 gaim_prefs_destroy() | |
670 { | |
671 gaim_prefs_remove("/"); | |
672 } | |
673 | |
674 static void | |
675 do_callbacks(const char* name, struct gaim_pref *pref) | |
676 { | |
677 GSList *cbs; | |
678 struct gaim_pref *cb_pref; | |
679 for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
680 for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
681 struct pref_cb *cb = cbs->data; | |
682 cb->func(name, pref->type, pref->value.generic, cb->data); | |
683 } | |
684 } | |
685 } | |
686 | |
687 void | |
688 gaim_prefs_trigger_callback(const char *name) | |
689 { | |
690 struct gaim_pref *pref = find_pref(name); | |
691 | |
692 if(!pref) { | |
693 gaim_debug_error("prefs", | |
694 "gaim_prefs_trigger_callback: Unknown pref %s\n", name); | |
695 return; | |
696 } | |
697 | |
698 do_callbacks(name, pref); | |
699 } | |
700 | |
701 void | |
702 gaim_prefs_set_generic(const char *name, gpointer value) | |
703 { | |
704 struct gaim_pref *pref = find_pref(name); | |
705 | |
706 if(!pref) { | |
707 gaim_debug_error("prefs", | |
708 "gaim_prefs_set_generic: Unknown pref %s\n", name); | |
709 return; | |
710 } | |
711 | |
712 pref->value.generic = value; | |
713 do_callbacks(name, pref); | |
714 } | |
715 | |
716 void | |
717 gaim_prefs_set_bool(const char *name, gboolean value) | |
718 { | |
719 struct gaim_pref *pref = find_pref(name); | |
720 | |
721 if(pref) { | |
722 if(pref->type != GAIM_PREF_BOOLEAN) { | |
723 gaim_debug_error("prefs", | |
724 "gaim_prefs_set_bool: %s not a boolean pref\n", name); | |
725 return; | |
726 } | |
727 | |
728 if(pref->value.boolean != value) { | |
729 pref->value.boolean = value; | |
730 do_callbacks(name, pref); | |
731 } | |
732 } else { | |
733 gaim_prefs_add_bool(name, value); | |
734 } | |
735 } | |
736 | |
737 void | |
738 gaim_prefs_set_int(const char *name, int value) | |
739 { | |
740 struct gaim_pref *pref = find_pref(name); | |
741 | |
742 if(pref) { | |
743 if(pref->type != GAIM_PREF_INT) { | |
744 gaim_debug_error("prefs", | |
745 "gaim_prefs_set_int: %s not an integer pref\n", name); | |
746 return; | |
747 } | |
748 | |
749 if(pref->value.integer != value) { | |
750 pref->value.integer = value; | |
751 do_callbacks(name, pref); | |
752 } | |
753 } else { | |
754 gaim_prefs_add_int(name, value); | |
755 } | |
756 } | |
757 | |
758 void | |
759 gaim_prefs_set_string(const char *name, const char *value) | |
760 { | |
761 struct gaim_pref *pref = find_pref(name); | |
762 | |
763 if(pref) { | |
764 if(pref->type != GAIM_PREF_STRING) { | |
765 gaim_debug_error("prefs", | |
766 "gaim_prefs_set_string: %s not a string pref\n", name); | |
767 return; | |
768 } | |
769 | |
770 if((value && !pref->value.string) || | |
771 (!value && pref->value.string) || | |
13638 | 772 (value && pref->value.string && |
773 strcmp(pref->value.string, value))) { | |
10443 | 774 g_free(pref->value.string); |
775 pref->value.string = g_strdup(value); | |
776 do_callbacks(name, pref); | |
777 } | |
778 } else { | |
779 gaim_prefs_add_string(name, value); | |
780 } | |
781 } | |
782 | |
783 void | |
784 gaim_prefs_set_string_list(const char *name, GList *value) | |
785 { | |
786 struct gaim_pref *pref = find_pref(name); | |
787 if(pref) { | |
788 GList *tmp; | |
789 | |
790 if(pref->type != GAIM_PREF_STRING_LIST) { | |
791 gaim_debug_error("prefs", | |
792 "gaim_prefs_set_string_list: %s not a string list pref\n", | |
793 name); | |
794 return; | |
795 } | |
796 | |
797 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
798 g_free(tmp->data); | |
799 | |
800 g_list_free(pref->value.stringlist); | |
801 pref->value.stringlist = NULL; | |
802 | |
803 for(tmp = value; tmp; tmp = tmp->next) | |
804 pref->value.stringlist = g_list_append(pref->value.stringlist, | |
805 g_strdup(tmp->data)); | |
806 | |
807 do_callbacks(name, pref); | |
808 | |
809 } else { | |
810 gaim_prefs_add_string_list(name, value); | |
811 } | |
812 } | |
813 | |
814 gboolean | |
815 gaim_prefs_exists(const char *name) | |
816 { | |
817 struct gaim_pref *pref = find_pref(name); | |
818 | |
819 if (pref != NULL) | |
820 return TRUE; | |
821 | |
822 return FALSE; | |
823 } | |
824 | |
825 GaimPrefType | |
826 gaim_prefs_get_type(const char *name) | |
827 { | |
828 struct gaim_pref *pref = find_pref(name); | |
829 | |
830 if (pref == NULL) | |
831 return GAIM_PREF_NONE; | |
832 | |
833 return (pref->type); | |
834 } | |
835 | |
836 gboolean | |
837 gaim_prefs_get_bool(const char *name) | |
838 { | |
839 struct gaim_pref *pref = find_pref(name); | |
840 | |
841 if(!pref) { | |
842 gaim_debug_error("prefs", | |
843 "gaim_prefs_get_bool: Unknown pref %s\n", name); | |
844 return FALSE; | |
845 } else if(pref->type != GAIM_PREF_BOOLEAN) { | |
846 gaim_debug_error("prefs", | |
847 "gaim_prefs_get_bool: %s not a boolean pref\n", name); | |
848 return FALSE; | |
849 } | |
850 | |
851 return pref->value.boolean; | |
852 } | |
853 | |
854 int | |
855 gaim_prefs_get_int(const char *name) | |
856 { | |
857 struct gaim_pref *pref = find_pref(name); | |
858 | |
859 if(!pref) { | |
860 gaim_debug_error("prefs", | |
861 "gaim_prefs_get_int: Unknown pref %s\n", name); | |
862 return 0; | |
863 } else if(pref->type != GAIM_PREF_INT) { | |
864 gaim_debug_error("prefs", | |
865 "gaim_prefs_get_int: %s not an integer pref\n", name); | |
866 return 0; | |
867 } | |
868 | |
869 return pref->value.integer; | |
870 } | |
871 | |
872 const char * | |
873 gaim_prefs_get_string(const char *name) | |
874 { | |
875 struct gaim_pref *pref = find_pref(name); | |
876 | |
877 if(!pref) { | |
878 gaim_debug_error("prefs", | |
879 "gaim_prefs_get_string: Unknown pref %s\n", name); | |
880 return NULL; | |
881 } else if(pref->type != GAIM_PREF_STRING) { | |
882 gaim_debug_error("prefs", | |
883 "gaim_prefs_get_string: %s not a string pref\n", name); | |
884 return NULL; | |
885 } | |
886 | |
887 return pref->value.string; | |
888 } | |
889 | |
890 GList * | |
891 gaim_prefs_get_string_list(const char *name) | |
892 { | |
893 struct gaim_pref *pref = find_pref(name); | |
894 GList *ret = NULL, *tmp; | |
895 | |
896 if(!pref) { | |
897 gaim_debug_error("prefs", | |
898 "gaim_prefs_get_string_list: Unknown pref %s\n", name); | |
899 return NULL; | |
900 } else if(pref->type != GAIM_PREF_STRING_LIST) { | |
901 gaim_debug_error("prefs", | |
902 "gaim_prefs_get_string_list: %s not a string list pref\n", name); | |
903 return NULL; | |
904 } | |
905 | |
906 for(tmp = pref->value.stringlist; tmp; tmp = tmp->next) | |
907 ret = g_list_append(ret, g_strdup(tmp->data)); | |
908 | |
909 return ret; | |
910 } | |
911 | |
912 void | |
913 gaim_prefs_rename(const char *oldname, const char *newname) | |
914 { | |
915 struct gaim_pref *oldpref, *newpref; | |
916 | |
917 oldpref = find_pref(oldname); | |
918 | |
919 /* it's already been renamed, call off the dogs */ | |
920 if(!oldpref) | |
921 return; | |
922 | |
13710
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
923 if (oldpref->first_child != NULL) /* can't rename parents */ |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
924 { |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
925 gaim_debug_error("prefs", "Unable to rename %s to %s: can't rename parents\n", oldname, newname); |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
926 return; |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
927 } |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
928 |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
929 |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
930 newpref = find_pref(newname); |
10443 | 931 |
13710
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
932 if (newpref == NULL) |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
933 { |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
934 gaim_debug_error("prefs", "Unable to rename %s to %s: new pref not created\n", oldname, newname); |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
935 return; |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
936 } |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
937 |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
938 if (oldpref->type != newpref->type) |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
939 { |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
940 gaim_debug_error("prefs", "Unable to rename %s to %s: differing types\n", oldname, newname); |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
941 return; |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
942 } |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
943 |
848c9685ecc7
[gaim-migrate @ 16114]
Richard Laager <rlaager@wiktel.com>
parents:
13709
diff
changeset
|
944 gaim_debug_info("prefs", "Renaming %s to %s\n", oldname, newname); |
10443 | 945 |
946 switch(oldpref->type) { | |
947 case GAIM_PREF_NONE: | |
948 break; | |
949 case GAIM_PREF_BOOLEAN: | |
950 gaim_prefs_set_bool(newname, oldpref->value.boolean); | |
951 break; | |
952 case GAIM_PREF_INT: | |
953 gaim_prefs_set_int(newname, oldpref->value.integer); | |
954 break; | |
955 case GAIM_PREF_STRING: | |
956 gaim_prefs_set_string(newname, oldpref->value.string); | |
957 break; | |
958 case GAIM_PREF_STRING_LIST: | |
959 gaim_prefs_set_string_list(newname, oldpref->value.stringlist); | |
960 break; | |
961 } | |
962 | |
963 remove_pref(oldpref); | |
964 } | |
965 | |
966 void | |
967 gaim_prefs_rename_boolean_toggle(const char *oldname, const char *newname) | |
968 { | |
969 struct gaim_pref *oldpref, *newpref; | |
970 | |
971 oldpref = find_pref(oldname); | |
972 | |
973 /* it's already been renamed, call off the cats */ | |
974 if(!oldpref) | |
975 return; | |
976 | |
13709
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
977 if (oldpref->type != GAIM_PREF_BOOLEAN) |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
978 { |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
979 gaim_debug_error("prefs", "Unable to rename %s to %s: old pref not a boolean\n", oldname, newname); |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
980 return; |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
981 } |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
982 |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
983 if (oldpref->first_child != NULL) /* can't rename parents */ |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
984 { |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
985 gaim_debug_error("prefs", "Unable to rename %s to %s: can't rename parents\n", oldname, newname); |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
986 return; |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
987 } |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
988 |
11736 | 989 |
13709
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
990 newpref = find_pref(newname); |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
991 |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
992 if (newpref == NULL) |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
993 { |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
994 gaim_debug_error("prefs", "Unable to rename %s to %s: new pref not created\n", oldname, newname); |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
995 return; |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
996 } |
10443 | 997 |
13709
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
998 if (oldpref->type != newpref->type) |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
999 { |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
1000 gaim_debug_error("prefs", "Unable to rename %s to %s: differing types\n", oldname, newname); |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
1001 return; |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
1002 } |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
1003 |
ff25f6d2b484
[gaim-migrate @ 16113]
Richard Laager <rlaager@wiktel.com>
parents:
13638
diff
changeset
|
1004 gaim_debug_info("prefs", "Renaming and toggling %s to %s\n", oldname, newname); |
10443 | 1005 gaim_prefs_set_bool(newname, !(oldpref->value.boolean)); |
1006 | |
1007 remove_pref(oldpref); | |
1008 } | |
1009 | |
1010 guint | |
1011 gaim_prefs_connect_callback(void *handle, const char *name, GaimPrefCallback func, gpointer data) | |
1012 { | |
1013 struct gaim_pref *pref; | |
1014 struct pref_cb *cb; | |
1015 static guint cb_id = 0; | |
1016 | |
1017 pref = find_pref(name); | |
1018 if (pref == NULL) | |
1019 return 0; | |
1020 | |
1021 cb = g_new0(struct pref_cb, 1); | |
1022 | |
1023 cb->func = func; | |
1024 cb->data = data; | |
1025 cb->id = ++cb_id; | |
1026 cb->handle = handle; | |
1027 | |
1028 pref->callbacks = g_slist_append(pref->callbacks, cb); | |
1029 | |
1030 return cb->id; | |
1031 } | |
1032 | |
1033 static gboolean | |
1034 disco_callback_helper(struct gaim_pref *pref, guint callback_id) | |
1035 { | |
1036 GSList *cbs; | |
1037 struct gaim_pref *child; | |
1038 | |
1039 if(!pref) | |
1040 return FALSE; | |
1041 | |
1042 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { | |
1043 struct pref_cb *cb = cbs->data; | |
1044 if(cb->id == callback_id) { | |
11719
109ee3bfeac5
[gaim-migrate @ 14010]
Richard Laager <rlaager@wiktel.com>
parents:
11698
diff
changeset
|
1045 pref->callbacks = g_slist_delete_link(pref->callbacks, cbs); |
10443 | 1046 g_free(cb); |
1047 return TRUE; | |
1048 } | |
1049 } | |
1050 | |
1051 for(child = pref->first_child; child; child = child->sibling) { | |
1052 if(disco_callback_helper(child, callback_id)) | |
1053 return TRUE; | |
1054 } | |
1055 | |
1056 return FALSE; | |
1057 } | |
1058 | |
1059 void | |
1060 gaim_prefs_disconnect_callback(guint callback_id) | |
1061 { | |
1062 disco_callback_helper(&prefs, callback_id); | |
1063 } | |
1064 | |
1065 static void | |
1066 disco_callback_helper_handle(struct gaim_pref *pref, void *handle) | |
1067 { | |
1068 GSList *cbs; | |
1069 struct gaim_pref *child; | |
1070 | |
1071 if(!pref) | |
1072 return; | |
1073 | |
1074 cbs = pref->callbacks; | |
1075 while (cbs != NULL) { | |
1076 struct pref_cb *cb = cbs->data; | |
1077 if(cb->handle == handle) { | |
11719
109ee3bfeac5
[gaim-migrate @ 14010]
Richard Laager <rlaager@wiktel.com>
parents:
11698
diff
changeset
|
1078 pref->callbacks = g_slist_delete_link(pref->callbacks, cbs); |
10443 | 1079 g_free(cb); |
1080 cbs = pref->callbacks; | |
1081 } else | |
1082 cbs = cbs->next; | |
1083 } | |
1084 | |
1085 for(child = pref->first_child; child; child = child->sibling) | |
1086 disco_callback_helper_handle(child, handle); | |
1087 } | |
1088 | |
1089 void | |
1090 gaim_prefs_disconnect_by_handle(void *handle) | |
1091 { | |
1092 g_return_if_fail(handle != NULL); | |
1093 | |
1094 disco_callback_helper_handle(&prefs, handle); | |
1095 } | |
1096 | |
1097 void | |
1098 gaim_prefs_update_old() | |
1099 { | |
8900 | 1100 /* Remove some no-longer-used prefs */ |
9594 | 1101 gaim_prefs_remove("/core/away/auto_response/enabled"); |
1102 gaim_prefs_remove("/core/away/auto_response/idle_only"); | |
8948 | 1103 gaim_prefs_remove("/core/away/auto_response/in_active_conv"); |
1104 gaim_prefs_remove("/core/away/auto_response/sec_before_resend"); | |
9594 | 1105 gaim_prefs_remove("/core/away/auto_response"); |
11654 | 1106 gaim_prefs_remove("/core/away/default_message"); |
10353 | 1107 gaim_prefs_remove("/core/buddies/use_server_alias"); |
8942 | 1108 gaim_prefs_remove("/core/conversations/away_back_on_send"); |
8900 | 1109 gaim_prefs_remove("/core/conversations/send_urls_as_links"); |
8942 | 1110 gaim_prefs_remove("/core/conversations/im/show_login"); |
8998 | 1111 gaim_prefs_remove("/core/conversations/chat/show_join"); |
1112 gaim_prefs_remove("/core/conversations/chat/show_leave"); | |
9251 | 1113 gaim_prefs_remove("/core/conversations/combine_chat_im"); |
10389 | 1114 gaim_prefs_remove("/core/conversations/use_alias_for_title"); |
11698 | 1115 gaim_prefs_remove("/core/logging/log_signon_signoff"); |
1116 gaim_prefs_remove("/core/logging/log_idle_state"); | |
1117 gaim_prefs_remove("/core/logging/log_away_state"); | |
1118 gaim_prefs_remove("/core/logging/log_own_states"); | |
13040
b705e30efe61
[gaim-migrate @ 15399]
Richard Laager <rlaager@wiktel.com>
parents:
12822
diff
changeset
|
1119 gaim_prefs_remove("/core/status/scores/hidden"); |
11959 | 1120 gaim_prefs_remove("/plugins/core/autorecon/hide_connected_error"); |
1121 gaim_prefs_remove("/plugins/core/autorecon/hide_connecting_error"); | |
1122 gaim_prefs_remove("/plugins/core/autorecon/hide_reconnecting_dialog"); | |
1123 gaim_prefs_remove("/plugins/core/autorecon/restore_state"); | |
1124 gaim_prefs_remove("/plugins/core/autorecon"); | |
8900 | 1125 } |
10443 | 1126 |
1127 void * | |
1128 gaim_prefs_get_handle(void) | |
1129 { | |
1130 static int handle; | |
1131 | |
1132 return &handle; | |
1133 } | |
1134 | |
1135 void | |
1136 gaim_prefs_init(void) | |
1137 { | |
1138 void *handle = gaim_prefs_get_handle(); | |
1139 | |
1140 prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
1141 | |
1142 gaim_prefs_connect_callback(handle, "/", prefs_save_cb, NULL); | |
1143 | |
1144 gaim_prefs_add_none("/core"); | |
1145 gaim_prefs_add_none("/plugins"); | |
1146 gaim_prefs_add_none("/plugins/core"); | |
1147 gaim_prefs_add_none("/plugins/lopl"); | |
1148 gaim_prefs_add_none("/plugins/prpl"); | |
1149 | |
1150 /* Away */ | |
1151 gaim_prefs_add_none("/core/away"); | |
12573 | 1152 gaim_prefs_add_string("/core/away/idle_reporting", "system"); |
10443 | 1153 gaim_prefs_add_bool("/core/away/away_when_idle", TRUE); |
1154 gaim_prefs_add_int("/core/away/mins_before_away", 5); | |
1155 | |
1156 /* Away -> Auto-Reply */ | |
1157 if (!gaim_prefs_exists("/core/away/auto_response/enabled") || | |
12615
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1158 !gaim_prefs_exists("/core/away/auto_response/idle_only")) |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1159 { |
10443 | 1160 gaim_prefs_add_string("/core/away/auto_reply", "awayidle"); |
12615
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1161 } |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1162 else |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1163 { |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1164 if (!gaim_prefs_get_bool("/core/away/auto_response/enabled")) |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1165 { |
10443 | 1166 gaim_prefs_add_string("/core/away/auto_reply", "never"); |
12615
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1167 } |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1168 else |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1169 { |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1170 if (gaim_prefs_get_bool("/core/away/auto_response/idle_only")) |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1171 { |
10443 | 1172 gaim_prefs_add_string("/core/away/auto_reply", "awayidle"); |
12615
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1173 } |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1174 else |
e3ca84a8b551
[gaim-migrate @ 14951]
Richard Laager <rlaager@wiktel.com>
parents:
12599
diff
changeset
|
1175 { |
10443 | 1176 gaim_prefs_add_string("/core/away/auto_reply", "away"); |
1177 } | |
1178 } | |
1179 } | |
1180 | |
1181 /* Buddies */ | |
1182 gaim_prefs_add_none("/core/buddies"); | |
1183 | |
1184 /* Contact Priority Settings */ | |
1185 gaim_prefs_add_none("/core/contact"); | |
1186 gaim_prefs_add_bool("/core/contact/last_match", FALSE); | |
12164
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
11959
diff
changeset
|
1187 gaim_prefs_remove("/core/contact/offline_score"); |
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
11959
diff
changeset
|
1188 gaim_prefs_remove("/core/contact/away_score"); |
281ab2ecc08c
[gaim-migrate @ 14465]
Richard Laager <rlaager@wiktel.com>
parents:
11959
diff
changeset
|
1189 gaim_prefs_remove("/core/contact/idle_score"); |
10443 | 1190 } |
1191 | |
1192 void | |
1193 gaim_prefs_uninit() | |
1194 { | |
1195 if (save_timer != 0) | |
1196 { | |
1197 gaim_timeout_remove(save_timer); | |
1198 save_timer = 0; | |
1199 sync_prefs(); | |
1200 } | |
1201 | |
1202 gaim_prefs_disconnect_by_handle(gaim_prefs_get_handle()); | |
1203 } |