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