aboutsummaryrefslogtreecommitdiff
path: root/src/settings.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-09-10 18:21:25 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2017-09-10 18:21:25 +0200
commit10a7765c7b6a1d62cb9ca2406145fd3799f5197c (patch)
tree5e73db926c878412593708624b0caf2bffadccfe /src/settings.cpp
parentSplit off Bookmarks into static lib (diff)
downloadsmolbote-10a7765c7b6a1d62cb9ca2406145fd3799f5197c.tar.xz
Config should now automatically update when changed
Diffstat (limited to 'src/settings.cpp')
-rw-r--r--src/settings.cpp62
1 files changed, 45 insertions, 17 deletions
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 <QDir>
#include <QFile>
#include <QFileInfo>
+#include <QFileSystemWatcher>
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());