KDeclarative

configpropertymap.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2020 David Edmundson <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "configpropertymap.h"
9 
10 #include <QJSValue>
11 #include <QPointer>
12 #include <KCoreConfigSkeleton>
13 
14 #include <functional>
15 
16 namespace KDeclarative {
17 
18 class ConfigPropertyMapPrivate {
19 public:
20  ConfigPropertyMapPrivate(ConfigPropertyMap *map)
21  : q(map)
22  {
23  }
24 
25  enum LoadConfigOption {
26  DontEmitValueChanged,
27  EmitValueChanged
28  };
29 
30  void loadConfig(LoadConfigOption option);
31  void writeConfig();
32  void writeConfigValue(const QString &key, const QVariant &value);
33 
34  ConfigPropertyMap *q;
36  bool updatingConfigValue = false;
37  bool autosave = true;
38  bool notify = false;
39 };
40 
41 ConfigPropertyMap::ConfigPropertyMap(KCoreConfigSkeleton *config, QObject *parent)
42  : QQmlPropertyMap(this, parent),
43  d(new ConfigPropertyMapPrivate(this))
44 {
45  d->config = config;
46 
47  // Reload the config only if the change signal has *not* been emitted by ourselves updating the config
48  connect(config, &KCoreConfigSkeleton::configChanged, this, [this] () {
49  if (!d->updatingConfigValue) {
50  d->loadConfig(ConfigPropertyMapPrivate::EmitValueChanged);
51  }
52  });
53  connect(this, &ConfigPropertyMap::valueChanged, this,
54  [this](const QString &key, const QVariant &value){d->writeConfigValue(key, value);});
55 
56  d->loadConfig(ConfigPropertyMapPrivate::DontEmitValueChanged);
57 }
58 
59 ConfigPropertyMap::~ConfigPropertyMap()
60 {
61  if (d->autosave) {
62  d->writeConfig();
63  }
64  delete d;
65 }
66 
68 {
69  return d->autosave;
70 }
71 
72 void ConfigPropertyMap::setAutosave(bool autosave)
73 {
74  d->autosave = autosave;
75 }
76 
77 bool ConfigPropertyMap::isNotify() const
78 {
79  return d->notify;
80 }
81 
82 void ConfigPropertyMap::setNotify(bool notify)
83 {
84  d->notify = notify;
85 }
86 
87 QVariant ConfigPropertyMap::updateValue(const QString &key, const QVariant &input)
88 {
89  Q_UNUSED(key);
90  if (input.userType() == qMetaTypeId<QJSValue>()) {
91  return input.value<QJSValue>().toVariant();
92  }
93  return input;
94 }
95 
96 bool ConfigPropertyMap::isImmutable(const QString &key) const
97 {
98  KConfigSkeletonItem *item = d->config.data()->findItem(key);
99  if (item) {
100  return item->isImmutable();
101  }
102 
103  return false;
104 }
105 
106 void ConfigPropertyMapPrivate::loadConfig(ConfigPropertyMapPrivate::LoadConfigOption option)
107 {
108  if (!config) {
109  return;
110  }
111 
112  const auto &items = config.data()->items();
113  for (KConfigSkeletonItem *item : items) {
114  q->insert(item->key() + QStringLiteral("Default"), item->getDefault());
115  q->insert(item->key(), item->property());
116  if (option == EmitValueChanged) {
117  Q_EMIT q->valueChanged(item->key(), item->property());
118  }
119  }
120 }
121 
122 void ConfigPropertyMapPrivate::writeConfig()
123 {
124  if (!config) {
125  return;
126  }
127 
128  const auto lstItems = config.data()->items();
129  for (KConfigSkeletonItem *item : lstItems) {
130  item->setWriteFlags(notify ? KConfigBase::Notify : KConfigBase::Normal);
131  item->setProperty(q->value(item->key()));
132  }
133 
134  if (autosave) {
135  updatingConfigValue = true;
136  config.data()->save();
137  updatingConfigValue = false;
138  }
139 }
140 
141 void ConfigPropertyMapPrivate::writeConfigValue(const QString &key, const QVariant &value)
142 {
143  KConfigSkeletonItem *item = config.data()->findItem(key);
144  if (item) {
145  updatingConfigValue = true;
147  item->setProperty(value);
148  if (autosave) {
149  config.data()->save();
150  //why read? read will update KConfigSkeletonItem::mLoadedValue,
151  //allowing a write operation to be performed next time
152  config.data()->read();
153  }
154  updatingConfigValue = false;
155  }
156 }
157 
158 }
159 
160 #include "moc_configpropertymap.cpp"
virtual void setProperty(const QVariant &p)=0
bool isImmutable() const
T value() const const
KSharedConfigPtr config()
KConfigSkeletonItem * findItem(const QString &name) const
void setWriteFlags(KConfigBase::WriteConfigFlags flags)
int userType() const const
bool isAutosave() const
Indicates if the map calls save() on the config object on each write or not.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KConfigSkeletonItem::List items() const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 22:41:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.