summaryrefslogtreecommitdiffstats
path: root/src/resource-manager.cpp
diff options
context:
space:
mode:
authorJon A. Cruz <jon@joncruz.org>2011-05-10 07:48:37 +0000
committerJon A. Cruz <jon@joncruz.org>2011-05-10 07:48:37 +0000
commit010da2dbdf05c0fa2ea791cb9d75a558551c8413 (patch)
tree0b160646c86bdb82e4c4a1c07f3ae00fd51b7c77 /src/resource-manager.cpp
parentAdded simple usage of most recent file locations. (diff)
downloadinkscape-010da2dbdf05c0fa2ea791cb9d75a558551c8413.tar.gz
inkscape-010da2dbdf05c0fa2ea791cb9d75a558551c8413.zip
Convert fixed paths to relative, including ..
(bzr r10205)
Diffstat (limited to 'src/resource-manager.cpp')
-rw-r--r--src/resource-manager.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/resource-manager.cpp b/src/resource-manager.cpp
index 0ccc05d49..14a5ccfb8 100644
--- a/src/resource-manager.cpp
+++ b/src/resource-manager.cpp
@@ -25,6 +25,76 @@
namespace Inkscape {
+std::vector<std::string> splitPath( std::string const &path )
+{
+ std::vector<std::string> parts;
+
+ std::string prior;
+ std::string tmp = path;
+ while ( !tmp.empty() && (tmp != prior) ) {
+ prior = tmp;
+
+ parts.push_back( Glib::path_get_basename(tmp) );
+ tmp = Glib::path_get_dirname(tmp);
+ }
+ if ( !parts.empty() ) {
+ std::reverse(parts.begin(), parts.end());
+ }
+
+ return parts;
+}
+
+std::string convertPathToRelative( std::string const &path, std::string const &docbase )
+{
+ std::string result = path;
+
+ if ( !path.empty() && Glib::path_is_absolute(path) ) {
+ // Whack the parts into pieces
+
+ std::vector<std::string> parts = splitPath(path);
+ std::vector<std::string> baseParts = splitPath(docbase);
+
+ // TODO debug g_message("+++++++++++++++++++++++++");
+ for ( std::vector<std::string>::iterator it = parts.begin(); it != parts.end(); ++it ) {
+ // TODO debug g_message(" [%s]", it->c_str());
+ }
+ // TODO debug g_message(" - - - - - - - - - - - - - - - ");
+ for ( std::vector<std::string>::iterator it = baseParts.begin(); it != baseParts.end(); ++it ) {
+ // TODO debug g_message(" [%s]", it->c_str());
+ }
+ // TODO debug g_message("+++++++++++++++++++++++++");
+
+ if ( !parts.empty() && !baseParts.empty() && (parts[0] == baseParts[0]) ) {
+ // Both paths have the same root. We can proceed.
+ while ( !parts.empty() && !baseParts.empty() && (parts[0] == baseParts[0]) ) {
+ parts.erase( parts.begin() );
+ baseParts.erase( baseParts.begin() );
+ }
+
+ // TODO debug g_message("+++++++++++++++++++++++++");
+ for ( std::vector<std::string>::iterator it = parts.begin(); it != parts.end(); ++it ) {
+ // TODO debug g_message(" [%s]", it->c_str());
+ }
+ // TODO debug g_message(" - - - - - - - - - - - - - - - ");
+ for ( std::vector<std::string>::iterator it = baseParts.begin(); it != baseParts.end(); ++it ) {
+ // TODO debug g_message(" [%s]", it->c_str());
+ }
+ // TODO debug g_message("+++++++++++++++++++++++++");
+
+ if ( !parts.empty() ) {
+ result.clear();
+
+ for ( size_t i = 0; i < baseParts.size(); ++i ) {
+ parts.insert(parts.begin(), "..");
+ }
+ result = Glib::build_filename( parts );
+ // TODO debug g_message("----> [%s]", result.c_str());
+ }
+ }
+ }
+
+ return result;
+}
class ResourceManagerImpl : public ResourceManager {
@@ -202,6 +272,11 @@ std::map<Glib::ustring, Glib::ustring> ResourceManagerImpl::locateLinks(Glib::us
}
if ( exists ) {
+ if ( Glib::path_is_absolute( remainder ) ) {
+ // TODO debug g_message("Need to convert to relative if possible [%s]", remainder.c_str());
+ remainder = convertPathToRelative( remainder, docbase );
+ }
+
bool isAbsolute = Glib::path_is_absolute( remainder );
Glib::ustring replacement = isAbsolute ? Glib::filename_to_uri( remainder ) : Glib::filename_to_utf8( remainder );
result[*it] = replacement;