KConfig

kconfigpropertymap.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2020 David Edmundson <[email protected]>
4  SPDX-FileCopyrightText: 2021 Alexander Lohnau <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "kconfigpropertymap.h"
10 
11 #include <KCoreConfigSkeleton>
12 #include <QJSValue>
13 #include <QPointer>
14 
15 #include <functional>
16 
17 class KConfigPropertyMapPrivate
18 {
19 public:
20  KConfigPropertyMapPrivate(KConfigPropertyMap *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 
36  bool updatingConfigValue = false;
37  bool notify = false;
38 };
39 
40 KConfigPropertyMap::KConfigPropertyMap(KCoreConfigSkeleton *config, QObject *parent)
41  : QQmlPropertyMap(this, parent)
42  , d(new KConfigPropertyMapPrivate(this))
43 {
44  Q_ASSERT(config);
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(KConfigPropertyMapPrivate::EmitValueChanged);
51  }
52  });
53  connect(this, &KConfigPropertyMap::valueChanged, this, [this](const QString &key, const QVariant &value) {
54  d->writeConfigValue(key, value);
55  });
56 
57  d->loadConfig(KConfigPropertyMapPrivate::DontEmitValueChanged);
58 }
59 
60 KConfigPropertyMap::~KConfigPropertyMap() = default;
61 
63 {
64  return d->notify;
65 }
66 
68 {
69  d->notify = notify;
70 }
71 
73 {
74  d->writeConfig();
75 }
76 
77 QVariant KConfigPropertyMap::updateValue(const QString &key, const QVariant &input)
78 {
79  Q_UNUSED(key);
80  if (input.userType() == qMetaTypeId<QJSValue>()) {
81  return input.value<QJSValue>().toVariant();
82  }
83  return input;
84 }
85 
87 {
88  KConfigSkeletonItem *item = d->config.data()->findItem(key);
89  if (item) {
90  return item->isImmutable();
91  }
92 
93  return false;
94 }
95 
96 void KConfigPropertyMapPrivate::loadConfig(KConfigPropertyMapPrivate::LoadConfigOption option)
97 {
98  if (!config) {
99  return;
100  }
101 
102  const auto &items = config.data()->items();
103  for (KConfigSkeletonItem *item : items) {
104  q->insert(item->key() + QStringLiteral("Default"), item->getDefault());
105  q->insert(item->key(), item->property());
106  if (option == EmitValueChanged) {
107  Q_EMIT q->valueChanged(item->key(), item->property());
108  }
109  }
110 }
111 
112 void KConfigPropertyMapPrivate::writeConfig()
113 {
114  if (!config) {
115  return;
116  }
117 
118  const auto lstItems = config.data()->items();
119  for (KConfigSkeletonItem *item : lstItems) {
120  item->setWriteFlags(notify ? KConfigBase::Notify : KConfigBase::Normal);
121  item->setProperty(q->value(item->key()));
122  }
123 
124 }
125 
126 void KConfigPropertyMapPrivate::writeConfigValue(const QString &key, const QVariant &value)
127 {
128  KConfigSkeletonItem *item = config.data()->findItem(key);
129  if (item) {
130  updatingConfigValue = true;
132  item->setProperty(value);
133  updatingConfigValue = false;
134  }
135 }
136 
void valueChanged(const QString &key, const QVariant &value)
virtual void setProperty(const QVariant &p)=0
Set item to p.
T value() const const
@ Normal
Save the entry to the application specific config file without a locale tag.
Definition: kconfigbase.h:57
void configChanged()
This signal is emitted when the configuration change.
Class for handling preferences settings for an application.
Q_INVOKABLE bool isImmutable(const QString &key) const
Whether the value at the given key is immutable.
@ Notify
Notify remote KConfigWatchers of changes (requires DBus support) Implied persistent.
Definition: kconfigbase.h:51
bool isNotify() const
Whether notifications on config changes are enabled.
void setWriteFlags(KConfigBase::WriteConfigFlags flags)
The write flags to be used when writing configuration.
int userType() const const
void writeConfig()
Saves the state of the property map on disk.
KSharedConfigPtr config()
Class for storing a preferences setting.
bool isImmutable() const
Return if the entry can be modified.
void setNotify(bool notify)
Enable or disable notifications on config changes.
QFuture< void > map(Sequence &sequence, MapFunctor function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Jun 28 2022 04:08:29 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.