Mercurial > pidgin
annotate src/prefs.c @ 5529:e7747cae9710
[gaim-migrate @ 5929]
Fixed some bugs on loading, and added a couple default keys.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Mon, 26 May 2003 07:06:20 +0000 |
| parents | 156e65ca910f |
| children | b4c32b9a797d |
| 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 |
| 5440 | 274 g_return_if_fail(pref != NULL); |
| 275 g_return_if_fail(pref->type == GAIM_PREF_BOOLEAN); | |
| 4325 | 276 |
| 5440 | 277 if(pref->value.boolean != value) { |
| 278 pref->value.boolean = value; | |
| 279 do_callbacks(name, pref); | |
| 4288 | 280 } |
| 4324 | 281 } |
| 4325 | 282 |
| 5440 | 283 void gaim_prefs_set_int(const char *name, int value) { |
| 284 struct gaim_pref *pref = find_pref(name); | |
| 4325 | 285 |
| 5440 | 286 g_return_if_fail(pref != NULL); |
| 287 g_return_if_fail(pref->type == GAIM_PREF_INT); | |
| 4325 | 288 |
| 5440 | 289 if(pref->value.integer != value) { |
| 290 pref->value.integer = value; | |
| 291 do_callbacks(name, pref); | |
| 292 } | |
| 4326 | 293 } |
| 294 | |
| 5451 | 295 void gaim_prefs_set_string(const char *name, const char *value) { |
| 5440 | 296 struct gaim_pref *pref = find_pref(name); |
| 4325 | 297 |
| 5440 | 298 g_return_if_fail(pref != NULL); |
| 299 g_return_if_fail(pref->type == GAIM_PREF_STRING); | |
| 4325 | 300 |
| 5440 | 301 if(strcmp(pref->value.string, value)) { |
| 302 g_free(pref->value.string); | |
| 303 pref->value.string = g_strdup(value); | |
| 304 do_callbacks(name, pref); | |
| 4325 | 305 } |
| 306 } | |
| 307 | |
| 5440 | 308 gpointer gaim_prefs_get_generic(const char *name) { |
| 309 struct gaim_pref *pref = find_pref(name); | |
| 4325 | 310 |
| 5440 | 311 g_return_val_if_fail(pref != NULL, NULL); |
| 4288 | 312 |
| 5440 | 313 return pref->value.generic; |
| 4288 | 314 } |
| 315 | |
| 5440 | 316 gboolean gaim_prefs_get_bool(const char *name) { |
| 317 struct gaim_pref *pref = find_pref(name); | |
| 3427 | 318 |
| 5440 | 319 g_return_val_if_fail(pref != NULL, FALSE); |
| 320 g_return_val_if_fail(pref->type == GAIM_PREF_BOOLEAN, FALSE); | |
| 3472 | 321 |
| 5440 | 322 return pref->value.boolean; |
| 3366 | 323 } |
| 324 | |
| 5440 | 325 int gaim_prefs_get_int(const char *name) { |
| 326 struct gaim_pref *pref = find_pref(name); | |
| 3500 | 327 |
| 5440 | 328 g_return_val_if_fail(pref != NULL, 0); |
| 329 g_return_val_if_fail(pref->type == GAIM_PREF_INT, 0); | |
| 3366 | 330 |
| 5440 | 331 return pref->value.integer; |
| 3366 | 332 } |
| 333 | |
| 5440 | 334 char *gaim_prefs_get_string(const char *name) { |
| 335 struct gaim_pref *pref = find_pref(name); | |
| 3366 | 336 |
| 5440 | 337 g_return_val_if_fail(pref != NULL, NULL); |
| 338 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
|
339 |
| 5440 | 340 return pref->value.string; |
|
4469
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
341 } |
|
d76095396a0e
[gaim-migrate @ 4744]
Christian Hammond <chipx86@chipx86.com>
parents:
4461
diff
changeset
|
342 |
| 5440 | 343 guint gaim_prefs_connect_callback(const char *name, GaimPrefCallback func, gpointer data) |
| 344 { | |
| 345 struct gaim_pref *pref = find_pref(name); | |
| 346 struct pref_cb *cb; | |
| 347 static guint cb_id = 0; | |
| 3366 | 348 |
| 5440 | 349 if(!pref) |
| 350 return 0; | |
| 3366 | 351 |
| 5440 | 352 cb = g_new0(struct pref_cb, 1); |
|
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
353 |
| 5440 | 354 cb->func = func; |
| 355 cb->data = data; | |
| 356 cb->id = ++cb_id; | |
| 4991 | 357 |
| 5440 | 358 pref->callbacks = g_slist_append(pref->callbacks, cb); |
| 3366 | 359 |
| 5440 | 360 return cb->id; |
| 3366 | 361 } |
| 362 | |
| 5440 | 363 gboolean disco_callback_helper(struct gaim_pref *pref, guint callback_id) { |
| 364 GSList *cbs; | |
| 365 struct gaim_pref *child; | |
| 2254 | 366 |
| 5440 | 367 if(!pref) |
| 368 return FALSE; | |
|
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1840
diff
changeset
|
369 |
| 5440 | 370 for(cbs = pref->callbacks; cbs; cbs = cbs->next) { |
| 371 struct pref_cb *cb = cbs->data; | |
| 372 if(cb->id == callback_id) { | |
| 373 pref->callbacks = g_slist_remove(pref->callbacks, cb); | |
| 374 g_free(cb); | |
| 375 return TRUE; | |
| 4428 | 376 } |
| 377 } | |
| 378 | |
| 5440 | 379 for(child = pref->first_child; child; child = child->sibling) { |
| 380 if(disco_callback_helper(child, callback_id)) | |
| 381 return TRUE; | |
| 4428 | 382 } |
|
4451
ce5b64fac95d
[gaim-migrate @ 4726]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4449
diff
changeset
|
383 |
| 5440 | 384 return FALSE; |
|
1757
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
385 } |
|
3dfe4aefd366
[gaim-migrate @ 1767]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1755
diff
changeset
|
386 |
| 5440 | 387 void gaim_prefs_disconnect_callback(guint callback_id) { |
| 388 disco_callback_helper(&prefs, callback_id); | |
|
2262
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
389 } |
|
9c8f353331e7
[gaim-migrate @ 2272]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2254
diff
changeset
|
390 |
| 5440 | 391 static void gaim_prefs_write(FILE *f, struct gaim_pref *pref, int depth) { |
| 392 struct gaim_pref *tmp; | |
| 393 char *esc; | |
| 394 int i; | |
| 3500 | 395 |
| 5440 | 396 if(!pref) { |
| 397 pref = &prefs; | |
| 3551 | 398 |
| 5440 | 399 fprintf(f, "<?xml version='1.0' encoding='UTF-8' ?>\n"); |
| 400 fprintf(f, "<pref name='/'"); | |
| 401 } else { | |
| 402 for(i=0; i<depth; i++) | |
| 403 fprintf(f, "\t"); | |
| 404 esc = g_markup_escape_text(pref->name, -1); | |
| 405 fprintf(f, "<pref name='%s'", esc); | |
| 406 g_free(esc); | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
407 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
408 |
| 5440 | 409 switch(pref->type) { |
| 410 case GAIM_PREF_NONE: | |
| 411 break; | |
| 412 case GAIM_PREF_BOOLEAN: | |
| 413 fprintf(f, " type='bool' value='%d'", pref->value.boolean); | |
| 414 break; | |
| 415 case GAIM_PREF_INT: | |
| 416 fprintf(f, " type='int' value='%d'", pref->value.integer); | |
| 417 break; | |
| 418 case GAIM_PREF_STRING: | |
| 419 esc = g_markup_escape_text(pref->value.string, -1); | |
| 420 fprintf(f, " type='string' value='%s'", esc); | |
| 421 g_free(esc); | |
| 422 break; | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
423 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
424 |
| 5440 | 425 if(pref->first_child) { |
| 426 fprintf(f, ">\n"); | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
427 |
| 5440 | 428 for(tmp = pref->first_child; tmp; tmp = tmp->sibling) |
| 429 gaim_prefs_write(f, tmp, depth+1); | |
| 430 for(i=0; i<depth; i++) | |
| 431 fprintf(f, "\t"); | |
| 432 fprintf(f, "</pref>\n"); | |
| 433 } else { | |
| 434 fprintf(f, " />\n"); | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
435 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
436 } |
|
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
437 |
| 5440 | 438 void gaim_prefs_save() { |
| 439 /* FIXME: do this with timers so we don't save so damn often */ | |
| 440 gaim_prefs_sync(); | |
| 441 } | |
|
5205
fefad67de2c7
[gaim-migrate @ 5573]
Christian Hammond <chipx86@chipx86.com>
parents:
5105
diff
changeset
|
442 |
| 5440 | 443 void gaim_prefs_sync() { |
| 444 FILE *file; | |
| 445 const char *user_dir = gaim_user_dir(); | |
| 446 char *filename; | |
| 447 char *filename_real; | |
| 3551 | 448 |
| 5440 | 449 if(!user_dir) |
| 450 return; | |
| 3551 | 451 |
| 5440 | 452 file = fopen(user_dir, "r"); |
| 453 if(!file) | |
| 454 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
| 455 else | |
| 456 fclose(file); | |
| 3551 | 457 |
| 5440 | 458 filename = g_build_filename(user_dir, "prefs.xml.save", NULL); |
| 3551 | 459 |
| 5440 | 460 if((file = fopen(filename, "w"))) { |
| 461 gaim_prefs_write(file, NULL, 0); | |
| 462 fclose(file); | |
| 463 chmod(filename, S_IRUSR | S_IWUSR); | |
| 464 } else { | |
| 465 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Unable to write %s\n", | |
| 466 filename); | |
| 467 } | |
| 3551 | 468 |
| 5440 | 469 filename_real = g_build_filename(user_dir, "prefs.xml", NULL); |
| 470 if(rename(filename, filename_real) < 0) | |
| 471 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error renaming %s to %s\n", | |
| 472 filename, filename_real); | |
| 3551 | 473 |
| 5440 | 474 g_free(filename); |
| 475 g_free(filename_real); | |
| 3551 | 476 } |
| 477 | |
| 5440 | 478 static GList *prefs_stack = NULL; |
|
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
479 |
| 5440 | 480 static void prefs_start_element_handler (GMarkupParseContext *context, |
| 481 const gchar *element_name, | |
| 482 const gchar **attribute_names, | |
| 483 const gchar **attribute_values, | |
| 484 gpointer user_data, | |
| 485 GError **error) { | |
| 486 GaimPrefType pref_type = GAIM_PREF_NONE; | |
| 487 int i; | |
| 488 const char *pref_name = NULL, *pref_value = NULL; | |
| 489 GString *pref_name_full; | |
| 490 GList *tmp; | |
| 3366 | 491 |
| 5440 | 492 if(strcmp(element_name, "pref")) |
| 493 return; | |
| 3500 | 494 |
| 5440 | 495 for(i = 0; attribute_names[i]; i++) { |
| 496 if(!strcmp(attribute_names[i], "name")) { | |
| 497 pref_name = attribute_values[i]; | |
| 498 } else if(!strcmp(attribute_names[i], "type")) { | |
| 499 if(!strcmp(attribute_values[i], "bool")) | |
| 500 pref_type = GAIM_PREF_BOOLEAN; | |
| 501 else if(!strcmp(attribute_values[i], "int")) | |
| 502 pref_type = GAIM_PREF_INT; | |
| 503 else if(!strcmp(attribute_values[i], "string")) | |
| 504 pref_type = GAIM_PREF_STRING; | |
| 505 else | |
| 506 return; | |
| 507 } else if(!strcmp(attribute_names[i], "value")) { | |
| 508 pref_value = attribute_values[i]; | |
| 509 } | |
| 510 } | |
|
873
789df4b47508
[gaim-migrate @ 883]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
864
diff
changeset
|
511 |
| 5440 | 512 if(!pref_name || !strcmp(pref_name, "/")) |
| 513 return; | |
|
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
514 |
| 5440 | 515 pref_name_full = g_string_new(pref_name); |
|
652
4d3285caa191
[gaim-migrate @ 662]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
619
diff
changeset
|
516 |
|
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
517 for(tmp = prefs_stack; tmp; tmp = tmp->next) { |
| 5440 | 518 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
| 519 pref_name_full = g_string_prepend(pref_name_full, tmp->data); | |
| 520 } | |
| 1170 | 521 |
| 5440 | 522 pref_name_full = g_string_prepend_c(pref_name_full, '/'); |
|
1253
8342d3aab1f1
[gaim-migrate @ 1263]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
523 |
|
5529
e7747cae9710
[gaim-migrate @ 5929]
Christian Hammond <chipx86@chipx86.com>
parents:
5458
diff
changeset
|
524 if(!find_pref(pref_name_full->str)) { |
| 5444 | 525 switch(pref_type) { |
| 526 case GAIM_PREF_NONE: | |
| 527 gaim_prefs_add_none(pref_name_full->str); | |
| 528 break; | |
| 529 case GAIM_PREF_BOOLEAN: | |
| 530 gaim_prefs_add_bool(pref_name_full->str, atoi(pref_value)); | |
| 531 break; | |
| 532 case GAIM_PREF_INT: | |
| 533 gaim_prefs_add_int(pref_name_full->str, atoi(pref_value)); | |
| 534 break; | |
| 535 case GAIM_PREF_STRING: | |
| 536 gaim_prefs_add_string(pref_name_full->str, pref_value); | |
| 537 break; | |
| 538 } | |
| 539 } else { | |
| 540 switch(pref_type) { | |
| 541 case GAIM_PREF_NONE: | |
| 542 break; | |
| 543 case GAIM_PREF_BOOLEAN: | |
| 544 gaim_prefs_set_bool(pref_name_full->str, atoi(pref_value)); | |
| 545 break; | |
| 546 case GAIM_PREF_INT: | |
| 547 gaim_prefs_set_int(pref_name_full->str, atoi(pref_value)); | |
| 548 break; | |
| 549 case GAIM_PREF_STRING: | |
| 550 gaim_prefs_set_string(pref_name_full->str, pref_value); | |
| 551 break; | |
| 552 } | |
| 5440 | 553 } |
| 1170 | 554 |
| 5440 | 555 prefs_stack = g_list_prepend(prefs_stack, g_strdup(pref_name)); |
| 556 g_string_free(pref_name_full, TRUE); | |
| 1170 | 557 } |
| 558 | |
| 5440 | 559 static void prefs_end_element_handler(GMarkupParseContext *context, |
| 560 const gchar *element_name, gpointer user_data, GError **error) { | |
| 561 if(!strcmp(element_name, "pref")) { | |
| 562 prefs_stack = g_list_delete_link(prefs_stack, prefs_stack); | |
| 563 } | |
| 1170 | 564 } |
| 565 | |
| 5440 | 566 static GMarkupParser prefs_parser = { |
| 567 prefs_start_element_handler, | |
| 568 prefs_end_element_handler, | |
| 569 NULL, | |
| 570 NULL, | |
| 571 NULL | |
| 572 }; | |
| 1170 | 573 |
| 5440 | 574 void gaim_prefs_load() { |
| 575 gchar *filename = g_build_filename(gaim_user_dir(), "prefs.xml", NULL); | |
| 576 gchar *contents = NULL; | |
| 577 gsize length; | |
| 578 GMarkupParseContext *context; | |
| 579 GError *error = NULL; | |
|
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
580 |
| 5440 | 581 if(!filename) |
| 582 return; | |
| 583 | |
| 584 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename); | |
|
5314
1f901484599d
[gaim-migrate @ 5686]
Christian Hammond <chipx86@chipx86.com>
parents:
5297
diff
changeset
|
585 |
| 5440 | 586 if(!g_file_get_contents(filename, &contents, &length, &error)) { |
| 587 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n", | |
| 588 error->message); | |
| 589 g_error_free(error); | |
| 590 return; | |
| 1170 | 591 } |
| 592 | |
| 5440 | 593 context = g_markup_parse_context_new(&prefs_parser, 0, NULL, NULL); |
| 594 | |
| 595 if(!g_markup_parse_context_parse(context, contents, length, NULL)) { | |
| 596 g_markup_parse_context_free(context); | |
| 597 g_free(contents); | |
| 598 return; | |
| 599 } | |
| 600 | |
| 601 if(!g_markup_parse_context_end_parse(context, NULL)) { | |
| 602 gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename); | |
| 603 g_markup_parse_context_free(context); | |
| 604 g_free(contents); | |
| 605 return; | |
| 606 } | |
| 607 | |
| 608 g_markup_parse_context_free(context); | |
| 609 g_free(contents); | |
| 610 | |
| 611 gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename); | |
| 612 g_free(filename); | |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
613 } |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1002
diff
changeset
|
614 |
| 3366 | 615 |
