• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • config
kconfigdata.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDE libraries
3 Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
4 Copyright (c) 1999-2000 Preston Brown <pbrown@kde.org>
5 Copyright (C) 1996-2000 Matthias Kalle Dalheimer <kalle@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include <kconfigdata.h>
24
25QDebug operator<<(QDebug dbg, const KEntryKey& key)
26{
27 dbg.nospace() << "[" << key.mGroup << ", " << key.mKey << (key.bLocal?" localized":"") <<
28 (key.bDefault?" default":"") << (key.bRaw?" raw":"") << "]";
29 return dbg.space();
30}
31
32QDebug operator<<(QDebug dbg, const KEntry& entry)
33{
34 dbg.nospace() << "[" << entry.mValue << (entry.bDirty?" dirty":"") <<
35 (entry.bGlobal?" global":"") << (entry.bImmutable?" immutable":"") <<
36 (entry.bDeleted?" deleted":"") << (entry.bReverted?" reverted":"") <<
37 (entry.bExpand?" expand":"") << "]";
38
39 return dbg.space();
40}
41
42QMap< KEntryKey, KEntry >::Iterator KEntryMap::findExactEntry(const QByteArray& group, const QByteArray& key, KEntryMap::SearchFlags flags)
43{
44 KEntryKey theKey(group, key, bool(flags&SearchLocalized), bool(flags&SearchDefaults));
45 return find(theKey);
46}
47
48QMap< KEntryKey, KEntry >::Iterator KEntryMap::findEntry(const QByteArray& group, const QByteArray& key, KEntryMap::SearchFlags flags)
49{
50 KEntryKey theKey(group, key, false, bool(flags&SearchDefaults));
51
52 // try the localized key first
53 if (flags&SearchLocalized) {
54 theKey.bLocal = true;
55
56 Iterator it = find(theKey);
57 if (it != end())
58 return it;
59
60 theKey.bLocal = false;
61 }
62 return find(theKey);
63}
64
65QMap< KEntryKey, KEntry >::ConstIterator KEntryMap::findEntry(const QByteArray& group, const QByteArray& key, KEntryMap::SearchFlags flags) const
66{
67 KEntryKey theKey(group, key, false, bool(flags&SearchDefaults));
68
69 // try the localized key first
70 if (flags&SearchLocalized) {
71 theKey.bLocal = true;
72
73 ConstIterator it = find(theKey);
74 if (it != constEnd())
75 return it;
76
77 theKey.bLocal = false;
78 }
79 return find(theKey);
80}
81
82bool KEntryMap::setEntry(const QByteArray& group, const QByteArray& key, const QByteArray& value, KEntryMap::EntryOptions options)
83{
84 KEntryKey k;
85 KEntry e;
86 bool newKey = false;
87
88 const Iterator it = findExactEntry(group, key, SearchFlags(options>>16));
89
90 if (key.isEmpty()) { // inserting a group marker
91 k.mGroup = group;
92 e.bImmutable = (options&EntryImmutable);
93 if (options&EntryDeleted) {
94 qWarning("Internal KConfig error: cannot mark groups as deleted");
95 }
96 if(it == end()) {
97 insert(k, e);
98 return true;
99 } else if(it.value() == e) {
100 return false;
101 }
102
103 it.value() = e;
104 return true;
105 }
106
107
108 if (it != end()) {
109 if (it->bImmutable)
110 return false; // we cannot change this entry. Inherits group immutability.
111 k = it.key();
112 e = *it;
113 //qDebug() << "found existing entry for key" << k;
114 } else {
115 // make sure the group marker is in the map
116 KEntryMap const *that = this;
117 ConstIterator cit = that->findEntry(group);
118 if (cit == constEnd())
119 insert(KEntryKey(group), KEntry());
120 else if (cit->bImmutable)
121 return false; // this group is immutable, so we cannot change this entry.
122
123 k = KEntryKey(group, key);
124 newKey = true;
125 }
126
127 // set these here, since we may be changing the type of key from the one we found
128 k.bLocal = (options&EntryLocalized);
129 k.bDefault = (options&EntryDefault);
130 k.bRaw = (options&EntryRawKey);
131
132 e.mValue = value;
133 e.bDirty = e.bDirty || (options&EntryDirty);
134 e.bGlobal = (options&EntryGlobal); //we can't use || here, because changes to entries in
135 //kdeglobals would be written to kdeglobals instead
136 //of the local config file, regardless of the globals flag
137 e.bImmutable = e.bImmutable || (options&EntryImmutable);
138 if (value.isNull())
139 e.bDeleted = e.bDeleted || (options&EntryDeleted);
140 else
141 e.bDeleted = false; // setting a value to a previously deleted entry
142 e.bExpand = (options&EntryExpansion);
143 e.bReverted = false;
144
145 if(newKey)
146 {
147 //qDebug() << "inserting" << k << "=" << value;
148 insert(k, e);
149 if(k.bDefault)
150 {
151 k.bDefault = false;
152 //qDebug() << "also inserting" << k << "=" << value;
153 insert(k, e);
154 }
155 // TODO check for presence of unlocalized key
156 return true;
157 } else {
158// KEntry e2 = it.value();
159 if(it.value() != e)
160 {
161 //qDebug() << "changing" << k << "from" << e.mValue << "to" << value;
162 it.value() = e;
163 if(k.bDefault)
164 {
165 KEntryKey nonDefaultKey(k);
166 nonDefaultKey.bDefault = false;
167 insert(nonDefaultKey, e);
168 }
169 if (!(options & EntryLocalized)) {
170 KEntryKey theKey(group, key, true, false);
171 //qDebug() << "non-localized entry, remove localized one:" << theKey;
172 remove(theKey);
173 if (k.bDefault) {
174 theKey.bDefault = true;
175 remove(theKey);
176 }
177 }
178 return true;
179 } else {
180 //qDebug() << k << "was already set to" << e.mValue;
181 if (!(options & EntryLocalized)) {
182 //qDebug() << "unchanged non-localized entry, remove localized one.";
183 KEntryKey theKey(group, key, true, false);
184 bool ret = false;
185 Iterator cit = find(theKey);
186 if (cit != end()) {
187 erase(cit);
188 ret = true;
189 }
190 if (k.bDefault) {
191 theKey.bDefault = true;
192 Iterator cit = find(theKey);
193 if (cit != end()) {
194 erase(cit);
195 return true;
196 }
197 }
198 return ret;
199 }
200 //qDebug() << "localized entry, unchanged, return false";
201 // When we are writing a default, we know that the non-
202 // default is the same as the default, so we can simply
203 // use the same branch.
204 return false;
205 }
206 }
207}
208
209QString KEntryMap::getEntry(const QByteArray& group, const QByteArray& key, const QString& defaultValue, KEntryMap::SearchFlags flags, bool* expand) const
210{
211 const ConstIterator it = findEntry(group, key, flags);
212 QString theValue = defaultValue;
213
214 if (it != constEnd() && !it->bDeleted) {
215 if (!it->mValue.isNull()) {
216 const QByteArray data=it->mValue;
217 theValue = QString::fromUtf8(data.constData(), data.length());
218 if (expand)
219 *expand = it->bExpand;
220 }
221 }
222
223 return theValue;
224}
225
226bool KEntryMap::hasEntry(const QByteArray& group, const QByteArray& key, KEntryMap::SearchFlags flags) const
227{
228 const ConstIterator it = findEntry(group, key, flags);
229 if (it == constEnd())
230 return false;
231 if (it->bDeleted)
232 return false;
233 if (key.isNull()) { // looking for group marker
234 return it->mValue.isNull();
235 }
236 // if it->bReverted, we'll just return true; the real answer depends on lookup up with SearchDefaults, though.
237 return true;
238}
239
240bool KEntryMap::getEntryOption(const QMap< KEntryKey, KEntry >::ConstIterator& it, KEntryMap::EntryOption option) const
241{
242 if (it != constEnd()) {
243 switch (option) {
244 case EntryDirty:
245 return it->bDirty;
246 case EntryLocalized:
247 return it.key().bLocal;
248 case EntryGlobal:
249 return it->bGlobal;
250 case EntryImmutable:
251 return it->bImmutable;
252 case EntryDeleted:
253 return it->bDeleted;
254 case EntryExpansion:
255 return it->bExpand;
256 default:
257 break; // fall through
258 }
259 }
260
261 return false;
262}
263
264void KEntryMap::setEntryOption(QMap< KEntryKey, KEntry >::Iterator it, KEntryMap::EntryOption option, bool bf)
265{
266 if (it != end()) {
267 switch (option) {
268 case EntryDirty:
269 it->bDirty = bf;
270 break;
271 case EntryGlobal:
272 it->bGlobal = bf;
273 break;
274 case EntryImmutable:
275 it->bImmutable = bf;
276 break;
277 case EntryDeleted:
278 it->bDeleted = bf;
279 break;
280 case EntryExpansion:
281 it->bExpand = bf;
282 break;
283 default:
284 break; // fall through
285 }
286 }
287}
288
289bool KEntryMap::revertEntry(const QByteArray& group, const QByteArray& key, KEntryMap::SearchFlags flags)
290{
291 Q_ASSERT((flags & KEntryMap::SearchDefaults) == 0);
292 Iterator entry = findEntry(group, key, flags);
293 if (entry != end()) {
294 //qDebug() << "reverting" << entry.key() << " = " << entry->mValue;
295 if (entry->bReverted) { // already done before
296 return false;
297 }
298
299 KEntryKey defaultKey(entry.key());
300 defaultKey.bDefault = true;
301 //qDebug() << "looking up default entry with key=" << defaultKey;
302 const ConstIterator defaultEntry = constFind(defaultKey);
303 if (defaultEntry != constEnd()) {
304 Q_ASSERT(defaultEntry.key().bDefault);
305 //qDebug() << "found, update entry";
306 *entry = *defaultEntry; // copy default value, for subsequent lookups
307 } else {
308 entry->mValue = QByteArray();
309 }
310 entry->bDirty = true;
311 entry->bReverted = true; // skip it when writing out to disk
312
313 //qDebug() << "Here's what we have now:" << *this;
314 return true;
315 }
316 return false;
317}
KEntryMap
type specifying a map of entries (key,value pairs).
Definition: kconfigdata.h:153
KEntryMap::getEntry
QString getEntry(const QByteArray &group, const QByteArray &key, const QString &defaultValue=QString(), SearchFlags flags=SearchFlags(), bool *expand=0) const
Definition: kconfigdata.cpp:209
KEntryMap::findExactEntry
Iterator findExactEntry(const QByteArray &group, const QByteArray &key=QByteArray(), SearchFlags flags=SearchFlags())
Definition: kconfigdata.cpp:42
KEntryMap::revertEntry
bool revertEntry(const QByteArray &group, const QByteArray &key, SearchFlags flags=SearchFlags())
Definition: kconfigdata.cpp:289
KEntryMap::findEntry
Iterator findEntry(const QByteArray &group, const QByteArray &key=QByteArray(), SearchFlags flags=SearchFlags())
KEntryMap::getEntryOption
bool getEntryOption(const ConstIterator &it, EntryOption option) const
KEntryMap::setEntryOption
void setEntryOption(Iterator it, EntryOption option, bool bf)
KEntryMap::EntryOption
EntryOption
Definition: kconfigdata.h:161
KEntryMap::EntryImmutable
@ EntryImmutable
Definition: kconfigdata.h:164
KEntryMap::EntryDirty
@ EntryDirty
Definition: kconfigdata.h:162
KEntryMap::EntryLocalized
@ EntryLocalized
Definition: kconfigdata.h:169
KEntryMap::EntryExpansion
@ EntryExpansion
Definition: kconfigdata.h:166
KEntryMap::EntryGlobal
@ EntryGlobal
Definition: kconfigdata.h:163
KEntryMap::EntryRawKey
@ EntryRawKey
Definition: kconfigdata.h:167
KEntryMap::EntryDeleted
@ EntryDeleted
Definition: kconfigdata.h:165
KEntryMap::EntryDefault
@ EntryDefault
Definition: kconfigdata.h:168
KEntryMap::SearchLocalized
@ SearchLocalized
Definition: kconfigdata.h:157
KEntryMap::SearchDefaults
@ SearchDefaults
Definition: kconfigdata.h:156
KEntryMap::setEntry
bool setEntry(const QByteArray &group, const QByteArray &key, const QByteArray &value, EntryOptions options)
Returns true if the entry gets dirtied or false in other case.
KEntryMap::hasEntry
bool hasEntry(const QByteArray &group, const QByteArray &key=QByteArray(), SearchFlags flags=SearchFlags()) const
Definition: kconfigdata.cpp:226
QMap
QString
defaultValue
QString defaultValue(const QString &t)
Definition: kconfig_compiler.cpp:950
operator<<
QDebug operator<<(QDebug dbg, const KEntryKey &key)
Definition: kconfigdata.cpp:25
kconfigdata.h
KMacroExpander::group
@ group
Definition: kmacroexpander_unix.cpp:34
KEntryKey
key structure holding both the actual key and the group to which it belongs.
Definition: kconfigdata.h:90
KEntryKey::mKey
QByteArray mKey
The actual key of the entry in question.
Definition: kconfigdata.h:103
KEntryKey::bDefault
bool bDefault
Entry indicates if this is a default value.
Definition: kconfigdata.h:111
KEntryKey::bRaw
bool bRaw
Definition: kconfigdata.h:116
KEntryKey::mGroup
QByteArray mGroup
The "group" to which this EntryKey belongs.
Definition: kconfigdata.h:99
KEntryKey::bLocal
bool bLocal
Entry is localised or not.
Definition: kconfigdata.h:107
KEntry
map/dict/list config node entry.
Definition: kconfigdata.h:36
KEntry::bImmutable
bool bImmutable
Entry can not be modified.
Definition: kconfigdata.h:54
KEntry::bGlobal
bool bGlobal
Entry should be written to the global config file.
Definition: kconfigdata.h:50
KEntry::bExpand
bool bExpand
Whether to apply dollar expansion or not.
Definition: kconfigdata.h:62
KEntry::bDirty
bool bDirty
Must the entry be written back to disk?
Definition: kconfigdata.h:46
KEntry::bReverted
bool bReverted
Entry has been reverted to its default value (from a more global file).
Definition: kconfigdata.h:66
KEntry::bDeleted
bool bDeleted
Entry has been deleted.
Definition: kconfigdata.h:58
KEntry::mValue
QByteArray mValue
Definition: kconfigdata.h:42
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal