Sun, 18 May 2003 21:54:11 +0000
[gaim-migrate @ 5823]
this would be helpful
| 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
6f7d28b0f98d
[gaim-migrate @ 359]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
340
diff
changeset
|
22 | #ifdef HAVE_CONFIG_H |
|
2090
bab8b7e309db
[gaim-migrate @ 2100]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
2074
diff
changeset
|
23 | #include <config.h> |
|
349
6f7d28b0f98d
[gaim-migrate @ 359]
Eric Warmenhoven <warmenhoven@yahoo.com>
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
4ec5559caae9
[gaim-migrate @ 4230]
Herman Bloggs <herman@bluedigits.com>
parents:
4010
diff
changeset
|
36 | #ifdef _WIN32 |
|
4ec5559caae9
[gaim-migrate @ 4230]
Herman Bloggs <herman@bluedigits.com>
parents:
4010
diff
changeset
|
37 | #include "win32dep.h" |
|
4ec5559caae9
[gaim-migrate @ 4230]
Herman Bloggs <herman@bluedigits.com>
parents:
4010
diff
changeset
|
38 | #endif |
|
4ec5559caae9
[gaim-migrate @ 4230]
Herman Bloggs <herman@bluedigits.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 | ||
| 66 | void gaim_prefs_init() { | |
| 67 | prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
| 68 | ||
| 69 | /* XXX: this is where you would want to put prefs declarations */ | |
| 70 | } | |
| 3366 | 71 | |
| 5440 | 72 | static char *pref_full_name(struct gaim_pref *pref) { |
| 73 | GString *name; | |
| 74 | struct gaim_pref *parent; | |
| 75 | if(!pref) | |
| 76 | return NULL; | |
| 77 | ||
| 78 | if(pref == &prefs) | |
| 79 | return g_strdup("/"); | |
| 80 | ||
| 81 | name = g_string_new(pref->name); | |
| 82 | parent = pref->parent; | |
| 3366 | 83 | |
| 5440 | 84 | for(parent = pref->parent; parent && parent->name; parent = parent->parent) { |
| 85 | name = g_string_prepend_c(name, '/'); | |
| 86 | name = g_string_prepend(name, parent->name); | |
| 87 | } | |
| 88 | g_string_free(name, FALSE); | |
| 89 | return name->str; | |
| 90 | } | |
| 91 | ||
| 92 | static struct gaim_pref *find_pref(const char *name) | |
| 93 | { | |
| 94 | if(!name || name[0] != '/') { | |
| 95 | return NULL; | |
| 96 | } else if(name[1] == '\0') { | |
| 97 | return &prefs; | |
| 98 | } else { | |
| 99 | return g_hash_table_lookup(prefs_hash, name); | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | static struct gaim_pref *find_pref_parent(const char *name) | |
| 104 | { | |
| 105 | char *parent_name = g_path_get_dirname(name); | |
| 106 | struct gaim_pref *ret = &prefs; | |
| 107 | ||
| 108 | if(strcmp(parent_name, "/")) { | |
| 109 | ret = find_pref(parent_name); | |
| 110 | } | |
| 1026 | 111 | |
| 5440 | 112 | g_free(parent_name); |
| 113 | return ret; | |
| 114 | } | |
| 115 | ||
| 116 | static void free_pref_value(struct gaim_pref *pref) { | |
| 117 | switch(pref->type) { | |
| 118 | case GAIM_PREF_BOOLEAN: | |
| 119 | pref->value.boolean = FALSE; | |
| 120 | case GAIM_PREF_INT: | |
| 121 | pref->value.integer = 0; | |
| 122 | break; | |
| 123 | case GAIM_PREF_STRING: | |
| 124 | g_free(pref->value.string); | |
| 125 | pref->value.string = NULL; | |
| 126 | break; | |
| 127 | case GAIM_PREF_NONE: | |
| 128 | break; | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | static struct gaim_pref *add_pref(GaimPrefType type, const char *name) { | |
| 133 | struct gaim_pref *parent; | |
| 134 | struct gaim_pref *me; | |
| 135 | struct gaim_pref *sibling; | |
| 136 | char *my_name = g_path_get_basename(name); | |
| 137 | ||
| 138 | parent = find_pref_parent(name); | |
| 139 | ||
| 140 | if(!parent) | |
| 141 | return NULL; | |
|
1525
b4ece1a718cd
[gaim-migrate @ 1535]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1455
diff
changeset
|
142 | |
| 5440 | 143 | for(sibling = parent->first_child; sibling; sibling = sibling->sibling) { |
| 144 | if(!strcmp(sibling->name, my_name)) { | |
| 145 | g_free(my_name); | |
| 146 | return NULL; | |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 150 | me = g_new0(struct gaim_pref, 1); | |
| 151 | me->type = type; | |
| 152 | me->name = my_name; | |
| 153 | ||
| 154 | me->parent = parent; | |
| 155 | if(parent->first_child) { | |
| 156 | /* blatant abuse of a for loop */ | |
| 157 | for(sibling = parent->first_child; sibling->sibling; | |
| 158 | sibling = sibling->sibling); | |
| 159 | sibling->sibling = me; | |
| 160 | } else { | |
| 161 | parent->first_child = me; | |
| 162 | } | |
| 163 | ||
| 164 | g_hash_table_insert(prefs_hash, g_strdup(name), (gpointer)me); | |
| 165 | ||
| 166 | return me; | |
| 167 | } | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
168 | |
| 5440 | 169 | void gaim_prefs_add_none(const char *name) { |
| 170 | add_pref(GAIM_PREF_NONE, name); | |
| 171 | } | |
| 172 | ||
| 173 | void gaim_prefs_add_bool(const char *name, gboolean value) { | |
| 174 | struct gaim_pref *pref = add_pref(GAIM_PREF_BOOLEAN, name); | |
| 175 | ||
| 176 | if(!pref) | |
| 177 | return; | |
| 178 | ||
| 179 | pref->value.boolean = value; | |
| 180 | } | |
| 3630 | 181 | |
| 5440 | 182 | void gaim_prefs_add_int(const char *name, int value) { |
| 183 | struct gaim_pref *pref = add_pref(GAIM_PREF_INT, name); | |
| 184 | ||
| 185 | if(!pref) | |
| 186 | return; | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
187 | |
| 5440 | 188 | pref->value.integer = value; |
| 189 | } | |
| 190 | ||
| 191 | void gaim_prefs_add_string(const char *name, const char *value) { | |
| 192 | struct gaim_pref *pref = add_pref(GAIM_PREF_STRING, name); | |
| 193 | ||
| 194 | if(!pref) | |
| 195 | return; | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
196 | |
| 5440 | 197 | pref->value.string = g_strdup(value); |
| 198 | } | |
| 199 | ||
| 200 | void remove_pref(struct gaim_pref *pref) { | |
| 201 | char *name; | |
| 202 | ||
| 203 | if(!pref || pref == &prefs) | |
| 204 | return; | |
| 205 | ||
| 206 | if(pref->parent->first_child == pref) { | |
| 207 | pref->parent->first_child = pref->sibling; | |
| 208 | } else { | |
| 209 | struct gaim_pref *sib = pref->parent->first_child; | |
| 210 | while(sib->sibling != pref) | |
| 211 | sib = sib->sibling; | |
| 212 | sib->sibling = pref->sibling; | |
| 213 | } | |
| 214 | ||
| 215 | name = pref_full_name(pref); | |
| 216 | ||
| 217 | g_hash_table_remove(prefs_hash, name); | |
| 218 | g_free(name); | |
| 219 | ||
| 220 | free_pref_value(pref); | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
221 | |
| 5440 | 222 | g_slist_free(pref->callbacks); |
| 223 | g_free(pref->name); | |
| 224 | g_free(pref); | |
| 225 | } | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
226 | |
| 5440 | 227 | void gaim_prefs_remove(const char *name) { |
| 228 | struct gaim_pref *pref = find_pref(name); | |
| 229 | struct gaim_pref *child, *child2; | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
230 | |
| 5440 | 231 | if(!pref) |
| 232 | return; | |
| 233 | child = pref->first_child; | |
| 234 | while(child) { | |
| 235 | child2 = child; | |
| 236 | child = child->sibling; | |
| 237 | remove_pref(child2); | |
| 238 | } | |
| 239 | ||
| 240 | remove_pref(pref); | |
| 241 | } | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
242 | |
| 5440 | 243 | void gaim_prefs_destroy() { |
| 244 | gaim_prefs_remove("/"); | |
| 245 | } | |
| 246 | ||
| 247 | static void do_callbacks(const char* name, struct gaim_pref *pref) { | |
| 248 | GSList *cbs; | |
| 249 | struct gaim_pref *cb_pref; | |
| 250 | for(cb_pref = pref; cb_pref; cb_pref = cb_pref->parent) { | |
| 251 | for(cbs = cb_pref->callbacks; cbs; cbs = cbs->next) { | |
| 252 | struct pref_cb *cb = cbs->data; | |
| 253 | cb->func(name, pref->type, pref->value.generic, cb->data); | |
| 4215 | 254 | } |
| 255 | } | |
|
1525
b4ece1a718cd
[gaim-migrate @ 1535]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1455
diff
changeset
|
256 | } |
|
b4ece1a718cd
[gaim-migrate @ 1535]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1455
diff
changeset
|
257 | |
| 5440 | 258 | void gaim_prefs_set_generic(const char *name, gpointer value) { |
| 259 | struct gaim_pref *pref = find_pref(name); | |
| 3366 | 260 | |
| 5440 | 261 | g_return_if_fail(pref != NULL); |
| 3500 | 262 | |
| 5440 | 263 | pref->value.generic = value; |
| 264 | do_callbacks(name, pref); | |
| 3366 | 265 | } |
| 266 | ||
| 5440 | 267 | void gaim_prefs_set_bool(const char *name, gboolean value) { |
| 268 | struct gaim_pref *pref = find_pref(name); | |
| 4288 | 269 | |
| 5440 | 270 | g_return_if_fail(pref != NULL); |
| 271 | g_return_if_fail(pref->type == GAIM_PREF_BOOLEAN); | |
| 4325 | 272 | |
| 5440 | 273 | if(pref->value.boolean != value) { |
| 274 | pref->value.boolean = value; | |
| 275 | do_callbacks(name, pref); | |
| 4288 | 276 | } |
| 4324 | 277 | } |
| 4325 | 278 | |
| 5440 | 279 | void gaim_prefs_set_int(const char *name, int value) { |
| 280 | struct gaim_pref *pref = find_pref(name); | |
| 4325 | 281 | |
| 5440 | 282 | g_return_if_fail(pref != NULL); |
| 283 | g_return_if_fail(pref->type == GAIM_PREF_INT); | |
| 4325 | 284 | |
| 5440 | 285 | if(pref->value.integer != value) { |
| 286 | pref->value.integer = value; | |
| 287 | do_callbacks(name, pref); | |
| 288 | } | |
| 4326 | 289 | } |
| 290 | ||
| 5440 | 291 | void gaim_prefs_set_string(const char *name, char *value) { |
| 292 | struct gaim_pref *pref = find_pref(name); | |
| 4325 | 293 | |
| 5440 | 294 | g_return_if_fail(pref != NULL); |
| 295 | g_return_if_fail(pref->type == GAIM_PREF_STRING); | |
| 4325 | 296 | |
| 5440 | 297 | if(strcmp(pref->value.string, value)) { |
| 298 | g_free(pref->value.string); | |
| 299 | pref->value.string = g_strdup(value); | |
| 300 | do_callbacks(name, pref); | |
| 4325 | 301 | } |
| 302 | } | |
| 303 | ||
| 5440 | 304 | gpointer gaim_prefs_get_generic(const char *name) { |
| 305 | struct gaim_pref *pref = find_pref(name); | |
| 4325 | 306 | |
| 5440 | 307 | g_return_val_if_fail(pref != NULL, NULL); |
| 4288 | 308 | |
| 5440 | 309 | return pref->value.generic; |
| 4288 | 310 | } |
| 311 | ||
| 5440 | 312 | gboolean gaim_prefs_get_bool(const char *name) { |
| 313 | struct gaim_pref *pref = find_pref(name); | |
| 3427 | 314 | |
| 5440 | 315 | g_return_val_if_fail(pref != NULL, FALSE); |
| 316 | g_return_val_if_fail(pref->type == GAIM_PREF_BOOLEAN, FALSE); | |
|
3472
3939deb42c1e
[gaim-migrate @ 3523]
Robert McQueen <robot101@debian.org>
parents:
3466
diff
changeset
|
317 | |
| 5440 | 318 | return pref->value.boolean; |
| 3366 | 319 | } |
| 320 | ||
| 5440 | 321 | int gaim_prefs_get_int(const char *name) { |
| 322 | struct gaim_pref *pref = find_pref(name); | |
| 3500 | 323 | |
| 5440 | 324 | g_return_val_if_fail(pref != NULL, 0); |
| 325 | g_return_val_if_fail(pref->type == GAIM_PREF_INT, 0); | |
| 3366 | 326 | |
| 5440 | 327 | return pref->value.integer; |
| 3366 | 328 | } |
| 329 | ||
| 5440 | 330 | char *gaim_prefs_get_string(const char *name) { |
| 331 | struct gaim_pref *pref = find_pref(name); | |
| 3366 | 332 | |
| 5440 | 333 | g_return_val_if_fail(pref != NULL, NULL); |
| 334 | g_return_val_if_fail(pref->type == GAIM_PREF_STRING, NULL); | |
|
4469
ef60c820b884
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
335 | |
| 5440 | 336 | return pref->value.string; |
|
4469
ef60c820b884
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
337 | } |
|
ef60c820b884
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
338 | |
| 5440 | 339 | guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
| 340 | { | |
| 341 | struct gaim_pref *pref = find_pref(name); | |
| 342 | struct pref_cb *cb; | |
| 343 | static guint cb_id = 0; | |
| 3366 | 344 | |
| 5440 | 345 | if(!pref) |
| 346 | return 0; | |
| 3366 | 347 | |
| 5440 | 348 | cb = g_new0(struct pref_cb, 1); |
|
1881
bcd5d457cdbb
[gaim-migrate @ 1891]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1840
diff
changeset
|
349 | |
| 5440 | 350 | cb->func = func; |
| 351 | cb->data = data; | |
| 352 | cb->id = ++cb_id; | |
| 4991 | 353 | |
| 5440 | 354 | pref->callbacks = g_slist_append(pref->callbacks, cb); |
| 3366 | 355 | |
| 5440 | 356 | return cb->id; |
| 3366 | 357 | } |
| 358 | ||
| 5440 | 359 | gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
| 360 | GSList *cbs; | |
| 361 | struct gaim_pref *child; | |
| 2254 | 362 | |
| 5440 | 363 | if(!pref) |
| 364 | return FALSE; | |
|
1881
bcd5d457cdbb
[gaim-migrate @ 1891]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1840
diff
changeset
|
365 | |
| 5440 | 366 | for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
| 367 | struct pref_cb *cb = cbs->data; | |
| 368 | if(cb->id == callback_id) { | |
| 369 | pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
| 370 | g_free(cb); | |
| 371 | return TRUE; | |
| 4428 | 372 | } |
| 373 | } | |
| 374 | ||
| 5440 | 375 | for(child = pref->first_child; child; child = child->sibling) { |
| 376 | if(disco_callback_helper(child, callback_id)) | |
| 377 | return TRUE; | |
| 4428 | 378 | } |
|
4451
5a484f11e395
[gaim-migrate @ 4726]
Herman Bloggs <herman@bluedigits.com>
parents:
4449
diff
changeset
|
379 | |
| 5440 | 380 | return FALSE; |
|
1757
8c57846a4691
[gaim-migrate @ 1767]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1755
diff
changeset
|
381 | } |
|
8c57846a4691
[gaim-migrate @ 1767]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1755
diff
changeset
|
382 | |
| 5440 | 383 | void gaim_prefs_disconnect_callback(guint callback_id) { |
| 384 | disco_callback_helper(&prefs, callback_id); | |
| 2262 | 385 | } |
| 386 | ||
| 5440 | 387 | static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
| 388 | struct gaim_pref *tmp; | |
| 389 | char *esc; | |
| 390 | int i; | |
| 3500 | 391 | |
| 5440 | 392 | if(!pref) { |
| 393 | pref = &prefs; | |
| 3551 | 394 | |
| 5440 | 395 | fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
| 396 | fprintf(f, "<pref name='/'"); | |
| 397 | } else { | |
| 398 | for(i=0; i<depth; i++) | |
| 399 | fprintf(f, "\t"); | |
| 400 | esc = g_markup_escape_text(pref->name, -1); | |
| 401 | fprintf(f, "<pref name='%s'", esc); | |
| 402 | g_free(esc); | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
403 | } |
|
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
404 | |
| 5440 | 405 | switch(pref->type) { |
| 406 | case GAIM_PREF_NONE: | |
| 407 | break; | |
| 408 | case GAIM_PREF_BOOLEAN: | |
| 409 | fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
| 410 | break; | |
| 411 | case GAIM_PREF_INT: | |
| 412 | fprintf(f, " type='int' value='%d'", pref->value.integer); | |
| 413 | break; | |
| 414 | case GAIM_PREF_STRING: | |
| 415 | esc = g_markup_escape_text(pref->value.string, -1); | |
| 416 | fprintf(f, " type='string' value='%s'", esc); | |
| 417 | g_free(esc); | |
| 418 | break; | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
419 | } |
|
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
420 | |
| 5440 | 421 | if(pref->first_child) { |
| 422 | fprintf(f, ">\n"); | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
423 | |
| 5440 | 424 | for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
| 425 | gaim_prefs_write(f, tmp, depth+1); | |
| 426 | for(i=0; i<depth; i++) | |
| 427 | fprintf(f, "\t"); | |
| 428 | fprintf(f, "</pref>\n"); | |
| 429 | } else { | |
| 430 | fprintf(f, " />\n"); | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
431 | } |
|
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
432 | } |
|
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
433 | |
| 5440 | 434 | void gaim_prefs_save() { |
| 435 | /* FIXME: do this with timers so we don't save so damn often */ | |
| 436 | gaim_prefs_sync(); | |
| 437 | } | |
|
5205
242b8aa81328
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
438 | |
| 5440 | 439 | void gaim_prefs_sync() { |
| 440 | FILE *file; | |
| 441 | const char *user_dir = gaim_user_dir(); | |
| 442 | char *filename; | |
| 443 | char *filename_real; | |
| 3551 | 444 | |
| 5440 | 445 | if(!user_dir) |
| 446 | return; | |
| 3551 | 447 | |
| 5440 | 448 | file = fopen(user_dir, "r"); |
| 449 | if(!file) | |
| 450 | mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 451 | else | |
| 452 | fclose(file); | |
| 3551 | 453 | |
| 5440 | 454 | filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
| 3551 | 455 | |
| 5440 | 456 | if((file = fopen(filename, "w"))) { |
| 457 | gaim_prefs_write(file, NULL, 0); | |
| 458 | fclose(file); | |
| 459 | chmod(filename, S_IRUSR | S_IWUSR); | |
| 460 | } else { | |
| 461 | gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
| 462 | filename); | |
| 463 | } | |
| 3551 | 464 | |
| 5440 | 465 | filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
| 466 | if(rename(filename, filename_real) < 0) | |
| 467 | gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
| 468 | filename, filename_real); | |
| 3551 | 469 | |
| 5440 | 470 | g_free(filename); |
| 471 | g_free(filename_real); | |
| 3551 | 472 | } |
| 473 | ||
| 5440 | 474 | static GList *prefs_stack = NULL; |
|
873
d40eff5fc359
[gaim-migrate @ 883]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
864
diff
changeset
|
475 | |
| 5440 | 476 | static void prefs_start_element_handler (GMarkupParseContext *context, |
| 477 | const gchar *element_name, | |
| 478 | const gchar **attribute_names, | |
| 479 | const gchar **attribute_values, | |
| 480 | gpointer user_data, | |
| 481 | GError **error) { | |
| 482 | GaimPrefType pref_type = GAIM_PREF_NONE; | |
| 483 | int i; | |
| 484 | const char *pref_name = NULL, *pref_value = NULL; | |
| 485 | GString *pref_name_full; | |
| 486 | GList *tmp; | |
| 3366 | 487 | |
| 5440 | 488 | if(strcmp(element_name, "pref")) |
| 489 | return; | |
| 3500 | 490 | |
| 5440 | 491 | for(i = 0; attribute_names[i]; i++) { |
| 492 | if(!strcmp(attribute_names[i], "name")) { | |
| 493 | pref_name = attribute_values[i]; | |
| 494 | } else if(!strcmp(attribute_names[i], "type")) { | |
| 495 | if(!strcmp(attribute_values[i], "bool")) | |
| 496 | pref_type = GAIM_PREF_BOOLEAN; | |
| 497 | else if(!strcmp(attribute_values[i], "int")) | |
| 498 | pref_type = GAIM_PREF_INT; | |
| 499 | else if(!strcmp(attribute_values[i], "string")) | |
| 500 | pref_type = GAIM_PREF_STRING; | |
| 501 | else | |
| 502 | return; | |
| 503 | } else if(!strcmp(attribute_names[i], "value")) { | |
| 504 | pref_value = attribute_values[i]; | |
| 505 | } | |
| 506 | } | |
|
873
d40eff5fc359
[gaim-migrate @ 883]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
864
diff
changeset
|
507 | |
| 5440 | 508 | if(!pref_name || !strcmp(pref_name, "/")) |
| 509 | return; | |
|
652
dd4ccd3e5c72
[gaim-migrate @ 662]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
619
diff
changeset
|
510 | |
| 5440 | 511 | pref_name_full = g_string_new(pref_name); |
|
652
dd4ccd3e5c72
[gaim-migrate @ 662]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
619
diff
changeset
|
512 | |
| 5440 | 513 | for(tmp = prefs_stack; tmp; tmp = tmp->prev) { |
| 514 | pref_name_full = g_string_prepend_c(pref_name_full, '/'); | |
| 515 | pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
| 516 | } | |
| 1170 | 517 | |
| 5440 | 518 | pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
|
1253
f02697a6aada
[gaim-migrate @ 1263]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1250
diff
changeset
|
519 | |
| 5440 | 520 | switch(pref_type) { |
| 521 | case GAIM_PREF_NONE: | |
| 522 | gaim_prefs_add_none(pref_name_full->str); | |
| 523 | break; | |
| 524 | case GAIM_PREF_BOOLEAN: | |
| 525 | gaim_prefs_add_bool(pref_name_full->str, atoi(pref_value)); | |
| 526 | break; | |
| 527 | case GAIM_PREF_INT: | |
| 528 | gaim_prefs_add_int(pref_name_full->str, atoi(pref_value)); | |
| 529 | break; | |
| 530 | case GAIM_PREF_STRING: | |
| 531 | gaim_prefs_add_string(pref_name_full->str, pref_value); | |
| 532 | break; | |
| 533 | } | |
| 1170 | 534 | |
| 5440 | 535 | prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
| 536 | g_string_free(pref_name_full, TRUE); | |
| 1170 | 537 | } |
| 538 | ||
| 5440 | 539 | static void prefs_end_element_handler(GMarkupParseContext *context, |
| 540 | const gchar *element_name, gpointer user_data, GError **error) { | |
| 541 | if(!strcmp(element_name, "pref")) { | |
| 542 | prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
| 543 | } | |
| 1170 | 544 | } |
| 545 | ||
| 5440 | 546 | static GMarkupParser prefs_parser = { |
| 547 | prefs_start_element_handler, | |
| 548 | prefs_end_element_handler, | |
| 549 | NULL, | |
| 550 | NULL, | |
| 551 | NULL | |
| 552 | }; | |
| 1170 | 553 | |
| 5440 | 554 | void gaim_prefs_load() { |
| 555 | gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); | |
| 556 | gchar *contents = NULL; | |
| 557 | gsize length; | |
| 558 | GMarkupParseContext *context; | |
| 559 | GError *error = NULL; | |
|
5314
56ef6a09fb99
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
560 | |
| 5440 | 561 | if(!filename) |
| 562 | return; | |
| 563 | ||
| 564 | gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
|
5314
56ef6a09fb99
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
565 | |
| 5440 | 566 | if(!g_file_get_contents(filename, &contents, &length, &error)) { |
| 567 | gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
| 568 | error->message); | |
| 569 | g_error_free(error); | |
| 570 | return; | |
| 1170 | 571 | } |
| 572 | ||
| 5440 | 573 | context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
| 574 | ||
| 575 | if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
| 576 | g_markup_parse_context_free(context); | |
| 577 | g_free(contents); | |
| 578 | return; | |
| 579 | } | |
| 580 | ||
| 581 | if(!g_markup_parse_context_end_parse(context, NULL)) { | |
| 582 | gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
| 583 | g_markup_parse_context_free(context); | |
| 584 | g_free(contents); | |
| 585 | return; | |
| 586 | } | |
| 587 | ||
| 588 | g_markup_parse_context_free(context); | |
| 589 | g_free(contents); | |
| 590 | ||
| 591 | gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
| 592 | g_free(filename); | |
|
1006
fb2f2a403962
[gaim-migrate @ 1016]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1002
diff
changeset
|
593 | } |
|
fb2f2a403962
[gaim-migrate @ 1016]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1002
diff
changeset
|
594 | |
| 3366 | 595 |