From: Mark D. <the...@us...> - 2004-12-25 18:33:37
|
Update of /cvsroot/gaim/gaim/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13999 Modified Files: blist.c gtkpluginpref.c pluginpref.c pluginpref.h status.c util.c util.h Log Message: I'm starting to feel better. Little change here, little change there. Mostly I added a function gaim_util_write_xml_file to be used to write our config files. Index: blist.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/blist.c,v retrieving revision 1.187 retrieving revision 1.188 diff -u -d -p -r1.187 -r1.188 --- blist.c 19 Dec 2004 17:35:59 -0000 1.187 +++ blist.c 25 Dec 2004 18:33:23 -0000 1.188 @@ -2389,20 +2389,16 @@ void gaim_blist_sync() char *filename; char *filename_real; - if (!user_dir) - return; + g_return_if_fail(user_dir != NULL); if (!blist_safe_to_write) { - gaim_debug(GAIM_DEBUG_WARNING, "blist save", + gaim_debug_warning("blist save", "AHH!! Tried to write the blist before we read it!\n"); return; } - file = fopen(user_dir, "r"); - if (!file) + if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); - else - fclose(file); filename = g_build_filename(user_dir, "blist.xml.save", NULL); @@ -2411,8 +2407,7 @@ void gaim_blist_sync() fclose(file); chmod(filename, S_IRUSR | S_IWUSR); } else { - gaim_debug(GAIM_DEBUG_ERROR, "blist save", "Unable to write %s\n", - filename); + gaim_debug_error("blist", "Unable to write %s\n", filename); g_free(filename); return; } @@ -2427,7 +2422,7 @@ void gaim_blist_sync() filename_real = g_build_filename(user_dir, "blist.xml", NULL); if (rename(filename, filename_real) < 0) - gaim_debug(GAIM_DEBUG_ERROR, "blist save", + gaim_debug_error("blist", "Error renaming %s to %s\n", filename, filename_real); g_free(filename); Index: gtkpluginpref.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/gtkpluginpref.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -p -r1.5 -r1.6 --- gtkpluginpref.c 2 Dec 2004 20:34:18 -0000 1.5 +++ gtkpluginpref.c 25 Dec 2004 18:33:27 -0000 1.6 @@ -56,7 +56,7 @@ make_string_pref(GtkWidget *parent, Gaim case GAIM_PLUGIN_PREF_CHOICE: gtk_label = gaim_gtk_prefs_dropdown_from_list(parent, pref_label, GAIM_PREF_STRING, pref_name, - gaim_plugin_pref_get_choices(pref)); + gaim_plugin_pref_get_choices(pref)); gtk_misc_set_alignment(GTK_MISC(gtk_label), 0, 0.5); if(sg) @@ -116,7 +116,7 @@ make_int_pref(GtkWidget *parent, GaimPlu case GAIM_PLUGIN_PREF_NONE: default: gaim_plugin_pref_get_bounds(pref, &min, &max); - gaim_gtk_prefs_labeled_spin_button(parent, pref_label, + gaim_gtk_prefs_labeled_spin_button(parent, pref_label, pref_name, min, max, sg); break; } Index: pluginpref.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/pluginpref.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -p -r1.4 -r1.5 --- pluginpref.c 23 Aug 2004 22:06:19 -0000 1.4 +++ pluginpref.c 25 Dec 2004 18:33:27 -0000 1.5 @@ -209,7 +209,7 @@ gaim_plugin_pref_set_bounds(GaimPluginPr pref->name); return; } - + if(min > max) { tmp = min; min = max; Index: pluginpref.h =================================================================== RCS file: /cvsroot/gaim/gaim/src/pluginpref.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -p -r1.6 -r1.7 --- pluginpref.h 2 Sep 2004 19:01:33 -0000 1.6 +++ pluginpref.h 25 Dec 2004 18:33:27 -0000 1.7 @@ -76,7 +76,7 @@ void gaim_plugin_pref_frame_add(GaimPlug * @return a GList of plugin preferences */ GList *gaim_plugin_pref_frame_get_prefs(GaimPluginPrefFrame *frame); - + /** * Create a new plugin preference * Index: status.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/status.c,v retrieving revision 1.39 retrieving revision 1.40 diff -u -d -p -r1.39 -r1.40 --- status.c 19 Dec 2004 20:32:32 -0000 1.39 +++ status.c 25 Dec 2004 18:33:27 -0000 1.40 @@ -1998,8 +1998,7 @@ gaim_statuses_load(void) gchar *filename; gchar *msg; - if (user_dir == NULL) - return; + g_return_if_fail(user_dir != NULL); filename = g_build_filename(user_dir, "status.xml", NULL); @@ -2022,5 +2021,7 @@ gaim_statuses_load(void) void gaim_statuses_sync(void) { - /* TODO: Write me, baby. */ + /* TODO: Only attempt to write if we've already read the file. */ + + //gaim_util_write_xml_file("status.xml", data); } Index: util.c =================================================================== RCS file: /cvsroot/gaim/gaim/src/util.c,v retrieving revision 1.331 retrieving revision 1.332 diff -u -d -p -r1.331 -r1.332 --- util.c 14 Dec 2004 22:50:54 -0000 1.331 +++ util.c 25 Dec 2004 18:33:27 -0000 1.332 @@ -1930,6 +1930,125 @@ int gaim_build_dir (const char *path, in } /* + * This function is long and beautiful, like my--um, yeah. Anyway, + * it includes lots of error checking so as we don't overwrite + * people's settings if there is a problem writing the new values. + */ +gboolean +gaim_util_write_xml_file(const char *filename, const char *data) +{ + const char *user_dir = gaim_user_dir(); + gchar *filename_temp, *filename_full; + FILE *file; + size_t datalen, byteswritten; + struct stat st; + + g_return_val_if_fail(user_dir != NULL, FALSE); + + gaim_debug_info("util", "Writing file %s to directory %s\n", + filename, user_dir); + + /* Ensure the user directory exists */ + if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) + { + if (mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) + { + gaim_debug_error("util", "Error creating directory %s: %s\n", + user_dir, strerror(errno)); + return FALSE; + } + } + + filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.xml", user_dir, filename); + filename_temp = g_strdup_printf("%s.save", filename_full); + + /* Remove an old temporary file, if one exists */ + if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) + { + if (unlink(filename_temp) == -1) + { + gaim_debug_error("util", "Error removing old file %s: %s\n", + filename_temp, strerror(errno)); + } + } + + /* Open file */ + file = fopen(filename_temp, "w"); + if (file == NULL) + { + gaim_debug_error("util", "Error opening file %s for writing: %s\n", + filename_temp, strerror(errno)); + g_free(filename_full); + g_free(filename_temp); + return FALSE; + } + + /* Write to file */ + datalen = strlen(data); + byteswritten = fwrite(data, 1, datalen, file); + + /* Close file */ + if (fclose(file) != 0) + { + gaim_debug_error("util", "Error closing file %s: %s\n", + filename_temp, strerror(errno)); + g_free(filename_full); + g_free(filename_temp); + return FALSE; + } + + /* Ensure the file is the correct size */ + if (byteswritten != datalen) + { + gaim_debug_error("util", "Error writing to file %s: Wrote %z bytes " + "but should have written %z; is your disk full?\n", + filename_temp, byteswritten, datalen); + g_free(filename_full); + g_free(filename_temp); + return FALSE; + } + /* Use stat to be absolutely sure. */ + if ((stat(filename_temp, &st) == -1) || (st.st_size != datalen)) + { + gaim_debug_error("util", "Error writing data to file %s: " + "Incomplete file written; is your disk full?\n", + filename_temp); + g_free(filename_full); + g_free(filename_temp); + return FALSE; + } + + /* Set file permissions */ + if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1) + { + gaim_debug_error("util", "Error setting permissions of file %s: %s\n", + filename_temp, strerror(errno)); + } + + /* Remove the old file, if it exists */ + if (g_file_test(filename_full, G_FILE_TEST_EXISTS)) + { + if (unlink(filename_full) == -1) + { + gaim_debug_error("util", "Error removing old file %s: %s\n", + filename_full, strerror(errno)); + } + } + + /* Rename to the REAL name */ + if (rename(filename_temp, filename_full) == -1) + { + gaim_debug_error("util", "Error renaming %s to %s: %s\n", + filename_temp, filename_full, strerror(errno)); + } + + g_free(filename_full); + g_free(filename_temp); + + return TRUE; +} + +/* * Like mkstemp() but returns a file pointer, uses a pre-set template, * uses the semantics of tempnam() for the directory to use and allocates * the space for the filepath. Index: util.h =================================================================== RCS file: /cvsroot/gaim/gaim/src/util.h,v retrieving revision 1.67 retrieving revision 1.68 diff -u -d -p -r1.67 -r1.68 --- util.h 9 Dec 2004 01:08:14 -0000 1.67 +++ util.h 25 Dec 2004 18:33:27 -0000 1.68 @@ -377,6 +377,20 @@ void set_gaim_user_dir(const char *dir); int gaim_build_dir(const char *path, int mode); /** + * Write a null-terminated string of data to a file of the given name + * in the Gaim user directory ($HOME/.gaim by default). The data is + * typically a serialized version of one of Gaim's config files, such + * as prefs.xml, accounts.xml, etc. And the string is typically + * obtained using xmlnode_to_formatted_str. + * + * @param filename The basename of the file to write in the gaim_user_dir. + * @param data A null-terminated string of data to write. + * + * @return TRUE if the file was written successfully. FALSE otherwise. + */ +gboolean gaim_util_write_xml_file(const char *filename, const char *data); + +/** * Creates a temporary file and returns a file pointer to it. * * This is like mkstemp(), but returns a file pointer and uses a |