Save relative paths in fastresume files

Conditionally change absolute paths to relative in the fastresume data files.
The condition is specified by user via a command line parameter and
paths are relative to the profile dir.

On Windows the convertion to relative path is performed if the path and
the profile are on the same drive only.
This commit is contained in:
Eugene Shalygin
2016-05-13 20:32:47 +02:00
parent 0710a59bf5
commit a8d95dd8bd
9 changed files with 127 additions and 21 deletions

View File

@@ -207,3 +207,44 @@ SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) con
const QString settingsFileName {QDir(configLocation()).absoluteFilePath(name + QLatin1String(CONF_FILE_EXTENSION))};
return SettingsPtr(new QSettings(settingsFileName, QSettings::IniFormat));
}
QString Private::NoConvertConverter::fromPortablePath(const QString &portablePath) const
{
return portablePath;
}
QString Private::NoConvertConverter::toPortablePath(const QString &path) const
{
return path;
}
Private::Converter::Converter(const QString &basePath)
: m_baseDir {basePath}
{
m_baseDir.makeAbsolute();
}
QString Private::Converter::toPortablePath(const QString &path) const
{
if (path.isEmpty() || m_baseDir.path().isEmpty())
return path;
#ifdef Q_OS_WIN
if (QDir::isAbsolutePath(path)) {
QChar driveLeter = path[0].toUpper();
QChar baseDriveLetter = m_baseDir.path()[0].toUpper();
bool onSameDrive = (driveLeter.category() == QChar::Letter_Uppercase) && (driveLeter == baseDriveLetter);
if (!onSameDrive)
return path;
}
#endif
return m_baseDir.relativeFilePath(path);
}
QString Private::Converter::fromPortablePath(const QString &portablePath) const
{
if (QDir::isAbsolutePath(portablePath))
return portablePath;
return QDir::cleanPath(m_baseDir.absoluteFilePath(portablePath));
}

View File

@@ -91,5 +91,31 @@ namespace Private
static constexpr const char *dataDirName = "data";
static constexpr const char *downloadsDirName = "downloads";
};
class PathConverter
{
public:
virtual QString toPortablePath(const QString &path) const = 0;
virtual QString fromPortablePath(const QString &portablePath) const = 0;
virtual ~PathConverter() = default;
};
class NoConvertConverter: public PathConverter
{
public:
QString toPortablePath(const QString &path) const override;
QString fromPortablePath(const QString &portablePath) const override;
};
class Converter: public PathConverter
{
public:
Converter(const QString &basePath);
QString toPortablePath(const QString &path) const override;
QString fromPortablePath(const QString &portablePath) const override;
private:
QDir m_baseDir;
};
}
#endif // QBT_PROFILE_P_H