aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/resources.qrc1
-rw-r--r--smolbote.qbs4
-rw-r--r--src/browser.cpp41
-rw-r--r--src/forms/aboutdialog.cpp5
-rw-r--r--src/mainwindow.cpp10
-rw-r--r--src/settings.cpp152
-rw-r--r--src/settings.h19
7 files changed, 141 insertions, 91 deletions
diff --git a/data/resources.qrc b/data/resources.qrc
index 9387efe..b2b6be5 100644
--- a/data/resources.qrc
+++ b/data/resources.qrc
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file alias="icon.svg">poi.svg</file>
+ <file>poi.toml</file>
</qresource>
</RCC>
diff --git a/smolbote.qbs b/smolbote.qbs
index 074b44b..3ffd488 100644
--- a/smolbote.qbs
+++ b/smolbote.qbs
@@ -60,6 +60,8 @@ Project {
"src/browser.h",
"src/interfaces.h",
"src/main.cpp",
+ "src/settings.cpp",
+ "src/settings.h",
"src/singleapplication.cpp",
"src/singleapplication.h",
]
@@ -162,8 +164,6 @@ Project {
files: [
"data/resources.qrc",
- "src/settings.cpp",
- "src/settings.h",
"src/webengine/webview.cpp",
"src/webengine/webview.h",
]
diff --git a/src/browser.cpp b/src/browser.cpp
index 9ee7315..355c24b 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -162,36 +162,43 @@ BlockerManager *Browser::blocklists()
void Browser::setConfigPath(const QString &path)
{
- // set custom config path if any
- if(!path.isEmpty()) {
- m_settings = new Settings(path);
- return;
- }
-
- // check if config file exists for this user
- QString cpath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/poi.conf";
- if(QFile::exists(cpath)) {
- m_settings = new Settings(cpath);
- }
+ QString configLocation, defaultsLocation;
+ // set defaults location
// check system-specific locations
#ifdef Q_OS_LINUX
- else if(QFile::exists("/usr/share/smolbote/poi.conf")) {
- m_settings = new Settings("/usr/share/smolbote/poi.conf");
+ if(QFile::exists("/usr/share/smolbote/poi.conf")) {
+ defaultsLocation = "/usr/share/smolbote/poi.conf";
} else if(QFile::exists("/usr/local/share/smolbote/poi.conf")) {
- m_settings = new Settings("/usr/local/share/smolbote/poi.conf");
+ defaultsLocation = "/usr/local/share/smolbote/poi.conf";
}
#endif
- else {
- m_settings = new Settings();
+ // if no default config is found, then use the built-in one
+ if(defaultsLocation.isEmpty()) {
+ 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);
+
if(m_settings->isEmpty()) {
// There are no keys in the settings
QMessageBox::information(0,
tr("Configuration is empty"),
- tr("The configuration file <i>%1</i> is empty. Using default values").arg(m_settings->filePath()));
+ tr("The configuration file <i>%1</i> is empty.<br>"
+ "Using default values from <i>%2</i>.").arg(configLocation, defaultsLocation));
}
}
diff --git a/src/forms/aboutdialog.cpp b/src/forms/aboutdialog.cpp
index 1b8c915..2fe312b 100644
--- a/src/forms/aboutdialog.cpp
+++ b/src/forms/aboutdialog.cpp
@@ -51,9 +51,10 @@ AboutDialog::AboutDialog(QWidget *parent) :
#elif defined __GNUC__
"Compiled with GCC " __VERSION__ "<br>"
#endif
- "Configuration lives in %2"
+ "Configuration lives in %2<br>"
+ "Default configuration lives in %3"
"</p>")
- .arg(qApp->applicationLongVersion(), sSettings->filePath()));
+ .arg(qApp->applicationLongVersion(), sSettings->configurationPath(), sSettings->defaultsPath()));
ui->toolBox->addItem(detailsLabel, tr("Details"));
QLabel *libsLabel = new QLabel(this);
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index eae09e6..a72a784 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -57,14 +57,14 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) :
// Add the toolbars
// tabToolBar: main menu and tab list
- tabToolBar->setMovable(sSettings->value("window.ui.tabtoolbarMovable", true).toBool());
+ tabToolBar->setMovable(sSettings->value("window.ui.tabtoolbarMovable").toBool());
tabToolBar->addWidget(menuBar);
tabToolBar->addWidget(tabBar);
this->addToolBar(Qt::TopToolBarArea, tabToolBar);
this->addToolBarBreak(Qt::TopToolBarArea);
// navigationToolBar: address bar
- navigationToolBar->setMovable(sSettings->value("window.ui.navtoolbarMovable", true).toBool());
+ navigationToolBar->setMovable(sSettings->value("window.ui.navtoolbarMovable").toBool());
// page actions
QToolButton *backButton = new QToolButton(this);
@@ -104,11 +104,11 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) :
if(!defaultUrl.isEmpty()) {
newTab(defaultUrl);
} else {
- newTab(sSettings->value("general.homepage", QUrl("about:blank")).toUrl());
+ newTab(sSettings->value("general.homepage").toUrl());
}
- resize(sSettings->value("window.width", 800).toInt(), sSettings->value("window.height", 600).toInt());
- if(sSettings->value("window.maximized", false).toBool()) {
+ resize(sSettings->value("window.width").toInt(), sSettings->value("window.height").toInt());
+ if(sSettings->value("window.maximized").toBool()) {
showMaximized();
}
}
diff --git a/src/settings.cpp b/src/settings.cpp
index c1485b1..a0fad7f 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -21,56 +21,55 @@
#include "settings.h"
#include <QStandardPaths>
#include <fstream>
+#include <cstdio>
#include <QDir>
+#include <QFile>
#include <QFileInfo>
-Settings::Settings(const QString &configFile)
+Settings::Settings(const QString &configFile, const QString &defaultsFile)
{
- std::ifstream ifs(configFile.toStdString().c_str());
- if(ifs.is_open()) {
- toml::ParseResult pr = toml::parse(ifs);
+ values = parse(configFile);
+ defaults = parse(defaultsFile);
- if(!pr.valid()) {
- qWarning("Invalid configuration: %s", pr.errorReason.c_str());
- return;
- }
-
- v = pr.value;
- ifs.close();
-
- } else {
- qWarning("Cannot open configuration: %s", qUtf8Printable(configFile));
- return;
- }
+ m_configurationPath = configFile;
+ m_defaultsPath = defaultsFile;
- path = configFile;
homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
settingsLocation = QFileInfo(configFile).dir().absolutePath();
cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
- qDebug("Read configuration: [%s]", qUtf8Printable(configFile));
- qDebug("$home | '%s'", qUtf8Printable(homeLocation));
- qDebug("$settings | '%s'", qUtf8Printable(settingsLocation));
- qDebug("$cache | '%s'", qUtf8Printable(cacheLocation));
+#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));
+#endif
}
Settings::~Settings()
{
}
-QString Settings::filePath() const
+QString Settings::configurationPath() const
{
- return path;
+ return m_configurationPath;
+}
+
+QString Settings::defaultsPath() const
+{
+ return m_defaultsPath;
}
bool Settings::isEmpty() const
{
- return v.empty();
+ return values.empty();
}
bool Settings::contains(const QString &key)
{
- const toml::Value *x = v.find(key.toStdString());
+ const toml::Value *x = values.find(key.toStdString());
if(x) {
return true;
} else {
@@ -78,45 +77,19 @@ bool Settings::contains(const QString &key)
}
}
-QVariant Settings::value(const QString &key, const QVariant &defaultValue) const
+QVariant Settings::value(const QString &key) const
{
- const toml::Value *x = v.find(key.toStdString());
-
- // check if value exists
- if(!x) {
- return defaultValue;
- }
+ const toml::Value *cValue = values.find(key.toStdString());
+ const toml::Value *dValue = defaults.find(key.toStdString());
QVariant r;
- switch (x->type()) {
- case toml::Value::NULL_TYPE:
- r = defaultValue;
- break;
-
- case toml::Value::BOOL_TYPE:
- r = QVariant(x->as<bool>());
- break;
-
- case toml::Value::INT_TYPE:
- r = QVariant(x->as<int>());
- break;
- case toml::Value::DOUBLE_TYPE:
- r = QVariant(x->as<double>());
- break;
-
- case toml::Value::STRING_TYPE:
- r = QVariant(QString::fromStdString(x->as<std::string>()));
- break;
-
- case toml::Value::ARRAY_TYPE:
- r = fromList(x);
- break;
-
- default:
- qWarning("Unhandled type in configuration");
- r = defaultValue;
- break;
+ if(values.has(key.toStdString())) {
+ r = valueToVariant(cValue);
+ } else {
+ if(defaults.has(key.toStdString())) {
+ r = valueToVariant(dValue);
+ }
}
// check if key is a path, in which case replace '~' with the home location
@@ -133,6 +106,30 @@ QVariant Settings::value(const QString &key, const QVariant &defaultValue) const
return r;
}
+toml::Value Settings::parse(const QString &filename)
+{
+ toml::Value r;
+
+ if(!filename.isEmpty()) {
+ QFile file(filename);
+ if(file.open(QIODevice::ReadOnly)) {
+ std::stringstream d(file.readAll().toStdString());
+ file.close();
+
+ toml::ParseResult result = toml::parse(d);
+ if(!result.valid()) {
+ qWarning("Invalid configuration: %s", result.errorReason.c_str());
+ }
+ r = result.value;
+
+ } else {
+ qWarning("Cannot open configuration: %s", qUtf8Printable(filename));
+ }
+ }
+
+ return r;
+}
+
QVariant Settings::fromList(const toml::Value *list) const
{
QStringList l;
@@ -144,3 +141,38 @@ QVariant Settings::fromList(const toml::Value *list) const
return QVariant(l);
}
+
+QVariant Settings::valueToVariant(const toml::Value *value) const
+{
+ QVariant r;
+ switch (value->type()) {
+ case toml::Value::NULL_TYPE:
+ break;
+
+ case toml::Value::BOOL_TYPE:
+ r = QVariant(value->as<bool>());
+ break;
+
+ case toml::Value::INT_TYPE:
+ r = QVariant(value->as<int>());
+ break;
+
+ case toml::Value::DOUBLE_TYPE:
+ r = QVariant(value->as<double>());
+ break;
+
+ case toml::Value::STRING_TYPE:
+ r = QVariant(QString::fromStdString(value->as<std::string>()));
+ break;
+
+ case toml::Value::ARRAY_TYPE:
+ r = fromList(value);
+ break;
+
+ default:
+ qWarning("Unhandled type in configuration");
+ break;
+ }
+
+ return r;
+}
diff --git a/src/settings.h b/src/settings.h
index 76cf949..673158e 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -27,19 +27,28 @@
class Settings
{
public:
- explicit Settings(const QString &configFile = QString());
+ explicit Settings(const QString &configFile, const QString &defaultsFile = QString());
~Settings();
- QString filePath() const;
+ QString configurationPath() const;
+ QString defaultsPath() const;
+
bool isEmpty() const;
bool contains(const QString &key);
- QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
+ QVariant value(const QString &key) const;
private:
+ toml::Value parse(const QString &filename);
+
QVariant fromList(const toml::Value *list) const;
+ QVariant valueToVariant(const toml::Value *value) const;
+
+ toml::Value values;
+ toml::Value defaults;
+
+ QString m_configurationPath;
+ QString m_defaultsPath;
- toml::Value v;
- QString path;
QString homeLocation;
QString settingsLocation;
QString cacheLocation;