Preferences::Preferences() : _prefs_basename(PREFERENCES_FILE_NAME), _prefs_dir(""), _prefs_filename(""), _prefs_doc(NULL), _use_gui(true), _quiet(false), _loaded(false), _writable(false) { // profile_path essentailly returns the argument prefixed by the profile directory. gchar *path = profile_path(NULL); _prefs_dir = path; g_free(path); path = profile_path(_prefs_basename.data()); _prefs_filename = path; g_free(path); _loadDefaults(); }
/** \brief A function to open the error log file. */ void Extension::error_file_open (void) { gchar * ext_error_file = profile_path(EXTENSION_ERROR_LOG_FILENAME); gchar * filename = g_filename_from_utf8( ext_error_file, -1, NULL, NULL, NULL ); error_file.open(filename); if (!error_file.is_open()) { g_warning(_("Could not create extension error log file '%s'"), filename); } g_free(filename); g_free(ext_error_file); };
/** * @brief Load the user's customized preferences * * Tries to load the user's preferences.xml file. If there is none, creates it. * Displays dialog boxes on any errors. */ void Preferences::load(bool use_gui, bool quiet) { Glib::ustring const not_saved = _("Inkscape will run with default settings, " "and new settings will not be saved. "); _use_gui = use_gui; _quiet = quiet; _loaded = true; // NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose // 1. Does the file exist? if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_EXISTS)) { // No - we need to create one. // Does the profile directory exist? if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_EXISTS)) { // No - create the profile directory if (g_mkdir(_prefs_dir.data(), 0755)) { // the creation failed //_errorDialog(Glib::ustring::compose(_("Cannot create profile directory %1."), // Glib::filename_to_utf8(_prefs_dir)), not_saved); gchar *msg = g_strdup_printf(_("Cannot create profile directory %s."), Glib::filename_to_utf8(_prefs_dir).data()); _errorDialog(msg, not_saved); g_free(msg); return; } // create some subdirectories for user stuff char const *user_dirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL}; for(int i=0; user_dirs[i]; ++i) { char *dir = profile_path(user_dirs[i]); g_mkdir(dir, 0755); g_free(dir); } } else if (!g_file_test(_prefs_dir.data(), G_FILE_TEST_IS_DIR)) { // The profile dir is not actually a directory //_errorDialog(Glib::ustring::compose(_("%1 is not a valid directory."), // Glib::filename_to_utf8(_prefs_dir)), not_saved); gchar *msg = g_strdup_printf(_("%s is not a valid directory."), Glib::filename_to_utf8(_prefs_dir).data()); _errorDialog(msg, not_saved); g_free(msg); return; } // The profile dir exists and is valid. if (!g_file_set_contents(_prefs_filename.data(), preferences_skeleton, PREFERENCES_SKELETON_SIZE, NULL)) { // The write failed. //_errorDialog(Glib::ustring::compose(_("Failed to create the preferences file %1."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("Failed to create the preferences file %s."), Glib::filename_to_utf8(_prefs_filename).data()); _errorDialog(msg, not_saved); g_free(msg); return; } // The prefs file was just created. // We can return now and skip the rest of the load process. _writable = true; return; } // Yes, the pref file exists. // 2. Is it a regular file? if (!g_file_test(_prefs_filename.data(), G_FILE_TEST_IS_REGULAR)) { //_errorDialog(Glib::ustring::compose(_("The preferences file %1 is not a regular file."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The preferences file %s is not a regular file."), Glib::filename_to_utf8(_prefs_filename).data()); _errorDialog(msg, not_saved); g_free(msg); return; } // 3. Is the file readable? gchar *prefs_xml = NULL; gsize len = 0; if (!g_file_get_contents(_prefs_filename.data(), &prefs_xml, &len, NULL)) { //_errorDialog(Glib::ustring::compose(_("The preferences file %1 could not be read."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The preferences file %s could not be read."), Glib::filename_to_utf8(_prefs_filename).data()); _errorDialog(msg, not_saved); g_free(msg); return; } // 4. Is it valid XML? Inkscape::XML::Document *prefs_read = sp_repr_read_mem(prefs_xml, len, NULL); g_free(prefs_xml); if (!prefs_read) { //_errorDialog(Glib::ustring::compose(_("The preferences file %1 is not a valid XML document."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The preferences file %s is not a valid XML document."), Glib::filename_to_utf8(_prefs_filename).data()); _errorDialog(msg, not_saved); g_free(msg); return; } // 5. Basic sanity check: does the root element have a correct name? if (strcmp(prefs_read->root()->name(), "inkscape")) { //_errorDialog(Glib::ustring::compose(_("The file %1 is not a valid Inkscape preferences file."), // Glib::filename_to_utf8(_prefs_filename)), not_saved); gchar *msg = g_strdup_printf(_("The file %s is not a valid Inkscape preferences file."), Glib::filename_to_utf8(_prefs_filename).data()); _errorDialog(msg, not_saved); g_free(msg); Inkscape::GC::release(prefs_read); return; } // Merge the loaded prefs with defaults. _prefs_doc->root()->mergeFrom(prefs_read->root(), "id"); Inkscape::GC::release(prefs_read); _writable = true; }