From 10a7765c7b6a1d62cb9ca2406145fd3799f5197c Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 10 Sep 2017 18:21:25 +0200 Subject: Config should now automatically update when changed --- src/settings.cpp | 62 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 17 deletions(-) (limited to 'src/settings.cpp') diff --git a/src/settings.cpp b/src/settings.cpp index a0fad7f..638e3bd 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -25,31 +25,54 @@ #include #include #include +#include Settings::Settings(const QString &configFile, const QString &defaultsFile) { - values = parse(configFile); - defaults = parse(defaultsFile); - m_configurationPath = configFile; m_defaultsPath = defaultsFile; + // homeLocation is the user's home folder homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); - settingsLocation = QFileInfo(configFile).dir().absolutePath(); + + // settingsLocation is the location of the configFile + if(QFile::exists(configFile)) { + settingsLocation = QFileInfo(configFile).dir().absolutePath(); + } else { + // if file doesn't exist, use the generic location + settingsLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation); + } + + // cacheLocation cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); + userValues = parse(m_configurationPath); + defaultValues = parse(m_defaultsPath); + + m_watcher = new QFileSystemWatcher(); + m_watcher->addPath(configFile); + QObject::connect(m_watcher, &QFileSystemWatcher::fileChanged, [this](const QString &path) { + if(path == m_configurationPath) { +#ifdef QT_DEBUG + qDebug("Reloading user configuration"); +#endif + userValues = parse(m_configurationPath); + } + }); + #ifdef QT_DEBUG qDebug(">> Configuration"); - qDebug("- values: [%s]", qUtf8Printable(configFile)); - qDebug("- defaults: [%s]", qUtf8Printable(defaultsFile)); - qDebug("- $home | '%s'", qUtf8Printable(homeLocation)); - qDebug("- $settings | '%s'", qUtf8Printable(settingsLocation)); - qDebug("- $cache | '%s'", qUtf8Printable(cacheLocation)); + qDebug("- userconf: [%s]", qUtf8Printable(m_configurationPath)); + qDebug("- defaults: [%s]", qUtf8Printable(m_defaultsPath)); + qDebug("- $home [%s]", qUtf8Printable(homeLocation)); + qDebug("- $settings [%s]", qUtf8Printable(settingsLocation)); + qDebug("- $cache [%s]", qUtf8Printable(cacheLocation)); #endif } Settings::~Settings() { + m_watcher->deleteLater(); } QString Settings::configurationPath() const @@ -64,13 +87,15 @@ QString Settings::defaultsPath() const bool Settings::isEmpty() const { - return values.empty(); + return userValues.empty(); } bool Settings::contains(const QString &key) { - const toml::Value *x = values.find(key.toStdString()); - if(x) { + const toml::Value *x = userValues.find(key.toStdString()); + const toml::Value *y = defaultValues.find(key.toStdString()); + + if(x || y) { return true; } else { return false; @@ -79,15 +104,15 @@ bool Settings::contains(const QString &key) QVariant Settings::value(const QString &key) const { - const toml::Value *cValue = values.find(key.toStdString()); - const toml::Value *dValue = defaults.find(key.toStdString()); + const toml::Value *cValue = userValues.find(key.toStdString()); + const toml::Value *dValue = defaultValues.find(key.toStdString()); QVariant r; - if(values.has(key.toStdString())) { + if(userValues.has(key.toStdString())) { r = valueToVariant(cValue); } else { - if(defaults.has(key.toStdString())) { + if(defaultValues.has(key.toStdString())) { r = valueToVariant(dValue); } } @@ -110,7 +135,10 @@ toml::Value Settings::parse(const QString &filename) { toml::Value r; - if(!filename.isEmpty()) { + if(filename.isEmpty()) { + qWarning("Empty configuration path."); + + } else { QFile file(filename); if(file.open(QIODevice::ReadOnly)) { std::stringstream d(file.readAll().toStdString()); -- cgit v1.2.1