2021-11-05 23:58:23 +03:00
|
|
|
/*
|
2022-03-03 19:00:49 +03:00
|
|
|
* Copyright (C) 2018-2022 QuasarApp.
|
2021-11-05 23:58:23 +03:00
|
|
|
* Distributed under the lgplv3 software license, see the accompanying
|
|
|
|
* Everyone is permitted to copy and distribute verbatim copies
|
|
|
|
* of this license document, but changing it is not allowed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "isettings.h"
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2021-11-06 18:07:15 +03:00
|
|
|
namespace QuasarAppUtils {
|
|
|
|
|
|
|
|
ISettings* ISettings::_settings = nullptr;
|
2021-11-05 23:58:23 +03:00
|
|
|
|
|
|
|
ISettings::ISettings(SettingsSaveMode mode) {
|
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
|
2021-12-24 15:21:23 +03:00
|
|
|
void ISettings::clearCache() {
|
|
|
|
_cache.clear();
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:58:23 +03:00
|
|
|
SettingsSaveMode ISettings::getMode() const {
|
|
|
|
return _mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ISettings::setMode(const SettingsSaveMode &mode) {
|
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:55:05 +03:00
|
|
|
ISettings *ISettings::instance(){
|
2021-11-18 00:26:44 +03:00
|
|
|
return _settings;
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:58:23 +03:00
|
|
|
QVariant ISettings::getValue(const QString &key, const QVariant &def) {
|
2021-11-18 15:01:53 +03:00
|
|
|
|
|
|
|
if (!_cache.contains(key)) {
|
|
|
|
_cache[key] = getValueImplementation(key, def);
|
|
|
|
}
|
|
|
|
|
2021-11-19 00:34:50 +03:00
|
|
|
auto result = _cache.value(key, def);
|
|
|
|
|
|
|
|
if (result.isNull()) {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-11-05 23:58:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ISettings::getStrValue(const QString &key, const QString &def) {
|
|
|
|
return getValue(key, QVariant(def)).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ISettings::sync() {
|
2021-11-18 15:01:53 +03:00
|
|
|
for (auto it = _cache.begin(); it != _cache.end(); ++it) {
|
|
|
|
setValueImplementation(it.key(), it.value());
|
|
|
|
}
|
|
|
|
|
2021-11-06 10:20:20 +03:00
|
|
|
return syncImplementation();
|
2021-11-05 23:58:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ISettings::setValue(const QString key, const QVariant &value) {
|
2021-11-18 15:01:53 +03:00
|
|
|
|
2021-11-19 21:54:45 +03:00
|
|
|
if (_cache.contains(key) && _cache.value(key) == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-18 15:01:53 +03:00
|
|
|
_cache[key] = value;
|
2021-11-05 23:58:23 +03:00
|
|
|
|
|
|
|
emit valueChanged(key, value);
|
|
|
|
emit valueStrChanged(key, value.toString());
|
|
|
|
|
|
|
|
if (_mode == SettingsSaveMode::Auto) {
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ISettings::setStrValue(const QString &key, const QString &value) {
|
|
|
|
setValue(key, value);
|
|
|
|
}
|
2021-11-06 18:07:15 +03:00
|
|
|
|
|
|
|
}
|