-
Notifications
You must be signed in to change notification settings - Fork 2
/
isettings.cpp
128 lines (93 loc) · 2.92 KB
/
isettings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (C) 2018-2024 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"
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;
}
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!");
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() {
auto &defaultConfig = settingsMap();
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() {
for (auto it = _cache.begin(); it != _cache.end(); ++it) {
setValueImplementation(it.key(), it.value());
}
return syncImplementation();
}
void ISettings::forceReloadCache() {
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!");
if (_cache.contains(key) && _cache.value(key) == value) {
return;
}
_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);
}
}