2020-07-06 14:21:39 +02:00
|
|
|
#include "../headers/scstwsettings.h"
|
|
|
|
|
|
|
|
ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2020-07-10 15:20:13 +02:00
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
QDir dir(path);
|
2020-07-11 11:15:06 +02:00
|
|
|
this->settingsFile = new QFile(path + "/settings.json");
|
2020-07-10 15:20:13 +02:00
|
|
|
if(!this->settingsFile->exists())
|
|
|
|
if(!dir.mkpath(path))
|
|
|
|
qFatal("[FATAL] Couldn't create settings dir %s", qPrintable(path));
|
|
|
|
|
2020-07-06 14:21:39 +02:00
|
|
|
if(!this->settingsFile->open(QFile::ReadWrite))
|
2020-07-10 15:20:13 +02:00
|
|
|
qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path + "/settings.json"));
|
2020-07-06 14:21:39 +02:00
|
|
|
|
|
|
|
if(!this->loadSettingsFromFile() && this->settingsFile->size() != 0)
|
2020-07-11 11:15:06 +02:00
|
|
|
qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path + "/settings.json"));
|
2020-07-06 14:21:39 +02:00
|
|
|
|
|
|
|
connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ScStwSettings::readSetting(BaseStationSetting key) {
|
2020-07-10 15:20:13 +02:00
|
|
|
return this->readSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), int(key), 0);
|
2020-07-06 14:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) {
|
|
|
|
return this->writeSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), value, int(key), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVariant) {
|
|
|
|
this->setDefaultSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), defaultVariant, int(key), 0);
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:20:13 +02:00
|
|
|
QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) {
|
|
|
|
Q_UNUSED(keyInt)
|
|
|
|
Q_UNUSED(keyLevel)
|
2020-07-06 14:21:39 +02:00
|
|
|
return this->settingsCache[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) {
|
|
|
|
if(!this->settingsCache.contains(key))
|
|
|
|
this->settingsCache.insert(key, value);
|
|
|
|
else if (this->settingsCache[key] == value)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
this->settingsCache[key] = value;
|
|
|
|
|
|
|
|
emit this->settingChanged(keyInt, keyLevel);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScStwSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) {
|
|
|
|
if(!this->settingsCache.contains(key))
|
|
|
|
this->writeSetting(key, defaultVariant, keyInt, keyLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool ScStwSettings::writeSettingsToFile() {
|
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(this->settingsCache);
|
|
|
|
|
|
|
|
// overwrite file
|
|
|
|
this->settingsFile->reset();
|
|
|
|
this->settingsFile->write(doc.toJson(QJsonDocument::Indented));
|
|
|
|
this->settingsFile->flush();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwSettings::loadSettingsFromFile() {
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(this->settingsFile->readAll());
|
|
|
|
|
|
|
|
if(doc.toVariant().type() != QVariant::Map)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this->settingsCache = doc.toVariant().toMap();
|
|
|
|
return true;
|
|
|
|
}
|