summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 88225ce)
raw | patch | inline | side by side (parent: 88225ce)
author | joncruz <joncruz@users.sourceforge.net> | |
Mon, 4 May 2009 03:58:23 +0000 (03:58 +0000) | ||
committer | joncruz <joncruz@users.sourceforge.net> | |
Mon, 4 May 2009 03:58:23 +0000 (03:58 +0000) |
src/inkscape.cpp | patch | blob | history | |
src/inkview.cpp | patch | blob | history | |
src/preferences.cpp | patch | blob | history | |
src/preferences.h | patch | blob | history |
diff --git a/src/inkscape.cpp b/src/inkscape.cpp
index 25b3cc5a96ecf21e5655374b7b759c281935ee17..5d9e217a82f4061fdb43813db9a882b43730be49 100644 (file)
--- a/src/inkscape.cpp
+++ b/src/inkscape.cpp
#include <glibmm/i18n.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkmessagedialog.h>
+#include <gtkmm/messagedialog.h>
#include <signal.h>
#include <string>
#include "application/application.h"
}
+class InkErrorHandler : public Inkscape::ErrorReporter {
+public:
+ InkErrorHandler(bool useGui) : Inkscape::ErrorReporter(),
+ _useGui(useGui)
+ {}
+ virtual ~InkErrorHandler() {}
+
+ virtual void handleError( Glib::ustring const& primary, Glib::ustring const& secondary ) const
+ {
+ if (_useGui) {
+ Gtk::MessageDialog err(primary, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
+ err.set_secondary_text(secondary);
+ err.run();
+ } else {
+ g_message("%s", primary.data());
+ g_message("%s", secondary.data());
+ }
+ }
+
+private:
+ bool _useGui;
+};
void
inkscape_application_init (const gchar *argv0, gboolean use_gui)
/* Load the preferences and menus */
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
- prefs->load(use_gui, false);
+ InkErrorHandler* handler = new InkErrorHandler(use_gui);
+ prefs->setErrorHandler(handler);
+
inkscape_load_menus(inkscape);
sp_input_load_from_preferences();
diff --git a/src/inkview.cpp b/src/inkview.cpp
index bb675f0ee4e26ad26b40ffcf55b7567741ea3d88..cd9d5524bdcd1376dc3d77ad479a0185eef1b06b 100644 (file)
--- a/src/inkview.cpp
+++ b/src/inkview.cpp
Inkscape::GC::init();
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
- prefs->load(false, true); // keep quiet about any failures
gtk_init (&argc, (char ***) &argv);
diff --git a/src/preferences.cpp b/src/preferences.cpp
index 16b775b345243e21d1691ce0e76404dceb93354e..a6409b5ba4e6d226939f69d2697831a939240a03 100644 (file)
--- a/src/preferences.cpp
+++ b/src/preferences.cpp
*/
/* Authors:
* Krzysztof Kosiński <tweenk.pl@gmail.com>
+ * Jon A. Cruz <jon@joncruz.org>
*
- * Copyright (C) 2008 Authors
+ * Copyright (C) 2008,2009 Authors
*
* Released under GNU GPL. Read the file 'COPYING' for more information.
*/
+#include <cstring>
+#include <glibmm/fileutils.h>
+#include <glibmm/i18n.h>
+#include <glib.h>
+#include <glib/gstdio.h>
#include "preferences.h"
#include "preferences-skeleton.h"
#include "inkscape.h"
-#include "xml/repr.h"
#include "xml/node-observer.h"
#include "xml/node-iterators.h"
#include "xml/attribute-record.h"
-#include <cstring>
-#include <vector>
-#include <glibmm/fileutils.h>
-#include <glibmm/i18n.h>
-#include <glib.h>
-#include <glib/gstdio.h>
-#include <gtkmm/messagedialog.h>
#define PREFERENCES_FILE_NAME "preferences.xml"
_prefs_basename(PREFERENCES_FILE_NAME),
_prefs_dir(""),
_prefs_filename(""),
- _prefs_doc(NULL),
- _use_gui(true),
- _quiet(false),
- _loaded(false),
+ _prefs_doc(0),
+ _errorHandler(0),
_writable(false)
{
// profile_path essentailly returns the argument prefixed by the profile directory.
g_free(path);
_loadDefaults();
+ _load();
}
Preferences::~Preferences()
* @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)
+void Preferences::_load()
{
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;
-
+ "and new settings will not be saved. ");
+
// NOTE: After we upgrade to Glib 2.16, use Glib::ustring::compose
// 1. Does the file exist?
// No - create the profile directory
if (g_mkdir(_prefs_dir.data(), 0755)) {
// the creation failed
- //_errorDialog(Glib::ustring::compose(_("Cannot create profile directory %1."),
+ //_reportError(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);
+ _reportError(msg, not_saved);
g_free(msg);
return;
}
} 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."),
+ //_reportError(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);
+ _reportError(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."),
+ //_reportError(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);
+ _reportError(msg, not_saved);
g_free(msg);
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."),
+ //_reportError(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);
+ _reportError(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."),
+ //_reportError(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);
+ _reportError(msg, not_saved);
g_free(msg);
return;
}
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."),
+ //_reportError(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);
+ _reportError(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."),
+ //_reportError(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);
+ _reportError(msg, not_saved);
g_free(msg);
Inkscape::GC::release(prefs_read);
return;
@@ -615,17 +608,10 @@ void Preferences::_keySplit(Glib::ustring const &pref_path, Glib::ustring &node_
node_key = pref_path.substr(0, pref_path.rfind('/'));
}
-void Preferences::_errorDialog(Glib::ustring const &msg, Glib::ustring const &secondary)
+void Preferences::_reportError(Glib::ustring const &msg, Glib::ustring const &secondary)
{
- if (_quiet) return;
- if (_use_gui) {
- Gtk::MessageDialog err(
- msg, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
- err.set_secondary_text(secondary);
- err.run();
- } else {
- g_message("%s", msg.data());
- g_message("%s", secondary.data());
+ if (_errorHandler) {
+ _errorHandler->handleError(msg, secondary);
}
}
@@ -634,6 +620,11 @@ Preferences::Entry const Preferences::_create_pref_value(Glib::ustring const &pa
return Entry(path, ptr);
}
+void Preferences::setErrorHandler(ErrorReporter* handler)
+{
+ _errorHandler = handler;
+}
+
void Preferences::unload(bool save)
{
if(_instance)
diff --git a/src/preferences.h b/src/preferences.h
index fcdade8161fae7ef4208340a65bec5d88e8e9b16..3c25a520fdd9eb8bb3228a5e92458adac7225832 100644 (file)
--- a/src/preferences.h
+++ b/src/preferences.h
*/
/* Authors:
* Krzysztof Kosiński <tweenk.pl@gmail.com>
+ * Jon A. Cruz <jon@joncruz.org>
*
- * Copyright (C) 2008 Authors
+ * Copyright (C) 2008,2009 Authors
*
* Released under GNU GPL. Read the file 'COPYING' for more information.
*/
namespace Inkscape {
+class ErrorReporter {
+public:
+ virtual void handleError(Glib::ustring const& primary, Glib::ustring const& secondary ) const = 0;
+};
+
/**
- * @brief Preference storage class
+ * @brief Preference storage class.
*
* This is a singleton that allows one to access the user preferences stored in
* the preferences.xml file. The preferences are stored in a file system-like
* the path must start with a slash, and not contain a trailing slash.
* An example of a correct path would be "/options/some_group/some_option".
*
- * All preferences are loaded when the first singleton pointer is requested,
- * or when the static load() method is called. Before loading, the static
- * variable @c use_gui should be set accordingly. To save the preferences,
- * the method save() or the static function unload() can be used.
+ * All preferences are loaded when the first singleton pointer is requested.
+ * To save the preferences, the method save() or the static function unload()
+ * can be used.
*
* In future, this will be a virtual base from which specific backends
* derive (e.g. GConf, flat XML file...)
// utility methods
- /**
- * @name Load stored preferences and save them to the disk.
- * @{
- */
-
- /**
- * @brief Load the preferences from the default location.
- *
- * Loads the stored user preferences and enables saving them. If there's
- * no preferences file in the expected location, it creates it. Any changes
- * made to the preferences before loading will be overridden by the stored
- * prefs. Not calling load() is sometimes useful, e.g. for testing.
- *
- * @param use_gui Whether to use dialogs to notify about errors when
- * loading the preferences. Set to false in console mode.
- * @param quiet Whether to output any messages about preference loading.
- * If this is true, the use_gui parameter is ignored.
- */
- void load(bool use_gui=true, bool quiet=false);
-
/**
* @brief Save all preferences to the hard disk.
*
return _instance;
}
+ void setErrorHandler(ErrorReporter* handler);
+
/**
* @brief Unload all preferences
* @param save Whether to save the preferences; defaults to true
Preferences();
~Preferences();
void _loadDefaults();
+ void _load();
void _getRawValue(Glib::ustring const &path, gchar const *&result);
void _setRawValue(Glib::ustring const &path, gchar const *value);
- void _errorDialog(Glib::ustring const &, Glib::ustring const &);
+ void _reportError(Glib::ustring const &, Glib::ustring const &);
void _keySplit(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key);
XML::Node *_getNode(Glib::ustring const &pref_path, bool create=false);
XML::Node *_findObserverNode(Glib::ustring const &pref_path, Glib::ustring &node_key, Glib::ustring &attr_key, bool create);
std::string _prefs_dir; ///< Directory in which to look for the prefs file
std::string _prefs_filename; ///< Full filename (with directory) of the prefs file
XML::Document *_prefs_doc; ///< XML document storing all the preferences
- bool _use_gui; ///< Use GUI error notifications?
- bool _quiet; ///< Display any messages about loading?
- bool _loaded; ///< Was a load attempt made?
+ ErrorReporter* _errorHandler; ///< Pointer to object reporting errors.
bool _writable; ///< Will the preferences be saved at exit?
/// Wrapper class for XML node observers