Changeset View
Changeset View
Standalone View
Standalone View
src/kdeclarative/configpropertymap.cpp
Show All 40 Lines | 31 | public: | |||
---|---|---|---|---|---|
41 | 41 | | |||
42 | void loadConfig(LoadConfigOption option); | 42 | void loadConfig(LoadConfigOption option); | ||
43 | void writeConfig(); | 43 | void writeConfig(); | ||
44 | void writeConfigValue(const QString &key, const QVariant &value); | 44 | void writeConfigValue(const QString &key, const QVariant &value); | ||
45 | 45 | | |||
46 | ConfigPropertyMap *q; | 46 | ConfigPropertyMap *q; | ||
47 | QPointer<KCoreConfigSkeleton> config; | 47 | QPointer<KCoreConfigSkeleton> config; | ||
48 | bool updatingConfigValue = false; | 48 | bool updatingConfigValue = false; | ||
49 | bool autosave = true; | ||||
49 | }; | 50 | }; | ||
50 | 51 | | |||
51 | ConfigPropertyMap::ConfigPropertyMap(KCoreConfigSkeleton *config, QObject *parent) | 52 | ConfigPropertyMap::ConfigPropertyMap(KCoreConfigSkeleton *config, QObject *parent) | ||
52 | : QQmlPropertyMap(this, parent), | 53 | : QQmlPropertyMap(this, parent), | ||
53 | d(new ConfigPropertyMapPrivate(this)) | 54 | d(new ConfigPropertyMapPrivate(this)) | ||
54 | { | 55 | { | ||
55 | d->config = config; | 56 | d->config = config; | ||
56 | 57 | | |||
57 | // Reload the config only if the change signal has *not* been emitted by ourselves updating the config | 58 | // Reload the config only if the change signal has *not* been emitted by ourselves updating the config | ||
58 | connect(config, &KCoreConfigSkeleton::configChanged, this, [this] () { | 59 | connect(config, &KCoreConfigSkeleton::configChanged, this, [this] () { | ||
59 | if (!d->updatingConfigValue) { | 60 | if (!d->updatingConfigValue) { | ||
60 | d->loadConfig(ConfigPropertyMapPrivate::EmitValueChanged); | 61 | d->loadConfig(ConfigPropertyMapPrivate::EmitValueChanged); | ||
61 | } | 62 | } | ||
62 | }); | 63 | }); | ||
63 | connect(this, &ConfigPropertyMap::valueChanged, this, | 64 | connect(this, &ConfigPropertyMap::valueChanged, this, | ||
64 | [this](const QString &key, const QVariant &value){d->writeConfigValue(key, value);}); | 65 | [this](const QString &key, const QVariant &value){d->writeConfigValue(key, value);}); | ||
65 | 66 | | |||
66 | d->loadConfig(ConfigPropertyMapPrivate::DontEmitValueChanged); | 67 | d->loadConfig(ConfigPropertyMapPrivate::DontEmitValueChanged); | ||
67 | } | 68 | } | ||
68 | 69 | | |||
69 | ConfigPropertyMap::~ConfigPropertyMap() | 70 | ConfigPropertyMap::~ConfigPropertyMap() | ||
70 | { | 71 | { | ||
72 | if (d->autosave) { | ||||
71 | d->writeConfig(); | 73 | d->writeConfig(); | ||
74 | } | ||||
72 | delete d; | 75 | delete d; | ||
73 | } | 76 | } | ||
74 | 77 | | |||
78 | bool KDeclarative::ConfigPropertyMap::isAutosave() const | ||||
79 | { | ||||
80 | return d->autosave; | ||||
81 | } | ||||
82 | | ||||
83 | void ConfigPropertyMap::setAutosave(bool autosave) | ||||
84 | { | ||||
85 | d->autosave = autosave; | ||||
86 | } | ||||
87 | | ||||
75 | QVariant ConfigPropertyMap::updateValue(const QString &key, const QVariant &input) | 88 | QVariant ConfigPropertyMap::updateValue(const QString &key, const QVariant &input) | ||
76 | { | 89 | { | ||
77 | Q_UNUSED(key); | 90 | Q_UNUSED(key); | ||
78 | if (input.userType() == qMetaTypeId<QJSValue>()) { | 91 | if (input.userType() == qMetaTypeId<QJSValue>()) { | ||
79 | return input.value<QJSValue>().toVariant(); | 92 | return input.value<QJSValue>().toVariant(); | ||
80 | } | 93 | } | ||
81 | return input; | 94 | return input; | ||
82 | } | 95 | } | ||
Show All 29 Lines | 124 | if (!config) { | |||
112 | return; | 125 | return; | ||
113 | } | 126 | } | ||
114 | 127 | | |||
115 | const auto lstItems = config.data()->items(); | 128 | const auto lstItems = config.data()->items(); | ||
116 | for (KConfigSkeletonItem *item : lstItems) { | 129 | for (KConfigSkeletonItem *item : lstItems) { | ||
117 | item->setProperty(q->value(item->key())); | 130 | item->setProperty(q->value(item->key())); | ||
118 | } | 131 | } | ||
119 | 132 | | |||
133 | if (autosave) { | ||||
120 | updatingConfigValue = true; | 134 | updatingConfigValue = true; | ||
121 | config.data()->save(); | 135 | config.data()->save(); | ||
122 | updatingConfigValue = false; | 136 | updatingConfigValue = false; | ||
123 | } | 137 | } | ||
138 | } | ||||
124 | 139 | | |||
125 | void ConfigPropertyMapPrivate::writeConfigValue(const QString &key, const QVariant &value) | 140 | void ConfigPropertyMapPrivate::writeConfigValue(const QString &key, const QVariant &value) | ||
126 | { | 141 | { | ||
127 | KConfigSkeletonItem *item = config.data()->findItem(key); | 142 | KConfigSkeletonItem *item = config.data()->findItem(key); | ||
128 | if (item) { | 143 | if (item) { | ||
129 | updatingConfigValue = true; | 144 | updatingConfigValue = true; | ||
130 | item->setProperty(value); | 145 | item->setProperty(value); | ||
146 | if (autosave) { | ||||
131 | config.data()->save(); | 147 | config.data()->save(); | ||
132 | //why read? read will update KConfigSkeletonItem::mLoadedValue, | 148 | //why read? read will update KConfigSkeletonItem::mLoadedValue, | ||
133 | //allowing a write operation to be performed next time | 149 | //allowing a write operation to be performed next time | ||
134 | config.data()->read(); | 150 | config.data()->read(); | ||
151 | } | ||||
135 | updatingConfigValue = false; | 152 | updatingConfigValue = false; | ||
136 | } | 153 | } | ||
137 | } | 154 | } | ||
138 | 155 | | |||
139 | } | 156 | } | ||
140 | 157 | | |||
141 | #include "moc_configpropertymap.cpp" | 158 | #include "moc_configpropertymap.cpp" |