From c2d8810625892a8bedcaa192df9b4165e46609ee Mon Sep 17 00:00:00 2001 From: Peter Moulder Date: Tue, 7 Apr 2009 05:31:32 +0000 Subject: Move Inkscape::IO::fixupHrefs to Inkscape::XML::rebase_hrefs in new file xml/rebase-hrefs.*. Give it the old and new base directory, so that it can change hrefs without relying on sodipodi:absref. Also create a new function sp_repr_save_rebased_file that changes all xlink:href attributes as it writes them, so that we don't need to modify the owning document. (Especially useful for "Save a copy" or exporting.) extension/system.cpp: (Inkscape::Extension::save): Don't call sp_document_set_uri even temporarily other than at the same time as calling rebase_hrefs. (Otherwise, existing relative hrefs will point nowhere.) When exporting plain SVG, change the relative hrefs according to the destination filename. (bzr r7643) --- src/file.cpp | 99 ++---------------------------------------------------------- 1 file changed, 2 insertions(+), 97 deletions(-) (limited to 'src/file.cpp') diff --git a/src/file.cpp b/src/file.cpp index f2a4884d4..0bc68f862 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -63,6 +63,7 @@ #include "ui/dialog/ocaldialogs.h" #include "ui/view/view-widget.h" #include "uri.h" +#include "xml/rebase-hrefs.h" #ifdef WITH_GNOME_VFS # include @@ -924,7 +925,7 @@ file_import(SPDocument *in_doc, const Glib::ustring &uri, } if (doc != NULL) { - Inkscape::IO::fixupHrefs(doc, in_doc->base, true); + Inkscape::XML::rebase_hrefs(doc, in_doc->base, true); Inkscape::XML::Document *xml_in_doc = sp_document_repr_doc(in_doc); prevent_id_clashes(doc, in_doc); @@ -1479,102 +1480,6 @@ sp_file_print_preview(gpointer /*object*/, gpointer /*data*/) } -void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns ) -{ - //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base ); - - if ( 0 ) { - gchar const* things[] = { - "data:foo,bar", - "http://www.google.com/image.png", - "ftp://ssd.com/doo", - "/foo/dee/bar.svg", - "foo.svg", - "file:/foo/dee/bar.svg", - "file:///foo/dee/bar.svg", - "file:foo.svg", - "/foo/bar\xe1\x84\x92.svg", - "file:///foo/bar\xe1\x84\x92.svg", - "file:///foo/bar%e1%84%92.svg", - "/foo/bar%e1%84%92.svg", - "bar\xe1\x84\x92.svg", - "bar%e1%84%92.svg", - NULL - }; - g_message("+------"); - for ( int i = 0; things[i]; i++ ) - { - try - { - URI uri(things[i]); - gboolean isAbs = g_path_is_absolute( things[i] ); - gchar *str = uri.toString(); - g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs, - (int)uri.isRelative(), - uri.getScheme(), - uri.getPath(), - uri.getOpaque(), - things[i], - str ); - g_free(str); - } - catch ( MalformedURIException err ) - { - dump_str( things[i], "MalformedURIException" ); - xmlChar *redo = xmlURIEscape((xmlChar const *)things[i]); - g_message(" gone from [%s] to [%s]", things[i], redo ); - if ( redo == NULL ) - { - URI again = URI::fromUtf8( things[i] ); - gboolean isAbs = g_path_is_absolute( things[i] ); - gchar *str = again.toString(); - g_message( "abs:%d isRel:%d scheme:[%s] path:[%s][%s] uri[%s] / [%s]", (int)isAbs, - (int)again.isRelative(), - again.getScheme(), - again.getPath(), - again.getOpaque(), - things[i], - str ); - g_free(str); - g_message(" ----"); - } - } - } - g_message("+------"); - } - - GSList const *images = sp_document_get_resource_list(doc, "image"); - for (GSList const *l = images; l != NULL; l = l->next) { - Inkscape::XML::Node *ir = SP_OBJECT_REPR(l->data); - - const gchar *href = ir->attribute("xlink:href"); - - // First try to figure out an absolute path to the asset - //g_message("image href [%s]", href ); - if (spns && !g_path_is_absolute(href)) { - const gchar *absref = ir->attribute("sodipodi:absref"); - const gchar *base_href = g_build_filename(base, href, NULL); - //g_message(" absr [%s]", absref ); - - if ( absref && Inkscape::IO::file_test(absref, G_FILE_TEST_EXISTS) && !Inkscape::IO::file_test(base_href, G_FILE_TEST_EXISTS)) - { - // only switch over if the absref is valid while href is not - href = absref; - //g_message(" copied absref to href"); - } - } - - // Once we have an absolute path, convert it relative to the new location - if (href && g_path_is_absolute(href)) { - const gchar *relname = sp_relative_path_from_path(href, base); - //g_message(" setting to [%s]", relname ); - ir->setAttribute("xlink:href", relname); - } -// TODO next refinement is to make the first choice keeping the relative path as-is if -// based on the new location it gives us a valid file. - } -} - /* Local Variables: -- cgit v1.2.3