Utility to ease access to file which path is stored as absolute. It can translate obsolete paths into new ones, for instance, if the user change the organisation of his file tree. The new path is only asked one time. For all other requests, the obsolete path is searched in a map and if found, the file path is modified. More...
#include <PathTranslator.h>
Public Member Functions | |
void | addPath (QString obsoletePath, QString newPath) |
PathTranslator () | |
QString | translate (const QString &originalFile, const QString &title, const QString &fileType) |
~PathTranslator () |
Utility to ease access to file which path is stored as absolute. It can translate obsolete paths into new ones, for instance, if the user change the organisation of his file tree. The new path is only asked one time. For all other requests, the obsolete path is searched in a map and if found, the file path is modified.
QGpCoreTools::PathTranslator::PathTranslator | ( | ) | [inline] |
{}
QGpCoreTools::PathTranslator::~PathTranslator | ( | ) | [inline] |
{}
void QGpCoreTools::PathTranslator::addPath | ( | QString | obsoletePath, |
QString | newPath | ||
) |
Add the different root between obsoletePath and newPath to this translator
References TRACE.
Referenced by translate().
{ TRACE; //qDebug("Entering PathTranslator::addPath( %s, %s)",obsoletePath.toAscii().data(),newPath.toAscii().data()); while( !isRoot(obsoletePath) && !isRoot(newPath)) { QString obsName=dirName(obsoletePath); QString newName=dirName(newPath); if(obsName!=newName) { //qDebug("Adding translation for %s to %s", obsoletePath.toAscii().data(), newPath.toAscii().data()); insert (obsoletePath, newPath); return; } obsoletePath.chop(obsName.count() + 1); newPath.chop(newName.count() + 1); //qDebug("Choped obsoletePath: %s",obsoletePath.toAscii().data()); //qDebug("Choped newPath: %s",newPath.toAscii().data()); } if(obsoletePath!=newPath) { //qDebug("Adding translation for %s to %s", obsoletePath.toAscii().data(), newPath.toAscii().data()); insert (obsoletePath, newPath); } }
QString QGpCoreTools::PathTranslator::translate | ( | const QString & | originalFile, |
const QString & | title, | ||
const QString & | fileType | ||
) |
Returns a translated path (even file name can be translated for lower or upper letters)
fileType must contain '%1' to be replace by file name
References addPath(), and TRACE.
Referenced by QGpCoreTools::CoreApplicationPrivate::translatePath().
{ TRACE; QString path=platformIndependantPath(originalFile); QFileInfo file(path); path=file.path(); QString relPath; iterator it; //qDebug("Entering PathTranslator::translate(%s,%s,%s)",path.toAscii().data(),title.toAscii().data(), // fileType.toAscii().data()); while(true) { it=find(path); if(it!=end()) { path=it.value(); //qDebug("Path replaced by %s",path.toAscii().data()); path += relPath; //qDebug("definitive path %s",path.toAscii().data()); QDir d (path); if(d.exists()) { QFileInfo fi(d.absoluteFilePath(file.fileName())); if(fileExists(fi)) return fi.absoluteFilePath(); } return askPath(file, title, fileType, d.absolutePath()); } if(isRoot(path)) break; QString name=dirName(path); path.chop(name.count() + 1); relPath=QDir::separator() + name + relPath; //qDebug("Choped path: %s",path.toAscii().data()); } // A commom situation where current path corresponds to the base path of original file path // This is the case after opening a Geopsy database using the file browser. QString dName=dirName(file.path()); QDir d; if(d.dirName()==dName) { QFileInfo finew(d.absoluteFilePath(file.fileName())); if(fileExists(finew)) { addPath(file.absolutePath(), finew.absolutePath()); return finew.absoluteFilePath(); } } d.cd(dName); if(d.exists()) { QFileInfo finew(d.absoluteFilePath(file.fileName())); if(fileExists(finew)) { addPath(file.absolutePath(), finew.absolutePath()); return finew.absoluteFilePath(); } } return askPath(file, title, fileType); }