diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-09-10 18:21:25 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-09-10 18:21:25 +0200 |
commit | 10a7765c7b6a1d62cb9ca2406145fd3799f5197c (patch) | |
tree | 5e73db926c878412593708624b0caf2bffadccfe | |
parent | Split off Bookmarks into static lib (diff) | |
download | smolbote-10a7765c7b6a1d62cb9ca2406145fd3799f5197c.tar.xz |
Config should now automatically update when changed
-rw-r--r-- | src/browser.cpp | 35 | ||||
-rw-r--r-- | src/browser.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 9 | ||||
-rw-r--r-- | src/settings.cpp | 62 | ||||
-rw-r--r-- | src/settings.h | 7 | ||||
-rw-r--r-- | src/singleapplication.cpp | 1 | ||||
-rw-r--r-- | src/singleapplication.h | 4 | ||||
-rw-r--r-- | tools/qbs/GitRepo.js | 2 |
8 files changed, 77 insertions, 45 deletions
diff --git a/src/browser.cpp b/src/browser.cpp index 8db1980..60cdb03 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -39,13 +39,6 @@ Browser::Browser(int &argc, char *argv[]) : m_blocklistManager = nullptr; m_plugin = nullptr; - - setApplicationName("smolbote"); -#ifdef GIT_VERSION - setApplicationVersion(GIT_VERSION); -#else - setApplicationVersion("1.0.0"); -#endif } Browser::~Browser() @@ -160,10 +153,24 @@ BlockerManager *Browser::blocklists() return m_blocklistManager; } -void Browser::setConfigPath(const QString &path) +void Browser::loadSettings(const QString &path) { QString configLocation, defaultsLocation; + // set custom config path if any + if(!path.isEmpty()) { + configLocation = path; + + } else { + // no custom config has been set + // check if config file exists for this user + QString cpath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/poi.conf"; + if(QFile::exists(cpath)) { + configLocation = cpath; + } + // else there is no user overrides + } + // set defaults location // check system-specific locations #ifdef Q_OS_LINUX @@ -179,18 +186,6 @@ void Browser::setConfigPath(const QString &path) defaultsLocation = ":/poi.toml"; } - // set custom config path if any - if(!path.isEmpty()) { - configLocation = path; - - } else { - // check if config file exists for this user - QString cpath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/poi.conf"; - if(QFile::exists(cpath)) { - configLocation = cpath; - } - } - m_settings = new Settings(configLocation, defaultsLocation); #ifdef QT_DEBUG diff --git a/src/browser.h b/src/browser.h index 7a5d300..35ca0df 100644 --- a/src/browser.h +++ b/src/browser.h @@ -50,7 +50,7 @@ public: QString applicationLongVersion() const; - void setConfigPath(const QString &path); + void loadSettings(const QString &path); void loadPlugins(); void loadProfiles(); diff --git a/src/main.cpp b/src/main.cpp index 9c12b64..9dd77fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,13 @@ int main(int argc, char *argv[]) { // Browser app(argc, argv); + app.setApplicationName("smolbote"); +#ifdef GIT_VERSION + app.setApplicationVersion(GIT_VERSION); +#else + app.setApplicationVersion("1.0.0"); +#endif + if(app.isRunning()) { qDebug("Another instance is running, returning..."); return 0; @@ -86,7 +93,7 @@ int main(int argc, char *argv[]) app.setAttribute(Qt::AA_EnableHighDpiScaling); // Set configuration - app.setConfigPath(parser.value(configOption)); + app.loadSettings(parser.value(configOption)); // Load profiles app.loadProfiles(); 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()); diff --git a/src/settings.h b/src/settings.h index 673158e..0c0cfc7 100644 --- a/src/settings.h +++ b/src/settings.h @@ -24,6 +24,7 @@ #include <toml/toml.h> #include <QVariant> +class QFileSystemWatcher; class Settings { public: @@ -43,8 +44,8 @@ private: QVariant fromList(const toml::Value *list) const; QVariant valueToVariant(const toml::Value *value) const; - toml::Value values; - toml::Value defaults; + toml::Value userValues; + toml::Value defaultValues; QString m_configurationPath; QString m_defaultsPath; @@ -52,6 +53,8 @@ private: QString homeLocation; QString settingsLocation; QString cacheLocation; + + QFileSystemWatcher *m_watcher; }; #endif // SETTINGS_H diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp index 3a23bfb..cb08e35 100644 --- a/src/singleapplication.cpp +++ b/src/singleapplication.cpp @@ -19,6 +19,7 @@ ******************************************************************************/ #include "singleapplication.h" +#include <QLocalServer> #include <QLocalSocket> SingleApplication::SingleApplication(int &argc, char **argv) : QApplication(argc, argv) diff --git a/src/singleapplication.h b/src/singleapplication.h index 7b1fce9..5d979cd 100644 --- a/src/singleapplication.h +++ b/src/singleapplication.h @@ -22,10 +22,8 @@ #define SINGLEAPPLICATION_H #include <QApplication> -#include <QLocalServer> -#include <QVector> -#include <QUrl> +class QLocalServer; class SingleApplication : public QApplication { Q_OBJECT diff --git a/tools/qbs/GitRepo.js b/tools/qbs/GitRepo.js index e5ecb03..04021c8 100644 --- a/tools/qbs/GitRepo.js +++ b/tools/qbs/GitRepo.js @@ -1,4 +1,4 @@ -var Process = loadExtension("qbs.Process") +var Process = require("qbs.Process") function read(workingDirectory) { var git = new Process(); |