From 1021fd42dc8a6be2b2c6fe036651db49cf72a97f Mon Sep 17 00:00:00 2001 From: pjrm Date: Tue, 7 Apr 2009 08:05:23 +0000 Subject: [PATCH] sp_document_change_uri_and_hrefs: New function. --- src/document.cpp | 82 +++++++++++++++++++++++++++++++++--------------- src/document.h | 4 ++- 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index 7dbfe0d2f..1b6e14694 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -61,6 +61,7 @@ #include "transf_mat_3x4.h" #include "unit-constants.h" #include "xml/repr.h" +#include "xml/rebase-hrefs.h" #define SP_DOCUMENT_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE - 1) @@ -608,54 +609,83 @@ void SPDocument::fitToRect(Geom::Rect const &rect) } } -void sp_document_set_uri(SPDocument *document, gchar const *uri) +static void +do_change_uri(SPDocument *const document, gchar const *const filename, bool const rebase) { g_return_if_fail(document != NULL); - if (document->name) { - g_free(document->name); - document->name = NULL; - } - if (document->base) { - g_free(document->base); - document->base = NULL; - } - if (document->uri) { - g_free(document->uri); - document->uri = NULL; - } - - if (uri) { + gchar *new_base; + gchar *new_name; + gchar *new_uri; + if (filename) { #ifndef WIN32 - document->uri = prepend_current_dir_if_relative(uri); + new_uri = prepend_current_dir_if_relative(filename); #else // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test! - document->uri = g_strdup(uri); + new_uri = g_strdup(filename); #endif - /* fixme: Think, what this means for images (Lauris) */ - document->base = g_path_get_dirname(document->uri); - document->name = g_path_get_basename(document->uri); - + new_base = g_path_get_dirname(document->uri); + new_name = g_path_get_basename(document->uri); } else { - document->uri = g_strdup_printf(_("Unnamed document %d"), ++doc_count); - document->base = NULL; - document->name = g_strdup(document->uri); + new_uri = g_strdup_printf(_("Unnamed document %d"), ++doc_count); + new_base = NULL; + new_name = g_strdup(document->uri); } // Update saveable repr attributes. Inkscape::XML::Node *repr = sp_document_repr_root(document); - // changing uri in the document repr must not be not undoable - bool saved = sp_document_get_undo_sensitive(document); + + // Changing uri in the document repr must not be not undoable. + bool const saved = sp_document_get_undo_sensitive(document); sp_document_set_undo_sensitive(document, false); + if (rebase) { + Inkscape::XML::rebase_hrefs(document, new_base, true); + } + repr->setAttribute("sodipodi:docname", document->name); sp_document_set_undo_sensitive(document, saved); + + g_free(document->name); + g_free(document->base); + g_free(document->uri); + document->name = new_name; + document->base = new_base; + document->uri = new_uri; + document->priv->uri_set_signal.emit(document->uri); } +/** + * Sets base, name and uri members of \a document. Doesn't update + * any relative hrefs in the document: thus, this is primarily for + * newly-created documents. + * + * \see sp_document_change_uri_and_hrefs + */ +void sp_document_set_uri(SPDocument *document, gchar const *filename) +{ + g_return_if_fail(document != NULL); + + do_change_uri(document, filename, false); +} + +/** + * Changes the base, name and uri members of \a document, and updates any + * relative hrefs in the document to be relative to the new base. + * + * \see sp_document_set_uri + */ +void sp_document_change_uri_and_hrefs(SPDocument *document, gchar const *filename) +{ + g_return_if_fail(document != NULL); + + do_change_uri(document, filename, true); +} + void sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height) { diff --git a/src/document.h b/src/document.h index fc35611f3..0cb08e593 100644 --- a/src/document.h +++ b/src/document.h @@ -264,7 +264,9 @@ SPItem *sp_document_item_at_point (SPDocument *document, unsigned int key, Geom GSList *sp_document_items_at_points(SPDocument *document, unsigned const key, std::vector points); SPItem *sp_document_group_at_point (SPDocument *document, unsigned int key, Geom::Point const p); -void sp_document_set_uri(SPDocument *document, const gchar *uri); +void sp_document_set_uri(SPDocument *document, gchar const *uri); +void sp_document_change_uri_and_hrefs(SPDocument *document, gchar const *uri); + void sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height); unsigned int vacuum_document(SPDocument *document); -- 2.30.2