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

KDECore

  • kdecore
  • config
kconfigdata.h
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 #ifndef KCONFIGDATA_H
24 #define KCONFIGDATA_H
25 
26 #include <QtCore/QByteArray>
27 #include <QtCore/QString>
28 #include <QtCore/QMap>
29 #include <QtCore/QDebug>
30 
35 struct KEntry
36 {
38  KEntry()
39  : mValue(), bDirty(false),
40  bGlobal(false), bImmutable(false), bDeleted(false), bExpand(false) {}
42  QByteArray mValue;
46  bool bDirty :1;
50  bool bGlobal:1;
54  bool bImmutable:1;
58  bool bDeleted:1;
62  bool bExpand:1;
63 };
64 
65 // These operators are used to check whether an entry which is about
66 // to be written equals the previous value. As such, this intentionally
67 // omits the dirty flag from the comparison.
68 inline bool operator ==(const KEntry &k1, const KEntry &k2)
69 {
70  return k1.bGlobal == k2.bGlobal && k1.bImmutable == k2.bImmutable
71  && k1.bDeleted == k2.bDeleted && k1.bExpand == k2.bExpand
72  && k1.mValue == k2.mValue;
73 }
74 
75 inline bool operator !=(const KEntry &k1, const KEntry &k2)
76 {
77  return !(k1 == k2);
78 }
79 
85 struct KEntryKey
86 {
88  KEntryKey(const QByteArray& _group = QByteArray(),
89  const QByteArray& _key = QByteArray(), bool isLocalized=false, bool isDefault=false)
90  : mGroup(_group), mKey(_key), bLocal(isLocalized), bDefault(isDefault), bRaw(false)
91  { ; }
95  QByteArray mGroup;
99  QByteArray mKey;
103  bool bLocal :1;
107  bool bDefault:1;
112  bool bRaw:1;
113 };
114 
120 inline bool operator <(const KEntryKey &k1, const KEntryKey &k2)
121 {
122  int result = qstrcmp(k1.mGroup, k2.mGroup);
123  if (result != 0) {
124  return result < 0;
125  }
126 
127  result = qstrcmp(k1.mKey, k2.mKey);
128  if (result != 0) {
129  return result < 0;
130  }
131 
132  if (k1.bLocal != k2.bLocal)
133  return k1.bLocal;
134  return (!k1.bDefault && k2.bDefault);
135 }
136 
144 class KEntryMap : public QMap<KEntryKey, KEntry>
145 {
146  public:
147  enum SearchFlag {
148  SearchDefaults=1,
149  SearchLocalized=2
150  };
151  Q_DECLARE_FLAGS(SearchFlags, SearchFlag)
152 
153  enum EntryOption {
154  EntryDirty=1,
155  EntryGlobal=2,
156  EntryImmutable=4,
157  EntryDeleted=8,
158  EntryExpansion=16,
159  EntryRawKey=32,
160  EntryDefault=(SearchDefaults<<16),
161  EntryLocalized=(SearchLocalized<<16)
162  };
163  Q_DECLARE_FLAGS(EntryOptions, EntryOption)
164 
165  Iterator findExactEntry(const QByteArray& group, const QByteArray& key = QByteArray(),
166  SearchFlags flags = SearchFlags())
167  {
168  KEntryKey theKey(group, key, false, bool(flags&SearchDefaults));
169 
170  // try the localized key first
171  if (flags&SearchLocalized) {
172  theKey.bLocal = true;
173  return find(theKey);
174  }
175  return find(theKey);
176  }
177 
178  Iterator findEntry(const QByteArray& group, const QByteArray& key = QByteArray(),
179  SearchFlags flags = SearchFlags())
180  {
181  KEntryKey theKey(group, key, false, bool(flags&SearchDefaults));
182 
183  // try the localized key first
184  if (flags&SearchLocalized) {
185  theKey.bLocal = true;
186 
187  Iterator it = find(theKey);
188  if (it != end())
189  return it;
190 
191  theKey.bLocal = false;
192  }
193  return find(theKey);
194  }
195 
196  ConstIterator findEntry(const QByteArray& group, const QByteArray& key = QByteArray(),
197  SearchFlags flags = SearchFlags()) const
198  {
199  KEntryKey theKey(group, key, false, bool(flags&SearchDefaults));
200 
201  // try the localized key first
202  if (flags&SearchLocalized) {
203  theKey.bLocal = true;
204 
205  ConstIterator it = find(theKey);
206  if (it != constEnd())
207  return it;
208 
209  theKey.bLocal = false;
210  }
211  return find(theKey);
212  }
213 
217  bool setEntry(const QByteArray& group, const QByteArray& key,
218  const QByteArray& value, EntryOptions options)
219  {
220  KEntryKey k;
221  KEntry e;
222  bool newKey = false;
223 
224  const Iterator it = findExactEntry(group, key, SearchFlags(options>>16));
225 
226  if (key.isEmpty()) { // inserting a group marker
227  k.mGroup = group;
228  e.bImmutable = (options&EntryImmutable);
229  if (options&EntryDeleted) { qWarning("Internal KConfig error: cannot mark groups as deleted"); }
230  if(it == end()) {
231  insert(k, e);
232  return true;
233  } else if(it.value() == e) {
234  return false;
235  }
236 
237  it.value() = e;
238  return true;
239  }
240 
241 
242  if (it != end()) {
243  if (it->bImmutable)
244  return false; // we cannot change this entry. Inherits group immutability.
245  k = it.key();
246  e = *it;
247  } else {
248  // make sure the group marker is in the map
249  KEntryMap const *that = this;
250  ConstIterator cit = that->findEntry(group);
251  if (cit == constEnd())
252  insert(KEntryKey(group), KEntry());
253  else if (cit->bImmutable)
254  return false; // this group is immutable, so we cannot change this entry.
255 
256  k = KEntryKey(group, key);
257  newKey = true;
258  }
259 
260  // set these here, since we may be changing the type of key from the one we found
261  k.bLocal = (options&EntryLocalized);
262  k.bDefault = (options&EntryDefault);
263  k.bRaw = (options&EntryRawKey);
264 
265  //qDebug() << "changing [" << group << "," << key << "] =" << e.mValue;
266  e.mValue = value;
267  e.bDirty = e.bDirty || (options&EntryDirty);
268  e.bGlobal = (options&EntryGlobal); //we can't use || here, because changes to entries in
269  //kdeglobals would be written to kdeglobals instead
270  //of the local config file, regardless of the globals flag
271  e.bImmutable = e.bImmutable || (options&EntryImmutable);
272  if (value.isNull())
273  e.bDeleted = e.bDeleted || (options&EntryDeleted);
274  else
275  e.bDeleted = false; // setting a value to a previously deleted entry
276  e.bExpand = (options&EntryExpansion);
277 
278  //qDebug() << "to [" << group << "," << key << "] =" << e.mValue << "newKey=" << newKey;
279  if(newKey)
280  {
281  insert(k, e);
282  if(k.bDefault)
283  {
284  k.bDefault = false;
285  insert(k, e);
286  }
287  // TODO check for presence of unlocalized key
288  return true;
289  } else {
290 // KEntry e2 = it.value();
291  if(it.value() != e)
292  {
293  it.value() = e;
294  if(k.bDefault)
295  {
296  k.bDefault = false;
297  insert(k, e);
298  }
299  if (!(options & EntryLocalized)) {
300  KEntryKey theKey(group, key, true, false);
301  remove(theKey);
302  if (k.bDefault) {
303  theKey.bDefault = false;
304  remove(theKey);
305  }
306  }
307  return true;
308  } else {
309  if (!(options & EntryLocalized)) {
310  KEntryKey theKey(group, key, true, false);
311  bool ret = false;
312  Iterator cit = find(theKey);
313  if (cit != end()) {
314  erase(cit);
315  ret = true;
316  }
317  if (k.bDefault) {
318  theKey.bDefault = false;
319  Iterator cit = find(theKey);
320  if (cit != end()) {
321  erase(cit);
322  return true;
323  }
324  }
325  return ret;
326  }
327  // When we are writing a default, we know that the non-
328  // default is the same as the default, so we can simply
329  // use the same branch.
330  return false;
331  }
332  }
333  }
334 
335  void setEntry(const QByteArray& group, const QByteArray& key,
336  const QString & value, EntryOptions options)
337  {
338  setEntry(group, key, value.toUtf8(), options);
339  }
340 
341  QString getEntry(const QByteArray& group, const QByteArray& key,
342  const QString & defaultValue = QString(),
343  SearchFlags flags = SearchFlags(),
344  bool * expand=0) const
345  {
346  const ConstIterator it = findEntry(group, key, flags);
347  QString theValue = defaultValue;
348 
349  if (it != constEnd() && !it->bDeleted) {
350  if (!it->mValue.isNull()) {
351  const QByteArray data=it->mValue;
352  theValue = QString::fromUtf8(data.constData(), data.length());
353  if (expand)
354  *expand = it->bExpand;
355  }
356  }
357 
358  return theValue;
359  }
360 
361 
362 
363  bool hasEntry(const QByteArray& group, const QByteArray& key = QByteArray(),
364  SearchFlags flags = SearchFlags()) const
365  {
366  const ConstIterator it = findEntry(group, key, flags);
367  if (it == constEnd())
368  return false;
369  if (it->bDeleted)
370  return false;
371  if (key.isNull()) { // looking for group marker
372  return it->mValue.isNull();
373  }
374  return true;
375  }
376 
377  bool getEntryOption(const ConstIterator& it, EntryOption option) const
378  {
379  if (it != constEnd()) {
380  switch (option) {
381  case EntryDirty:
382  return it->bDirty;
383  case EntryLocalized:
384  return it.key().bLocal;
385  case EntryGlobal:
386  return it->bGlobal;
387  case EntryImmutable:
388  return it->bImmutable;
389  case EntryDeleted:
390  return it->bDeleted;
391  case EntryExpansion:
392  return it->bExpand;
393  default:
394  break; // fall through
395  }
396  }
397 
398  return false;
399  }
400  bool getEntryOption(const QByteArray& group, const QByteArray& key,
401  SearchFlags flags, EntryOption option) const
402  {
403  return getEntryOption(findEntry(group, key, flags), option);
404  }
405 
406  void setEntryOption(Iterator it, EntryOption option, bool bf)
407  {
408  if (it != end()) {
409  switch (option) {
410  case EntryDirty:
411  it->bDirty = bf;
412  break;
413  case EntryGlobal:
414  it->bGlobal = bf;
415  break;
416  case EntryImmutable:
417  it->bImmutable = bf;
418  break;
419  case EntryDeleted:
420  it->bDeleted = bf;
421  break;
422  case EntryExpansion:
423  it->bExpand = bf;
424  break;
425  default:
426  break; // fall through
427  }
428  }
429  }
430  void setEntryOption(const QByteArray& group, const QByteArray& key, SearchFlags flags,
431  EntryOption option, bool bf)
432  {
433  setEntryOption(findEntry(group, key, flags), option, bf);
434  }
435 
436  void revertEntry(const QByteArray& group, const QByteArray& key, SearchFlags flags=SearchFlags())
437  {
438  Iterator entry = findEntry(group, key, flags);
439  if (entry != end()) {
440  //qDebug() << "reverting [" << group << "," << key << "] = " << entry->mValue;
441  const ConstIterator defaultEntry(entry+1);
442  if (defaultEntry != constEnd() && defaultEntry.key().bDefault) {
443  *entry = *defaultEntry;
444  entry->bDirty = true;
445  } else if (!entry->mValue.isNull()){
446  entry->mValue = QByteArray();
447  entry->bDirty = true;
448  entry->bDeleted = true;
449  }
450  //qDebug() << "to [" << group << "," << key << "] =" << entry->mValue;
451  }
452  }
453 };
454 Q_DECLARE_OPERATORS_FOR_FLAGS(KEntryMap::SearchFlags)
455 Q_DECLARE_OPERATORS_FOR_FLAGS(KEntryMap::EntryOptions)
456 
462 typedef QMap<KEntryKey, KEntry>::Iterator KEntryMapIterator;
463 
471 typedef QMap<KEntryKey, KEntry>::ConstIterator KEntryMapConstIterator;
472 
473 #endif
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 12:05:01 by doxygen 1.8.1.1 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.10.4 API Reference

Skip menu "kdelibs-4.10.4 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