Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / file.cpp
index 420eaec6393c7038ba576ad9f7a99f4beb4bc4d2..e0ecd50843f8b96e1f298fbb6c3e45c593d10f66 100644 (file)
@@ -68,9 +68,9 @@
 # include <libgnomevfs/gnome-vfs.h>
 #endif
 
-#ifdef WITH_INKBOARD
-#include "jabber_whiteboard/session-manager.h"
-#endif
+//#ifdef WITH_INKBOARD
+//#include "jabber_whiteboard/session-manager.h"
+//#endif
 
 #ifdef WIN32
 #include <windows.h>
@@ -138,31 +138,48 @@ sp_file_new(const Glib::ustring &templ)
     return dt;
 }
 
-SPDesktop*
-sp_file_new_default()
+SPDesktop* sp_file_new_default()
 {
     std::list<gchar *> sources;
     sources.push_back( profile_path("templates") ); // first try user's local dir
     sources.push_back( g_strdup(INKSCAPE_TEMPLATESDIR) ); // then the system templates dir
-
-    while (!sources.empty()) {
-        gchar *dirname = sources.front();
-        if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
-
-            // TRANSLATORS: default.svg is localizable - this is the name of the default document
-            //  template. This way you can localize the default pagesize, translate the name of
-            //  the default layer, etc. If you wish to localize this file, please create a
-            //  localized share/templates/default.xx.svg file, where xx is your language code.
-            char *default_template = g_build_filename(dirname, _("default.svg"), NULL);
-            if (Inkscape::IO::file_test(default_template, G_FILE_TEST_IS_REGULAR)) {
-                return sp_file_new(default_template);
+    std::list<gchar const*> baseNames;
+    gchar const* localized = _("default.svg");
+    if (strcmp("default.svg", localized) != 0) {
+        baseNames.push_back(localized);
+    }
+    baseNames.push_back("default.svg");
+    gchar *foundTemplate = 0;
+
+    for (std::list<gchar const*>::iterator nameIt = baseNames.begin(); (nameIt != baseNames.end()) && !foundTemplate; ++nameIt) {
+        for (std::list<gchar *>::iterator it = sources.begin(); (it != sources.end()) && !foundTemplate; ++it) {
+            gchar *dirname = *it;
+            if ( Inkscape::IO::file_test( dirname, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ) ) {
+
+                // TRANSLATORS: default.svg is localizable - this is the name of the default document
+                //  template. This way you can localize the default pagesize, translate the name of
+                //  the default layer, etc. If you wish to localize this file, please create a
+                //  localized share/templates/default.xx.svg file, where xx is your language code.
+                char *tmp = g_build_filename(dirname, *nameIt, NULL);
+                if (Inkscape::IO::file_test(tmp, G_FILE_TEST_IS_REGULAR)) {
+                    foundTemplate = tmp;
+                } else {
+                    g_free(tmp);
+                }
             }
         }
-        g_free(dirname);
-        sources.pop_front();
     }
 
-    return sp_file_new("");
+    for (std::list<gchar *>::iterator it = sources.begin(); it != sources.end(); ++it) {
+        g_free(*it);
+    }
+
+    SPDesktop* desk = sp_file_new(foundTemplate ? foundTemplate : "");
+    if (foundTemplate) {
+        g_free(foundTemplate);
+        foundTemplate = 0;
+    }
+    return desk;
 }
 
 
@@ -591,6 +608,14 @@ file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
         g_free(text);
         g_free(safeUri);
         return FALSE;
+    } catch (Inkscape::Extension::Output::file_read_only &e) {
+        gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
+        gchar *text = g_strdup_printf(_("File %s is write protected. Please remove write protection and try again."), safeUri);
+        SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
+        sp_ui_error_dialog(text);
+        g_free(text);
+        g_free(safeUri);
+        return FALSE;
     } catch (Inkscape::Extension::Output::save_failed &e) {
         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
         gchar *text = g_strdup_printf(_("File %s could not be saved."), safeUri);
@@ -604,6 +629,9 @@ file_save(Gtk::Window &parentWindow, SPDocument *doc, const Glib::ustring &uri,
         return FALSE;
     } catch (Inkscape::Extension::Output::no_overwrite &e) {
         return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
+    } catch (...) {
+        SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Document not saved."));
+        return FALSE;
     }
 
     SP_ACTIVE_DESKTOP->event_log->rememberFileSave();
@@ -837,8 +865,8 @@ sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
     if (doc->isModifiedSinceSave()) {
         if ( doc->uri == NULL )
         {
-            // Hier sollte in Argument mitgegeben werden, das anzeigt, daß das Dokument das erste
-            // Mal gespeichert wird, so daß als default .svg ausgewählt wird und nicht die zuletzt
+            // Hier sollte in Argument mitgegeben werden, das anzeigt, da das Dokument das erste
+            // Mal gespeichert wird, so da� als default .svg ausgew�hlt wird und nicht die zuletzt
             // benutzte "Save as ..."-Endung
             return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
         } else {
@@ -855,6 +883,10 @@ sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
                 ext = fn.substr( pos );
             }
             success = file_save(parentWindow, doc, fn, Inkscape::Extension::db.get(ext.c_str()), FALSE, TRUE, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS);
+            if (success == false) {
+                // give the user the chance to change filename or extension
+                return sp_file_save_dialog(parentWindow, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG);
+            }
         }
     } else {
         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No changes need to be saved."));
@@ -1130,7 +1162,7 @@ sp_file_import(Gtk::Window &parentWindow)
  *  Display an Export dialog, export as the selected type if OK pressed
  */
 bool
-sp_file_export_dialog(void *widget)
+sp_file_export_dialog(Gtk::Window &parentWindow)
 {
     //# temp hack for 'doc' until we can switch to this dialog
     SPDocument *doc = SP_ACTIVE_DOCUMENT;
@@ -1192,6 +1224,7 @@ sp_file_export_dialog(void *widget)
     //# Show the Export dialog
     Inkscape::UI::Dialog::FileExportDialog *exportDialogInstance =
         Inkscape::UI::Dialog::FileExportDialog::create(
+            parentWindow,
             export_path,
             Inkscape::UI::Dialog::EXPORT_TYPES,
             (char const *) _("Select file to export to"),
@@ -1220,7 +1253,7 @@ sp_file_export_dialog(void *widget)
         else
             g_warning( "Error converting save filename to UTF-8." );
 
-        success = file_save(doc, fileName, selectionType, TRUE, FALSE);
+        success = file_save(parentWindow, doc, fileName, selectionType, TRUE, FALSE, Inkscape::Extension::FILE_SAVE_METHOD_EXPORT);
 
         if (success) {
             Glib::RefPtr<Gtk::RecentManager> recent = Gtk::RecentManager::get_default();
@@ -1243,7 +1276,7 @@ sp_file_export_dialog(void *widget)
  *
  */
 bool
-sp_file_export_dialog(void */*widget*/)
+sp_file_export_dialog(Gtk::Window &/*parentWindow*/)
 {
     sp_export_dialog();
     return true;
@@ -1258,6 +1291,7 @@ sp_file_export_dialog(void */*widget*/)
 /**
  *  Display an Export dialog, export as the selected type if OK pressed
  */
+/*
 bool
 sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
 {
@@ -1394,10 +1428,11 @@ sp_file_export_to_ocal_dialog(Gtk::Window &parentWindow)
 
     return success;
 }
-
+*/
 /**
  * Export the current document to OCAL
  */
+/*
 void
 sp_file_export_to_ocal(Gtk::Window &parentWindow)
 {
@@ -1409,7 +1444,7 @@ sp_file_export_to_ocal(Gtk::Window &parentWindow)
     if (success)
         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Document exported..."));
 }
-
+*/
 
 /*######################
 ## I M P O R T  F R O M  O C A L