• Skip to content
  • Skip to link menu
KDE 4.1 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 
00070     KConfigGroup(KConfigBase *master, const QString &group);
00071     KConfigGroup(KConfigBase *master, const char *group);
00072 
00079     KConfigGroup(const KConfigBase *master, const QString &group);
00080     KConfigGroup(const KConfigBase *master, const char *group);
00081 
00082     KConfigGroup(const KSharedConfigPtr &master, const QString& group);
00083     KConfigGroup(const KSharedConfigPtr &master, const char* group);
00084 
00085     KConfigGroup(const KConfigGroup &);
00086     KConfigGroup &operator=(const KConfigGroup &);
00087 
00088     ~KConfigGroup();
00089 
00096     bool isValid() const;
00097 
00102     QString name() const;
00103 
00108     bool exists() const;
00109 
00111     void sync();
00112 
00114     void markAsClean();
00115 
00117     AccessMode accessMode() const;
00118 
00122     KConfig* config();
00123     const KConfig* config() const;
00124 
00130     KDE_DEPRECATED void changeGroup( const QString &group );
00131     KDE_DEPRECATED void changeGroup( const char *group);
00132 
00140     void copyTo(KConfigBase *other, WriteConfigFlags pFlags = Normal) const;
00141 
00150     void reparent(KConfigBase* parent, WriteConfigFlags pFlags = Normal);
00151 
00157     KConfigGroup parent() const;
00158 
00162     QStringList groupList() const;
00163 
00167     QStringList keyList() const;
00168 
00173     void deleteGroup(WriteConfigFlags pFlags=Normal);
00174     using KConfigBase::deleteGroup;
00175 
00195     template <typename T>
00196         inline T readEntry(const QString& key, const T& aDefault) const
00197             { return readCheck(key.toUtf8().constData(), aDefault); }
00198     template <typename T>
00199         inline T readEntry(const char *key, const T& aDefault) const
00200             { return readCheck(key, aDefault); }
00201 
00209     QVariant readEntry(const QString& key, const QVariant &aDefault) const;
00210     QVariant readEntry(const char* key, const QVariant &aDefault) const;
00211 
00220     QString readEntry(const char* key, const QString& aDefault ) const;
00221     QString readEntry(const QString& key, const QString& aDefault) const;
00222 
00230     QString readEntry(const QString &key, const char * aDefault = 0) const;
00231     QString readEntry(const char *key, const char *aDefault = 0 ) const;
00232 
00243     QVariantList readEntry( const QString &key, const QVariantList& aDefault ) const;
00244     QVariantList readEntry( const char* key, const QVariantList& aDefault ) const;
00245 
00252     QStringList readEntry(const QString &key, const QStringList& aDefault) const;
00253     QStringList readEntry(const char* key, const QStringList& aDefault) const;
00254 
00261     template<typename T>
00262         inline QList<T> readEntry(const QString& key, const QList<T> &aDefault) const
00263             { return readListCheck(key.toUtf8().constData(), aDefault); }
00264     template<typename T>
00265         inline QList<T> readEntry(const char* key, const QList<T> &aDefault) const
00266             { return readListCheck(key, aDefault); }
00267 
00275     QStringList readXdgListEntry(const QString& pKey, const QStringList& aDefault = QStringList()) const;
00276     QStringList readXdgListEntry(const char* pKey, const QStringList& aDefault = QStringList()) const;
00277 
00289     QString readPathEntry( const QString& pKey, const QString & aDefault ) const;
00290     QString readPathEntry( const char *key, const QString & aDefault ) const;
00291 
00303     QStringList readPathEntry( const QString& pKey, const QStringList& aDefault ) const;
00304     QStringList readPathEntry( const char *key, const QStringList& aDefault ) const;
00305 
00314     QString readEntryUntranslated( const QString& pKey,
00315                                    const QString& aDefault = QString() ) const;
00316     QString readEntryUntranslated( const char *key,
00317                                    const QString& aDefault = QString() ) const;
00318 
00326     void writeEntry( const QString& key, const QVariant& value,
00327                      WriteConfigFlags pFlags = Normal );
00328     void writeEntry( const char *key, const QVariant& value,
00329                      WriteConfigFlags pFlags = Normal );
00330 
00338     void writeEntry( const QString& key, const QString& value,
00339                      WriteConfigFlags pFlags = Normal );
00340     void writeEntry( const char *key, const QString& value,
00341                      WriteConfigFlags pFlags = Normal );
00342 
00350     void writeEntry( const QString& key, const QByteArray& value,
00351                      WriteConfigFlags pFlags = Normal );
00352     void writeEntry( const char *key, const QByteArray& value,
00353                      WriteConfigFlags pFlags = Normal );
00354 
00362     void writeEntry(const QString &key, const char *value, WriteConfigFlags pFlags = Normal);
00363     void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags = Normal);
00364 
00372     template <typename T>
00373         inline void writeEntry( const char *key, const T& value, WriteConfigFlags pFlags = Normal )
00374             { writeCheck( key, value, pFlags ); }
00375 
00376     template <typename T>
00377         inline void writeEntry( const QString& key, const T& value, WriteConfigFlags pFlags = Normal )
00378             { writeCheck( key.toUtf8().constData(), value, pFlags ); }
00379 
00389     void writeEntry( const QString& key, const QStringList &value,
00390                      WriteConfigFlags pFlags = Normal );
00391 
00392     void writeEntry( const char* key, const QStringList &value,
00393                      WriteConfigFlags pFlags = Normal );
00394 
00404     void writeEntry( const QString& key, const QVariantList &value,
00405                      WriteConfigFlags pFlags = Normal );
00406 
00407     void writeEntry( const char* key, const QVariantList &value,
00408                      WriteConfigFlags pFlags = Normal );
00409 
00417     template <typename T>
00418         inline void writeEntry(const QString& key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
00419             { writeListCheck( key.toUtf8().constData(), value, pFlags ); }
00420 
00421     template <typename T>
00422         inline void writeEntry(const char* key, const QList<T> &value, WriteConfigFlags pFlags = Normal)
00423             { writeListCheck( key, value, pFlags ); }
00424 
00435     void writeXdgListEntry( const QString& pKey, const QStringList &value,
00436                             WriteConfigFlags pFlags = Normal );
00437     void writeXdgListEntry( const char *pKey, const QStringList &value,
00438                             WriteConfigFlags pFlags = Normal );
00439 
00451     void writePathEntry( const QString& pKey, const QString & path,
00452                          WriteConfigFlags pFlags = Normal );
00453     void writePathEntry( const char *pKey, const QString & path,
00454                          WriteConfigFlags pFlags = Normal );
00455 
00469     void writePathEntry( const QString& pKey, const QStringList &value,
00470                          WriteConfigFlags pFlags = Normal );
00471     void writePathEntry( const char *pKey, const QStringList &value,
00472                          WriteConfigFlags pFlags = Normal );
00473 
00481     void deleteEntry(const QString& pKey, WriteConfigFlags pFlags = Normal);
00482     void deleteEntry(const char *pKey, WriteConfigFlags pFlags = Normal);
00483 
00492     bool hasKey(const QString &key) const;
00493     bool hasKey(const char *key) const;
00494 
00499     bool isImmutable() const;
00500 
00507     bool isEntryImmutable(const QString &key) const;
00508     bool isEntryImmutable(const char *key) const;
00509 
00520     void revertToDefault(const QString &key);
00521     void revertToDefault(const char* key);
00522 
00548     bool hasDefault(const QString &key) const;
00549     bool hasDefault(const char* key) const;
00550 
00559     QMap<QString, QString> entryMap() const;
00560 
00561 protected:
00562     bool hasGroupImpl(const QByteArray &group) const;
00563     KConfigGroup groupImpl(const QByteArray &b);
00564     const KConfigGroup groupImpl(const QByteArray &b) const;
00565     void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags);
00566     bool isGroupImmutableImpl(const QByteArray& aGroup) const;
00567 
00568 private:
00569     QExplicitlySharedDataPointer<KConfigGroupPrivate> d;
00570 
00571     template<typename T>
00572     inline T readCheck(const char* key, const T &defaultValue) const;
00573 
00574     template<typename T>
00575     inline QList<T> readListCheck(const char* key, const QList<T> &defaultValue) const;
00576 
00577     template<typename T>
00578     inline void writeCheck(const char* key, const T &value, WriteConfigFlags pFlags);
00579 
00580     template<typename T>
00581     inline void writeListCheck(const char* key, const QList<T> &value, WriteConfigFlags pFlags);
00582 
00583     friend class KConfigGroupPrivate;
00584 
00594     static QVariant convertToQVariant(const char* pKey, const QByteArray& value, const QVariant& aDefault);
00595     friend class KServicePrivate; // XXX yeah, ugly^5
00596 };
00597 
00598 #define KCONFIGGROUP_ENUMERATOR_ERROR(ENUM) \
00599 "The Qt MetaObject system does not seem to know about \"" ENUM \
00600 "\" please use Q_ENUMS or Q_FLAGS to register it."
00601 
00602 #define KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum)                     \
00603 inline Class::Enum readEntry(const KConfigGroup& group, const char* key, const Class::Enum& def) \
00604 {                                                                          \
00605 const QMetaObject* M_obj = &Class::staticMetaObject;                       \
00606 const int M_index = M_obj->indexOfEnumerator(#Enum);                       \
00607 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl;     \
00608 const QMetaEnum M_enum = M_obj->enumerator(M_index);                       \
00609 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKey(def)));\
00610 return static_cast<Class::Enum>(M_enum.keyToValue(M_data.constData()));    \
00611 }                                                                          \
00612 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Enum& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
00613 {                                                                          \
00614 const QMetaObject* M_obj = &Class::staticMetaObject;                       \
00615 const int M_index = M_obj->indexOfEnumerator(#Enum);                       \
00616 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl;     \
00617 const QMetaEnum M_enum = M_obj->enumerator(M_index);                       \
00618 group.writeEntry(key, QByteArray(M_enum.valueToKey(value)), flags);              \
00619 }
00620 
00621 #define KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags)                    \
00622 inline Class::Flags readEntry(const KConfigGroup& group, const char* key, const Class::Flags& def) \
00623 {                                                                           \
00624 const QMetaObject* M_obj = &Class::staticMetaObject;                        \
00625 const int M_index = M_obj->indexOfEnumerator(#Flags);                       \
00626 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl;     \
00627 const QMetaEnum M_enum = M_obj->enumerator(M_index);                        \
00628 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKeys(def)));\
00629 return static_cast<Class::Flags>(M_enum.keysToValue(M_data.constData()));   \
00630 }                                                                           \
00631 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Flags& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\
00632 {                                                                           \
00633 const QMetaObject* M_obj = &Class::staticMetaObject;                        \
00634 const int M_index = M_obj->indexOfEnumerator(#Flags);                       \
00635 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl;     \
00636 const QMetaEnum M_enum = M_obj->enumerator(M_index);                        \
00637 group.writeEntry(key, QByteArray(M_enum.valueToKeys(value)), flags);              \
00638 }
00639 
00640 #include "conversion_check.h"
00641 
00642 template <typename T>
00643 T KConfigGroup::readCheck(const char* key, const T &defaultValue) const
00644 {
00645   ConversionCheck::to_QVariant<T>();
00646   return qvariant_cast<T>(readEntry(key, qVariantFromValue(defaultValue)));
00647 }
00648 
00649 template <typename T>
00650 QList<T> KConfigGroup::readListCheck(const char* key, const QList<T> &defaultValue) const
00651 {
00652   ConversionCheck::to_QVariant<T>();
00653   ConversionCheck::to_QString<T>();
00654 
00655   QVariantList data;
00656 
00657   Q_FOREACH(const T& value, defaultValue)
00658     data.append(qVariantFromValue(value));
00659 
00660   QList<T> list;
00661   Q_FOREACH (const QVariant &value, readEntry<QVariantList>(key, data)) {
00662     Q_ASSERT(qVariantCanConvert<T>(value));
00663     list.append( qvariant_cast<T>(value) );
00664   }
00665 
00666   return list;
00667 }
00668 
00669 template <typename T>
00670 void KConfigGroup::writeCheck( const char* key, const T& value,
00671                                WriteConfigFlags pFlags )
00672 {
00673     ConversionCheck::to_QVariant<T>();
00674     writeEntry( key, qVariantFromValue(value), pFlags );
00675 }
00676 
00677 template <typename T>
00678 void KConfigGroup::writeListCheck( const char* key, const QList<T>& list,
00679                                    WriteConfigFlags pFlags )
00680 {
00681   ConversionCheck::to_QVariant<T>();
00682   ConversionCheck::to_QString<T>();
00683   QVariantList data;
00684   Q_FOREACH(const T &value, list)
00685     data.append(qVariantFromValue(value));
00686 
00687   writeEntry( key, data, pFlags );
00688 }
00689 
00690 #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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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