static QString translatePath( QString path ) // krazy:exclude=passbyvalue { if (path.isEmpty()) return path; // only "our" $HOME should be interpreted path.replace(QLatin1Char('$'), QLatin1String("$$")); bool startsWithFile = path.startsWith(QLatin1String("file:"), Qt::CaseInsensitive); // return original path, if it refers to another type of URL (e.g. http:/), or // if the path is already relative to another directory if ((!startsWithFile && QFileInfo(path).isRelative()) || (startsWithFile && QFileInfo(path.mid(5)).isRelative())) return path; if (startsWithFile) path.remove(0,5); // strip leading "file:/" off the string // keep only one single '/' at the beginning - needed for cleanHomeDirPath() while (path[0] == QLatin1Char('/') && path[1] == QLatin1Char('/')) path.remove(0,1); // we can not use KGlobal::dirs()->relativeLocation("home", path) here, // since it would not recognize paths without a trailing '/'. // All of the 3 following functions to return the user's home directory // can return different paths. We have to test all them. const QString homeDir0 = QFile::decodeName(qgetenv("HOME")); const QString homeDir1 = QDir::homePath(); const QString homeDir2 = QDir(homeDir1).canonicalPath(); if (cleanHomeDirPath(path, homeDir0) || cleanHomeDirPath(path, homeDir1) || cleanHomeDirPath(path, homeDir2) ) { // qDebug() << "Path was replaced\n"; } if (startsWithFile) path.prepend(QString::fromLatin1("file://")); return path; }
static QString translatePath(QString path) { if(path.isEmpty()) return path; // only "our" $HOME should be interpreted path.replace('$', "$$"); bool startsWithFile = path.startsWith("file:", false); // return original path, if it refers to another type of URL (e.g. http:/), or // if the path is already relative to another directory if((!startsWithFile && path[0] != '/') || (startsWithFile && path[5] != '/')) return path; if(startsWithFile) path.remove(0, 5); // strip leading "file:/" off the string // keep only one single '/' at the beginning - needed for cleanHomeDirPath() while(path[0] == '/' && path[1] == '/') path.remove(0, 1); // we can not use KGlobal::dirs()->relativeLocation("home", path) here, // since it would not recognize paths without a trailing '/'. // All of the 3 following functions to return the user's home directory // can return different paths. We have to test all them. QString homeDir0 = QFile::decodeName(getenv("HOME")); QString homeDir1 = QDir::homeDirPath(); QString homeDir2 = QDir(homeDir1).canonicalPath(); if(cleanHomeDirPath(path, homeDir0) || cleanHomeDirPath(path, homeDir1) || cleanHomeDirPath(path, homeDir2)) { // kdDebug() << "Path was replaced\n"; } if(startsWithFile) path.prepend("file://"); return path; }