/*******************************************************************************
**
** smolbote: yet another qute browser
** Copyright (C) 2017 Xian Nox
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see .
**
******************************************************************************/
#include "settings.h"
#include
#include
#include
#include
Settings::Settings(const QString &configFile)
{
std::ifstream ifs(configFile.toStdString().c_str());
if(ifs.is_open()) {
toml::ParseResult pr = toml::parse(ifs);
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;
}
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));
}
Settings::~Settings()
{
}
QString Settings::filePath() const
{
return path;
}
bool Settings::isEmpty() const
{
return v.empty();
}
bool Settings::contains(const QString &key)
{
const toml::Value *x = v.find(key.toStdString());
if(x) {
return true;
} else {
return false;
}
}
QVariant Settings::value(const QString &key, const QVariant &defaultValue) const
{
const toml::Value *x = v.find(key.toStdString());
// check if value exists
if(!x) {
return defaultValue;
}
QVariant r;
switch (x->type()) {
case toml::Value::NULL_TYPE:
r = defaultValue;
break;
case toml::Value::BOOL_TYPE:
r = QVariant(x->as());
break;
case toml::Value::INT_TYPE:
r = QVariant(x->as());
break;
case toml::Value::DOUBLE_TYPE:
r = QVariant(x->as());
break;
case toml::Value::STRING_TYPE:
r = QVariant(QString::fromStdString(x->as()));
break;
case toml::Value::ARRAY_TYPE:
r = fromList(x);
break;
default:
qWarning("Unhandled type in configuration");
r = defaultValue;
break;
}
// check if key is a path, in which case replace '~' with the home location
if(key.endsWith(QLatin1String("path"), Qt::CaseInsensitive)) {
QString value = r.toString();
while(value.contains('$')) {
value.replace("$settings", settingsLocation);
value.replace("$cache", cacheLocation);
value.replace("$home", homeLocation);
}
r = QVariant(value);
}
return r;
}
QVariant Settings::fromList(const toml::Value *list) const
{
QStringList l;
for(const toml::Value &v : list->as()) {
// TODO check value type
l.append(QString::fromStdString(v.as()));
}
return QVariant(l);
}