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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
SPDX-FileCopyrightText: 2013 Marco Martin <notmart@gmail.com>
SPDX-FileCopyrightText: 2020 David Edmundson <davidedmundson@kde.org>
SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "kconfigpropertymap.h"
#include <KCoreConfigSkeleton>
#include <QJSValue>
#include <QPointer>
#include <functional>
class KConfigPropertyMapPrivate
{
public:
KConfigPropertyMapPrivate(KConfigPropertyMap *map)
: q(map)
{
}
enum LoadConfigOption {
DontEmitValueChanged,
EmitValueChanged,
};
void loadConfig(LoadConfigOption option);
void writeConfig();
void writeConfigValue(const QString &key, const QVariant &value);
KConfigPropertyMap *q;
QPointer<KCoreConfigSkeleton> config;
bool updatingConfigValue = false;
bool autosave = true;
bool notify = false;
};
KConfigPropertyMap::KConfigPropertyMap(KCoreConfigSkeleton *config, QObject *parent)
: QQmlPropertyMap(this, parent)
, d(new KConfigPropertyMapPrivate(this))
{
Q_ASSERT(config);
d->config = config;
// Reload the config only if the change signal has *not* been emitted by ourselves updating the config
connect(config, &KCoreConfigSkeleton::configChanged, this, [this]() {
if (!d->updatingConfigValue) {
d->loadConfig(KConfigPropertyMapPrivate::EmitValueChanged);
}
});
connect(this, &KConfigPropertyMap::valueChanged, this, [this](const QString &key, const QVariant &value) {
d->writeConfigValue(key, value);
});
d->loadConfig(KConfigPropertyMapPrivate::DontEmitValueChanged);
}
KConfigPropertyMap::~KConfigPropertyMap() = default;
bool KConfigPropertyMap::isNotify() const
{
return d->notify;
}
void KConfigPropertyMap::setNotify(bool notify)
{
d->notify = notify;
}
void KConfigPropertyMap::writeConfig()
{
d->writeConfig();
}
QVariant KConfigPropertyMap::updateValue(const QString &key, const QVariant &input)
{
Q_UNUSED(key);
if (input.userType() == qMetaTypeId<QJSValue>()) {
return input.value<QJSValue>().toVariant();
}
return input;
}
bool KConfigPropertyMap::isImmutable(const QString &key) const
{
KConfigSkeletonItem *item = d->config.data()->findItem(key);
if (item) {
return item->isImmutable();
}
return false;
}
void KConfigPropertyMapPrivate::loadConfig(KConfigPropertyMapPrivate::LoadConfigOption option)
{
if (!config) {
return;
}
const auto &items = config.data()->items();
for (KConfigSkeletonItem *item : items) {
q->insert(item->key() + QStringLiteral("Default"), item->getDefault());
q->insert(item->key(), item->property());
if (option == EmitValueChanged) {
Q_EMIT q->valueChanged(item->key(), item->property());
}
}
}
void KConfigPropertyMapPrivate::writeConfig()
{
if (!config) {
return;
}
const auto lstItems = config.data()->items();
for (KConfigSkeletonItem *item : lstItems) {
item->setWriteFlags(notify ? KConfigBase::Notify : KConfigBase::Normal);
item->setProperty(q->value(item->key()));
}
if (autosave) {
updatingConfigValue = true;
config.data()->save();
updatingConfigValue = false;
}
}
void KConfigPropertyMapPrivate::writeConfigValue(const QString &key, const QVariant &value)
{
KConfigSkeletonItem *item = config.data()->findItem(key);
if (item) {
updatingConfigValue = true;
item->setWriteFlags(notify ? KConfigBase::Notify : KConfigBase::Normal);
item->setProperty(value);
if (autosave) {
config.data()->save();
// why read? read will update KConfigSkeletonItem::mLoadedValue,
// allowing a write operation to be performed next time
config.data()->read();
}
updatingConfigValue = false;
}
}
|