KConfig

kconfiggroup.h
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2006, 2007 Thomas Braxton <[email protected]>
4  SPDX-FileCopyrightText: 1999 Preston Brown <[email protected]>
5  SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <[email protected]>
6  SPDX-FileCopyrightText: 2001 Waldo Bastian <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #ifndef KCONFIGGROUP_H
12 #define KCONFIGGROUP_H
13 
14 #include "kconfigbase.h"
15 
16 #include <kconfigcore_export.h>
17 
18 #include <QExplicitlySharedDataPointer>
19 #include <QVariant>
20 #include <QStringList>
21 
22 class KConfig;
23 class KConfigGroupPrivate;
24 class KSharedConfig;
25 
38 class KCONFIGCORE_EXPORT KConfigGroup : public KConfigBase
39 {
40 public:
46  KConfigGroup();
47 
56  KConfigGroup(KConfigBase *master, const QString &group);
58  KConfigGroup(KConfigBase *master, const char *group);
59 
68  KConfigGroup(const KConfigBase *master, const QString &group);
70  KConfigGroup(const KConfigBase *master, const char *group);
71 
75  KConfigGroup(const QExplicitlySharedDataPointer<KSharedConfig> &master, const char *group);
76 
80  KConfigGroup(const KConfigGroup &);
81  KConfigGroup &operator=(const KConfigGroup &);
82 
83  ~KConfigGroup();
84 
94  bool isValid() const;
95 
101  QString name() const;
102 
107  bool exists() const;
108 
114  bool sync() override;
115 
117  void markAsClean() override;
118 
120  AccessMode accessMode() const override;
121 
125  KConfig *config();
129  const KConfig *config() const;
130 
131 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0)
132 
138  KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Create another KConfigGroup from the parent of this group")
139  void changeGroup(const QString &group);
140 #endif
141 
142 #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0)
143 
149  KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Create another KConfigGroup from the parent of this group")
150  void changeGroup(const char *group);
151 #endif
152 
164  void copyTo(KConfigBase *other, WriteConfigFlags pFlags = Normal) const;
165 
181  void reparent(KConfigBase *parent, WriteConfigFlags pFlags = Normal);
182 
191  KConfigGroup parent() const;
192 
196  QStringList groupList() const override;
197 
201  QStringList keyList() const;
202 
210  void deleteGroup(WriteConfigFlags pFlags = Normal);
212 
235  template <typename T>
236  T readEntry(const QString &key, const T &aDefault) const
237  {
238  return readEntry(key.toUtf8().constData(), aDefault);
239  }
241  template <typename T>
242  T readEntry(const char *key, const T &aDefault) const;
243 
253  QVariant readEntry(const QString &key, const QVariant &aDefault) const;
255  QVariant readEntry(const char *key, const QVariant &aDefault) const;
256 
268  QString readEntry(const QString &key, const QString &aDefault) const;
270  QString readEntry(const char *key, const QString &aDefault) const;
271 
273  QString readEntry(const QString &key, const char *aDefault = nullptr) const;
275  QString readEntry(const char *key, const char *aDefault = nullptr) const;
276 
285  QVariantList readEntry(const QString &key, const QVariantList &aDefault) const;
287  QVariantList readEntry(const char *key, const QVariantList &aDefault) const;
288 
298  QStringList readEntry(const QString &key, const QStringList &aDefault) const;
300  QStringList readEntry(const char *key, const QStringList &aDefault) const;
301 
311  template<typename T>
312  QList<T> readEntry(const QString &key, const QList<T> &aDefault) const
313  {
314  return readEntry(key.toUtf8().constData(), aDefault);
315  }
317  template<typename T>
318  QList<T> readEntry(const char *key, const QList<T> &aDefault) const;
319 
330  QStringList readXdgListEntry(const QString &pKey, const QStringList &aDefault = QStringList()) const;
332  QStringList readXdgListEntry(const char *pKey, const QStringList &aDefault = QStringList()) const;
333 
345  QString readPathEntry(const QString &pKey, const QString &aDefault) const;
347  QString readPathEntry(const char *key, const QString &aDefault) const;
348 
360  QStringList readPathEntry(const QString &pKey, const QStringList &aDefault) const;
362  QStringList readPathEntry(const char *key, const QStringList &aDefault) const;
363 
373  QString readEntryUntranslated(const QString &pKey,
374  const QString &aDefault = QString()) const;
376  QString readEntryUntranslated(const char *key,
377  const QString &aDefault = QString()) const;
378 
388  void writeEntry(const QString &key, const QVariant &value,
389  WriteConfigFlags pFlags = Normal);
391  void writeEntry(const char *key, const QVariant &value,
392  WriteConfigFlags pFlags = Normal);
393 
395  void writeEntry(const QString &key, const QString &value,
396  WriteConfigFlags pFlags = Normal);
398  void writeEntry(const char *key, const QString &value,
399  WriteConfigFlags pFlags = Normal);
400 
402  void writeEntry(const QString &key, const QByteArray &value,
403  WriteConfigFlags pFlags = Normal);
405  void writeEntry(const char *key, const QByteArray &value,
406  WriteConfigFlags pFlags = Normal);
407 
409  void writeEntry(const QString &key, const char *value, WriteConfigFlags pFlags = Normal);
411  void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags = Normal);
412 
414  template <typename T>
415  void writeEntry(const char *key, const T &value, WriteConfigFlags pFlags = Normal);
417  template <typename T>
418  void writeEntry(const QString &key, const T &value, WriteConfigFlags pFlags = Normal)
419  {
420  writeEntry(key.toUtf8().constData(), value, pFlags);
421  }
422 
424  void writeEntry(const QString &key, const QStringList &value,
425  WriteConfigFlags pFlags = Normal);
427  void writeEntry(const char *key, const QStringList &value,
428  WriteConfigFlags pFlags = Normal);
429 
431  void writeEntry(const QString &key, const QVariantList &value,
432  WriteConfigFlags pFlags = Normal);
434  void writeEntry(const char *key, const QVariantList &value,
435  WriteConfigFlags pFlags = Normal);
436 
438  template <typename T>
439  void writeEntry(const QString &key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
440  {
441  writeEntry(key.toUtf8().constData(), value, pFlags);
442  }
444  template <typename T>
445  void writeEntry(const char *key, const QList<T> &value, WriteConfigFlags pFlags = Normal);
446 
457  void writeXdgListEntry(const QString &pKey, const QStringList &value,
458  WriteConfigFlags pFlags = Normal);
460  void writeXdgListEntry(const char *pKey, const QStringList &value,
461  WriteConfigFlags pFlags = Normal);
462 
476  void writePathEntry(const QString &pKey, const QString &path,
477  WriteConfigFlags pFlags = Normal);
479  void writePathEntry(const char *pKey, const QString &path,
480  WriteConfigFlags pFlags = Normal);
481 
495  void writePathEntry(const QString &pKey, const QStringList &value,
496  WriteConfigFlags pFlags = Normal);
498  void writePathEntry(const char *pKey, const QStringList &value,
499  WriteConfigFlags pFlags = Normal);
500 
511  void deleteEntry(const QString &pKey, WriteConfigFlags pFlags = Normal);
513  void deleteEntry(const char *pKey, WriteConfigFlags pFlags = Normal);
514 
530  bool hasKey(const QString &key) const;
532  bool hasKey(const char *key) const;
533 
539  bool isImmutable() const override;
540 
551  bool isEntryImmutable(const QString &key) const;
553  bool isEntryImmutable(const char *key) const;
554 
571  // TODO KF6 merge with the other one
572  void revertToDefault(const QString &key);
573  void revertToDefault(const QString &key, WriteConfigFlags pFlag);
574 
576  // TODO KF6 merge with the other one
577  void revertToDefault(const char *key);
578  void revertToDefault(const char *key, WriteConfigFlags pFlag);
579 
607  bool hasDefault(const QString &key) const;
609  bool hasDefault(const char *key) const;
610 
619  QMap<QString, QString> entryMap() const;
620 
621 protected:
622  bool hasGroupImpl(const QByteArray &group) const override;
623  KConfigGroup groupImpl(const QByteArray &b) override;
624  const KConfigGroup groupImpl(const QByteArray &b) const override;
625  void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags) override;
626  bool isGroupImmutableImpl(const QByteArray &aGroup) const override;
627 
628 private:
630 
631  friend class KConfigGroupPrivate;
632 
642  static QVariant convertToQVariant(const char *pKey, const QByteArray &value, const QVariant &aDefault);
643  friend class KServicePrivate; // XXX yeah, ugly^5
644 };
645 
646 #define KCONFIGGROUP_ENUMERATOR_ERROR(ENUM) \
647  "The Qt MetaObject system does not seem to know about \"" ENUM \
648  "\" please use Q_ENUM or Q_FLAG to register it."
649 
658 #define KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum) \
659  template<> \
660  Class::Enum KConfigGroup::readEntry(const char* key, const Class::Enum& def) const\
661  { \
662  const QMetaObject* M_obj = &Class::staticMetaObject; \
663  const int M_index = M_obj->indexOfEnumerator(#Enum); \
664  if(M_index == -1) qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Enum)); \
665  const QMetaEnum M_enum = M_obj->enumerator(M_index); \
666  const QByteArray M_data = readEntry(key, QByteArray(M_enum.valueToKey(def)));\
667  return static_cast<Class::Enum>(M_enum.keyToValue(M_data.constData())); \
668  } \
669  inline Class::Enum Q_DECL_DEPRECATED readEntry(const KConfigGroup& group, const char* key, const Class::Enum& def)\
670  { return group.readEntry(key, def); } \
671  template<> \
672  void KConfigGroup::writeEntry(const char* key, const Class::Enum& value, KConfigBase::WriteConfigFlags flags)\
673  { \
674  const QMetaObject* M_obj = &Class::staticMetaObject; \
675  const int M_index = M_obj->indexOfEnumerator(#Enum); \
676  if(M_index == -1) qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Enum)); \
677  const QMetaEnum M_enum = M_obj->enumerator(M_index); \
678  writeEntry(key, QByteArray(M_enum.valueToKey(value)), flags); \
679  } \
680  inline void Q_DECL_DEPRECATED writeEntry(KConfigGroup& group, const char* key, const Class::Enum& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
681  { group.writeEntry(key, value, flags); }
682 
687 #define KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags) \
688  template<> \
689  Class::Flags KConfigGroup::readEntry(const char* key, const Class::Flags& def) const\
690  { \
691  const QMetaObject* M_obj = &Class::staticMetaObject; \
692  const int M_index = M_obj->indexOfEnumerator(#Flags); \
693  if(M_index == -1) qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Flags)); \
694  const QMetaEnum M_enum = M_obj->enumerator(M_index); \
695  const QByteArray M_data = readEntry(key, QByteArray(M_enum.valueToKeys(def)));\
696  return static_cast<Class::Flags>(M_enum.keysToValue(M_data.constData())); \
697  } \
698  inline Class::Flags Q_DECL_DEPRECATED readEntry(const KConfigGroup& group, const char* key, const Class::Flags& def)\
699  { return group.readEntry(key, def);} \
700  template<> \
701  void KConfigGroup::writeEntry(const char* key, const Class::Flags& value, KConfigBase::WriteConfigFlags flags)\
702  { \
703  const QMetaObject* M_obj = &Class::staticMetaObject; \
704  const int M_index = M_obj->indexOfEnumerator(#Flags); \
705  if(M_index == -1) qFatal(KCONFIGGROUP_ENUMERATOR_ERROR(#Flags)); \
706  const QMetaEnum M_enum = M_obj->enumerator(M_index); \
707  writeEntry(key, QByteArray(M_enum.valueToKeys(value)), flags); \
708  } \
709  inline void Q_DECL_DEPRECATED writeEntry(KConfigGroup& group, const char* key, const Class::Flags& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
710  { group.writeEntry(key, value, flags); }
711 
712 #include "conversioncheck.h"
713 
714 template <typename T>
715 T KConfigGroup::readEntry(const char *key, const T &defaultValue) const
716 {
717  ConversionCheck::to_QVariant<T>();
718  return qvariant_cast<T>(readEntry(key, QVariant::fromValue(defaultValue)));
719 }
720 
721 template <typename T>
722 QList<T> KConfigGroup::readEntry(const char *key, const QList<T> &defaultValue) const
723 {
724  ConversionCheck::to_QVariant<T>();
725  ConversionCheck::to_QString<T>();
726 
727  QVariantList data;
728 
729  for (const T &value : defaultValue) {
730  data.append(QVariant::fromValue(value));
731  }
732 
733  QList<T> list;
734  const auto variantList = readEntry<QVariantList>(key, data);
735  for (const QVariant &value : variantList) {
736  Q_ASSERT(value.canConvert<T>());
737  list.append(qvariant_cast<T>(value));
738  }
739 
740  return list;
741 }
742 
743 template <typename T>
744 void KConfigGroup::writeEntry(const char *key, const T &value, WriteConfigFlags pFlags)
745 {
746  ConversionCheck::to_QVariant<T>();
747  writeEntry(key, QVariant::fromValue(value), pFlags);
748 }
749 
750 template <typename T>
751 void KConfigGroup::writeEntry(const char *key, const QList<T> &list, WriteConfigFlags pFlags)
752 {
753  ConversionCheck::to_QVariant<T>();
754  ConversionCheck::to_QString<T>();
755  QVariantList data;
756  for (const T &value : list) {
757  data.append(QVariant::fromValue(value));
758  }
759 
760  writeEntry(key, data, pFlags);
761 }
762 
763 #endif // KCONFIGGROUP_H
KConfig variant using shared memory.
Definition: ksharedconfig.h:31
virtual AccessMode accessMode() const =0
Returns the access mode of the app-config object.
void deleteGroup(const QByteArray &group, WriteConfigFlags flags=Normal)
Delete aGroup.
Definition: kconfigbase.cpp:61
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Writes a value to the configuration object.
virtual bool sync()=0
Syncs the configuration object that this group belongs to.
virtual QStringList groupList() const =0
Returns a list of groups that are known about.
void append(const T &value)
const char * constData() const const
virtual bool isImmutable() const =0
Checks whether this configuration object can be modified.
Interface to interact with configuration.
Definition: kconfigbase.h:30
QVariant fromValue(const T &value)
void writeEntry(const QString &key, const QList< T > &value, WriteConfigFlags pFlags=Normal)
Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
Definition: kconfiggroup.h:439
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:38
The central class of the KDE configuration data system.
Definition: kconfig.h:56
QList< T > readEntry(const QString &key, const QList< T > &aDefault) const
Reads a list of values from the config object.
Definition: kconfiggroup.h:312
AccessMode
Possible return values for accessMode().
Definition: kconfigbase.h:134
void writeEntry(const QString &key, const T &value, WriteConfigFlags pFlags=Normal)
Overload for writeEntry(const QString&, const QVariant&, WriteConfigFlags)
Definition: kconfiggroup.h:418
virtual void markAsClean()=0
Reset the dirty flags of all entries in the entry map, so the values will not be written to disk on a...
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfiggroup.h:236
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jul 3 2020 22:47:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.