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 #ifndef KDE_NO_DEPRECATED 00153 KDE_DEPRECATED void changeGroup(const QString &group); 00154 #endif 00155 00161 #ifndef KDE_NO_DEPRECATED 00162 KDE_DEPRECATED void changeGroup(const char *group); 00163 #endif 00164 00176 void copyTo(KConfigBase *other, WriteConfigFlags pFlags = Normal) const; 00177 00193 void reparent(KConfigBase *parent, WriteConfigFlags pFlags = Normal); 00194 00203 KConfigGroup parent() const; 00204 00208 QStringList groupList() const; 00209 00213 QStringList keyList() const; 00214 00222 void deleteGroup(WriteConfigFlags pFlags = Normal); 00223 using KConfigBase::deleteGroup; 00224 00247 template <typename T> 00248 inline T readEntry(const QString &key, const T &aDefault) const 00249 { return readCheck(key.toUtf8().constData(), aDefault); } 00251 template <typename T> 00252 inline T readEntry(const char *key, const T &aDefault) const 00253 { return readCheck(key, aDefault); } 00254 00264 QVariant readEntry(const QString &key, const QVariant &aDefault) const; 00266 QVariant readEntry(const char *key, const QVariant &aDefault) const; 00267 00279 QString readEntry(const QString &key, const QString &aDefault) const; 00281 QString readEntry(const char *key, const QString &aDefault) const; 00282 00284 QString readEntry(const QString &key, const char *aDefault = 0) const; 00286 QString readEntry(const char *key, const char *aDefault = 0) const; 00287 00296 QVariantList readEntry(const QString &key, const QVariantList &aDefault) const; 00298 QVariantList readEntry(const char *key, const QVariantList &aDefault) const; 00299 00309 QStringList readEntry(const QString &key, const QStringList &aDefault) const; 00311 QStringList readEntry(const char *key, const QStringList &aDefault) const; 00312 00322 template<typename T> 00323 inline QList<T> readEntry(const QString &key, const QList<T> &aDefault) const 00324 { return readListCheck(key.toUtf8().constData(), aDefault); } 00326 template<typename T> 00327 inline QList<T> readEntry(const char *key, const QList<T> &aDefault) const 00328 { return readListCheck(key, aDefault); } 00329 00340 QStringList readXdgListEntry(const QString &pKey, const QStringList &aDefault = QStringList()) const; 00342 QStringList readXdgListEntry(const char *pKey, const QStringList &aDefault = QStringList()) const; 00343 00355 QString readPathEntry(const QString &pKey, const QString &aDefault) const; 00357 QString readPathEntry(const char *key, const QString &aDefault) const; 00358 00370 QStringList readPathEntry(const QString &pKey, const QStringList &aDefault) const; 00372 QStringList readPathEntry(const char *key, const QStringList &aDefault) const; 00373 00383 QString readEntryUntranslated(const QString &pKey, 00384 const QString &aDefault = QString()) const; 00386 QString readEntryUntranslated(const char *key, 00387 const QString &aDefault = QString()) const; 00388 00398 void writeEntry(const QString &key, const QVariant &value, 00399 WriteConfigFlags pFlags = Normal); 00401 void writeEntry(const char *key, const QVariant &value, 00402 WriteConfigFlags pFlags = Normal); 00403 00405 void writeEntry(const QString &key, const QString &value, 00406 WriteConfigFlags pFlags = Normal); 00408 void writeEntry(const char *key, const QString &value, 00409 WriteConfigFlags pFlags = Normal); 00410 00412 void writeEntry(const QString &key, const QByteArray &value, 00413 WriteConfigFlags pFlags = Normal); 00415 void writeEntry(const char *key, const QByteArray &value, 00416 WriteConfigFlags pFlags = Normal); 00417 00419 void writeEntry(const QString &key, const char *value, WriteConfigFlags pFlags = Normal); 00421 void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags = Normal); 00422 00424 template <typename T> 00425 inline void writeEntry(const char *key, const T &value, WriteConfigFlags pFlags = Normal) 00426 { writeCheck( key, value, pFlags ); } 00428 template <typename T> 00429 inline void writeEntry(const QString &key, const T &value, WriteConfigFlags pFlags = Normal) 00430 { writeCheck( key.toUtf8().constData(), value, pFlags ); } 00431 00433 void writeEntry(const QString &key, const QStringList &value, 00434 WriteConfigFlags pFlags = Normal); 00436 void writeEntry(const char *key, const QStringList &value, 00437 WriteConfigFlags pFlags = Normal); 00438 00440 void writeEntry(const QString &key, const QVariantList &value, 00441 WriteConfigFlags pFlags = Normal); 00443 void writeEntry(const char *key, const QVariantList &value, 00444 WriteConfigFlags pFlags = Normal); 00445 00447 template <typename T> 00448 inline void writeEntry(const QString &key, const QList<T> &value, WriteConfigFlags pFlags = Normal) 00449 { writeListCheck( key.toUtf8().constData(), value, pFlags ); } 00451 template <typename T> 00452 inline void writeEntry(const char *key, const QList<T> &value, WriteConfigFlags pFlags = Normal) 00453 { writeListCheck( key, value, pFlags ); } 00454 00465 void writeXdgListEntry(const QString &pKey, const QStringList &value, 00466 WriteConfigFlags pFlags = Normal); 00468 void writeXdgListEntry(const char *pKey, const QStringList &value, 00469 WriteConfigFlags pFlags = Normal); 00470 00484 void writePathEntry(const QString &pKey, const QString &path, 00485 WriteConfigFlags pFlags = Normal); 00487 void writePathEntry(const char *pKey, const QString &path, 00488 WriteConfigFlags pFlags = Normal); 00489 00503 void writePathEntry(const QString &pKey, const QStringList &value, 00504 WriteConfigFlags pFlags = Normal); 00506 void writePathEntry(const char *pKey, const QStringList &value, 00507 WriteConfigFlags pFlags = Normal); 00508 00519 void deleteEntry(const QString &pKey, WriteConfigFlags pFlags = Normal); 00521 void deleteEntry(const char *pKey, WriteConfigFlags pFlags = Normal); 00522 00538 bool hasKey(const QString &key) const; 00540 bool hasKey(const char *key) const; 00541 00547 bool isImmutable() const; 00548 00559 bool isEntryImmutable(const QString &key) const; 00561 bool isEntryImmutable(const char *key) const; 00562 00579 void revertToDefault(const QString &key); 00581 void revertToDefault(const char* key); 00582 00610 bool hasDefault(const QString &key) const; 00612 bool hasDefault(const char *key) const; 00613 00622 QMap<QString, QString> entryMap() const; 00623 00624 protected: 00625 bool hasGroupImpl(const QByteArray &group) const; 00626 KConfigGroup groupImpl(const QByteArray &b); 00627 const KConfigGroup groupImpl(const QByteArray &b) const; 00628 void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags); 00629 bool isGroupImmutableImpl(const QByteArray &aGroup) const; 00630 00631 private: 00632 QExplicitlySharedDataPointer<KConfigGroupPrivate> d; 00633 00634 template<typename T> 00635 inline T readCheck(const char *key, const T &defaultValue) const; 00636 00637 template<typename T> 00638 inline QList<T> readListCheck(const char *key, const QList<T> &defaultValue) const; 00639 00640 template<typename T> 00641 inline void writeCheck(const char *key, const T &value, WriteConfigFlags pFlags); 00642 00643 template<typename T> 00644 inline void writeListCheck(const char *key, const QList<T> &value, WriteConfigFlags pFlags); 00645 00646 friend class KConfigGroupPrivate; 00647 00657 static QVariant convertToQVariant(const char *pKey, const QByteArray &value, const QVariant &aDefault); 00658 friend class KServicePrivate; // XXX yeah, ugly^5 00659 }; 00660 00661 #define KCONFIGGROUP_ENUMERATOR_ERROR(ENUM) \ 00662 "The Qt MetaObject system does not seem to know about \"" ENUM \ 00663 "\" please use Q_ENUMS or Q_FLAGS to register it." 00664 00676 #define KCONFIGGROUP_DECLARE_ENUM_QOBJECT(Class, Enum) \ 00677 inline Class::Enum readEntry(const KConfigGroup& group, const char* key, const Class::Enum& def) \ 00678 { \ 00679 const QMetaObject* M_obj = &Class::staticMetaObject; \ 00680 const int M_index = M_obj->indexOfEnumerator(#Enum); \ 00681 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl; \ 00682 const QMetaEnum M_enum = M_obj->enumerator(M_index); \ 00683 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKey(def)));\ 00684 return static_cast<Class::Enum>(M_enum.keyToValue(M_data.constData())); \ 00685 } \ 00686 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Enum& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\ 00687 { \ 00688 const QMetaObject* M_obj = &Class::staticMetaObject; \ 00689 const int M_index = M_obj->indexOfEnumerator(#Enum); \ 00690 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Enum) << endl; \ 00691 const QMetaEnum M_enum = M_obj->enumerator(M_index); \ 00692 group.writeEntry(key, QByteArray(M_enum.valueToKey(value)), flags); \ 00693 } 00694 00699 #define KCONFIGGROUP_DECLARE_FLAGS_QOBJECT(Class, Flags) \ 00700 inline Class::Flags readEntry(const KConfigGroup& group, const char* key, const Class::Flags& def) \ 00701 { \ 00702 const QMetaObject* M_obj = &Class::staticMetaObject; \ 00703 const int M_index = M_obj->indexOfEnumerator(#Flags); \ 00704 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl; \ 00705 const QMetaEnum M_enum = M_obj->enumerator(M_index); \ 00706 const QByteArray M_data = group.readEntry(key, QByteArray(M_enum.valueToKeys(def)));\ 00707 return static_cast<Class::Flags>(M_enum.keysToValue(M_data.constData())); \ 00708 } \ 00709 inline void writeEntry(KConfigGroup& group, const char* key, const Class::Flags& value, KConfigBase::WriteConfigFlags flags = KConfigBase::Normal)\ 00710 { \ 00711 const QMetaObject* M_obj = &Class::staticMetaObject; \ 00712 const int M_index = M_obj->indexOfEnumerator(#Flags); \ 00713 kFatal(M_index == -1) << KCONFIGGROUP_ENUMERATOR_ERROR(#Flags) << endl; \ 00714 const QMetaEnum M_enum = M_obj->enumerator(M_index); \ 00715 group.writeEntry(key, QByteArray(M_enum.valueToKeys(value)), flags); \ 00716 } 00717 00718 #include "conversion_check.h" 00719 00720 template <typename T> 00721 T KConfigGroup::readCheck(const char *key, const T &defaultValue) const 00722 { 00723 ConversionCheck::to_QVariant<T>(); 00724 return qvariant_cast<T>(readEntry(key, qVariantFromValue(defaultValue))); 00725 } 00726 00727 template <typename T> 00728 QList<T> KConfigGroup::readListCheck(const char *key, const QList<T> &defaultValue) const 00729 { 00730 ConversionCheck::to_QVariant<T>(); 00731 ConversionCheck::to_QString<T>(); 00732 00733 QVariantList data; 00734 00735 Q_FOREACH(const T& value, defaultValue) 00736 data.append(qVariantFromValue(value)); 00737 00738 QList<T> list; 00739 Q_FOREACH (const QVariant &value, readEntry<QVariantList>(key, data)) { 00740 Q_ASSERT(qVariantCanConvert<T>(value)); 00741 list.append(qvariant_cast<T>(value)); 00742 } 00743 00744 return list; 00745 } 00746 00747 template <typename T> 00748 void KConfigGroup::writeCheck(const char *key, const T &value, 00749 WriteConfigFlags pFlags) 00750 { 00751 ConversionCheck::to_QVariant<T>(); 00752 writeEntry(key, qVariantFromValue(value), pFlags); 00753 } 00754 00755 template <typename T> 00756 void KConfigGroup::writeListCheck(const char *key, const QList<T> &list, 00757 WriteConfigFlags pFlags) 00758 { 00759 ConversionCheck::to_QVariant<T>(); 00760 ConversionCheck::to_QString<T>(); 00761 QVariantList data; 00762 Q_FOREACH(const T &value, list) { 00763 data.append(qVariantFromValue(value)); 00764 } 00765 00766 writeEntry(key, data, pFlags); 00767 } 00768 00769 #endif // KCONFIGGROUP_H
KDE 4.6 API Reference