diff --git a/src/core/kcoreconfigskeleton.cpp b/src/core/kcoreconfigskeleton.cpp index 7566301..be2fe27 100644 --- a/src/core/kcoreconfigskeleton.cpp +++ b/src/core/kcoreconfigskeleton.cpp @@ -1,1599 +1,1617 @@ /* This file is part of KOrganizer. Copyright (c) 2000,2001 Cornelius Schumacher Copyright (c) 2003 Waldo Bastian This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kcoreconfigskeleton.h" #include "kcoreconfigskeleton_p.h" #include static QString obscuredString(const QString &str) { QString result; const QChar *unicode = str.unicode(); for (int i = 0; i < str.length(); ++i) // yes, no typo. can't encode ' ' or '!' because // they're the unicode BOM. stupid scrambling. stupid. result += (unicode[ i ].unicode() <= 0x21) ? unicode[ i ] : QChar(0x1001F - unicode[ i ].unicode()); return result; } KConfigSkeletonItemPrivate::~KConfigSkeletonItemPrivate() = default; KConfigSkeletonItem::KConfigSkeletonItem(const QString &_group, const QString &_key) : mGroup(_group) , mKey(_key) , d_ptr(new KConfigSkeletonItemPrivate) { } KConfigSkeletonItem::KConfigSkeletonItem(KConfigSkeletonItemPrivate &dd, const QString &_group, const QString &_key) : mGroup(_group) , mKey(_key) , d_ptr(&dd) { } KConfigSkeletonItem::~KConfigSkeletonItem() { delete d_ptr; } void KConfigSkeletonItem::setGroup(const QString &_group) { mGroup = _group; } QString KConfigSkeletonItem::group() const { return mGroup; } void KConfigSkeletonItem::setKey(const QString &_key) { mKey = _key; } QString KConfigSkeletonItem::key() const { return mKey; } void KConfigSkeletonItem::setName(const QString &_name) { mName = _name; } QString KConfigSkeletonItem::name() const { return mName; } void KConfigSkeletonItem::setLabel(const QString &l) { Q_D(KConfigSkeletonItem); d->mLabel = l; } QString KConfigSkeletonItem::label() const { Q_D(const KConfigSkeletonItem); return d->mLabel; } void KConfigSkeletonItem::setToolTip(const QString &t) { Q_D(KConfigSkeletonItem); d->mToolTip = t; } QString KConfigSkeletonItem::toolTip() const { Q_D(const KConfigSkeletonItem); return d->mToolTip; } void KConfigSkeletonItem::setWhatsThis(const QString &w) { Q_D(KConfigSkeletonItem); d->mWhatsThis = w; } QString KConfigSkeletonItem::whatsThis() const { Q_D(const KConfigSkeletonItem); return d->mWhatsThis; } void KConfigSkeletonItem::setWriteFlags(KConfigBase::WriteConfigFlags flags) { Q_D(KConfigSkeletonItem); d->mWriteFlags = flags; } KConfigBase::WriteConfigFlags KConfigSkeletonItem::writeFlags() const { Q_D(const KConfigSkeletonItem); return d->mWriteFlags; } QVariant KConfigSkeletonItem::minValue() const { return QVariant(); } QVariant KConfigSkeletonItem::maxValue() const { return QVariant(); } bool KConfigSkeletonItem::isImmutable() const { Q_D(const KConfigSkeletonItem); return d->mIsImmutable; } bool KConfigSkeletonItem::isDefault() const { Q_D(const KConfigSkeletonItem); return d->mIsDefaultImpl(); } bool KConfigSkeletonItem::isSaveNeeded() const { Q_D(const KConfigSkeletonItem); return d->mIsSaveNeededImpl(); } void KConfigSkeletonItem::readImmutability(const KConfigGroup &group) { Q_D(KConfigSkeletonItem); d->mIsImmutable = group.isEntryImmutable(mKey); } void KConfigSkeletonItem::setIsDefaultImpl(const std::function &impl) { Q_D(KConfigSkeletonItem); d->mIsDefaultImpl = impl; } void KConfigSkeletonItem::setIsSaveNeededImpl(const std::function &impl) { Q_D(KConfigSkeletonItem); d->mIsSaveNeededImpl = impl; } KPropertySkeletonItem::KPropertySkeletonItem(QObject *object, const QByteArray &propertyName, const QVariant &defaultValue) : KConfigSkeletonItem(*new KPropertySkeletonItemPrivate(object, propertyName, defaultValue), {}, {}) { setIsDefaultImpl([this] { Q_D(const KPropertySkeletonItem); return d->mReference == d->mDefaultValue; }); setIsSaveNeededImpl([this] { Q_D(const KPropertySkeletonItem); return d->mReference != d->mLoadedValue; }); } QVariant KPropertySkeletonItem::property() const { Q_D(const KPropertySkeletonItem); return d->mReference; } void KPropertySkeletonItem::setProperty(const QVariant &p) { Q_D(KPropertySkeletonItem); + if (d->mReference == p) { + return; + } d->mReference = p; + if (d->mNotifyFunction) { + d->mNotifyFunction(); + } } bool KPropertySkeletonItem::isEqual(const QVariant &p) const { Q_D(const KPropertySkeletonItem); return d->mReference == p; } void KPropertySkeletonItem::readConfig(KConfig *) { Q_D(KPropertySkeletonItem); - d->mReference = d->mObject->property(d->mPropertyName.constData()); + setProperty(d->mObject->property(d->mPropertyName.constData())); d->mLoadedValue = d->mReference; } void KPropertySkeletonItem::writeConfig(KConfig *) { Q_D(KPropertySkeletonItem); d->mObject->setProperty(d->mPropertyName.constData(), d->mReference); d->mLoadedValue = d->mReference; } void KPropertySkeletonItem::readDefault(KConfig *) { Q_D(KPropertySkeletonItem); - d->mReference = d->mConstDefaultValue; + setProperty(d->mConstDefaultValue); } void KPropertySkeletonItem::setDefault() { Q_D(KPropertySkeletonItem); - d->mReference = d->mDefaultValue; + setProperty(d->mDefaultValue); } void KPropertySkeletonItem::swapDefault() { Q_D(KPropertySkeletonItem); + if (d->mReference == d->mDefaultValue) { + return; + } std::swap(d->mReference, d->mDefaultValue); + if (d->mNotifyFunction) { + d->mNotifyFunction(); + } +} + +void KPropertySkeletonItem::setNotifyFunction(const std::function &impl) +{ + Q_D(KPropertySkeletonItem); + d->mNotifyFunction = impl; } KCoreConfigSkeleton::ItemString::ItemString(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue, Type type) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue), mType(type) { } void KCoreConfigSkeleton::ItemString::writeConfig(KConfig *config) { if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else if (mType == Path) { cg.writePathEntry(mKey, mReference, writeFlags()); } else if (mType == Password) { cg.writeEntry(mKey, obscuredString(mReference), writeFlags()); } else { cg.writeEntry(mKey, mReference, writeFlags()); } mLoadedValue = mReference; } } void KCoreConfigSkeleton::ItemString::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); if (mType == Path) { mReference = cg.readPathEntry(mKey, mDefault); } else if (mType == Password) { QString val = cg.readEntry(mKey, obscuredString(mDefault)); mReference = obscuredString(val); } else { mReference = cg.readEntry(mKey, mDefault); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemString::setProperty(const QVariant &p) { mReference = p.toString(); } bool KCoreConfigSkeleton::ItemString::isEqual(const QVariant &v) const { return mReference == v.toString(); } QVariant KCoreConfigSkeleton::ItemString::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemPassword::ItemPassword(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue) : ItemString(_group, _key, reference, defaultValue, Password) { } KCoreConfigSkeleton::ItemPath::ItemPath(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue) : ItemString(_group, _key, reference, defaultValue, Path) { } KCoreConfigSkeleton::ItemUrl::ItemUrl(const QString &_group, const QString &_key, QUrl &reference, const QUrl &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemUrl::writeConfig(KConfig *config) { if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else { cg.writeEntry(mKey, mReference.toString(), writeFlags()); } mLoadedValue = mReference; } } void KCoreConfigSkeleton::ItemUrl::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = QUrl(cg.readEntry(mKey, mDefault.toString())); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemUrl::setProperty(const QVariant &p) { mReference = qvariant_cast(p); } bool KCoreConfigSkeleton::ItemUrl::isEqual(const QVariant &v) const { return mReference == qvariant_cast(v); } QVariant KCoreConfigSkeleton::ItemUrl::property() const { return QVariant::fromValue(mReference); } KCoreConfigSkeleton::ItemProperty::ItemProperty(const QString &_group, const QString &_key, QVariant &reference, const QVariant &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemProperty::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemProperty::setProperty(const QVariant &p) { mReference = p; } bool KCoreConfigSkeleton::ItemProperty::isEqual(const QVariant &v) const { //this might cause problems if the QVariants are not of default types return mReference == v; } QVariant KCoreConfigSkeleton::ItemProperty::property() const { return mReference; } KCoreConfigSkeleton::ItemBool::ItemBool(const QString &_group, const QString &_key, bool &reference, bool defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemBool::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemBool::setProperty(const QVariant &p) { mReference = p.toBool(); } bool KCoreConfigSkeleton::ItemBool::isEqual(const QVariant &v) const { return mReference == v.toBool(); } QVariant KCoreConfigSkeleton::ItemBool::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemInt::ItemInt(const QString &_group, const QString &_key, qint32 &reference, qint32 defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) , mHasMin(false), mHasMax(false) { } void KCoreConfigSkeleton::ItemInt::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); if (mHasMin) { mReference = qMax(mReference, mMin); } if (mHasMax) { mReference = qMin(mReference, mMax); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemInt::setProperty(const QVariant &p) { mReference = p.toInt(); } bool KCoreConfigSkeleton::ItemInt::isEqual(const QVariant &v) const { return mReference == v.toInt(); } QVariant KCoreConfigSkeleton::ItemInt::property() const { return QVariant(mReference); } QVariant KCoreConfigSkeleton::ItemInt::minValue() const { if (mHasMin) { return QVariant(mMin); } return QVariant(); } QVariant KCoreConfigSkeleton::ItemInt::maxValue() const { if (mHasMax) { return QVariant(mMax); } return QVariant(); } void KCoreConfigSkeleton::ItemInt::setMinValue(qint32 v) { mHasMin = true; mMin = v; } void KCoreConfigSkeleton::ItemInt::setMaxValue(qint32 v) { mHasMax = true; mMax = v; } KCoreConfigSkeleton::ItemLongLong::ItemLongLong(const QString &_group, const QString &_key, qint64 &reference, qint64 defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) , mHasMin(false), mHasMax(false) { } void KCoreConfigSkeleton::ItemLongLong::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); if (mHasMin) { mReference = qMax(mReference, mMin); } if (mHasMax) { mReference = qMin(mReference, mMax); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemLongLong::setProperty(const QVariant &p) { mReference = p.toLongLong(); } bool KCoreConfigSkeleton::ItemLongLong::isEqual(const QVariant &v) const { return mReference == v.toLongLong(); } QVariant KCoreConfigSkeleton::ItemLongLong::property() const { return QVariant(mReference); } QVariant KCoreConfigSkeleton::ItemLongLong::minValue() const { if (mHasMin) { return QVariant(mMin); } return QVariant(); } QVariant KCoreConfigSkeleton::ItemLongLong::maxValue() const { if (mHasMax) { return QVariant(mMax); } return QVariant(); } void KCoreConfigSkeleton::ItemLongLong::setMinValue(qint64 v) { mHasMin = true; mMin = v; } void KCoreConfigSkeleton::ItemLongLong::setMaxValue(qint64 v) { mHasMax = true; mMax = v; } KCoreConfigSkeleton::ItemEnum::ItemEnum(const QString &_group, const QString &_key, qint32 &reference, const QList &choices, qint32 defaultValue) : ItemInt(_group, _key, reference, defaultValue), mChoices(choices) { } void KCoreConfigSkeleton::ItemEnum::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); if (!cg.hasKey(mKey)) { mReference = mDefault; } else { int i = 0; mReference = -1; QString tmp = cg.readEntry(mKey, QString()).toLower(); for (QList::ConstIterator it = mChoices.constBegin(); it != mChoices.constEnd(); ++it, ++i) { if ((*it).name.toLower() == tmp) { mReference = i; break; } } if (mReference == -1) { mReference = cg.readEntry(mKey, mDefault); } } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemEnum::writeConfig(KConfig *config) { if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else if ((mReference >= 0) && (mReference < mChoices.count())) { cg.writeEntry(mKey, mChoices[mReference].name, writeFlags()); } else { cg.writeEntry(mKey, mReference, writeFlags()); } mLoadedValue = mReference; } } QList KCoreConfigSkeleton::ItemEnum::choices() const { return mChoices; } QList KCoreConfigSkeleton::ItemEnum::choices2() const { return mChoices; } KCoreConfigSkeleton::ItemUInt::ItemUInt(const QString &_group, const QString &_key, quint32 &reference, quint32 defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) , mHasMin(false), mHasMax(false) { } void KCoreConfigSkeleton::ItemUInt::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); if (mHasMin) { mReference = qMax(mReference, mMin); } if (mHasMax) { mReference = qMin(mReference, mMax); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemUInt::setProperty(const QVariant &p) { mReference = p.toUInt(); } bool KCoreConfigSkeleton::ItemUInt::isEqual(const QVariant &v) const { return mReference == v.toUInt(); } QVariant KCoreConfigSkeleton::ItemUInt::property() const { return QVariant(mReference); } QVariant KCoreConfigSkeleton::ItemUInt::minValue() const { if (mHasMin) { return QVariant(mMin); } return QVariant(); } QVariant KCoreConfigSkeleton::ItemUInt::maxValue() const { if (mHasMax) { return QVariant(mMax); } return QVariant(); } void KCoreConfigSkeleton::ItemUInt::setMinValue(quint32 v) { mHasMin = true; mMin = v; } void KCoreConfigSkeleton::ItemUInt::setMaxValue(quint32 v) { mHasMax = true; mMax = v; } KCoreConfigSkeleton::ItemULongLong::ItemULongLong(const QString &_group, const QString &_key, quint64 &reference, quint64 defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) , mHasMin(false), mHasMax(false) { } void KCoreConfigSkeleton::ItemULongLong::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); if (mHasMin) { mReference = qMax(mReference, mMin); } if (mHasMax) { mReference = qMin(mReference, mMax); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemULongLong::setProperty(const QVariant &p) { mReference = p.toULongLong(); } bool KCoreConfigSkeleton::ItemULongLong::isEqual(const QVariant &v) const { return mReference == v.toULongLong(); } QVariant KCoreConfigSkeleton::ItemULongLong::property() const { return QVariant(mReference); } QVariant KCoreConfigSkeleton::ItemULongLong::minValue() const { if (mHasMin) { return QVariant(mMin); } return QVariant(); } QVariant KCoreConfigSkeleton::ItemULongLong::maxValue() const { if (mHasMax) { return QVariant(mMax); } return QVariant(); } void KCoreConfigSkeleton::ItemULongLong::setMinValue(quint64 v) { mHasMin = true; mMin = v; } void KCoreConfigSkeleton::ItemULongLong::setMaxValue(quint64 v) { mHasMax = true; mMax = v; } KCoreConfigSkeleton::ItemDouble::ItemDouble(const QString &_group, const QString &_key, double &reference, double defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) , mHasMin(false), mHasMax(false) { } void KCoreConfigSkeleton::ItemDouble::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); if (mHasMin) { mReference = qMax(mReference, mMin); } if (mHasMax) { mReference = qMin(mReference, mMax); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemDouble::setProperty(const QVariant &p) { mReference = p.toDouble(); } bool KCoreConfigSkeleton::ItemDouble::isEqual(const QVariant &v) const { return mReference == v.toDouble(); } QVariant KCoreConfigSkeleton::ItemDouble::property() const { return QVariant(mReference); } QVariant KCoreConfigSkeleton::ItemDouble::minValue() const { if (mHasMin) { return QVariant(mMin); } return QVariant(); } QVariant KCoreConfigSkeleton::ItemDouble::maxValue() const { if (mHasMax) { return QVariant(mMax); } return QVariant(); } void KCoreConfigSkeleton::ItemDouble::setMinValue(double v) { mHasMin = true; mMin = v; } void KCoreConfigSkeleton::ItemDouble::setMaxValue(double v) { mHasMax = true; mMax = v; } KCoreConfigSkeleton::ItemRect::ItemRect(const QString &_group, const QString &_key, QRect &reference, const QRect &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemRect::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemRect::setProperty(const QVariant &p) { mReference = p.toRect(); } bool KCoreConfigSkeleton::ItemRect::isEqual(const QVariant &v) const { return mReference == v.toRect(); } QVariant KCoreConfigSkeleton::ItemRect::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemPoint::ItemPoint(const QString &_group, const QString &_key, QPoint &reference, const QPoint &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemPoint::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemPoint::setProperty(const QVariant &p) { mReference = p.toPoint(); } bool KCoreConfigSkeleton::ItemPoint::isEqual(const QVariant &v) const { return mReference == v.toPoint(); } QVariant KCoreConfigSkeleton::ItemPoint::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemSize::ItemSize(const QString &_group, const QString &_key, QSize &reference, const QSize &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemSize::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemSize::setProperty(const QVariant &p) { mReference = p.toSize(); } bool KCoreConfigSkeleton::ItemSize::isEqual(const QVariant &v) const { return mReference == v.toSize(); } QVariant KCoreConfigSkeleton::ItemSize::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemDateTime::ItemDateTime(const QString &_group, const QString &_key, QDateTime &reference, const QDateTime &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemDateTime::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); mReference = cg.readEntry(mKey, mDefault); mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemDateTime::setProperty(const QVariant &p) { mReference = p.toDateTime(); } bool KCoreConfigSkeleton::ItemDateTime::isEqual(const QVariant &v) const { return mReference == v.toDateTime(); } QVariant KCoreConfigSkeleton::ItemDateTime::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemStringList::ItemStringList(const QString &_group, const QString &_key, QStringList &reference, const QStringList &defaultValue) : KConfigSkeletonGenericItem(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemStringList::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); if (!cg.hasKey(mKey)) { mReference = mDefault; } else { mReference = cg.readEntry(mKey, mDefault); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemStringList::setProperty(const QVariant &p) { mReference = p.toStringList(); } bool KCoreConfigSkeleton::ItemStringList::isEqual(const QVariant &v) const { return mReference == v.toStringList(); } QVariant KCoreConfigSkeleton::ItemStringList::property() const { return QVariant(mReference); } KCoreConfigSkeleton::ItemPathList::ItemPathList(const QString &_group, const QString &_key, QStringList &reference, const QStringList &defaultValue) : ItemStringList(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemPathList::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); if (!cg.hasKey(mKey)) { mReference = mDefault; } else { mReference = cg.readPathEntry(mKey, QStringList()); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemPathList::writeConfig(KConfig *config) { if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else { QStringList sl = mReference; cg.writePathEntry(mKey, sl, writeFlags()); } mLoadedValue = mReference; } } KCoreConfigSkeleton::ItemUrlList::ItemUrlList(const QString &_group, const QString &_key, QList &reference, const QList &defaultValue) : KConfigSkeletonGenericItem >(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemUrlList::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); if (!cg.hasKey(mKey)) { mReference = mDefault; } else { QStringList strList; for (const QUrl &url : qAsConst(mDefault)) { strList.append(url.toString()); } mReference.clear(); const QStringList readList = cg.readEntry(mKey, strList); for (const QString &str : readList) { mReference.append(QUrl(str)); } } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemUrlList::writeConfig(KConfig *config) { if (mReference != mLoadedValue) { // WABA: Is this test needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else { QStringList strList; for (const QUrl &url : qAsConst(mReference)) { strList.append(url.toString()); } cg.writeEntry(mKey, strList, writeFlags()); } mLoadedValue = mReference; } } void KCoreConfigSkeleton::ItemUrlList::setProperty(const QVariant &p) { mReference = qvariant_cast >(p); } bool KCoreConfigSkeleton::ItemUrlList::isEqual(const QVariant &v) const { return mReference == qvariant_cast >(v); } QVariant KCoreConfigSkeleton::ItemUrlList::property() const { return QVariant::fromValue >(mReference); } KCoreConfigSkeleton::ItemIntList::ItemIntList(const QString &_group, const QString &_key, QList &reference, const QList &defaultValue) : KConfigSkeletonGenericItem >(_group, _key, reference, defaultValue) { } void KCoreConfigSkeleton::ItemIntList::readConfig(KConfig *config) { KConfigGroup cg(config, mGroup); if (!cg.hasKey(mKey)) { mReference = mDefault; } else { mReference = cg.readEntry(mKey, mDefault); } mLoadedValue = mReference; readImmutability(cg); } void KCoreConfigSkeleton::ItemIntList::setProperty(const QVariant &p) { mReference = qvariant_cast< QList >(p); } bool KCoreConfigSkeleton::ItemIntList::isEqual(const QVariant &v) const { return mReference == qvariant_cast< QList >(v); } QVariant KCoreConfigSkeleton::ItemIntList::property() const { return QVariant::fromValue< QList >(mReference); } //static int kCoreConfigSkeletionDebugArea() { static int s_area = KDebug::registerArea("kdecore (KConfigSkeleton)"); return s_area; } KCoreConfigSkeleton::KCoreConfigSkeleton(const QString &configname, QObject *parent) : QObject(parent), d(new KCoreConfigSkeletonPrivate) { //qDebug() << "Creating KCoreConfigSkeleton (" << (void *)this << ")"; d->mConfig = KSharedConfig::openConfig(configname, KConfig::FullConfig); } KCoreConfigSkeleton::KCoreConfigSkeleton(KSharedConfig::Ptr pConfig, QObject *parent) : QObject(parent), d(new KCoreConfigSkeletonPrivate) { //qDebug() << "Creating KCoreConfigSkeleton (" << (void *)this << ")"; d->mConfig = std::move(pConfig); } KCoreConfigSkeleton::~KCoreConfigSkeleton() { delete d; } void KCoreConfigSkeleton::setCurrentGroup(const QString &group) { d->mCurrentGroup = group; } QString KCoreConfigSkeleton::currentGroup() const { return d->mCurrentGroup; } KConfig *KCoreConfigSkeleton::config() { return d->mConfig.data(); } const KConfig *KCoreConfigSkeleton::config() const { return d->mConfig.data(); } KSharedConfig::Ptr KCoreConfigSkeleton::sharedConfig() const { return d->mConfig; } void KCoreConfigSkeleton::setSharedConfig(KSharedConfig::Ptr pConfig) { d->mConfig = std::move(pConfig); } KConfigSkeletonItem::List KCoreConfigSkeleton::items() const { return d->mItems; } bool KCoreConfigSkeleton::useDefaults(bool b) { if (b == d->mUseDefaults) { return d->mUseDefaults; } d->mUseDefaults = b; KConfigSkeletonItem::List::ConstIterator it; for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { (*it)->swapDefault(); } usrUseDefaults(b); return !d->mUseDefaults; } void KCoreConfigSkeleton::setDefaults() { KConfigSkeletonItem::List::ConstIterator it; for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { (*it)->setDefault(); } usrSetDefaults(); } void KCoreConfigSkeleton::load() { d->mConfig->reparseConfiguration(); read(); } void KCoreConfigSkeleton::read() { KConfigSkeletonItem::List::ConstIterator it; for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { (*it)->readConfig(d->mConfig.data()); } usrRead(); } bool KCoreConfigSkeleton::isDefaults() const { KConfigSkeletonItem::List::ConstIterator it; for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { if (!(*it)->isDefault()) { return false; } } return true; } bool KCoreConfigSkeleton::isSaveNeeded() const { KConfigSkeletonItem::List::ConstIterator it; for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { if ((*it)->isSaveNeeded()) { return true; } } return false; } bool KCoreConfigSkeleton::save() { //qDebug(); KConfigSkeletonItem::List::ConstIterator it; for (it = d->mItems.constBegin(); it != d->mItems.constEnd(); ++it) { (*it)->writeConfig(d->mConfig.data()); } if (!usrSave()) { return false; } if (d->mConfig->isDirty()) { if (!d->mConfig->sync()) { return false; } emit configChanged(); } return true; } bool KCoreConfigSkeleton::usrUseDefaults(bool) { return false; } void KCoreConfigSkeleton::usrSetDefaults() { } void KCoreConfigSkeleton::usrRead() { #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) usrReadConfig(); #endif } #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) void KCoreConfigSkeleton::usrReadConfig() { } #endif bool KCoreConfigSkeleton::usrSave() { #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) return usrWriteConfig(); #else return true; #endif } #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) bool KCoreConfigSkeleton::usrWriteConfig() { return true; } #endif void KCoreConfigSkeleton::addItem(KConfigSkeletonItem *item, const QString &name) { if (d->mItems.contains(item)) { if (item->name() == name || (name.isEmpty() && item->name() == item->key())) { // nothing to do -> it is already in our collection // and the name isn't changing return; } d->mItemDict.remove(item->name()); } else { d->mItems.append(item); } item->setName(name.isEmpty() ? item->key() : name); d->mItemDict.insert(item->name(), item); item->readDefault(d->mConfig.data()); item->readConfig(d->mConfig.data()); } void KCoreConfigSkeleton::removeItem(const QString &name) { KConfigSkeletonItem *item = d->mItemDict.value(name); if (item) { d->mItems.removeAll(item); d->mItemDict.remove(item->name()); delete item; } } void KCoreConfigSkeleton::clearItems() { KConfigSkeletonItem::List items = d->mItems; d->mItems.clear(); d->mItemDict.clear(); qDeleteAll(items); } KCoreConfigSkeleton::ItemString *KCoreConfigSkeleton::addItemString(const QString &name, QString &reference, const QString &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemString *item; item = new KCoreConfigSkeleton::ItemString(d->mCurrentGroup, key.isEmpty() ? name : key, reference, defaultValue, KCoreConfigSkeleton::ItemString::Normal); addItem(item, name); return item; } KCoreConfigSkeleton::ItemPassword *KCoreConfigSkeleton::addItemPassword(const QString &name, QString &reference, const QString &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemPassword *item; item = new KCoreConfigSkeleton::ItemPassword(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemPath *KCoreConfigSkeleton::addItemPath(const QString &name, QString &reference, const QString &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemPath *item; item = new KCoreConfigSkeleton::ItemPath(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemProperty *KCoreConfigSkeleton::addItemProperty(const QString &name, QVariant &reference, const QVariant &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemProperty *item; item = new KCoreConfigSkeleton::ItemProperty(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemBool *KCoreConfigSkeleton::addItemBool(const QString &name, bool &reference, bool defaultValue, const QString &key) { KCoreConfigSkeleton::ItemBool *item; item = new KCoreConfigSkeleton::ItemBool(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemInt *KCoreConfigSkeleton::addItemInt(const QString &name, qint32 &reference, qint32 defaultValue, const QString &key) { KCoreConfigSkeleton::ItemInt *item; item = new KCoreConfigSkeleton::ItemInt(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemUInt *KCoreConfigSkeleton::addItemUInt(const QString &name, quint32 &reference, quint32 defaultValue, const QString &key) { KCoreConfigSkeleton::ItemUInt *item; item = new KCoreConfigSkeleton::ItemUInt(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemLongLong *KCoreConfigSkeleton::addItemLongLong(const QString &name, qint64 &reference, qint64 defaultValue, const QString &key) { KCoreConfigSkeleton::ItemLongLong *item; item = new KCoreConfigSkeleton::ItemLongLong(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) KCoreConfigSkeleton::ItemLongLong *KCoreConfigSkeleton::addItemInt64( const QString &name, qint64 &reference, qint64 defaultValue, const QString &key) { return addItemLongLong(name, reference, defaultValue, key); } #endif KCoreConfigSkeleton::ItemULongLong *KCoreConfigSkeleton::addItemULongLong(const QString &name, quint64 &reference, quint64 defaultValue, const QString &key) { KCoreConfigSkeleton::ItemULongLong *item; item = new KCoreConfigSkeleton::ItemULongLong(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) KCoreConfigSkeleton::ItemULongLong *KCoreConfigSkeleton::addItemUInt64( const QString &name, quint64 &reference, quint64 defaultValue, const QString &key) { return addItemULongLong(name, reference, defaultValue, key); } #endif KCoreConfigSkeleton::ItemDouble *KCoreConfigSkeleton::addItemDouble(const QString &name, double &reference, double defaultValue, const QString &key) { KCoreConfigSkeleton::ItemDouble *item; item = new KCoreConfigSkeleton::ItemDouble(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemRect *KCoreConfigSkeleton::addItemRect(const QString &name, QRect &reference, const QRect &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemRect *item; item = new KCoreConfigSkeleton::ItemRect(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemPoint *KCoreConfigSkeleton::addItemPoint(const QString &name, QPoint &reference, const QPoint &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemPoint *item; item = new KCoreConfigSkeleton::ItemPoint(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemSize *KCoreConfigSkeleton::addItemSize(const QString &name, QSize &reference, const QSize &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemSize *item; item = new KCoreConfigSkeleton::ItemSize(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemDateTime *KCoreConfigSkeleton::addItemDateTime(const QString &name, QDateTime &reference, const QDateTime &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemDateTime *item; item = new KCoreConfigSkeleton::ItemDateTime(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemStringList *KCoreConfigSkeleton::addItemStringList(const QString &name, QStringList &reference, const QStringList &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemStringList *item; item = new KCoreConfigSkeleton::ItemStringList(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } KCoreConfigSkeleton::ItemIntList *KCoreConfigSkeleton::addItemIntList(const QString &name, QList &reference, const QList &defaultValue, const QString &key) { KCoreConfigSkeleton::ItemIntList *item; item = new KCoreConfigSkeleton::ItemIntList(d->mCurrentGroup, key.isNull() ? name : key, reference, defaultValue); addItem(item, name); return item; } bool KCoreConfigSkeleton::isImmutable(const QString &name) const { KConfigSkeletonItem *item = findItem(name); return !item || item->isImmutable(); } KConfigSkeletonItem *KCoreConfigSkeleton::findItem(const QString &name) const { return d->mItemDict.value(name); } KConfigCompilerSignallingItem::KConfigCompilerSignallingItem(KConfigSkeletonItem* item, QObject* object, KConfigCompilerSignallingItem::NotifyFunction targetFunction, quint64 userData) : KConfigSkeletonItem(item->group(), item->key()), mItem(item), mTargetFunction(targetFunction), mObject(object), mUserData(userData) { Q_ASSERT(mTargetFunction); Q_ASSERT(mItem); Q_ASSERT(mObject); setIsDefaultImpl([this] { return mItem->isDefault(); }); setIsSaveNeededImpl([this] { return mItem->isSaveNeeded(); }); } KConfigCompilerSignallingItem::~KConfigCompilerSignallingItem() { } bool KConfigCompilerSignallingItem::isEqual(const QVariant& p) const { return mItem->isEqual(p); } QVariant KConfigCompilerSignallingItem::property() const { return mItem->property(); } void KConfigCompilerSignallingItem::readConfig(KConfig* c) { QVariant oldValue = mItem->property(); mItem->readConfig(c); //readConfig() changes mIsImmutable, update it here as well KConfigGroup cg(c, mGroup ); readImmutability(cg); if (!mItem->isEqual(oldValue)) { invokeNotifyFunction(); } } void KConfigCompilerSignallingItem::readDefault(KConfig* c) { mItem->readDefault(c); //readDefault() changes mIsImmutable, update it here as well KConfigGroup cg(c, mGroup ); readImmutability(cg); } void KConfigCompilerSignallingItem::writeConfig(KConfig* c) { mItem->writeConfig(c); } void KConfigCompilerSignallingItem::setDefault() { QVariant oldValue = mItem->property(); mItem->setDefault(); if (!mItem->isEqual(oldValue)) { invokeNotifyFunction(); } } void KConfigCompilerSignallingItem::setProperty(const QVariant& p) { if (!mItem->isEqual(p)) { mItem->setProperty(p); invokeNotifyFunction(); } } void KConfigCompilerSignallingItem::swapDefault() { QVariant oldValue = mItem->property(); mItem->swapDefault(); if (!mItem->isEqual(oldValue)) { invokeNotifyFunction(); } } void KConfigCompilerSignallingItem::setWriteFlags(KConfigBase::WriteConfigFlags flags) { mItem->setWriteFlags(flags); } KConfigBase::WriteConfigFlags KConfigCompilerSignallingItem::writeFlags() const { return mItem->writeFlags(); } diff --git a/src/core/kcoreconfigskeleton.h b/src/core/kcoreconfigskeleton.h index a6f58eb..7abc405 100644 --- a/src/core/kcoreconfigskeleton.h +++ b/src/core/kcoreconfigskeleton.h @@ -1,1608 +1,1614 @@ /* * This file is part of KDE. * * Copyright (c) 2001,2002,2003 Cornelius Schumacher * Copyright (c) 2003 Waldo Bastian * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KCORECONFIGSKELETON_H #define KCORECONFIGSKELETON_H #include #include #include #include #include #include #include #include #include class KCoreConfigSkeletonPrivate; class KConfigSkeletonItemPrivate; /** * \class KConfigSkeletonItem kcoreconfigskeleton.h * * @short Class for storing a preferences setting * @author Cornelius Schumacher * @see KCoreConfigSkeleton * * This class represents one preferences setting as used by @ref KCoreConfigSkeleton. * Subclasses of KConfigSkeletonItem implement storage functions for a certain type of * setting. Normally you don't have to use this class directly. Use the special * addItem() functions of KCoreConfigSkeleton instead. If you subclass this class you will * have to register instances with the function KCoreConfigSkeleton::addItem(). */ class KCONFIGCORE_EXPORT KConfigSkeletonItem { Q_DECLARE_PRIVATE(KConfigSkeletonItem) public: typedef QList < KConfigSkeletonItem * >List; typedef QHash < QString, KConfigSkeletonItem * > Dict; typedef QHash < QString, KConfigSkeletonItem * >::Iterator DictIterator; /** * Constructor. * * @param _group Config file group. * @param _key Config file key. */ KConfigSkeletonItem(const QString &_group, const QString &_key); /** * Destructor. */ virtual ~KConfigSkeletonItem(); /** * Set config file group. */ void setGroup(const QString &_group); /** * Return config file group. */ QString group() const; /** * Set config file key. */ void setKey(const QString &_key); /** * Return config file key. */ QString key() const; /** * Set internal name of entry. */ void setName(const QString &_name); /** * Return internal name of entry. */ QString name() const; /** Set label providing a translated one-line description of the item. */ void setLabel(const QString &l); /** Return label of item. See setLabel(). */ QString label() const; /** Set ToolTip description of item. @since 4.2 */ void setToolTip(const QString &t); /** Return ToolTip description of item. See setToolTip(). @since 4.2 */ QString toolTip() const; /** Set WhatsThis description of item. */ void setWhatsThis(const QString &w); /** Return WhatsThis description of item. See setWhatsThis(). */ QString whatsThis() const; /** The write flags to be used when writing configuration. @since 5.58 */ void setWriteFlags(KConfigBase::WriteConfigFlags flags); /** Return write flags to be used when writing configuration. They should be passed to every call of writeEntry() and revertToDefault(). @since 5.58 */ KConfigBase::WriteConfigFlags writeFlags() const; /** * This function is called by @ref KCoreConfigSkeleton to read the value for this setting * from a config file. */ virtual void readConfig(KConfig *) = 0; /** * This function is called by @ref KCoreConfigSkeleton to write the value of this setting * to a config file. * Make sure to pass writeFlags() to every call of writeEntry() and revertToDefault(). */ virtual void writeConfig(KConfig *) = 0; /** * Read global default value. */ virtual void readDefault(KConfig *) = 0; /** * Set item to @p p */ virtual void setProperty(const QVariant &p) = 0; /** * Check whether the item is equal to p. * * Use this function to compare items that use custom types, * because QVariant::operator== will not work for those. * * @param p QVariant to compare to * @return true if the item is equal to p, false otherwise */ virtual bool isEqual(const QVariant &p) const = 0; /** * Return item as property */ virtual QVariant property() const = 0; /** * Return minimum value of item or invalid if not specified */ virtual QVariant minValue() const; /** * Return maximum value of item or invalid if not specified */ virtual QVariant maxValue() const; /** * Sets the current value to the default value. */ virtual void setDefault() = 0; /** * Exchanges the current value with the default value * Used by KCoreConfigSkeleton::useDefaults(bool); */ virtual void swapDefault() = 0; /** * Return if the entry can be modified. */ bool isImmutable() const; /** * Indicates if the item is set to its default value. * * @since 5.64 */ bool isDefault() const; /** * Indicates if the item has a different value than the * previously loaded value. * * @since 5.64 */ bool isSaveNeeded() const; protected: explicit KConfigSkeletonItem(KConfigSkeletonItemPrivate &dd, const QString &_group, const QString &_key); /** * sets mIsImmutable to true if mKey in config is immutable * @param group KConfigGroup to check if mKey is immutable in */ void readImmutability(const KConfigGroup &group); QString mGroup; ///< The group name for this item QString mKey; ///< The config key for this item QString mName; ///< The name of this item // HACK: Necessary to avoid introducing new virtuals in KConfigSkeletonItem // KF6: Use proper pure virtuals in KConfigSkeletonItem void setIsDefaultImpl(const std::function &impl); void setIsSaveNeededImpl(const std::function &impl); KConfigSkeletonItemPrivate *const d_ptr; }; class KPropertySkeletonItemPrivate; /** * \class KPropertySkeletonItem kcoreconfigskeleton.h * * @short Class for proxying a QObject property as a preferences setting * @author Kevin Ottens * @see KConfigSkeletonItem * * This class represents one preferences setting as used by @ref KCoreConfigSkeleton. * Unlike other @ref KConfigSkeletonItem subclasses, this one won't store the preference * in KConfig but will use a QObject property as storage. * You will have to register instances of this class with the function KCoreConfigSkeleton::addItem(). * * @since 5.65 */ class KCONFIGCORE_EXPORT KPropertySkeletonItem : public KConfigSkeletonItem { Q_DECLARE_PRIVATE(KPropertySkeletonItem) public: /** * Constructor * * @param object The QObject instance which we'll manage the property of * @param propertyName The name of the property in @p object which we'll manage * @param defaultValue The default value of the property */ KPropertySkeletonItem(QObject *object, const QByteArray &propertyName, const QVariant &defaultValue); /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant &) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::readConfig(KConfig *) */ void readConfig(KConfig *) override; /** @copydoc KConfigSkeletonItem::writeConfig(KConfig *) */ void writeConfig(KConfig *) override; /** @copydoc KConfigSkeletonItem::readDefault(KConfig *) */ void readDefault(KConfig *) override; /** @copydoc KConfigSkeletonItem::setDefault() */ void setDefault() override; /** @copydoc KConfigSkeletonItem::swapDefault() */ void swapDefault() override; + + /** + * Set a notify function, it will be invoked when the value of the property changes. + * @since 5.68 + */ + void setNotifyFunction(const std::function &impl); }; /** * \class KConfigSkeletonGenericItem kcoreconfigskeleton.h */ template < typename T > class KConfigSkeletonGenericItem: public KConfigSkeletonItem { public: /** @copydoc KConfigSkeletonItem(const QString&, const QString&) @param reference The initial value to hold in the item @param defaultValue The default value for the item */ KConfigSkeletonGenericItem(const QString &_group, const QString &_key, T &reference, T defaultValue) : KConfigSkeletonItem(_group, _key), mReference(reference), mDefault(defaultValue), mLoadedValue(defaultValue) { setIsDefaultImpl([this] { return mReference == mDefault; }); setIsSaveNeededImpl([this] { return mReference != mLoadedValue; }); } /** * Set value of this KConfigSkeletonItem. */ void setValue(const T &v) { mReference = v; } /** * Return value of this KConfigSkeletonItem. */ T &value() { return mReference; } /** * Return const value of this KConfigSkeletonItem. */ const T &value() const { return mReference; } /** Set default value for this item. */ virtual void setDefaultValue(const T &v) { mDefault = v; } /** Set the value for this item to the default value */ void setDefault() override { mReference = mDefault; } /** @copydoc KConfigSkeletonItem::writeConfig(KConfig *) */ void writeConfig(KConfig *config) override { if (mReference != mLoadedValue) { // Is this needed? KConfigGroup cg(config, mGroup); if ((mDefault == mReference) && !cg.hasDefault(mKey)) { cg.revertToDefault(mKey, writeFlags()); } else { cg.writeEntry(mKey, mReference, writeFlags()); } mLoadedValue = mReference; } } /** @copydoc KConfigSkeletonItem::readDefault(KConfig*) */ void readDefault(KConfig *config) override { config->setReadDefaults(true); readConfig(config); config->setReadDefaults(false); mDefault = mReference; } /** @copydoc KConfigSkeletonItem::swapDefault() */ void swapDefault() override { T tmp = mReference; mReference = mDefault; mDefault = tmp; } protected: T &mReference; ///< Stores the value for this item T mDefault; ///< The default value for this item T mLoadedValue; }; /** * \class KConfigSkeletonChangeNotifyingItem kcoreconfigskeleton.h * * @author Alex Richardson * @see KConfigSkeletonItem * * * This class wraps a @ref KConfigSkeletonItem and invokes a function whenever the value changes. * That function must take one quint64 parameter. Whenever the property value of the wrapped KConfigSkeletonItem * changes this function will be invoked with the stored user data passed in the constructor. * It does not call a function with the new value since this class is designed solely for the kconfig_compiler generated * code and is therefore probably not suited for any other usecases. */ class KCONFIGCORE_EXPORT KConfigCompilerSignallingItem : public KConfigSkeletonItem { public: typedef void (QObject::* NotifyFunction)(quint64 arg); /** * Constructor. * * @param item the KConfigSkeletonItem to wrap * @param targetFunction the method to invoke whenever the value of @p item changes * @param object The object on which the method is invoked. * @param userData This data will be passed to @p targetFunction on every property change */ KConfigCompilerSignallingItem(KConfigSkeletonItem *item, QObject* object, NotifyFunction targetFunction, quint64 userData); ~KConfigCompilerSignallingItem() override; void readConfig(KConfig *) override; void writeConfig(KConfig *) override; void readDefault(KConfig *) override; void setProperty(const QVariant &p) override; bool isEqual(const QVariant &p) const override; QVariant property() const override; void setDefault() override; void swapDefault() override; // shadow the method in KConfigSkeletonItem, which should be fine for autogenerated code // KF6 TODO - fix this void setWriteFlags(KConfigBase::WriteConfigFlags flags); KConfigBase::WriteConfigFlags writeFlags() const; private: inline void invokeNotifyFunction() { // call the pointer to member function using the strange ->* operator (mObject->*mTargetFunction)(mUserData); } private: QScopedPointer mItem; NotifyFunction mTargetFunction; QObject* mObject; quint64 mUserData; }; /** * \class KCoreConfigSkeleton kcoreconfigskeleton.h * * @short Class for handling preferences settings for an application. * @author Cornelius Schumacher * @see KConfigSkeletonItem * * This class provides an interface to preferences settings. Preferences items * can be registered by the addItem() function corresponding to the data type of * the setting. KCoreConfigSkeleton then handles reading and writing of config files and * setting of default values. * * Normally you will subclass KCoreConfigSkeleton, add data members for the preferences * settings and register the members in the constructor of the subclass. * * Example: * \code * class MyPrefs : public KCoreConfigSkeleton * { * public: * MyPrefs() * { * setCurrentGroup("MyGroup"); * addItemBool("MySetting1", mMyBool, false); * addItemPoint("MySetting2", mMyPoint, QPoint(100, 200)); * * setCurrentGroup("MyOtherGroup"); * addItemDouble("MySetting3", mMyDouble, 3.14); * } * * bool mMyBool; * QPoint mMyPoint; * double mMyDouble; * } * \endcode * * It might be convenient in many cases to make this subclass of KCoreConfigSkeleton a * singleton for global access from all over the application without passing * references to the KCoreConfigSkeleton object around. * * You can write the data to the configuration file by calling @ref save() * and read the data from the configuration file by calling @ref readConfig(). * If you want to watch for config changes, use @ref configChanged() signal. * * If you have items, which are not covered by the existing addItem() functions * you can add customized code for reading, writing and default setting by * implementing the functions @ref usrUseDefaults(), @ref usrRead() and * @ref usrSave(). * * Internally preferences settings are stored in instances of subclasses of * @ref KConfigSkeletonItem. You can also add KConfigSkeletonItem subclasses * for your own types and call the generic @ref addItem() to register them. * * In many cases you don't have to write the specific KCoreConfigSkeleton * subclasses yourself, but you can use \ref kconfig_compiler to automatically * generate the C++ code from an XML description of the configuration options. * * Use KConfigSkeleton if you need GUI types as well. */ class KCONFIGCORE_EXPORT KCoreConfigSkeleton : public QObject { Q_OBJECT public: /** * Class for handling a string preferences item. */ class KCONFIGCORE_EXPORT ItemString: public KConfigSkeletonGenericItem < QString > { public: enum Type { Normal, Password, Path }; /** @enum Type The type of string that is held in this item @var ItemString::Type ItemString::Normal A normal string @var ItemString::Type ItemString::Password A password string @var ItemString::Type ItemString::Path A path to a file or directory */ /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem @param type The type of string held by the item */ ItemString(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue = QLatin1String(""), // NOT QString() !! Type type = Normal); /** @copydoc KConfigSkeletonItem::writeConfig(KConfig*) */ void writeConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() const */ QVariant property() const override; private: Type mType; }; /** * Class for handling a password preferences item. */ class KCONFIGCORE_EXPORT ItemPassword: public ItemString { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemPassword(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue = QLatin1String("")); // NOT QString() !! }; /** * Class for handling a path preferences item. */ class KCONFIGCORE_EXPORT ItemPath: public ItemString { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemPath(const QString &_group, const QString &_key, QString &reference, const QString &defaultValue = QString()); }; /** * Class for handling a url preferences item. */ class KCONFIGCORE_EXPORT ItemUrl: public KConfigSkeletonGenericItem < QUrl > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemUrl(const QString &_group, const QString &_key, QUrl &reference, const QUrl &defaultValue = QUrl()); /** @copydoc KConfigSkeletonItem::writeConfig(KConfig*) */ void writeConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() const */ QVariant property() const override; }; /** * Class for handling a QVariant preferences item. */ class KCONFIGCORE_EXPORT ItemProperty: public KConfigSkeletonGenericItem < QVariant > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemProperty(const QString &_group, const QString &_key, QVariant &reference, const QVariant &defaultValue = QVariant()); void readConfig(KConfig *config) override; void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() const */ QVariant property() const override; }; /** * Class for handling a bool preferences item. */ class KCONFIGCORE_EXPORT ItemBool: public KConfigSkeletonGenericItem < bool > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemBool(const QString &_group, const QString &_key, bool &reference, bool defaultValue = true); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() const */ QVariant property() const override; }; /** * Class for handling a 32-bit integer preferences item. */ class KCONFIGCORE_EXPORT ItemInt: public KConfigSkeletonGenericItem < qint32 > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemInt(const QString &_group, const QString &_key, qint32 &reference, qint32 defaultValue = 0); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; /** Get the minimum value that is allowed to be stored in this item */ QVariant minValue() const override; /** Get the maximum value this is allowed to be stored in this item */ QVariant maxValue() const override; /** Set the minimum value for the item @sa minValue() */ void setMinValue(qint32); /** Set the maximum value for the item @sa maxValue */ void setMaxValue(qint32); private: bool mHasMin : 1; bool mHasMax : 1; qint32 mMin; qint32 mMax; }; /** * Class for handling a 64-bit integer preferences item. */ class KCONFIGCORE_EXPORT ItemLongLong: public KConfigSkeletonGenericItem < qint64 > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemLongLong(const QString &_group, const QString &_key, qint64 &reference, qint64 defaultValue = 0); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; /** @copydoc ItemInt::minValue() */ QVariant minValue() const override; /** @copydoc ItemInt::maxValue() */ QVariant maxValue() const override; /** @copydoc ItemInt::setMinValue(qint32) */ void setMinValue(qint64); /** @copydoc ItemInt::setMaxValue(qint32) */ void setMaxValue(qint64); private: bool mHasMin : 1; bool mHasMax : 1; qint64 mMin; qint64 mMax; }; #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0) typedef KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Use ItemLongLong") ItemLongLong ItemInt64; #endif /** * Class for handling enums. */ class KCONFIGCORE_EXPORT ItemEnum: public ItemInt { public: struct Choice { QString name; QString label; QString toolTip; QString whatsThis; }; /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem @param choices The list of enums that can be stored in this item */ ItemEnum(const QString &_group, const QString &_key, qint32 &reference, const QList &choices, qint32 defaultValue = 0); QList choices() const; /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::writeConfig(KConfig*) */ void writeConfig(KConfig *config) override; // Source compatibility with 4.x typedef Choice Choice2; QList choices2() const; private: QList mChoices; }; /** * Class for handling an unsigned 32-bit integer preferences item. */ class KCONFIGCORE_EXPORT ItemUInt: public KConfigSkeletonGenericItem < quint32 > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemUInt(const QString &_group, const QString &_key, quint32 &reference, quint32 defaultValue = 0); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; /** @copydoc ItemInt::minValue() */ QVariant minValue() const override; /** @copydoc ItemInt::maxValue() */ QVariant maxValue() const override; /** @copydoc ItemInt::setMinValue(qint32) */ void setMinValue(quint32); /** @copydoc ItemInt::setMaxValue(qint32) */ void setMaxValue(quint32); private: bool mHasMin : 1; bool mHasMax : 1; quint32 mMin; quint32 mMax; }; /** * Class for handling unsigned 64-bit integer preferences item. */ class KCONFIGCORE_EXPORT ItemULongLong: public KConfigSkeletonGenericItem < quint64 > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemULongLong(const QString &_group, const QString &_key, quint64 &reference, quint64 defaultValue = 0); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; /** @copydoc ItemInt::minValue() */ QVariant minValue() const override; /** @copydoc ItemInt::maxValue() */ QVariant maxValue() const override; /** @copydoc ItemInt::setMinValue(qint32) */ void setMinValue(quint64); /** @copydoc ItemInt::setMaxValue(qint32) */ void setMaxValue(quint64); private: bool mHasMin : 1; bool mHasMax : 1; quint64 mMin; quint64 mMax; }; #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0) typedef KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Use ItemULongLong") ItemULongLong ItemUInt64; #endif /** * Class for handling a floating point preference item. */ class KCONFIGCORE_EXPORT ItemDouble: public KConfigSkeletonGenericItem < double > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemDouble(const QString &_group, const QString &_key, double &reference, double defaultValue = 0); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; /** @copydoc ItemInt::minValue() */ QVariant minValue() const override; /** @copydoc ItemInt::maxValue() */ QVariant maxValue() const override; /** @copydoc ItemInt::setMinValue() */ void setMinValue(double); /** @copydoc ItemInt::setMaxValue() */ void setMaxValue(double); private: bool mHasMin : 1; bool mHasMax : 1; double mMin; double mMax; }; /** * Class for handling a QRect preferences item. */ class KCONFIGCORE_EXPORT ItemRect: public KConfigSkeletonGenericItem < QRect > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemRect(const QString &_group, const QString &_key, QRect &reference, const QRect &defaultValue = QRect()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; /** * Class for handling a QPoint preferences item. */ class KCONFIGCORE_EXPORT ItemPoint: public KConfigSkeletonGenericItem < QPoint > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemPoint(const QString &_group, const QString &_key, QPoint &reference, const QPoint &defaultValue = QPoint()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; /** * Class for handling a QSize preferences item. */ class KCONFIGCORE_EXPORT ItemSize: public KConfigSkeletonGenericItem < QSize > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemSize(const QString &_group, const QString &_key, QSize &reference, const QSize &defaultValue = QSize()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; /** * Class for handling a QDateTime preferences item. */ class KCONFIGCORE_EXPORT ItemDateTime: public KConfigSkeletonGenericItem < QDateTime > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemDateTime(const QString &_group, const QString &_key, QDateTime &reference, const QDateTime &defaultValue = QDateTime()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; /** * Class for handling a string list preferences item. */ class KCONFIGCORE_EXPORT ItemStringList: public KConfigSkeletonGenericItem < QStringList > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemStringList(const QString &_group, const QString &_key, QStringList &reference, const QStringList &defaultValue = QStringList()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; /** * Class for handling a path list preferences item. */ class KCONFIGCORE_EXPORT ItemPathList: public ItemStringList { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemPathList(const QString &_group, const QString &_key, QStringList &reference, const QStringList &defaultValue = QStringList()); /** @copydoc KConfigSkeletonItem::readConfig */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::writeConfig */ void writeConfig(KConfig *config) override; }; /** * Class for handling a url list preferences item. */ class KCONFIGCORE_EXPORT ItemUrlList: public KConfigSkeletonGenericItem < QList > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemUrlList(const QString &_group, const QString &_key, QList &reference, const QList &defaultValue = QList()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::writeConfig(KConfig*) */ void writeConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; /** * Class for handling an integer list preferences item. */ class KCONFIGCORE_EXPORT ItemIntList: public KConfigSkeletonGenericItem < QList < int > > { public: /** @copydoc KConfigSkeletonGenericItem::KConfigSkeletonGenericItem */ ItemIntList(const QString &_group, const QString &_key, QList < int > &reference, const QList < int > &defaultValue = QList < int >()); /** @copydoc KConfigSkeletonItem::readConfig(KConfig*) */ void readConfig(KConfig *config) override; /** @copydoc KConfigSkeletonItem::setProperty(const QVariant&) */ void setProperty(const QVariant &p) override; /** @copydoc KConfigSkeletonItem::isEqual(const QVariant &) const */ bool isEqual(const QVariant &p) const override; /** @copydoc KConfigSkeletonItem::property() */ QVariant property() const override; }; public: /** * Constructor. * * @param configname name of config file. If no name is given, the default * config file as returned by KSharedConfig::openConfig() is used * @param parent the parent object (see QObject documentation) */ explicit KCoreConfigSkeleton(const QString &configname = QString(), QObject *parent = nullptr); /** * Constructor. * * @param config configuration object to use * @param parent the parent object (see QObject documentation) */ explicit KCoreConfigSkeleton(KSharedConfig::Ptr config, QObject *parent = nullptr); /** * Destructor */ virtual ~KCoreConfigSkeleton(); /** * Set all registered items to their default values. * This method calls usrSetDefaults() after setting the defaults for the * registered items. You can override usrSetDefaults() in derived classes * if you have special requirements. * If you need more fine-grained control of setting the default values of * the registered items you can override setDefaults() in a derived class. */ virtual void setDefaults(); /** * Read preferences from config file. All registered items are set to the * values read from disk. * This method calls usrRead() after reading the settings of the * registered items from the KConfig. You can override usrRead() * in derived classes if you have special requirements. */ void load(); #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * @deprecated since 5.0, call load() instead (to reload from disk) or just read() * if the underlying KConfig object is already up-to-date. */ KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Use KCoreConfigSkeleton::load() or KCoreConfigSkeleton::read()") void readConfig() { load(); } #endif /** * Read preferences from the KConfig object. * This method assumes that the KConfig object was previously loaded, * i.e. it uses the in-memory values from KConfig without reloading from disk. * * This method calls usrRead() after reading the settings of the * registered items from the KConfig. You can override usrRead() * in derived classes if you have special requirements. * @since 5.0 */ void read(); /** * Indicates if all the registered items are set to their default value. * * @since 5.64 */ bool isDefaults() const; /** * Indicates if any registered item has a different value than the * previously loaded value. * * @since 5.64 */ bool isSaveNeeded() const; /** * Set the config file group for subsequent addItem() calls. It is valid * until setCurrentGroup() is called with a new argument. Call this before * you add any items. The default value is "No Group". */ void setCurrentGroup(const QString &group); /** * Returns the current group used for addItem() calls. */ QString currentGroup() const; /** * Register a custom @ref KConfigSkeletonItem with a given name. * * If the name parameter is null, take the name from KConfigSkeletonItem::key(). * Note that all names must be unique but that multiple entries can have * the same key if they reside in different groups. * * KCoreConfigSkeleton takes ownership of the KConfigSkeletonItem. */ void addItem(KConfigSkeletonItem *, const QString &name = QString()); /** * Register an item of type QString. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemString *addItemString(const QString &name, QString &reference, const QString &defaultValue = QLatin1String(""), // NOT QString() !! const QString &key = QString()); /** * Register a password item of type QString. The string value is written * encrypted to the config file. Note that the current encryption scheme * is very weak. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemPassword *addItemPassword(const QString &name, QString &reference, const QString &defaultValue = QLatin1String(""), const QString &key = QString()); /** * Register a path item of type QString. The string value is interpreted * as a path. This means, dollar expension is activated for this value, so * that e.g. $HOME gets expanded. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemPath *addItemPath(const QString &name, QString &reference, const QString &defaultValue = QLatin1String(""), const QString &key = QString()); /** * Register a property item of type QVariant. Note that only the following * QVariant types are allowed: String, StringList, Font, Point, Rect, Size, * Color, Int, UInt, Bool, Double, DateTime and Date. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemProperty *addItemProperty(const QString &name, QVariant &reference, const QVariant &defaultValue = QVariant(), const QString &key = QString()); /** * Register an item of type bool. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemBool *addItemBool(const QString &name, bool &reference, bool defaultValue = false, const QString &key = QString()); /** * Register an item of type qint32. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemInt *addItemInt(const QString &name, qint32 &reference, qint32 defaultValue = 0, const QString &key = QString()); /** * Register an item of type quint32. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemUInt *addItemUInt(const QString &name, quint32 &reference, quint32 defaultValue = 0, const QString &key = QString()); /** * Register an item of type qint64. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemLongLong *addItemLongLong(const QString &name, qint64 &reference, qint64 defaultValue = 0, const QString &key = QString()); #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * @deprecated Since 5.0, use addItemLongLong(). */ KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Use KCoreConfigSkeleton::addItemLongLong(...)") ItemLongLong *addItemInt64(const QString &name, qint64 &reference, qint64 defaultValue = 0, const QString &key = QString()); #endif /** * Register an item of type quint64 * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemULongLong *addItemULongLong(const QString &name, quint64 &reference, quint64 defaultValue = 0, const QString &key = QString()); #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * @deprecated Since 5.0, use addItemULongLong(). */ KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Use KCoreConfigSkeleton::addItemULongLong(...)") ItemULongLong *addItemUInt64(const QString &name, quint64 &reference, quint64 defaultValue = 0, const QString &key = QString()); #endif /** * Register an item of type double. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemDouble *addItemDouble(const QString &name, double &reference, double defaultValue = 0.0, const QString &key = QString()); /** * Register an item of type QRect. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemRect *addItemRect(const QString &name, QRect &reference, const QRect &defaultValue = QRect(), const QString &key = QString()); /** * Register an item of type QPoint. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemPoint *addItemPoint(const QString &name, QPoint &reference, const QPoint &defaultValue = QPoint(), const QString &key = QString()); /** * Register an item of type QSize. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemSize *addItemSize(const QString &name, QSize &reference, const QSize &defaultValue = QSize(), const QString &key = QString()); /** * Register an item of type QDateTime. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemDateTime *addItemDateTime(const QString &name, QDateTime &reference, const QDateTime &defaultValue = QDateTime(), const QString &key = QString()); /** * Register an item of type QStringList. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemStringList *addItemStringList(const QString &name, QStringList &reference, const QStringList &defaultValue = QStringList(), const QString &key = QString()); /** * Register an item of type QList. * * @param name Name used to identify this setting. Names must be unique. * @param reference Pointer to the variable, which is set by readConfig() * calls and read by save() calls. * @param defaultValue Default value, which is used when the config file * does not yet contain the key of this item. * @param key Key used in config file. If key is null, name is used as key. * @return The created item */ ItemIntList *addItemIntList(const QString &name, QList < int > &reference, const QList < int > &defaultValue = QList < int >(), const QString &key = QString()); /** * Return the @ref KConfig object used for reading and writing the settings. */ KConfig *config(); /** * Return the @ref KConfig object used for reading and writing the settings. */ const KConfig *config() const; /** * Return the @ref KConfig object used for reading and writing the settings. * @since 5.0 */ KSharedConfig::Ptr sharedConfig() const; /** * Set the @ref KSharedConfig object used for reading and writing the settings. */ void setSharedConfig(KSharedConfig::Ptr pConfig); /** * Return list of items managed by this KCoreConfigSkeleton object. */ KConfigSkeletonItem::List items() const; /** * Removes and deletes an item by name * @param name the name of the item to remove */ void removeItem(const QString &name); /** * Removes and deletes all items */ void clearItems(); /** * Return whether a certain item is immutable * @since 4.4 */ Q_INVOKABLE bool isImmutable(const QString &name) const; /** * Lookup item by name * @since 4.4 */ KConfigSkeletonItem *findItem(const QString &name) const; /** * Specify whether this object should reflect the actual values or the * default values. * This method is implemented by usrUseDefaults(), which can be overridden * in derived classes if you have special requirements and can call * usrUseDefaults() directly. * If you don't have control whether useDefaults() or usrUseDefaults() is * called override useDefaults() directly. * @param b true to make this object reflect the default values, * false to make it reflect the actual values. * @return The state prior to this call */ virtual bool useDefaults(bool b); public Q_SLOTS: /** * Write preferences to config file. The values of all registered items are * written to disk. * This method calls usrSave() after writing the settings from the * registered items to the KConfig. You can override usrSave() * in derived classes if you have special requirements. */ bool save(); #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 0) /** * @deprecated since 5.0, call save() instead. */ KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Use KCoreConfigSkeleton::save()") void writeConfig() { save(); } #endif Q_SIGNALS: /** * This signal is emitted when the configuration change. */ void configChanged(); protected: /** * Implemented by subclasses that use special defaults. * It replaces the default values with the actual values and * vice versa. Called from @ref useDefaults() * @param b true to make this object reflect the default values, * false to make it reflect the actual values. * @return The state prior to this call */ virtual bool usrUseDefaults(bool b); /** * Perform the actual setting of default values. * Override in derived classes to set special default values. * Called from @ref setDefaults() */ virtual void usrSetDefaults(); /** * Perform the actual reading of the configuration file. * Override in derived classes to read special config values. * Called from @ref read() */ virtual void usrRead(); /** * Perform the actual writing of the configuration file. * Override in derived classes to write special config values. * Called from @ref save() */ virtual bool usrSave(); #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) /** * @deprecated since 5.0, override usrRead instead. This method is still called from usrRead * for compatibility. */ KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Override KCoreConfigSkeleton::usrRead()") virtual void usrReadConfig(); #endif #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 0) /** * @deprecated since 5.0, override usrSave instead. This method is still called from usrSave * for compatibility. */ KCONFIGCORE_DEPRECATED_VERSION(5, 0, "Override KCoreConfigSkeleton::usrSave()") virtual bool usrWriteConfig(); #endif private: KCoreConfigSkeletonPrivate *const d; friend class KConfigSkeleton; }; #endif diff --git a/src/core/kcoreconfigskeleton_p.h b/src/core/kcoreconfigskeleton_p.h index 19f6f4b..871122c 100644 --- a/src/core/kcoreconfigskeleton_p.h +++ b/src/core/kcoreconfigskeleton_p.h @@ -1,92 +1,93 @@ /* This file is part of KOrganizer. Copyright (c) 2000,2001 Cornelius Schumacher Copyright (c) 2003 Waldo Bastian This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KCORECONFIGSKELETON_P_H #define KCORECONFIGSKELETON_P_H #include "kcoreconfigskeleton.h" class KCoreConfigSkeletonPrivate { public: KCoreConfigSkeletonPrivate() : mCurrentGroup(QStringLiteral("No Group")), mUseDefaults(false) {} ~KCoreConfigSkeletonPrivate() { KConfigSkeletonItem::List::ConstIterator it; for (it = mItems.constBegin(); it != mItems.constEnd(); ++it) { delete *it; } } QString mCurrentGroup; KSharedConfig::Ptr mConfig; // pointer to KConfig object KConfigSkeletonItem::List mItems; KConfigSkeletonItem::Dict mItemDict; bool mUseDefaults; }; class KConfigSkeletonItemPrivate { public: KConfigSkeletonItemPrivate() : mIsImmutable(true) , mWriteFlags(KConfigBase::Normal) {} virtual ~KConfigSkeletonItemPrivate(); bool mIsImmutable; ///< Indicates this item is immutable KConfigBase::WriteConfigFlags mWriteFlags; ///< The flags to pass to calls of writeEntry() and revertToDefault() QString mLabel; ///< The label for this item QString mToolTip; ///< The ToolTip text for this item QString mWhatsThis; ///< The What's This text for this item // HACK: Necessary to avoid introducing new virtuals in KConfigSkeletonItem std::function mIsDefaultImpl; std::function mIsSaveNeededImpl; }; class KPropertySkeletonItemPrivate : public KConfigSkeletonItemPrivate { public: KPropertySkeletonItemPrivate(QObject *object, const QByteArray &propertyName, const QVariant &defaultValue) : KConfigSkeletonItemPrivate() , mObject(object) , mPropertyName(propertyName) , mDefaultValue(defaultValue) , mConstDefaultValue(defaultValue) { mIsImmutable = false; } QObject *mObject; const QByteArray mPropertyName; QVariant mDefaultValue; const QVariant mConstDefaultValue; QVariant mReference; QVariant mLoadedValue; + std::function mNotifyFunction; }; #endif