Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / extension / system.cpp
index c202129308d88049841643ec3de44787afbedd31..cf58f2733f27285224af9f974934699ca93a16c2 100644 (file)
@@ -6,9 +6,12 @@
  *
  * Authors:
  *   Ted Gould <ted@gould.cx>
+ *   Johan Engelen <johan@shouraizou.nl>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
- * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
- * Copyright (C) 2002-2004 Authors
+ * Copyright (C) 2006-2007 Johan Engelen
+ * Copyright (C) 2002-2004 Ted Gould
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
@@ -19,6 +22,9 @@
 
 #include <interface.h>
 
+#include "system.h"
+#include "preferences.h"
+#include "extension.h"
 #include "db.h"
 #include "input.h"
 #include "output.h"
@@ -27,6 +33,8 @@
 #include "print.h"
 #include "implementation/script.h"
 #include "implementation/xslt.h"
+#include "xml/rebase-hrefs.h"
+#include "io/sys.h"
 /* #include "implementation/plugin.h" */
 
 namespace Inkscape {
@@ -90,11 +98,11 @@ open(Extension *key, gchar const *filename)
 
     SPDocument *doc = imod->open(filename);
     if (!doc) {
-        return NULL;
+        throw Input::open_failed();
     }
 
     if (last_chance_svg) {
-        /* We can't call sp_ui_error_dialog because we may be 
+        /* We can't call sp_ui_error_dialog because we may be
            running from the console, in which case calling sp_ui
            routines will cause a segfault.  See bug 1000350 - bryce */
         // sp_ui_error_dialog(_("Format autodetect failed. The file is being opened as SVG."));
@@ -103,13 +111,9 @@ open(Extension *key, gchar const *filename)
 
     /* This kinda overkill as most of these are already set, but I want
        to make sure for this release -- TJG */
-    Inkscape::XML::Node *repr = sp_document_repr_root(doc);
-    bool saved = sp_document_get_undo_sensitive(doc);
-    sp_document_set_undo_sensitive(doc, false);
-    repr->setAttribute("sodipodi:modified", NULL);
-    sp_document_set_undo_sensitive(doc, saved);
+    doc->setModifiedSinceSave(false);
 
-    sp_document_set_uri(doc, filename);
+    doc->setUri(filename);
 
     return doc;
 }
@@ -185,7 +189,8 @@ open_internal(Extension *in_plug, gpointer in_data)
  * Lastly, the save function is called in the module itself.
  */
 void
-save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, bool check_overwrite, bool official)
+save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension, bool check_overwrite, bool official,
+    Inkscape::Extension::FileSaveMethod save_method)
 {
     Output *omod;
     if (key == NULL) {
@@ -203,7 +208,7 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension,
         }
         /* If autodetect fails, save as Inkscape SVG */
         if (omod == NULL) {
-            omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE));
+            // omod = dynamic_cast<Output *>(db.get(SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE)); use exception and let user choose
         }
     } else {
         omod = dynamic_cast<Output *>(key);
@@ -220,8 +225,9 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension,
         throw Output::save_failed();
     }
 
