comparison src/blist.c @ 10426:30d9ec7d001b

[gaim-migrate @ 11678] Some rather key fixes to accounts and saved statuses, and make blist use our util function for reading (it's already using xmlnode). committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 26 Dec 2004 23:11:14 +0000
parents 3232e1a33899
children 16d63d8c26d8
comparison
equal deleted inserted replaced
10425:9903182f2aac 10426:30d9ec7d001b
33 #include "util.h" 33 #include "util.h"
34 #include "xmlnode.h" 34 #include "xmlnode.h"
35 35
36 #define PATHSIZE 1024 36 #define PATHSIZE 1024
37 37
38 GaimBuddyList *gaimbuddylist = NULL;
39 static GaimBlistUiOps *blist_ui_ops = NULL;
40 static guint blist_save_timer = 0;
41
42 struct gaim_blist_node_setting { 38 struct gaim_blist_node_setting {
43 enum { 39 enum {
44 GAIM_BLIST_NODE_SETTING_BOOL, 40 GAIM_BLIST_NODE_SETTING_BOOL,
45 GAIM_BLIST_NODE_SETTING_INT, 41 GAIM_BLIST_NODE_SETTING_INT,
46 GAIM_BLIST_NODE_SETTING_STRING 42 GAIM_BLIST_NODE_SETTING_STRING
50 int integer; 46 int integer;
51 char *string; 47 char *string;
52 } value; 48 } value;
53 }; 49 };
54 50
51 static GaimBlistUiOps *blist_ui_ops = NULL;
52
53 static GaimBuddyList *gaimbuddylist = NULL;
54 static guint blist_save_timer = 0;
55 static gboolean blist_loaded = FALSE;
55 56
56 57
57 /***************************************************************************** 58 /*****************************************************************************
58 * Private Utility functions * 59 * Private Utility functions *
59 *****************************************************************************/ 60 *****************************************************************************/
1779 } 1780 }
1780 } 1781 }
1781 return FALSE; 1782 return FALSE;
1782 } 1783 }
1783 1784
1784 static gboolean blist_safe_to_write = FALSE;
1785
1786 static void parse_setting(GaimBlistNode *node, xmlnode *setting) 1785 static void parse_setting(GaimBlistNode *node, xmlnode *setting)
1787 { 1786 {
1788 const char *name = xmlnode_get_attrib(setting, "name"); 1787 const char *name = xmlnode_get_attrib(setting, "name");
1789 const char *type = xmlnode_get_attrib(setting, "type"); 1788 const char *type = xmlnode_get_attrib(setting, "type");
1790 char *value = xmlnode_get_data(setting); 1789 char *value = xmlnode_get_data(setting);
1915 } 1914 }
1916 1915
1917 if (alias) 1916 if (alias)
1918 g_free(alias); 1917 g_free(alias);
1919 } 1918 }
1920
1921 1919
1922 static void parse_group(xmlnode *groupnode) 1920 static void parse_group(xmlnode *groupnode)
1923 { 1921 {
1924 const char *name = xmlnode_get_attrib(groupnode, "name"); 1922 const char *name = xmlnode_get_attrib(groupnode, "name");
1925 GaimGroup *group; 1923 GaimGroup *group;
1943 else if (!strcmp(cnode->name, "chat")) 1941 else if (!strcmp(cnode->name, "chat"))
1944 parse_chat(group, cnode); 1942 parse_chat(group, cnode);
1945 } 1943 }
1946 } 1944 }
1947 1945
1948 static gboolean gaim_blist_read(const char *filename) 1946 void gaim_blist_load()
1949 { 1947 {
1950 GError *error;
1951 gchar *contents = NULL;
1952 gsize length;
1953 xmlnode *gaim, *blist, *privacy; 1948 xmlnode *gaim, *blist, *privacy;
1954 1949
1955 gaim_debug_info("blist", "Reading %s\n", filename); 1950 blist_loaded = TRUE;
1956 1951
1957 if (!g_file_get_contents(filename, &contents, &length, &error)) { 1952 gaim = gaim_util_read_xml_from_file("blist.xml", _("buddy list"));
1958 gaim_debug_error("blist", "Error reading buddy list: %s\n", 1953
1959 error->message); 1954 if (gaim == NULL)
1960 g_error_free(error); 1955 return;
1961 return FALSE;
1962 }
1963
1964 gaim = xmlnode_from_str(contents, length);
1965
1966 if (gaim == NULL) {
1967 FILE *backup;
1968 char *name;
1969 gaim_debug_error("blist", "Error parsing buddy list\n");
1970 name = g_strdup_printf("%s~", filename);
1971 if ((backup = fopen(name, "w"))) {
1972 fwrite(contents, length, 1, backup);
1973 fclose(backup);
1974 chmod(name, S_IRUSR | S_IWUSR);
1975 } else {
1976 gaim_debug_error("blist", "Unable to write backup %s\n", name);
1977 }
1978 g_free(name);
1979 g_free(contents);
1980 return FALSE;
1981 }
1982
1983 g_free(contents);
1984 1956
1985 blist = xmlnode_get_child(gaim, "blist"); 1957 blist = xmlnode_get_child(gaim, "blist");
1986 if (blist) { 1958 if (blist) {
1987 xmlnode *groupnode; 1959 xmlnode *groupnode;
1988 for (groupnode = xmlnode_get_child(blist, "group"); groupnode != NULL; 1960 for (groupnode = xmlnode_get_child(blist, "group"); groupnode != NULL;
2030 } 2002 }
2031 } 2003 }
2032 } 2004 }
2033 } 2005 }
2034 2006
2035 gaim_debug_info("blist", "Finished reading buddy list\n");
2036
2037 xmlnode_free(gaim); 2007 xmlnode_free(gaim);
2038
2039 return TRUE;
2040 }
2041
2042 void gaim_blist_load()
2043 {
2044 const char *user_dir = gaim_user_dir();
2045 gchar *filename;
2046 gchar *msg;
2047
2048 blist_safe_to_write = TRUE;
2049
2050 if (user_dir == NULL)
2051 return;
2052
2053 filename = g_build_filename(user_dir, "blist.xml", NULL);
2054
2055 if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
2056 if (!gaim_blist_read(filename)) {
2057 msg = g_strdup_printf(_("An error was encountered parsing the "
2058 "file containing your buddy list (%s). It has not "
2059 "been loaded, and the old file has been renamed "
2060 "to blist.xml~."), filename);
2061 gaim_notify_error(NULL, NULL, _("Buddy List Error"), msg);
2062 g_free(msg);
2063 }
2064 }
2065
2066 g_free(filename);
2067 } 2008 }
2068 2009
2069 void 2010 void
2070 gaim_blist_request_add_buddy(GaimAccount *account, const char *username, 2011 gaim_blist_request_add_buddy(GaimAccount *account, const char *username,
2071 const char *group, const char *alias) 2012 const char *group, const char *alias)
2389 char *filename; 2330 char *filename;
2390 char *filename_real; 2331 char *filename_real;
2391 2332
2392 g_return_if_fail(user_dir != NULL); 2333 g_return_if_fail(user_dir != NULL);
2393 2334
2394 if (!blist_safe_to_write) { 2335 if (!blist_loaded) {
2395 gaim_debug_warning("blist save", 2336 gaim_debug_warning("blist save",
2396 "AHH!! Tried to write the blist before we read it!\n"); 2337 "AHH!! Tried to write the blist before we read it!\n");
2397 return; 2338 return;
2398 } 2339 }
2399 2340