QuasarAppLib/isettings.cpp

129 lines
2.9 KiB
C++
Raw Permalink Normal View History

/*
2024-12-30 22:39:49 +01:00
* Copyright (C) 2018-2025 QuasarApp.
* 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>
#include "qaglobalutils.h"
2021-11-06 18:07:15 +03:00
namespace QuasarAppUtils {
ISettings::ISettings(SettingsSaveMode mode) {
_mode = mode;
}
ISettings::~ISettings() {
if (_defaultConfig)
delete _defaultConfig;
}
void ISettings::clearCache() {
_cache.clear();
}
QHash<QString, QVariant> &ISettings::settingsMap() {
if (!_defaultConfig)
_defaultConfig = new QHash<QString, QVariant>(defaultSettings());
return *_defaultConfig;
}
SettingsSaveMode ISettings::getMode() const {
return _mode;
}
void ISettings::setMode(const SettingsSaveMode &mode) {
_mode = mode;
}
2023-04-07 15:01:10 +02:00
ISettings *ISettings::instance() {
return Service<ISettings>::instance();
}
bool ISettings::initService(std::unique_ptr<ISettings> obj) {
return Service<ISettings>::initService(std::move(obj));
}
QVariant ISettings::getValue(const QString &key, const QVariant &def) {
debug_assert(key.size(), "You can't use the empty key value!");
2021-11-18 15:01:53 +03:00
if (!_cache.contains(key)) {
QVariant defVal = def;
if (defVal.isNull()) {
defVal = settingsMap().value(key);
}
_cache[key] = getValueImplementation(key, def);
}
return _cache[key];
}
QString ISettings::getStrValue(const QString &key, const QString &def) {
if (def.isEmpty()) {
return getValue(key).toString();
}
return getValue(key, QVariant(def)).toString();
}
void ISettings::resetToDefault() {
2022-06-24 00:29:23 +03:00
auto &defaultConfig = settingsMap();
2022-06-24 00:29:23 +03:00
for (auto it = defaultConfig.begin(); it != defaultConfig.end(); ++it) {
if (!ignoreToRest(it.key()))
setValue(it.key(), defaultConfig.value(it.key()));
}
}
bool ISettings::ignoreToRest(const QString &) const {
return false;
}
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();
}
void ISettings::forceReloadCache() {
2022-06-24 00:29:23 +03:00
auto &defaultConfig = settingsMap();
for (auto it = defaultConfig.begin(); it != defaultConfig.end(); ++it) {
setValue(it.key(), getValueImplementation(it.key(), it.value()));
}
}
void ISettings::setValue(const QString &key, const QVariant &value) {
debug_assert(key.size(), "You can't use the empty key 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;
emit valueChanged(key, value);
emit valueStrChanged(key, value.toString());
if (_mode == SettingsSaveMode::Auto) {
setValueImplementation(key, value);
}
}
void ISettings::setStrValue(const QString &key, const QString &value) {
setValue(key, value);
}
2021-11-06 18:07:15 +03:00
}