feat: Add isDefaultValue for DConfig

Add isDefaultValue function.

Issue: https://github.com/linuxdeepin/dtk/issues/3
This commit is contained in:
YeShanShan 2023-09-13 17:56:15 +08:00 committed by yeshanshan
parent 9d338ebeb5
commit 271edf81b1
5 changed files with 76 additions and 0 deletions

View File

@ -69,6 +69,14 @@
@sa DBusBackend::name() @sa DBusBackend::name()
@sa QSettingBackend::name() @sa QSettingBackend::name()
@fn bool Dtk::Core::DConfigBackend::isDefaultValue(const QString &key)
@brief 检测指定配置项名称对应的值是否为默认值。
@param[in] key 配置项名称
@sa DConfig::isDefaultValue()
@sa FileBackend::isDefaultValue()
@sa DBusBackend::isDefaultValue()
@sa QSettingBackend::isDefaultValue()
@class Dtk::Core::QSettingBackend dconfig.h @class Dtk::Core::QSettingBackend dconfig.h
@brief QSetting后端,继承自DConfigBackend抽象接口,并实现了虚函数。 @brief QSetting后端,继承自DConfigBackend抽象接口,并实现了虚函数。
@sa DConfigBackend @sa DConfigBackend
@ -348,6 +356,10 @@ sudo make install
@fn bool Dtk::Core::DConfig::isValid() @fn bool Dtk::Core::DConfig::isValid()
@brief 判断此后端是否可用 @brief 判断此后端是否可用
@fn bool Dtk::Core::DConfig::isDefaultValue(const QString &key)
@brief 检测指定配置项名称对应的值是否为默认值。
@param[in] key 配置项名称
@fn QVariant Dtk::Core::DConfig::value(const QString &key, const QVariant &fallback) @fn QVariant Dtk::Core::DConfig::value(const QString &key, const QVariant &fallback)
@brief 根据配置项名称获得对应值 @brief 根据配置项名称获得对应值
@param[in] key 配置项名称 @param[in] key 配置项名称

View File

@ -22,6 +22,7 @@ public:
virtual void setValue(const QString &/*key*/, const QVariant &/*value*/) = 0; virtual void setValue(const QString &/*key*/, const QVariant &/*value*/) = 0;
virtual void reset(const QString &key) { setValue(key, QVariant());} virtual void reset(const QString &key) { setValue(key, QVariant());}
virtual QString name() const {return QString("");} virtual QString name() const {return QString("");}
virtual bool isDefaultValue(const QString &/*key*/) const { return true; }
}; };
class DConfigPrivate; class DConfigPrivate;
@ -53,6 +54,7 @@ public:
QStringList keyList() const; QStringList keyList() const;
bool isValid() const; bool isValid() const;
bool isDefaultValue(const QString &key) const;
QVariant value(const QString &key, const QVariant &fallback = QVariant()) const; QVariant value(const QString &key, const QVariant &fallback = QVariant()) const;
void setValue(const QString &key, const QVariant &value); void setValue(const QString &key, const QVariant &value);
void reset(const QString &key); void reset(const QString &key);

View File

@ -13,6 +13,10 @@ SPDX-License-Identifier: LGPL-3.0-or-later
<arg type='s' name='key' direction='in'/> <arg type='s' name='key' direction='in'/>
<arg type='v' name='value' direction='out'/> <arg type='v' name='value' direction='out'/>
</method> </method>
<method name='isDefaultValue'>
<arg type='s' name='key' direction='in'/>
<arg type='b' name='isDefaultValue' direction='out'/>
</method>
<method name='setValue'> <method name='setValue'>
<arg type='s' name='key' direction='in'/> <arg type='s' name='key' direction='in'/>
<arg type='v' name='value' direction='in'/> <arg type='v' name='value' direction='in'/>

View File

@ -88,6 +88,12 @@ static QString NoAppId;
@brief The unique identity of the backend configuration @brief The unique identity of the backend configuration
/*!
@~english
@fn bool DConfigBackend::isDefaultValue(const QString &key) const = 0
@sa DConfig::isDefaultValue()
*/ */
DConfigBackend::~DConfigBackend() DConfigBackend::~DConfigBackend()
@ -202,6 +208,13 @@ public:
return vg.isValid() ? vg : fallback; return vg.isValid() ? vg : fallback;
} }
virtual bool isDefaultValue(const QString &key) const override
{
// Don't fallback to generic configuration
const QVariant &vc = configFile->cacheValue(configCache.get(), key);
return !vc.isValid();
}
virtual void setValue(const QString &key, const QVariant &value) override virtual void setValue(const QString &key, const QVariant &value) override
{ {
// setValue's callerAppid is itself instead of config's appId. // setValue's callerAppid is itself instead of config's appId.
@ -379,6 +392,17 @@ public:
return decodeQDBusArgument(reply.value().variant()); return decodeQDBusArgument(reply.value().variant());
} }
virtual bool isDefaultValue(const QString &key) const override
{
auto reply = config->isDefaultValue(key);
reply.waitForFinished();
if (reply.isError()) {
qWarning() << "Wrong when calling `isDefaultValue`, key:" << key << ", error message:" << reply.error().message();
return false;
}
return reply.value();
}
virtual void setValue(const QString &key, const QVariant &value) override virtual void setValue(const QString &key, const QVariant &value) override
{ {
config->setValue(key, QDBusVariant(value)); config->setValue(key, QDBusVariant(value));
@ -689,6 +713,20 @@ bool DConfig::isValid() const
return !d->invalid(); return !d->invalid();
} }
/*!
@~english
* @brief Check whether the value is default according to the configuration item name
* @param key Configuration Item Name
* @return Return `true` if the value isn't been set, otherwise return `false`
*/
bool DConfig::isDefaultValue(const QString &key) const
{
D_DC(DConfig);
if (d->invalid())
return false;
return d->backend->isDefaultValue(key);
}
/*! /*!
@~english @~english
* @brief Get the corresponding value according to the configuration item name * @brief Get the corresponding value according to the configuration item name

View File

@ -206,3 +206,23 @@ TEST_F(ut_DConfig, DSettingsDConfigBackend)
ASSERT_EQ(backend.getOption("key2").toString(), QString("126")); ASSERT_EQ(backend.getOption("key2").toString(), QString("126"));
} }
} }
TEST_F(ut_DConfig, isDefaultValue) {
FileCopyGuard guard(":/data/dconf-example.meta.json", metaFilePath);
{
DConfig config(FILE_NAME);
ASSERT_EQ(config.isDefaultValue("key2"), true);
}
{
DConfig config(FILE_NAME);
config.setValue("key2", "126");
EXPECT_EQ(config.isDefaultValue("key2"), false);
}
{
DConfig config(FILE_NAME);
config.setValue("key2", "126");
config.reset("key2");
EXPECT_EQ(config.isDefaultValue("key2"), true);
}
}