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