diff --git a/src/extension/internal/svg.cpp b/src/extension/internal/svg.cpp index a94bc21..396813c 100644 --- a/src/extension/internal/svg.cpp +++ b/src/extension/internal/svg.cpp @@ -148,21 +148,16 @@ Svg::init(void) #define BUF_SIZE 8192 static gchar * -_load_uri (const gchar *uri) +_load_vfs_uri (GnomeVFSURI* const vfs_uri) { GnomeVFSHandle *handle = NULL; GnomeVFSFileSize bytes_read; - gsize bytesRead = 0; - gsize bytesWritten = 0; - GError* error = NULL; - gchar* uri_local = g_filename_from_utf8( uri, -1, &bytesRead, &bytesWritten, &error); + gsize bytesRead = 0; + gsize bytesWritten = 0; + GError* error = NULL; - if ( uri_local == NULL ) { - g_warning( "Error converting filename to locale encoding."); - } - - GnomeVFSResult result = gnome_vfs_open (&handle, uri_local, GNOME_VFS_OPEN_READ); + GnomeVFSResult result = gnome_vfs_open_uri (&handle, vfs_uri, GNOME_VFS_OPEN_READ); if (result != GNOME_VFS_OK) { g_warning("%s", gnome_vfs_result_to_string(result)); @@ -193,18 +188,25 @@ SPDocument * Svg::open (Inkscape::Extension::Input */*mod*/, const gchar *uri) { #ifdef WITH_GNOME_VFS - if (!gnome_vfs_initialized() || gnome_vfs_uri_is_local(gnome_vfs_uri_new(uri))) { - // Use built-in loader instead of VFS for this - return SPDocument::createNewDoc(uri, TRUE); - } - gchar * buffer = _load_uri(uri); - if (buffer == NULL) { - g_warning("Error: Could not open file '%s' with VFS\n", uri); - return NULL; + // We may already have a vfs_uri text with a method and a scheme + // (i.e. file:///path/file.svgz#gzip:) + GnomeVFSURI* const vfs_uri = gnome_vfs_uri_new(gnome_vfs_make_uri_from_input(uri)); + + // Delegate all file loading to gnome_vfs if up + SPDocument* doc; + if (gnome_vfs_initialized()) { + gchar * buffer = _load_vfs_uri(vfs_uri); + if (buffer == NULL) { + g_warning("Error: Could not open file '%s' with VFS\n", uri); + return NULL; + } + doc = SPDocument::createNewDocFromMem(buffer, strlen(buffer), 1); + g_free(buffer); + } else { + // Go through the built-in loader + doc = SPDocument::createNewDoc(uri, TRUE); } - SPDocument * doc = SPDocument::createNewDocFromMem(buffer, strlen(buffer), 1); - - g_free(buffer); + g_free(vfs_uri); return doc; #else return SPDocument::createNewDoc(uri, TRUE); diff --git a/src/extension/internal/svgz.cpp b/src/extension/internal/svgz.cpp index be94409..22557aa 100644 --- a/src/extension/internal/svgz.cpp +++ b/src/extension/internal/svgz.cpp @@ -19,6 +19,12 @@ #include "extension/extension.h" #include "extension/system.h" + +#ifdef WITH_GNOME_VFS +#include +# include +#endif + namespace Inkscape { namespace Extension { namespace Internal { @@ -85,6 +91,40 @@ Svgz::init(void) return; } +#ifdef WITH_GNOME_VFS +/** + \return A new document just for you! + \brief Overrides parent Svg::open(). + This function takes in a gnome_vfs_uri string of an SVGZ document + and makes sure we use the gzip method to open it before calling + parent Svg::open() + \param mod Module to use + \param vfs_uri_text The path to the file (UTF-8) + + This function is really simple, it just calls sp_document_new... +*/ +SPDocument * +Svgz::open (Inkscape::Extension::Input *mod, const gchar *vfs_uri_text) +{ + // + GnomeVFSURI* vfs_uri_current = gnome_vfs_uri_new(gnome_vfs_make_uri_from_input(vfs_uri_text)); + //GnomeVFSToplevelURI* toplevel_uri = gnome_vfs_uri_get_toplevel(vfs_uri); + + GnomeVFSURI* vfs_uri; + // make sure we are using the gzip method for vfs access + if(strcmp("gzip", vfs_uri_current->method_string) != 0) { + vfs_uri = gnome_vfs_uri_append_string(vfs_uri_current, "#gzip:"); + } else { + vfs_uri = gnome_vfs_uri_dup(vfs_uri_current); + } + g_free(vfs_uri_current); + + const gchar* uri_text = gnome_vfs_uri_to_string(vfs_uri, GNOME_VFS_URI_HIDE_NONE); + g_free(vfs_uri); + + return Svg::open(mod, uri_text); +} +#endif } } } // namespace inkscape, module, implementation diff --git a/src/extension/internal/svgz.h b/src/extension/internal/svgz.h index ba348f8..06bcac3 100644 --- a/src/extension/internal/svgz.h +++ b/src/extension/internal/svgz.h @@ -24,6 +24,10 @@ namespace Internal { class Svgz : public Svg { public: static void init( void ); +#ifdef WITH_GNOME_VFS + SPDocument *open( Inkscape::Extension::Input *mod, + const gchar *uri ) override; +#endif }; } } } // namespace Inkscape, Extension, Implementation diff --git a/src/extension/system.cpp b/src/extension/system.cpp index 6a95717..166c088 100644 --- a/src/extension/system.cpp +++ b/src/extension/system.cpp @@ -40,6 +40,9 @@ #include "inkscape.h" #include "document-undo.h" +#ifdef WITH_GNOME_VFS +# include +#endif namespace Inkscape { namespace Extension { @@ -72,9 +75,17 @@ SPDocument *open(Extension *key, gchar const *filename) { Input *imod = NULL; +#ifdef WITH_GNOME_VFS + // If we have a vfs uri an access method (e.g. #gzip:) + // strip it out for module autodetection + gchar* auto_detect_uri = gnome_vfs_make_uri_canonical_strip_fragment(filename); +#else + gchar* auto_detect_uri = strdup(filename); +#endif + if (key == NULL) { gpointer parray[2]; - parray[0] = (gpointer)filename; + parray[0] = (gpointer)auto_detect_uri; parray[1] = (gpointer)&imod; db.foreach(open_internal, (gpointer)&parray); } else { @@ -128,7 +139,9 @@ SPDocument *open(Extension *key, gchar const *filename) } } - doc->setUri(filename); + // Send the autodetect uri to have a coherent + // filename if we had a vfs method in uri + doc->setUri(auto_detect_uri); if (!show) { imod->set_gui(true); }