Mercurial > pidgin.yaz
comparison src/account.c @ 10423:3232e1a33899
[gaim-migrate @ 11675]
Set version numbers for all our xml files
Use xmlnode for writing accounts.xml
Add 2 conveniencey helper functions to xmlnode
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 26 Dec 2004 18:38:22 +0000 |
parents | bed2c96bc1fb |
children | 108151be77a3 |
comparison
equal
deleted
inserted
replaced
10422:e0cf2f3f9929 | 10423:3232e1a33899 |
---|---|
32 #include "request.h" | 32 #include "request.h" |
33 #include "server.h" | 33 #include "server.h" |
34 #include "signals.h" | 34 #include "signals.h" |
35 #include "status.h" | 35 #include "status.h" |
36 #include "util.h" | 36 #include "util.h" |
37 #include "xmlnode.h" | |
37 | 38 |
38 typedef enum | 39 typedef enum |
39 { | 40 { |
40 TAG_NONE = 0, | 41 TAG_NONE = 0, |
41 TAG_PROTOCOL, | 42 TAG_PROTOCOL, |
1351 | 1352 |
1352 return TRUE; | 1353 return TRUE; |
1353 } | 1354 } |
1354 | 1355 |
1355 static void | 1356 static void |
1356 write_setting(gpointer key, gpointer value, gpointer user_data) | 1357 setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data) |
1357 { | 1358 { |
1359 const char *name; | |
1358 GaimAccountSetting *setting; | 1360 GaimAccountSetting *setting; |
1359 const char *name; | 1361 xmlnode *node, *child; |
1360 FILE *fp; | 1362 char buf[20]; |
1361 | 1363 |
1364 name = (const char *)key; | |
1362 setting = (GaimAccountSetting *)value; | 1365 setting = (GaimAccountSetting *)value; |
1363 name = (const char *)key; | 1366 node = (xmlnode *)user_data; |
1364 fp = (FILE *)user_data; | 1367 |
1368 child = xmlnode_new("setting"); | |
1369 xmlnode_set_attrib(child, "name", name); | |
1365 | 1370 |
1366 if (setting->type == GAIM_PREF_INT) { | 1371 if (setting->type == GAIM_PREF_INT) { |
1367 fprintf(fp, " <setting name='%s' type='int'>%d</setting>\n", | 1372 xmlnode_set_attrib(child, "type", "int"); |
1368 name, setting->value.integer); | 1373 snprintf(buf, sizeof(buf), "%d", setting->value.integer); |
1374 xmlnode_insert_data(child, buf, -1); | |
1369 } | 1375 } |
1370 else if (setting->type == GAIM_PREF_STRING && setting->value.string != NULL) { | 1376 else if (setting->type == GAIM_PREF_STRING && setting->value.string != NULL) { |
1371 fprintf(fp, " <setting name='%s' type='string'>%s</setting>\n", | 1377 xmlnode_set_attrib(child, "type", "string"); |
1372 name, setting->value.string); | 1378 xmlnode_insert_data(child, setting->value.string, -1); |
1373 } | 1379 } |
1374 else if (setting->type == GAIM_PREF_BOOLEAN) { | 1380 else if (setting->type == GAIM_PREF_BOOLEAN) { |
1375 fprintf(fp, " <setting name='%s' type='bool'>%d</setting>\n", | 1381 xmlnode_set_attrib(child, "type", "bool"); |
1376 name, setting->value.bool); | 1382 snprintf(buf, sizeof(buf), "%d", setting->value.bool); |
1377 } | 1383 xmlnode_insert_data(child, buf, -1); |
1384 } | |
1385 | |
1386 xmlnode_insert_child(node, child); | |
1378 } | 1387 } |
1379 | 1388 |
1380 static void | 1389 static void |
1381 write_ui_setting_list(gpointer key, gpointer value, gpointer user_data) | 1390 ui_setting_to_xmlnode(gpointer key, gpointer value, gpointer user_data) |
1382 { | 1391 { |
1392 const char *ui; | |
1383 GHashTable *table; | 1393 GHashTable *table; |
1384 const char *ui; | 1394 xmlnode *node, *child; |
1385 FILE *fp; | 1395 |
1386 | 1396 ui = (const char *)key; |
1387 table = (GHashTable *)value; | 1397 table = (GHashTable *)value; |
1388 ui = (const char *)key; | 1398 node = (xmlnode *)user_data; |
1389 fp = (FILE *)user_data; | 1399 |
1390 | 1400 child = xmlnode_new("settings"); |
1391 fprintf(fp, " <settings ui='%s'>\n", ui); | 1401 xmlnode_set_attrib(child, "ui", ui); |
1392 g_hash_table_foreach(table, write_setting, fp); | 1402 g_hash_table_foreach(table, setting_to_xmlnode, child); |
1393 fprintf(fp, " </settings>\n"); | 1403 |
1394 } | 1404 xmlnode_insert_child(node, child); |
1395 | 1405 } |
1396 static void | 1406 |
1397 gaim_accounts_write(FILE *fp, GaimAccount *account) | 1407 static xmlnode * |
1398 { | 1408 proxy_settings_to_xmlnode(GaimProxyInfo *proxy_info) |
1409 { | |
1410 xmlnode *node, *child; | |
1411 GaimProxyType proxy_type; | |
1412 const char *value; | |
1413 int int_value; | |
1414 char buf[20]; | |
1415 | |
1416 proxy_type = gaim_proxy_info_get_type(proxy_info); | |
1417 | |
1418 node = xmlnode_new("proxy"); | |
1419 | |
1420 child = xmlnode_new_child_with_data(node, "type", | |
1421 (proxy_type == GAIM_PROXY_USE_GLOBAL ? "global" : | |
1422 proxy_type == GAIM_PROXY_NONE ? "none" : | |
1423 proxy_type == GAIM_PROXY_HTTP ? "http" : | |
1424 proxy_type == GAIM_PROXY_SOCKS4 ? "socks4" : | |
1425 proxy_type == GAIM_PROXY_SOCKS5 ? "socks5" : | |
1426 proxy_type == GAIM_PROXY_USE_ENVVAR ? "envvar" : "unknown"), -1); | |
1427 | |
1428 if (proxy_type != GAIM_PROXY_USE_GLOBAL && | |
1429 proxy_type != GAIM_PROXY_NONE && | |
1430 proxy_type != GAIM_PROXY_USE_ENVVAR) | |
1431 { | |
1432 if ((value = gaim_proxy_info_get_host(proxy_info)) != NULL) | |
1433 child = xmlnode_new_child_with_data(node, "host", value, -1); | |
1434 | |
1435 if ((int_value = gaim_proxy_info_get_port(proxy_info)) != 0) | |
1436 { | |
1437 snprintf(buf, sizeof(buf), "%d", int_value); | |
1438 child = xmlnode_new_child_with_data(node, "port", buf, -1); | |
1439 } | |
1440 | |
1441 if ((value = gaim_proxy_info_get_username(proxy_info)) != NULL) | |
1442 child = xmlnode_new_child_with_data(node, "username", value, -1); | |
1443 | |
1444 if ((value = gaim_proxy_info_get_password(proxy_info)) != NULL) | |
1445 child = xmlnode_new_child_with_data(node, "password", value, -1); | |
1446 } | |
1447 | |
1448 return node; | |
1449 } | |
1450 | |
1451 static xmlnode * | |
1452 account_to_xmlnode(GaimAccount *account) | |
1453 { | |
1454 xmlnode *node, *child; | |
1455 const char *tmp; | |
1399 GaimProxyInfo *proxy_info; | 1456 GaimProxyInfo *proxy_info; |
1400 GaimProxyType proxy_type; | 1457 |
1401 const char *password, *alias, *user_info, *buddy_icon; | 1458 node = xmlnode_new("account"); |
1402 char *esc; | 1459 |
1403 | 1460 child = xmlnode_new("protocol"); |
1404 fprintf(fp, " <account>\n"); | 1461 xmlnode_insert_data(child, gaim_account_get_protocol_id(account), -1); |
1405 fprintf(fp, " <protocol>%s</protocol>\n", account->protocol_id); | 1462 xmlnode_insert_child(node, child); |
1406 esc = g_markup_escape_text(gaim_account_get_username(account), -1); | 1463 |
1407 fprintf(fp, " <name>%s</name>\n", esc); | 1464 child = xmlnode_new("name"); |
1408 g_free(esc); | 1465 xmlnode_insert_data(child, gaim_account_get_username(account), -1); |
1466 xmlnode_insert_child(node, child); | |
1409 | 1467 |
1410 if (gaim_account_get_remember_password(account) && | 1468 if (gaim_account_get_remember_password(account) && |
1411 (password = gaim_account_get_password(account)) != NULL) { | 1469 ((tmp = gaim_account_get_password(account)) != NULL)) |
1412 esc = g_markup_escape_text(password, -1); | 1470 { |
1413 fprintf(fp, " <password>%s</password>\n", esc); | 1471 child = xmlnode_new("password"); |
1414 g_free(esc); | 1472 xmlnode_insert_data(child, tmp, -1); |
1415 } | 1473 xmlnode_insert_child(node, child); |
1416 | 1474 } |
1417 if ((alias = gaim_account_get_alias(account)) != NULL) { | 1475 |
1418 esc = g_markup_escape_text(alias, -1); | 1476 if ((tmp = gaim_account_get_alias(account)) != NULL) |
1419 fprintf(fp, " <alias>%s</alias>\n", esc); | 1477 { |
1420 g_free(esc); | 1478 child = xmlnode_new("alias"); |
1421 } | 1479 xmlnode_insert_data(child, tmp, -1); |
1422 | 1480 xmlnode_insert_child(node, child); |
1423 if ((user_info = gaim_account_get_user_info(account)) != NULL) { | 1481 } |
1424 esc = g_markup_escape_text(user_info, -1); | 1482 |
1425 gaim_str_strip_cr(esc); | 1483 if ((tmp = gaim_account_get_user_info(account)) != NULL) |
1426 fprintf(fp, " <userinfo>%s</userinfo>\n", esc); | 1484 { |
1427 g_free(esc); | 1485 /* TODO: Do we need to call gaim_str_strip_cr(tmp) here? */ |
1428 } | 1486 child = xmlnode_new("userinfo"); |
1429 | 1487 xmlnode_insert_data(child, tmp, -1); |
1430 if ((buddy_icon = gaim_account_get_buddy_icon(account)) != NULL) { | 1488 xmlnode_insert_child(node, child); |
1431 esc = g_markup_escape_text(buddy_icon, -1); | 1489 } |
1432 fprintf(fp, " <buddyicon>%s</buddyicon>\n", esc); | 1490 |
1433 g_free(esc); | 1491 if ((tmp = gaim_account_get_buddy_icon(account)) != NULL) |
1434 } | 1492 { |
1435 | 1493 child = xmlnode_new("buddyicon"); |
1436 fprintf(fp, " <settings>\n"); | 1494 xmlnode_insert_data(child, tmp, -1); |
1437 g_hash_table_foreach(account->settings, write_setting, fp); | 1495 xmlnode_insert_child(node, child); |
1438 fprintf(fp, " </settings>\n"); | 1496 } |
1439 | 1497 |
1440 g_hash_table_foreach(account->ui_settings, write_ui_setting_list, fp); | 1498 child = xmlnode_new("settings"); |
1499 g_hash_table_foreach(account->settings, setting_to_xmlnode, child); | |
1500 xmlnode_insert_child(node, child); | |
1501 | |
1502 g_hash_table_foreach(account->ui_settings, ui_setting_to_xmlnode, node); | |
1441 | 1503 |
1442 if ((proxy_info = gaim_account_get_proxy_info(account)) != NULL) | 1504 if ((proxy_info = gaim_account_get_proxy_info(account)) != NULL) |
1443 { | 1505 { |
1444 const char *value; | 1506 child = proxy_settings_to_xmlnode(proxy_info); |
1445 int int_value; | 1507 xmlnode_insert_child(node, child); |
1446 | 1508 } |
1447 proxy_type = gaim_proxy_info_get_type(proxy_info); | 1509 |
1448 | 1510 return node; |
1449 fprintf(fp, " <proxy>\n"); | 1511 } |
1450 fprintf(fp, " <type>%s</type>\n", | 1512 |
1451 (proxy_type == GAIM_PROXY_USE_GLOBAL ? "global" : | 1513 static xmlnode * |
1452 proxy_type == GAIM_PROXY_NONE ? "none" : | 1514 accounts_to_xmlnode(void) |
1453 proxy_type == GAIM_PROXY_HTTP ? "http" : | 1515 { |
1454 proxy_type == GAIM_PROXY_SOCKS4 ? "socks4" : | 1516 xmlnode *node, *child; |
1455 proxy_type == GAIM_PROXY_SOCKS5 ? "socks5" : | 1517 GList *cur; |
1456 proxy_type == GAIM_PROXY_USE_ENVVAR ? "envvar" : "unknown")); | 1518 |
1457 | 1519 node = xmlnode_new("accounts"); |
1458 if (proxy_type != GAIM_PROXY_USE_GLOBAL && | 1520 xmlnode_set_attrib(node, "version", "1.0"); |
1459 proxy_type != GAIM_PROXY_NONE && | 1521 |
1460 proxy_type != GAIM_PROXY_USE_ENVVAR) { | 1522 for (cur = gaim_accounts_get_all(); cur != NULL; cur = cur->next) |
1461 if ((value = gaim_proxy_info_get_host(proxy_info)) != NULL) | 1523 { |
1462 fprintf(fp, " <host>%s</host>\n", value); | 1524 child = account_to_xmlnode(cur->data); |
1463 | 1525 xmlnode_insert_child(node, child); |
1464 if ((int_value = gaim_proxy_info_get_port(proxy_info)) != 0) | 1526 } |
1465 fprintf(fp, " <port>%d</port>\n", int_value); | 1527 |
1466 | 1528 return node; |
1467 if ((value = gaim_proxy_info_get_username(proxy_info)) != NULL) | |
1468 fprintf(fp, " <username>%s</username>\n", value); | |
1469 | |
1470 if ((value = gaim_proxy_info_get_password(proxy_info)) != NULL) | |
1471 fprintf(fp, " <password>%s</password>\n", value); | |
1472 } | |
1473 | |
1474 fprintf(fp, " </proxy>\n"); | |
1475 } | |
1476 | |
1477 fprintf(fp, " </account>\n"); | |
1478 } | 1529 } |
1479 | 1530 |
1480 void | 1531 void |
1481 gaim_accounts_sync(void) | 1532 gaim_accounts_sync(void) |
1482 { | 1533 { |
1483 FILE *fp; | 1534 xmlnode *node; |
1484 struct stat st; | 1535 char *data; |
1485 const char *user_dir = gaim_user_dir(); | |
1486 char *filename; | |
1487 char *filename_real; | |
1488 | 1536 |
1489 if (!accounts_loaded) { | 1537 if (!accounts_loaded) { |
1490 gaim_debug_error("accounts", | 1538 gaim_debug_error("accounts", "Attempted to save accounts before they " |
1491 "Writing accounts to disk.\n"); | 1539 "were read!\n"); |
1492 schedule_accounts_save(); | 1540 } |
1493 return; | 1541 |
1494 } | 1542 node = accounts_to_xmlnode(); |
1495 | 1543 data = xmlnode_to_formatted_str(node, NULL); |
1496 if (user_dir == NULL) | 1544 gaim_util_write_data_to_file("accounts.xml", data, -1); |
1497 return; | 1545 g_free(data); |
1498 | 1546 xmlnode_free(node); |
1499 gaim_debug_info("accounts", "Writing accounts to disk.\n"); | |
1500 | |
1501 fp = fopen(user_dir, "r"); | |
1502 | |
1503 if (fp == NULL) | |
1504 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); | |
1505 else | |
1506 fclose(fp); | |
1507 | |
1508 filename = g_build_filename(user_dir, "accounts.xml.save", NULL); | |
1509 | |
1510 if ((fp = fopen(filename, "w")) != NULL) { | |
1511 GList *l; | |
1512 | |
1513 fprintf(fp, "<?xml version='1.0' encoding='UTF-8' ?>\n\n"); | |
1514 fprintf(fp, "<accounts>\n"); | |
1515 | |
1516 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) | |
1517 gaim_accounts_write(fp, l->data); | |
1518 | |
1519 fprintf(fp, "</accounts>\n"); | |
1520 | |
1521 fclose(fp); | |
1522 chmod(filename, S_IRUSR | S_IWUSR); | |
1523 } | |
1524 else { | |
1525 gaim_debug_error("accounts", "Unable to write %s\n", | |
1526 filename); | |
1527 g_free(filename); | |
1528 return; | |
1529 } | |
1530 | |
1531 if (stat(filename, &st) || (st.st_size == 0)) { | |
1532 gaim_debug_error("accounts", "Failed to save accounts\n"); | |
1533 unlink(filename); | |
1534 g_free(filename); | |
1535 return; | |
1536 } | |
1537 | |
1538 filename_real = g_build_filename(user_dir, "accounts.xml", NULL); | |
1539 | |
1540 if (rename(filename, filename_real) < 0) { | |
1541 gaim_debug_error("accounts", "Error renaming %s to %s\n", | |
1542 filename, filename_real); | |
1543 } | |
1544 | |
1545 g_free(filename); | |
1546 g_free(filename_real); | |
1547 } | 1547 } |
1548 | 1548 |
1549 void | 1549 void |
1550 gaim_accounts_add(GaimAccount *account) | 1550 gaim_accounts_add(GaimAccount *account) |
1551 { | 1551 { |