Browse Source

Improve Profile handling

Add Profile::rootPath and Profile::configurationName properties.
adaptive-webui-19844
Vladimir Golovnev (Glassez) 3 years ago
parent
commit
046b741700
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 12
      src/base/profile.cpp
  2. 3
      src/base/profile.h
  3. 42
      src/base/profile_p.cpp
  4. 33
      src/base/profile_p.h

12
src/base/profile.cpp

@ -45,7 +45,7 @@ Profile::Profile(const QString &rootProfilePath, const QString &configurationNam
ensureDirectoryExists(SpecialFolder::Data); ensureDirectoryExists(SpecialFolder::Data);
if (convertPathsToProfileRelative) if (convertPathsToProfileRelative)
m_pathConverterImpl = std::make_unique<Private::Converter>(m_profileImpl->baseDirectory()); m_pathConverterImpl = std::make_unique<Private::Converter>(m_profileImpl->basePath());
else else
m_pathConverterImpl = std::make_unique<Private::NoConvertConverter>(); m_pathConverterImpl = std::make_unique<Private::NoConvertConverter>();
} }
@ -93,6 +93,16 @@ QString Profile::location(const SpecialFolder folder) const
return result; return result;
} }
QString Profile::rootPath() const
{
return m_profileImpl->rootPath();
}
QString Profile::configurationName() const
{
return m_profileImpl->configurationName();
}
QString Profile::profileName() const QString Profile::profileName() const
{ {
return m_profileImpl->profileName(); return m_profileImpl->profileName();

3
src/base/profile.h

@ -62,6 +62,9 @@ public:
QString location(SpecialFolder folder) const; QString location(SpecialFolder folder) const;
SettingsPtr applicationSettings(const QString &name) const; SettingsPtr applicationSettings(const QString &name) const;
QString rootPath() const;
QString configurationName() const;
/// Returns either default name for configuration file (QCoreApplication::applicationName()) /// Returns either default name for configuration file (QCoreApplication::applicationName())
/// or the value, supplied via parameters /// or the value, supplied via parameters
QString profileName() const; QString profileName() const;

42
src/base/profile_p.cpp

@ -32,13 +32,18 @@
#include <QCoreApplication> #include <QCoreApplication>
Private::Profile::Profile(const QString &configurationName) Private::Profile::Profile(const QString &configurationName)
: m_configurationSuffix {configurationName.isEmpty() ? QString() : QLatin1Char('_') + configurationName} : m_configurationName {configurationName}
{ {
} }
QString Private::Profile::configurationName() const
{
return m_configurationName;
}
QString Private::Profile::configurationSuffix() const QString Private::Profile::configurationSuffix() const
{ {
return m_configurationSuffix; return (m_configurationName.isEmpty() ? QString() : QLatin1Char('_') + m_configurationName);
} }
QString Private::Profile::profileName() const QString Private::Profile::profileName() const
@ -47,11 +52,16 @@ QString Private::Profile::profileName() const
} }
Private::DefaultProfile::DefaultProfile(const QString &configurationName) Private::DefaultProfile::DefaultProfile(const QString &configurationName)
: Profile(configurationName) : Profile {configurationName}
{
}
QString Private::DefaultProfile::rootPath() const
{ {
return {};
} }
QString Private::DefaultProfile::baseDirectory() const QString Private::DefaultProfile::basePath() const
{ {
return QDir::homePath(); return QDir::homePath();
} }
@ -116,33 +126,43 @@ QString Private::DefaultProfile::locationWithConfigurationName(const QStandardPa
Private::CustomProfile::CustomProfile(const QString &rootPath, const QString &configurationName) Private::CustomProfile::CustomProfile(const QString &rootPath, const QString &configurationName)
: Profile {configurationName} : Profile {configurationName}
, m_rootDirectory {QDir(rootPath).absoluteFilePath(this->profileName())} , m_rootDir {rootPath}
, m_baseDir {m_rootDir.absoluteFilePath(profileName())}
, m_cacheLocation {m_baseDir.absoluteFilePath(QLatin1String("cache"))}
, m_configLocation {m_baseDir.absoluteFilePath(QLatin1String("config"))}
, m_dataLocation {m_baseDir.absoluteFilePath(QLatin1String("data"))}
, m_downloadLocation {m_baseDir.absoluteFilePath(QLatin1String("downloads"))}
{
}
QString Private::CustomProfile::rootPath() const
{ {
return m_rootDir.absolutePath();
} }
QString Private::CustomProfile::baseDirectory() const QString Private::CustomProfile::basePath() const
{ {
return m_rootDirectory.canonicalPath(); return m_baseDir.absolutePath();
} }
QString Private::CustomProfile::cacheLocation() const QString Private::CustomProfile::cacheLocation() const
{ {
return m_rootDirectory.absoluteFilePath(QLatin1String(cacheDirName)); return m_cacheLocation;
} }
QString Private::CustomProfile::configLocation() const QString Private::CustomProfile::configLocation() const
{ {
return m_rootDirectory.absoluteFilePath(QLatin1String(configDirName)); return m_configLocation;
} }
QString Private::CustomProfile::dataLocation() const QString Private::CustomProfile::dataLocation() const
{ {
return m_rootDirectory.absoluteFilePath(QLatin1String(dataDirName)); return m_dataLocation;
} }
QString Private::CustomProfile::downloadLocation() const QString Private::CustomProfile::downloadLocation() const
{ {
return m_rootDirectory.absoluteFilePath(QLatin1String(downloadsDirName)); return m_downloadLocation;
} }
SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) const SettingsPtr Private::CustomProfile::applicationSettings(const QString &name) const