-    if (!omod->prefs())
-        return;
+    if (!omod->prefs()) {
+        throw Output::save_cancelled();
+    }
 
     gchar *fileName = NULL;
     if (setextension) {
@@ -245,25 +251,87 @@ save(Extension *key, SPDocument *doc, gchar const *filename, bool setextension,
         throw Output::no_overwrite();
     }
 
-    omod->save(doc, fileName);
+    // test if the file exists and is writable
+    // the test only checks the file attributes and might pass where ACL does not allow to write
+    if (Inkscape::IO::file_test(filename, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_is_writable(filename)) {
+        g_free(fileName);
+        throw Output::file_read_only();
+    }
+
+    Inkscape::XML::Node *repr = doc->getReprRoot();
 
+
+    // remember attributes in case this is an unofficial save and/or overwrite fails
+    gchar *saved_uri = g_strdup(doc->getURI());
+    bool saved_modified = false;
+    gchar *saved_output_extension = NULL;
+    gchar *saved_dataloss = NULL;
+    saved_modified = doc->isModifiedSinceSave();
+    saved_output_extension = g_strdup(get_file_save_extension(save_method).c_str());
+    saved_dataloss = g_strdup(repr->attribute("inkscape:dataloss"));
     if (official) {
-        // save the filename for next use
-        sp_document_set_uri(doc, fileName);
-        bool saved = sp_document_get_undo_sensitive(doc);
-        sp_document_set_undo_sensitive (doc, false);
+        // The document is changing name/uri.
+        doc->changeUriAndHrefs(fileName);
+    }
+
+    // Update attributes:
+    {
+        bool const saved = DocumentUndo::getUndoSensitive(doc);
+        DocumentUndo::setUndoSensitive(doc, false);
+        {
             // also save the extension for next use
-            Inkscape::XML::Node *repr = sp_document_repr_root(doc);
-            repr->setAttribute("inkscape:output_extension", omod->get_id());
+            store_file_extension_in_prefs (omod->get_id(), save_method);
             // set the "dataloss" attribute if the chosen extension is lossy
-           repr->setAttribute("inkscape:dataloss", NULL);
-            if ( omod->causes_dataloss() ) {
-                   repr->setAttribute("inkscape:dataloss", "true");
+            repr->setAttribute("inkscape:dataloss", NULL);
+            if (omod->causes_dataloss()) {
+                repr->setAttribute("inkscape:dataloss", "true");
+            }
+        }
+        DocumentUndo::setUndoSensitive(doc, saved);
+        doc->setModifiedSinceSave(false);
+    }
+
+    try {
+        omod->save(doc, fileName);
+    }
+    catch(...) {
+        // revert attributes in case of official and overwrite
+        if(check_overwrite && official) {
+            bool const saved = DocumentUndo::getUndoSensitive(doc);
+            DocumentUndo::setUndoSensitive(doc, false);
+            {
+                store_file_extension_in_prefs (saved_output_extension, save_method);
+                repr->setAttribute("inkscape:dataloss", saved_dataloss);
             }
-            repr->setAttribute("sodipodi:modified", NULL);
-       sp_document_set_undo_sensitive (doc, saved);
+            DocumentUndo::setUndoSensitive(doc, saved);
+            doc->changeUriAndHrefs(saved_uri);
+        }
+        doc->setModifiedSinceSave(saved_modified);
+        // free used ressources
+        g_free(saved_output_extension);
+        g_free(saved_dataloss);
+        g_free(saved_uri);
+
+        g_free(fileName);
+
+        throw Inkscape::Extension::Output::save_failed();
     }
-    
+
+    // If it is an unofficial save, set the modified attributes back to what they were.
+    if ( !official) {
+        bool const saved = DocumentUndo::getUndoSensitive(doc);
+        DocumentUndo::setUndoSensitive(doc, false);
+        {
+            store_file_extension_in_prefs (saved_output_extension, save_method);
+            repr->setAttribute("inkscape:dataloss", saved_dataloss);
+        }
+        DocumentUndo::setUndoSensitive(doc, saved);
+        doc->setModifiedSinceSave(saved_modified);
+
+        g_free(saved_output_extension);
+        g_free(saved_dataloss);
+    }
+
     g_free(fileName);
     return;
 }
@@ -356,12 +424,10 @@ build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation
 
     g_return_val_if_fail(doc != NULL, NULL);
 
-    Inkscape::XML::Node *repr = sp_repr_document_root(doc);
+    Inkscape::XML::Node *repr = doc->root();
 
-    /* sp_repr_print(repr); */
-
-    if (strcmp(repr->name(), "inkscape-extension")) {
-        g_warning("Extension definition started with <%s> instead of <inkscape-extension>.  Extension will not be created.\n", repr->name());
+    if (strcmp(repr->name(), INKSCAPE_EXTENSION_NS "inkscape-extension")) {
+        g_warning("Extension definition started with <%s> instead of <" INKSCAPE_EXTENSION_NS "inkscape-extension>.  Extension will not be created. See http://wiki.inkscape.org/wiki/index.php/Extensions for reference.\n", repr->name());
         return NULL;
     }
 
@@ -369,19 +435,19 @@ build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation
     while (child_repr != NULL) {
         char const *element_name = child_repr->name();
         /* printf("Child: %s\n", child_repr->name()); */
-        if (!strcmp(element_name, "input")) {
+        if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "input")) {
             module_functional_type = MODULE_INPUT;
-        } else if (!strcmp(element_name, "output")) {
+        } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "output")) {
             module_functional_type = MODULE_OUTPUT;
-        } else if (!strcmp(element_name, "effect")) {
+        } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "effect")) {
             module_functional_type = MODULE_FILTER;
-        } else if (!strcmp(element_name, "print")) {
+        } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "print")) {
             module_functional_type = MODULE_PRINT;
-        } else if (!strcmp(element_name, "path-effect")) {
+        } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "path-effect")) {
             module_functional_type = MODULE_PATH_EFFECT;
-        } else if (!strcmp(element_name, "script")) {
+        } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "script")) {
             module_implementation_type = MODULE_EXTENSION;
-        } else if (!strcmp(element_name, "xslt")) {
+        } else if (!strcmp(element_name, INKSCAPE_EXTENSION_NS "xslt")) {
             module_implementation_type = MODULE_XSLT;
 #if 0
         } else if (!strcmp(element_name, "plugin")) {
@@ -464,9 +530,7 @@ build_from_reprdoc(Inkscape::XML::Document *doc, Implementation::Implementation
 Extension *
 build_from_file(gchar const *filename)
 {
-    /* TODO: Need to define namespace here, need to write the
-       DTD in general for this stuff */
-    Inkscape::XML::Document *doc = sp_repr_read_file(filename, NULL);
+    Inkscape::XML::Document *doc = sp_repr_read_file(filename, INKSCAPE_EXTENSION_URI);
     Extension *ext = build_from_reprdoc(doc, NULL);
     if (ext != NULL)
         Inkscape::GC::release(doc);
@@ -487,12 +551,112 @@ build_from_file(gchar const *filename)
 Extension *
 build_from_mem(gchar const *buffer, Implementation::Implementation *in_imp)
 {
-    Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), NULL);
+    Inkscape::XML::Document *doc = sp_repr_read_mem(buffer, strlen(buffer), INKSCAPE_EXTENSION_URI);
     Extension *ext = build_from_reprdoc(doc, in_imp);
     Inkscape::GC::release(doc);
     return ext;
 }
 
+/*
+ * TODO: Is it guaranteed that the returned extension is valid? If so, we can remove the check for
+ * filename_extension in sp_file_save_dialog().
+ */
+Glib::ustring
+get_file_save_extension (Inkscape::Extension::FileSaveMethod method) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    Glib::ustring extension;
+    switch (method) {
+        case FILE_SAVE_METHOD_SAVE_AS:
+        case FILE_SAVE_METHOD_TEMPORARY:
+            extension = prefs->getString("/dialogs/save_as/default");
+            break;
+        case FILE_SAVE_METHOD_SAVE_COPY:
+            extension = prefs->getString("/dialogs/save_copy/default");
+            break;
+        case FILE_SAVE_METHOD_INKSCAPE_SVG:
+            extension = SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE;
+            break;
+    }
+
+    if(extension.empty())
+        extension = SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE;
+
+    return extension;
+}
+
+Glib::ustring
+get_file_save_path (SPDocument *doc, FileSaveMethod method) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    Glib::ustring path;
+    switch (method) {
+        case FILE_SAVE_METHOD_SAVE_AS:
+        {
+            bool use_current_dir = prefs->getBool("/dialogs/save_as/use_current_dir", true);
+            if (doc->getURI() && use_current_dir) {
+                path = Glib::path_get_dirname(doc->getURI());
+            } else {
+                path = prefs->getString("/dialogs/save_as/path");
+            }
+            break;
+        }
+        case FILE_SAVE_METHOD_TEMPORARY:
+            path = prefs->getString("/dialogs/save_as/path");
+            break;
+        case FILE_SAVE_METHOD_SAVE_COPY:
+            path = prefs->getString("/dialogs/save_copy/path");
+            break;
+        case FILE_SAVE_METHOD_INKSCAPE_SVG:
+            if (doc->getURI()) {
+                path = Glib::path_get_dirname(doc->getURI());
+            } else {
+                // FIXME: should we use the save_as path here or something else? Maybe we should
+                // leave this as a choice to the user.
+                path = prefs->getString("/dialogs/save_as/path");
+            }
+    }
+
+    if(path.empty())
+        path = g_get_home_dir(); // Is this the most sensible solution? Note that we should avoid
+                                 // g_get_current_dir because this leads to problems on OS X where
+                                 // Inkscape opens the dialog inside application bundle when it is
+                                 // invoked for the first teim.
+
+    return path;
+}
+
+void
+store_file_extension_in_prefs (Glib::ustring extension, FileSaveMethod method) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    switch (method) {
+        case FILE_SAVE_METHOD_SAVE_AS:
+        case FILE_SAVE_METHOD_TEMPORARY:
+            prefs->setString("/dialogs/save_as/default", extension);
+            break;
+        case FILE_SAVE_METHOD_SAVE_COPY:
+            prefs->setString("/dialogs/save_copy/default", extension);
+            break;
+        case FILE_SAVE_METHOD_INKSCAPE_SVG:
+            // do nothing
+            break;
+    }
+}
+
+void
+store_save_path_in_prefs (Glib::ustring path, FileSaveMethod method) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    switch (method) {
+        case FILE_SAVE_METHOD_SAVE_AS:
+        case FILE_SAVE_METHOD_TEMPORARY:
+            prefs->setString("/dialogs/save_as/path", path);
+            break;
+        case FILE_SAVE_METHOD_SAVE_COPY:
+            prefs->setString("/dialogs/save_copy/path", path);
+            break;
+        case FILE_SAVE_METHOD_INKSCAPE_SVG:
+            // do nothing
+            break;
+    }
+}
 
 } } /* namespace Inkscape::Extension */