comparison libpurple/util.c @ 25776:e0a596a0a020

propagate from branch 'im.pidgin.pidgin' (head 69cdb2b9e20a69a98389b47c672718d5be222338) to branch 'im.pidgin.soc.2008.themes' (head 76c4c8e60f9192eff589f0416652aaab2ae9521d)
author Gary Kramlich <grim@reaperworld.com>
date Thu, 28 Aug 2008 13:00:23 +0000
parents 58e3b422e595 78b43f9c741b
children 6ebabe0841a0
comparison
equal deleted inserted replaced
24000:e9eb71dc21af 25776:e0a596a0a020
2769 } 2769 }
2770 2770
2771 xmlnode * 2771 xmlnode *
2772 purple_util_read_xml_from_file(const char *filename, const char *description) 2772 purple_util_read_xml_from_file(const char *filename, const char *description)
2773 { 2773 {
2774 const char *user_dir = purple_user_dir(); 2774 return xmlnode_from_file(purple_user_dir(), filename, description, "util");
2775 gchar *filename_full;
2776 GError *error = NULL;
2777 gchar *contents = NULL;
2778 gsize length;
2779 xmlnode *node = NULL;
2780
2781 g_return_val_if_fail(user_dir != NULL, NULL);
2782
2783 purple_debug_info("util", "Reading file %s from directory %s\n",
2784 filename, user_dir);
2785
2786 filename_full = g_build_filename(user_dir, filename, NULL);
2787
2788 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS))
2789 {
2790 purple_debug_info("util", "File %s does not exist (this is not "
2791 "necessarily an error)\n", filename_full);
2792 g_free(filename_full);
2793 return NULL;
2794 }
2795
2796 if (!g_file_get_contents(filename_full, &contents, &length, &error))
2797 {
2798 purple_debug_error("util", "Error reading file %s: %s\n",
2799 filename_full, error->message);
2800 g_error_free(error);
2801 }
2802
2803 if ((contents != NULL) && (length > 0))
2804 {
2805 node = xmlnode_from_str(contents, length);
2806
2807 /* If we were unable to parse the file then save its contents to a backup file */
2808 if (node == NULL)
2809 {
2810 gchar *filename_temp;
2811
2812 filename_temp = g_strdup_printf("%s~", filename);
2813 purple_debug_error("util", "Error parsing file %s. Renaming old "
2814 "file to %s\n", filename_full, filename_temp);
2815 purple_util_write_data_to_file(filename_temp, contents, length);
2816 g_free(filename_temp);
2817 }
2818
2819 g_free(contents);
2820 }
2821
2822 /* If we could not parse the file then show the user an error message */
2823 if (node == NULL)
2824 {
2825 gchar *title, *msg;
2826 title = g_strdup_printf(_("Error Reading %s"), filename);
2827 msg = g_strdup_printf(_("An error was encountered reading your "
2828 "%s. They have not been loaded, and the old file "
2829 "has been renamed to %s~."), description, filename_full);
2830 purple_notify_error(NULL, NULL, title, msg);
2831 g_free(title);
2832 g_free(msg);
2833 }
2834
2835 g_free(filename_full);
2836
2837 return node;
2838 } 2775 }
2839 2776
2840 /* 2777 /*
2841 * Like mkstemp() but returns a file pointer, uses a pre-set template, 2778 * Like mkstemp() but returns a file pointer, uses a pre-set template,
2842 * uses the semantics of tempnam() for the directory to use and allocates 2779 * uses the semantics of tempnam() for the directory to use and allocates