33
src/base/profile_p.h

@ -39,14 +39,23 @@ namespace Private
class Profile class Profile
{ {
public: public:
virtual QString baseDirectory() const = 0; virtual ~Profile() = default;
virtual QString rootPath() const = 0;
/**
* @brief The base path against to which portable (relative) paths are resolved
*/
virtual QString basePath() const = 0;
virtual QString cacheLocation() const = 0; virtual QString cacheLocation() const = 0;
virtual QString configLocation() const = 0; virtual QString configLocation() const = 0;
virtual QString dataLocation() const = 0; virtual QString dataLocation() const = 0;
virtual QString downloadLocation() const = 0; virtual QString downloadLocation() const = 0;
virtual SettingsPtr applicationSettings(const QString &name) const = 0; virtual SettingsPtr applicationSettings(const QString &name) const = 0;
virtual ~Profile() = default; QString configurationName() const;
/** /**
* @brief QCoreApplication::applicationName() with optional configuration name appended * @brief QCoreApplication::applicationName() with optional configuration name appended
@ -57,8 +66,9 @@ namespace Private
explicit Profile(const QString &configurationName); explicit Profile(const QString &configurationName);
QString configurationSuffix() const; QString configurationSuffix() const;
private: private:
QString m_configurationSuffix; QString m_configurationName;
}; };
/// Default implementation. Takes paths from system /// Default implementation. Takes paths from system
@ -67,7 +77,8 @@ namespace Private
public: public:
explicit DefaultProfile(const QString &configurationName); explicit DefaultProfile(const QString &configurationName);
QString baseDirectory() const override; QString rootPath() const override;
QString basePath() const override;
QString cacheLocation() const override; QString cacheLocation() const override;
QString configLocation() const override; QString configLocation() const override;
QString dataLocation() const override; QString dataLocation() const override;
@ -90,7 +101,8 @@ namespace Private
public: public:
CustomProfile(const QString &rootPath, const QString &configurationName); CustomProfile(const QString &rootPath, const QString &configurationName);
QString baseDirectory() const override; QString rootPath() const override;
QString basePath() const override;
QString cacheLocation() const override; QString cacheLocation() const override;
QString configLocation() const override; QString configLocation() const override;
QString dataLocation() const override; QString dataLocation() const override;
@ -98,11 +110,12 @@ namespace Private
SettingsPtr applicationSettings(const QString &name) const override; SettingsPtr applicationSettings(const QString &name) const override;
private: private:
QDir m_rootDirectory; const QDir m_rootDir;
static constexpr const char *cacheDirName = "cache"; const QDir m_baseDir;
static constexpr const char *configDirName = "config"; const QString m_cacheLocation;
static constexpr const char *dataDirName = "data"; const QString m_configLocation;
static constexpr const char *downloadsDirName = "downloads"; const QString m_dataLocation;
const QString m_downloadLocation;
}; };
class PathConverter class PathConverter

Loading…
Cancel
Save