• Skip to content
  • Skip to link menu
KDE 4.3 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 
00129     void sync();
00130 
00132     void markAsClean();
00133 
00135     AccessMode accessMode() const;
00136 
00140     KConfig* config();
00144     const KConfig* config() const;
00145 
00152     KDE_DEPRECATED void changeGroup(const QString &group);
00159     KDE_DEPRECATED void changeGroup(const char *group);
00160 
00172     void copyTo(KConfigBase *other, WriteConfigFlags pFlags = Normal) const;
00173 
00189     void reparent(KConfigBase *parent, WriteConfigFlags pFlags = Normal);
00190 
00199     KConfigGroup parent() const;
00200 
00204     QStringList groupList() const;
00205 
00209     QStringList keyList() const;
00210 
00218     void deleteGroup(WriteConfigFlags pFlags = Normal);
00219     using KConfigBase::deleteGroup;
00220 
00243     template <typename T>
00244         inline T readEntry(const QString &key, const T &aDefault) const
00245             { return readCheck(key.toUtf8().constData(), aDefault); }
00247     template <typename T>
00248         inline T readEntry(const char *key, const T &aDefault) const
00249             { return readCheck(key, aDefault); }
00250 
00260     QVariant readEntry(const QString &key, const QVariant &aDefault) const;
00262     QVariant readEntry(const char *key, const QVariant &aDefault) const;
00263 
00275     QString readEntry(const QString &key, const QString &aDefault) const;
00277     QString readEntry(const char *key, const QString &aDefault) const;
00278 
00280     QString readEntry(const QString &key, const char *aDefault = 0) const;
00282     QString readEntry(const char *key, const char *aDefault = 0) const;
00283 
00292     QVariantList readEntry(const QString &key, const QVariantList &aDefault) const;
00294     QVariantList readEntry(const char *key, const QVariantList &aDefault) const;
00295 
00305     QStringList readEntry(const QString &key, const QStringList &aDefault) const;
00307     QStringList readEntry(const char *key, const QStringList &aDefault) const;
00308 
00318     template<typename T>
00319         inline QList<T> readEntry(const QString &key, const QList<T> &aDefault) const
00320             { return readListCheck(key.toUtf8().constData(), aDefault); }
00322     template<typename T>
00323         inline QList<T> readEntry(const char *key, const QList<T> &aDefault) const
00324             { return readListCheck(key, aDefault); }
00325 
00336     QStringList readXdgListEntry(const QString &pKey, const QStringList &aDefault = QStringList()) const;
00338     QStringList readXdgListEntry(const char *pKey, const QStringList &aDefault = QStringList()) const;
00339 
00351     QString readPathEntry(const QString &pKey, const QString &aDefault) const;
00353     QString readPathEntry(const char *key, const QString &aDefault) const;
00354 
00366     QStringList readPathEntry(const QString &pKey, const QStringList &aDefault) const;
00368     QStringList readPathEntry(const char *key, const QStringList &aDefault) const;
00369 
00379     QString readEntryUntranslated(const QString &pKey,
00380                                   const QString &aDefault = QString()) const;
00382     QString readEntryUntranslated(const char *key,
00383                                   const QString &aDefault = QString()) const;
00384 
00394     void writeEntry(const QString &key, const QVariant &value,
00395                     WriteConfigFlags pFlags = Normal);
00397     void writeEntry(const char *key, const QVariant &value,
00398                     WriteConfigFlags pFlags = Normal);
00399 
00401     void writeEntry(const QString &key, const QString &value,
00402                     WriteConfigFlags pFlags = Normal);
00404     void writeEntry(const char *key, const QString &value,
00405                     WriteConfigFlags pFlags = Normal);
00406 
00408     void writeEntry(const QString &key, const QByteArray &value,
00409                     WriteConfigFlags pFlags = Normal);
00411     void writeEntry(const char *key, const QByteArray &value,
00412                     WriteConfigFlags pFlags = Normal);
00413 
00415     void writeEntry(const QString &key, const char *value, WriteConfigFlags pFlags = Normal);
00417     void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags = Normal);
00418 
00420     template <typename T>
00421         inline void writeEntry(const char *key, const T &value, WriteConfigFlags pFlags = Normal)
00422             { writeCheck( key, value, pFlags ); }
00424     template <typename T>
00425         inline void writeEntry(const QString &key, const T &value, WriteConfigFlags pFlags = Normal)
00426             { writeCheck( key.toUtf8().constData(), value, pFlags ); }
00427 
00429     void writeEntry(const QString &key, const QStringList &value,
00430                     WriteConfigFlags pFlags = Normal);
00432     void writeEntry(const char *key, const QStringList &value,
00433                     WriteConfigFlags pFlags = Normal);
00434 
00436     void writeEntry(const QString &key, const QVariantList &value,
00437                     WriteConfigFlags pFlags = Normal);
00439     void writeEntry(const char *key, const QVariantList &value,
00440                     WriteConfigFlags pFlags = Normal);
00441 
00443     template <typename T>
00444         inline void writeEntry(const QString &key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
00445             { writeListCheck( key.toUtf8().constData(), value, pFlags ); }
00447     template <typename T>
00448         inline void writeEntry(const char *key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
00449             { writeListCheck( key, value, pFlags ); }
00450 
00461     void writeXdgListEntry(const QString &pKey, const QStringList &value,
00462                            WriteConfigFlags pFlags = Normal);
00464     void writeXdgListEntry(const char *pKey, const QStringList &value,
00465                            WriteConfigFlags pFlags = Normal);
00466 
00480     void writePathEntry(const QString &pKey, const QString &path,
00481                         WriteConfigFlags pFlags = Normal);
00483     void writePathEntry(const char *pKey, const QString &path,
00484                         WriteConfigFlags pFlags = Normal);
00485 
00499     void writePathEntry(const QString &pKey, const QStringList &value,
00500                         WriteConfigFlags pFlags = Normal);
00502     void writePathEntry(const char *pKey, const QStringList &value,
00503                         WriteConfigFlags pFlags = Normal);
00504 
00515     void deleteEntry(const QString &pKey, WriteConfigFlags pFlags = Normal);
00517     void deleteEntry(const char *pKey, WriteConfigFlags pFlags = Normal);
00518 
00534     bool hasKey(const QString &key) const;
00536     bool hasKey(const char *key) const;
00537 
00543     bool isImmutable() const;
00544 
00555     bool isEntryImmutable(const QString &key) const;
00557     bool isEntryImmutable(const char *key) const;
00558 
00575     void revertToDefault(const QString &key);
00577     void revertToDefault(const char* key);
00578 
00606     bool hasDefault(const QString &key) const;
00608     bool hasDefault(const char *key) const;
00609 
00618     QMap<QString, QString> entryMap() const;
00619 
00620 protected:
00621     bool hasGroupImpl(const QByteArray &group) const;
00622     KConfigGroup groupImpl(const QByteArray &b);
00623     const KConfigGroup groupImpl(const QByteArray &b) const;
00624     void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags);
00625     bool isGroupImmutableImpl(const QByteArray &aGroup) const;
00626 
00627 private:
00628     QExplicitlySharedDataPointer<KConfigGroupPrivate> d;
00629 
00630     template<typename T>
00631     inline T readCheck(const char *key, const T &defaultValue) const;
00632 
00633     template<typename T>
00634     inline QList<T> readListCheck(const char *key, const QList<T> &defaultValue) const;
00635 
00636     template<typename T>
00637     inline void writeCheck(const char *key, const T &value, WriteConfigFlags pFlags);
00638 
00639     template<typename T>
00640     inline void writeListCheck(const char *key, const QList<T> &value, WriteConfigFlags pFlags);
00641 
00642     friend class KConfigGroupPrivate;
00643 
00653     static QVariant convertToQVariant(const char *pKey, const QByteArray &value, const QVariant &aDefault);
00654     friend class KServicePrivate; // XXX yeah, ugly^5
00655 };
00656 
00657 #define KCONFIGGROUP_ENUMERATOR_ERROR(ENUM) \
00658 "The Qt MetaObject system does not seem to know about \"" ENUM \
00659 "\" please use Q_ENUMS or Q_FLAGS to register it."
00660 
00672 #define KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum)                     \
00673 inline Class::Enum readEntry(const KConfigGroup& group, const char* key, const Class::Enum& def) \
00674 {                                                                          \
00675 const QMetaObject* M_obj = &Class::staticMetaObject;                       \
00676 const int M_index = M_obj->indexOfEnumerator(#Enum);                       \
00677 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl;     \
00678 const QMetaEnum M_enum = M_obj->enumerator(M_index);                       \
00679 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKey(def)));\
00680 return static_cast<Class::Enum>(M_enum.keyToValue(M_data.constData()));    \
00681 }                                                                          \
00682 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Enum& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
00683 {                                                                          \
00684 const QMetaObject* M_obj = &Class::staticMetaObject;                       \
00685 const int M_index = M_obj->indexOfEnumerator(#Enum);                       \
00686 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl;     \
00687 const QMetaEnum M_enum = M_obj->enumerator(M_index);                       \
00688 group.writeEntry(key, QByteArray(M_enum.valueToKey(value)), flags);              \
00689 }
00690 
00695 #define KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags)                    \
00696 inline Class::Flags readEntry(const KConfigGroup& group, const char* key, const Class::Flags& def) \
00697 {                                                                           \
00698 const QMetaObject* M_obj = &Class::staticMetaObject;                        \
00699 const int M_index = M_obj->indexOfEnumerator(#Flags);                       \
00700 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl;     \
00701 const QMetaEnum M_enum = M_obj->enumerator(M_index);                        \
00702 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKeys(def)));\
00703 return static_cast<Class::Flags>(M_enum.keysToValue(M_data.constData()));   \
00704 }                                                                           \
00705 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Flags& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
00706 {                                                                           \
00707 const QMetaObject* M_obj = &Class::staticMetaObject;                        \
00708 const int M_index = M_obj->indexOfEnumerator(#Flags);                       \
00709 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl;     \
00710 const QMetaEnum M_enum = M_obj->enumerator(M_index);                        \
00711 group.writeEntry(key, QByteArray(M_enum.valueToKeys(value)), flags);              \
00712 }
00713 
00714 #include "conversion_check.h"
00715 
00716 template <typename T>
00717 T KConfigGroup::readCheck(const char *key, const T &defaultValue) const
00718 {
00719   ConversionCheck::to_QVariant<T>();
00720   return qvariant_cast<T>(readEntry(key, qVariantFromValue(defaultValue)));
00721 }
00722 
00723 template <typename T>
00724 QList<T> KConfigGroup::readListCheck(const char *key, const QList<T> &defaultValue) const
00725 {
00726   ConversionCheck::to_QVariant<T>();
00727   ConversionCheck::to_QString<T>();
00728 
00729   QVariantList data;
00730 
00731   Q_FOREACH(const T& value, defaultValue)
00732     data.append(qVariantFromValue(value));
00733 
00734   QList<T> list;
00735   Q_FOREACH (const QVariant &value, readEntry<QVariantList>(key, data)) {
00736     Q_ASSERT(qVariantCanConvert<T>(value));
00737     list.append(qvariant_cast<T>(value));
00738   }
00739 
00740   return list;
00741 }
00742 
00743 template <typename T>
00744 void KConfigGroup::writeCheck(const char *key, const T &value,
00745                               WriteConfigFlags pFlags)
00746 {
00747     ConversionCheck::to_QVariant<T>();
00748     writeEntry(key, qVariantFromValue(value), pFlags);
00749 }
00750 
00751 template <typename T>
00752 void KConfigGroup::writeListCheck(const char *key, const QList<T> &list,
00753                                   WriteConfigFlags pFlags)
00754 {
00755   ConversionCheck::to_QVariant<T>();
00756   ConversionCheck::to_QString<T>();
00757   QVariantList data;
00758   Q_FOREACH(const T &value, list) {
00759     data.append(qVariantFromValue(value));
00760   }
00761 
00762   writeEntry(key, data, pFlags);
00763 }
00764 
00765 #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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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