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(), item->property());
115  if (option == EmitValueChanged) {
116  emit q->valueChanged(item->key(), item->property());
117  }
118  }
119 }
120 
121 void ConfigPropertyMapPrivate::writeConfig()
122 {
123  if (!config) {
124  return;
125  }
126 
127  const auto lstItems = config.data()->items();
128  for (KConfigSkeletonItem *item : lstItems) {
129  item->setWriteFlags(notify ? KConfigBase::Notify : KConfigBase::Normal);
130  item->setProperty(q->value(item->key()));
131  }
132 
133  if (autosave) {
134  updatingConfigValue = true;
135  config.data()->save();
136  updatingConfigValue = false;
137  }
138 }
139 
140 void ConfigPropertyMapPrivate::writeConfigValue(const QString &key, const QVariant &value)
141 {
142  KConfigSkeletonItem *item = config.data()->findItem(key);
143  if (item) {
144  updatingConfigValue = true;
146  item->setProperty(value);
147  if (autosave) {
148  config.data()->save();
149  //why read? read will update KConfigSkeletonItem::mLoadedValue,
150  //allowing a write operation to be performed next time
151  config.data()->read();
152  }
153  updatingConfigValue = false;
154  }
155 }
156 
157 }
158 
159 #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
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 14 2020 22:38:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.