• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kconfiggroup.h

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
00004    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00005    Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
00006    Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021    Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #ifndef KCONFIGGROUP_H
00025 #define KCONFIGGROUP_H
00026 
00027 #include "kconfigbase.h"
00028 
00029 #include <kdecore_export.h>
00030 
00031 #include <QtCore/QExplicitlySharedDataPointer>
00032 #include <QtCore/QVariant>
00033 #include <QtCore/QStringList>
00034 
00035 class KConfig;
00036 class KConfigGroupPrivate;
00037 class KSharedConfig;
00038 template <typename T> class KSharedPtr;
00039 typedef KSharedPtr<KSharedConfig> KSharedConfigPtr;
00040 
00053 class KDECORE_EXPORT KConfigGroup : public KConfigBase
00054 {
00055 public:
00061     KConfigGroup();
00062 
00071     KConfigGroup(KConfigBase *master, const QString &group);
00073     KConfigGroup(KConfigBase *master, const char *group);
00074 
00083     KConfigGroup(const KConfigBase *master, const QString &group);
00085     KConfigGroup(const KConfigBase *master, const char *group);
00086 
00088     KConfigGroup(const KSharedConfigPtr &master, const QString &group);
00090     KConfigGroup(const KSharedConfigPtr &master, const char *group);
00091 
00095     KConfigGroup(const KConfigGroup &);
00096     KConfigGroup &operator=(const KConfigGroup &);
00097 
00098     ~KConfigGroup();
00099 
00109     bool isValid() const;
00110 
00116     QString name() const;
00117 
00122     bool exists() const;
00123 
00125     void sync();
00126 
00128     void markAsClean();
00129 
00131     AccessMode accessMode() const;
00132 
00136     KConfig* config();
00140     const KConfig* config() const;
00141 
00148     KDE_DEPRECATED void changeGroup(const QString &group);
00155     KDE_DEPRECATED void changeGroup(const char *group);
00156 
00168     void copyTo(KConfigBase *other, WriteConfigFlags pFlags = Normal) const;
00169 
00185     void reparent(KConfigBase *parent, WriteConfigFlags pFlags = Normal);
00186 
00195     KConfigGroup parent() const;
00196 
00200     QStringList groupList() const;
00201 
00205     QStringList keyList() const;
00206 
00214     void deleteGroup(WriteConfigFlags pFlags = Normal);
00215     using KConfigBase::deleteGroup;
00216 
00239     template <typename T>
00240         inline T readEntry(const QString &key, const T &aDefault) const
00241             { return readCheck(key.toUtf8().constData(), aDefault); }
00243     template <typename T>
00244         inline T readEntry(const char *key, const T &aDefault) const
00245             { return readCheck(key, aDefault); }
00246 
00256     QVariant readEntry(const QString &key, const QVariant &aDefault) const;
00258     QVariant readEntry(const char *key, const QVariant &aDefault) const;
00259 
00271     QString readEntry(const QString &key, const QString &aDefault) const;
00273     QString readEntry(const char *key, const QString &aDefault) const;
00274 
00276     QString readEntry(const QString &key, const char *aDefault = 0) const;
00278     QString readEntry(const char *key, const char *aDefault = 0) const;
00279 
00288     QVariantList readEntry(const QString &key, const QVariantList &aDefault) const;
00290     QVariantList readEntry(const char *key, const QVariantList &aDefault) const;
00291 
00301     QStringList readEntry(const QString &key, const QStringList &aDefault) const;
00303     QStringList readEntry(const char *key, const QStringList &aDefault) const;
00304 
00314     template<typename T>
00315         inline QList<T> readEntry(const QString &key, const QList<T> &aDefault) const
00316             { return readListCheck(key.toUtf8().constData(), aDefault); }
00318     template<typename T>
00319         inline QList<T> readEntry(const char *key, const QList<T> &aDefault) const
00320             { return readListCheck(key, aDefault); }
00321 
00332     QStringList readXdgListEntry(const QString &pKey, const QStringList &aDefault = QStringList()) const;
00334     QStringList readXdgListEntry(const char *pKey, const QStringList &aDefault = QStringList()) const;
00335 
00347     QString readPathEntry(const QString &pKey, const QString &aDefault) const;
00349     QString readPathEntry(const char *key, const QString &aDefault) const;
00350 
00362     QStringList readPathEntry(const QString &pKey, const QStringList &aDefault) const;
00364     QStringList readPathEntry(const char *key, const QStringList &aDefault) const;
00365 
00375     QString readEntryUntranslated(const QString &pKey,
00376                                   const QString &aDefault = QString()) const;
00378     QString readEntryUntranslated(const char *key,
00379                                   const QString &aDefault = QString()) const;
00380 
00390     void writeEntry(const QString &key, const QVariant &value,
00391                     WriteConfigFlags pFlags = Normal);
00393     void writeEntry(const char *key, const QVariant &value,
00394                     WriteConfigFlags pFlags = Normal);
00395 
00397     void writeEntry(const QString &key, const QString &value,
00398                     WriteConfigFlags pFlags = Normal);
00400     void writeEntry(const char *key, const QString &value,
00401                     WriteConfigFlags pFlags = Normal);
00402 
00404     void writeEntry(const QString &key, const QByteArray &value,
00405                     WriteConfigFlags pFlags = Normal);
00407     void writeEntry(const char *key, const QByteArray &value,
00408                     WriteConfigFlags pFlags = Normal);
00409 
00411     void writeEntry(const QString &key, const char *value, WriteConfigFlags pFlags = Normal);
00413     void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags = Normal);
00414 
00416     template <typename T>
00417         inline void writeEntry(const char *key, const T &value, WriteConfigFlags pFlags = Normal)
00418             { writeCheck( key, value, pFlags ); }
00420     template <typename T>
00421         inline void writeEntry(const QString &key, const T &value, WriteConfigFlags pFlags = Normal)
00422             { writeCheck( key.toUtf8().constData(), value, pFlags ); }
00423 
00425     void writeEntry(const QString &key, const QStringList &value,
00426                     WriteConfigFlags pFlags = Normal);
00428     void writeEntry(const char *key, const QStringList &value,
00429                     WriteConfigFlags pFlags = Normal);
00430 
00432     void writeEntry(const QString &key, const QVariantList &value,
00433                     WriteConfigFlags pFlags = Normal);
00435     void writeEntry(const char *key, const QVariantList &value,
00436                     WriteConfigFlags pFlags = Normal);
00437 
00439     template <typename T>
00440         inline void writeEntry(const QString &key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
00441             { writeListCheck( key.toUtf8().constData(), value, pFlags ); }
00443     template <typename T>
00444         inline void writeEntry(const char *key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
00445             { writeListCheck( key, value, pFlags ); }
00446 
00457     void writeXdgListEntry(const QString &pKey, const QStringList &value,
00458                            WriteConfigFlags pFlags = Normal);
00460     void writeXdgListEntry(const char *pKey, const QStringList &value,
00461                            WriteConfigFlags pFlags = Normal);
00462 
00476     void writePathEntry(const QString &pKey, const QString &path,
00477                         WriteConfigFlags pFlags = Normal);
00479     void writePathEntry(const char *pKey, const QString &path,
00480                         WriteConfigFlags pFlags = Normal);
00481 
00495     void writePathEntry(const QString &pKey, const QStringList &value,
00496                         WriteConfigFlags pFlags = Normal);
00498     void writePathEntry(const char *pKey, const QStringList &value,
00499                         WriteConfigFlags pFlags = Normal);
00500 
00511     void deleteEntry(const QString &pKey, WriteConfigFlags pFlags = Normal);
00513     void deleteEntry(const char *pKey, WriteConfigFlags pFlags = Normal);
00514 
00530     bool hasKey(const QString &key) const;
00532     bool hasKey(const char *key) const;
00533 
00539     bool isImmutable() const;
00540 
00551     bool isEntryImmutable(const QString &key) const;
00553     bool isEntryImmutable(const char *key) const;
00554 
00571     void revertToDefault(const QString &key);
00573     void revertToDefault(const char* key);
00574 
00602     bool hasDefault(const QString &key) const;
00604     bool hasDefault(const char *key) const;
00605 
00614     QMap<QString, QString> entryMap() const;
00615 
00616 protected:
00617     bool hasGroupImpl(const QByteArray &group) const;
00618     KConfigGroup groupImpl(const QByteArray &b);
00619     const KConfigGroup groupImpl(const QByteArray &b) const;
00620     void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags);
00621     bool isGroupImmutableImpl(const QByteArray &aGroup) const;
00622 
00623 private:
00624     QExplicitlySharedDataPointer<KConfigGroupPrivate> d;
00625 
00626     template<typename T>
00627     inline T readCheck(const char *key, const T &defaultValue) const;
00628 
00629     template<typename T>
00630     inline QList<T> readListCheck(const char *key, const QList<T> &defaultValue) const;
00631 
00632     template<typename T>
00633     inline void writeCheck(const char *key, const T &value, WriteConfigFlags pFlags);
00634 
00635     template<typename T>
00636     inline void writeListCheck(const char *key, const QList<T> &value, WriteConfigFlags pFlags);
00637 
00638     friend class KConfigGroupPrivate;
00639 
00649     static QVariant convertToQVariant(const char *pKey, const QByteArray &value, const QVariant &aDefault);
00650     friend class KServicePrivate; // XXX yeah, ugly^5
00651 };
00652 
00653 #define KCONFIGGROUP_ENUMERATOR_ERROR(ENUM) \
00654 "The Qt MetaObject system does not seem to know about \"" ENUM \
00655 "\" please use Q_ENUMS or Q_FLAGS to register it."
00656 
00657 #define KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum)                     \
00658 inline Class::Enum readEntry(const KConfigGroup& group, const char* key, const Class::Enum& def) \
00659 {                                                                          \
00660 const QMetaObject* M_obj = &Class::staticMetaObject;                       \
00661 const int M_index = M_obj->indexOfEnumerator(#Enum);                       \
00662 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl;     \
00663 const QMetaEnum M_enum = M_obj->enumerator(M_index);                       \
00664 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKey(def)));\
00665 return static_cast<Class::Enum>(M_enum.keyToValue(M_data.constData()));    \
00666 }                                                                          \
00667 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Enum& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
00668 {                                                                          \
00669 const QMetaObject* M_obj = &Class::staticMetaObject;                       \
00670 const int M_index = M_obj->indexOfEnumerator(#Enum);                       \
00671 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl;     \
00672 const QMetaEnum M_enum = M_obj->enumerator(M_index);                       \
00673 group.writeEntry(key, QByteArray(M_enum.valueToKey(value)), flags);              \
00674 }
00675 
00676 #define KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags)                    \
00677 inline Class::Flags readEntry(const KConfigGroup& group, const char* key, const Class::Flags& def) \
00678 {                                                                           \
00679 const QMetaObject* M_obj = &Class::staticMetaObject;                        \
00680 const int M_index = M_obj->indexOfEnumerator(#Flags);                       \
00681 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl;     \
00682 const QMetaEnum M_enum = M_obj->enumerator(M_index);                        \
00683 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKeys(def)));\
00684 return static_cast<Class::Flags>(M_enum.keysToValue(M_data.constData()));   \
00685 }                                                                           \
00686 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Flags& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
00687 {                                                                           \
00688 const QMetaObject* M_obj = &Class::staticMetaObject;                        \
00689 const int M_index = M_obj->indexOfEnumerator(#Flags);                       \
00690 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl;     \
00691 const QMetaEnum M_enum = M_obj->enumerator(M_index);                        \
00692 group.writeEntry(key, QByteArray(M_enum.valueToKeys(value)), flags);              \
00693 }
00694 
00695 #include "conversion_check.h"
00696 
00697 template <typename T>
00698 T KConfigGroup::readCheck(const char *key, const T &defaultValue) const
00699 {
00700   ConversionCheck::to_QVariant<T>();
00701   return qvariant_cast<T>(readEntry(key, qVariantFromValue(defaultValue)));
00702 }
00703 
00704 template <typename T>
00705 QList<T> KConfigGroup::readListCheck(const char *key, const QList<T> &defaultValue) const
00706 {
00707   ConversionCheck::to_QVariant<T>();
00708   ConversionCheck::to_QString<T>();
00709 
00710   QVariantList data;
00711 
00712   Q_FOREACH(const T& value, defaultValue)
00713     data.append(qVariantFromValue(value));
00714 
00715   QList<T> list;
00716   Q_FOREACH (const QVariant &value, readEntry<QVariantList>(key, data)) {
00717     Q_ASSERT(qVariantCanConvert<T>(value));
00718     list.append(qvariant_cast<T>(value));
00719   }
00720 
00721   return list;
00722 }
00723 
00724 template <typename T>
00725 void KConfigGroup::writeCheck(const char *key, const T &value,
00726                               WriteConfigFlags pFlags)
00727 {
00728     ConversionCheck::to_QVariant<T>();
00729     writeEntry(key, qVariantFromValue(value), pFlags);
00730 }
00731 
00732 template <typename T>
00733 void KConfigGroup::writeListCheck(const char *key, const QList<T> &list,
00734                                   WriteConfigFlags pFlags)
00735 {
00736   ConversionCheck::to_QVariant<T>();
00737   ConversionCheck::to_QString<T>();
00738   QVariantList data;
00739   Q_FOREACH(const T &value, list) {
00740     data.append(qVariantFromValue(value));
00741   }
00742 
00743   writeEntry(key, data, pFlags);
00744 }
00745 
00746 #endif // KCONFIGGROUP_H

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal