Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / inkscape.cpp
index 25b3cc5a96ecf21e5655374b7b759c281935ee17..8506f05de30e8cc0a96a51f1e9974916f5d5199c 100644 (file)
@@ -15,6 +15,7 @@
 # include "config.h"
 #endif
 
+#include <errno.h>
 
 #include <map>
 #include "debug/simple-event.h"
@@ -41,6 +42,7 @@ using Inkscape::Extension::Internal::PrintWin32;
 #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"
@@ -139,9 +141,12 @@ static void (* segv_handler) (int) = SIG_DFL;
 static void (* abrt_handler) (int) = SIG_DFL;
 static void (* fpe_handler)  (int) = SIG_DFL;
 static void (* ill_handler)  (int) = SIG_DFL;
+#ifndef WIN32
 static void (* bus_handler)  (int) = SIG_DFL;
+#endif
 
-#define INKSCAPE_PROFILE_DIR "Inkscape"
+#define INKSCAPE_PROFILE_DIR "inkscape"
+#define INKSCAPE_PROFILE_DIR_047DEV "Inkscape"
 #define INKSCAPE_LEGACY_PROFILE_DIR ".inkscape"
 #define MENUS_FILE "menus.xml"
 
@@ -629,6 +634,7 @@ inkscape_crash_handler (int /*signum*/)
             gchar * location = homedir_path(c);
             Inkscape::IO::dump_fopen_call(location, "E");
             file = Inkscape::IO::fopen_utf8name(location, "w");
+            g_snprintf (c, 1024, "%s", location); // we want the complete path to be stored in c (for reporting purposes)
             g_free(location);
             if (!file) {
                 // try saving to /tmp
@@ -638,9 +644,14 @@ inkscape_crash_handler (int /*signum*/)
             }
             if (!file) {
                 // try saving to the current directory
+                gchar *curdir = g_get_current_dir();
                 g_snprintf (c, 1024, "inkscape-%.256s.%s.%d.svg", docname, sptstr, count);
                 Inkscape::IO::dump_fopen_call(c, "F");
                 file = Inkscape::IO::fopen_utf8name(c, "w");
+                // store the complete path in c so that it can be reported later
+                gchar * location = g_build_filename(curdir, c, NULL);
+                g_snprintf (c, 1024, "%s", location);
+                g_free(location);
             }
             if (file) {
                 sp_repr_save_stream (repr->document(), file, SP_SVG_NS_URI);
@@ -742,6 +753,28 @@ inkscape_crash_handler (int /*signum*/)
 }
 
 
+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)
@@ -762,10 +795,21 @@ 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);
-    inkscape_load_menus(inkscape);
-    sp_input_load_from_preferences();
-    
+    InkErrorHandler* handler = new InkErrorHandler(use_gui);
+    prefs->setErrorHandler(handler);
+    {
+        Glib::ustring msg;
+        Glib::ustring secondary;
+        if (prefs->getLastError( msg, secondary )) {
+            handler->handleError(msg, secondary);
+        }
+    }
+
+    if (use_gui) {
+        inkscape_load_menus(inkscape);
+        sp_input_load_from_preferences();
+    }
+
     /* set language for user interface according setting in preferences */
     Glib::ustring ui_language = prefs->getString("/ui/language");
     if(!ui_language.empty())
@@ -822,18 +866,21 @@ gboolean inkscape_app_use_gui( Inkscape::Application const * app )
  */
 bool inkscape_load_menus (Inkscape::Application */*inkscape*/)
 {
+    // TODO fix that fn is being leaked
     gchar *fn = profile_path(MENUS_FILE);
-    gchar *menus_xml = NULL; gsize len = 0;
+    gchar *menus_xml = NULL;
+    gsize len = 0;
 
     if (g_file_get_contents(fn, &menus_xml, &len, NULL)) {
         // load the menus_xml file
         INKSCAPE->menus = sp_repr_read_mem(menus_xml, len, NULL);
         g_free(menus_xml);
-        if (INKSCAPE->menus) return true;
+        if (INKSCAPE->menus) {
+            return true;
+        }
     }
     INKSCAPE->menus = sp_repr_read_mem(menus_skeleton, MENUS_SKELETON_SIZE, NULL);
-    if (INKSCAPE->menus) return true;
-    return false;
+    return (INKSCAPE->menus != 0);
 }
 
 
@@ -1342,7 +1389,15 @@ gchar *
 profile_path(const char *filename)
 {
     static const gchar *prefdir = NULL;
+
+
     if (!prefdir) {
+        // First check for a custom environment variable for a "portable app"
+        gchar const *val = g_getenv("INKSCAPE_PORTABLE_PROFILE_DIR");
+        if (val) {
+            prefdir = g_strdup(val);
+        }
+
 #ifdef HAS_SHGetSpecialFolderLocation
         // prefer c:\Documents and Settings\UserName\Application Data\ to
         // c:\Documents and Settings\userName\;
@@ -1396,14 +1451,45 @@ profile_path(const char *filename)
         if (!prefdir) {
             prefdir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR, NULL);
             gchar * legacyDir = homedir_path(INKSCAPE_LEGACY_PROFILE_DIR);
+            gchar * dev47Dir = g_build_filename(g_get_user_config_dir(), INKSCAPE_PROFILE_DIR_047DEV, NULL);
 
-            // TODO here is a point to hook in preference migration
+            bool needsMigration = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) );
+            if (needsMigration) {
+                // TODO here is a point to hook in preference migration
+                g_warning("Preferences need to be migrated from 0.46 or older %s to %s", legacyDir, prefdir);
+                Inkscape::Preferences::migrate( legacyDir, prefdir );
+            }
+
+            bool needsRenameWarning = ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( dev47Dir, G_FILE_TEST_EXISTS ) );
+            if (needsRenameWarning) {
+                g_warning("Preferences need to be copied from  %s to %s", legacyDir, prefdir);
+            }
 
-            if ( !Inkscape::IO::file_test( prefdir, G_FILE_TEST_EXISTS ) && Inkscape::IO::file_test( legacyDir, G_FILE_TEST_EXISTS ) ) {
-                prefdir = legacyDir;
+            g_free(legacyDir);
+            legacyDir = 0;
+            g_free(dev47Dir);
+            dev47Dir = 0;
+            // In case the XDG user config dir of the moment does not yet exist...
+            int mode = S_IRWXU;
+#ifdef S_IRGRP
+            mode |= S_IRGRP;
+#endif
+#ifdef S_IXGRP
+            mode |= S_IXGRP;
+#endif
+#ifdef S_IXOTH
+            mode |= S_IXOTH;
+#endif
+            if ( g_mkdir_with_parents(prefdir, mode) == -1 ) {
+                int problem = errno;
+                g_warning("Unable to create profile directory (%s) (%d)", g_strerror(problem), problem);
             } else {
-                g_free(legacyDir);
-                legacyDir = 0;
+                gchar const *userDirs[] = {"keys", "templates", "icons", "extensions", "palettes", NULL};
+                for (gchar const** name = userDirs; *name; ++name) {
+                    gchar *dir = g_build_filename(prefdir, *name, NULL);
+                    g_mkdir_with_parents(dir, mode);
+                    g_free(dir);
+                }
             }
         }
     }