Skip to content

Commit 7b6ffd5

Browse files
committed
feat: add isReadOnly method to DConfig
1. Added isReadOnly virtual method to DConfigBackend base class with default implementation returning false 2. Implemented isReadOnly in FileBackend by checking permissions from config file metadata 3. Implemented isReadOnly in DBusBackend by calling remote permissions method via D-Bus 4. Added public isReadOnly method to DConfig class that delegates to backend implementation 5. Updated documentation with Chinese and English API documentation for the new method 6. Added test configuration items with readwrite and readonly permissions in test data 7. Added unit tests to verify isReadOnly functionality for both permission types Log: Added DConfig.isReadOnly() method to check if configuration items are read-only Influence: 1. Test isReadOnly returns true for configuration items with readonly permissions 2. Test isReadOnly returns false for configuration items with readwrite permissions 3. Verify isReadOnly works correctly with both FileBackend and DBusBackend implementations 4. Test isReadOnly returns false when DConfig is in invalid state 5. Verify documentation is properly updated in both English and Chinese versions feat: DConfig新增isReadOnly接口 1. 在DConfigBackend基类中添加isReadOnly虚方法,默认实现返回false 2. 在FileBackend中实现isReadOnly,通过检查配置文件元数据中的权限设置 3. 在DBusBackend中实现isReadOnly,通过D-Bus调用远程权限方法 4. 在DConfig类中添加公共isReadOnly方法,委托给后端实现 5. 更新文档,添加中英文API文档说明新方法 6. 在测试数据中添加具有读写和只读权限的测试配置项 7. 添加单元测试验证isReadOnly功能对两种权限类型的处理 Log: 新增DConfig.isReadOnly()方法用于检查配置项是否为只读 Influence: 1. 测试isReadOnly对只读权限配置项返回true 2. 测试isReadOnly对读写权限配置项返回false 3. 验证isReadOnly在FileBackend和DBusBackend实现中都能正常工作 4. 测试当DConfig处于无效状态时isReadOnly返回false 5. 验证中英文文档都已正确更新
1 parent 421510c commit 7b6ffd5

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

docs/global/dconfig.zh_CN.dox

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@
6363
@sa DBusBackend::reset()
6464
@sa QSettingBackend::reset()
6565

66+
@fn bool Dtk::Core::DConfigBackend::isReadOnly(const QString &key)
67+
@brief 检测指定配置项是否为只读
68+
@param[in] key 配置项名称
69+
@return 如果配置项为只读返回true,否则返回false
70+
@sa DConfig::isReadOnly()
71+
@sa FileBackend::isReadOnly()
72+
@sa DBusBackend::isReadOnly()
73+
6674
@fn QString Dtk::Core::DConfigBackend::name() const = 0
6775
@brief 后端配置的唯一标识
6876
@sa FileBackend::name()
@@ -384,6 +392,11 @@ sudo make install
384392
@brief 设置其配置项对应的默认值,此值为经过override机制覆盖后的值,不一定为此配置文件中meta中定义的值
385393
@param[in] key 配置项名称
386394

395+
@fn bool Dtk::Core::DConfig::isReadOnly(const QString &key)
396+
@brief 检测指定配置项是否为只读
397+
@param[in] key 配置项名称
398+
@return 如果配置项为只读返回true,否则返回false
399+
387400
@fn QString Dtk::Core::DConfig::name()
388401
@brief 返回配置文件名称
389402

include/global/dconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class DConfigBackend {
2323
virtual void reset(const QString &key) { setValue(key, QVariant());}
2424
virtual QString name() const {return QString("");}
2525
virtual bool isDefaultValue(const QString &/*key*/) const { return true; }
26+
virtual bool isReadOnly(const QString &/*key*/) const { return false; }
2627
};
2728

2829
class DConfigPrivate;
@@ -61,6 +62,7 @@ class LIBDTKCORESHARED_EXPORT DConfig : public QObject, public DObject
6162
QVariant value(const QString &key, const QVariant &fallback = QVariant()) const;
6263
void setValue(const QString &key, const QVariant &value);
6364
void reset(const QString &key);
65+
bool isReadOnly(const QString &key) const;
6466

6567
QString name() const;
6668
QString subpath() const;

src/dconfig.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ static QString NoAppId;
8282
@sa DConfig::reset()
8383
*/
8484

85+
/*!
86+
@~english
87+
@fn bool DConfigBackend::isReadOnly(const QString &key) const = 0
88+
89+
@sa DConfig::isReadOnly()
90+
*/
91+
8592
/*!
8693
@~english
8794
@fn QString DConfigBackend::name() const = 0
@@ -230,6 +237,12 @@ class Q_DECL_HIDDEN FileBackend : public DConfigBackend
230237
setValue(key, QVariant());
231238
}
232239

240+
virtual bool isReadOnly(const QString &key) const override
241+
{
242+
const auto vc = configFile->meta()->permissions(key);
243+
return vc == DConfigFile::ReadOnly;
244+
}
245+
233246
virtual QString name() const override
234247
{
235248
return QString("FileBackend");
@@ -425,6 +438,18 @@ class Q_DECL_HIDDEN DBusBackend : public DConfigBackend
425438
<< ", error message:" << reply.error();
426439
}
427440

441+
virtual bool isReadOnly(const QString &key) const override
442+
{
443+
auto reply = config->permissions(key);
444+
reply.waitForFinished();
445+
if (reply.isError()) {
446+
qWarning() << "Failed to call `permissions`, key:" << key
447+
<< ", error message:" << reply.error().message();
448+
return false;
449+
}
450+
return reply.value() == QLatin1String("readonly");
451+
}
452+
428453
virtual QString name() const override
429454
{
430455
return QString("DBusBackend");
@@ -821,6 +846,21 @@ void DConfig::reset(const QString &key)
821846
d->backend->reset(key);
822847
}
823848

849+
/*!
850+
* @~english
851+
* @brief Check whether the configuration item is read-only
852+
* @param key Configuration Item Name
853+
* @return Return `true` if the configuration item is read-only, otherwise return `false`
854+
*/
855+
bool DConfig::isReadOnly(const QString &key) const
856+
{
857+
D_DC(DConfig);
858+
if (d->invalid())
859+
return false;
860+
861+
return d->backend->isReadOnly(key);
862+
}
863+
824864
/*!
825865
@~english
826866
* @brief Return configuration file name

tests/data/dconf-example.meta.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@
105105
"description": "I am public configure",
106106
"permissions": "readwrite",
107107
"visibility": "private"
108+
},
109+
"readwrite": {
110+
"value": true,
111+
"serial": 0,
112+
"flags": [],
113+
"name": "readwrite configure",
114+
"name[zh_CN]": "我是可读写配置",
115+
"description": "I am a readwrite configure",
116+
"permissions": "readwrite",
117+
"visibility": "private"
118+
},
119+
"readonly": {
120+
"value": true,
121+
"serial": 0,
122+
"flags": [],
123+
"name": "readonly configure",
124+
"name[zh_CN]": "我是只读配置",
125+
"description": "I am a readonly configure",
126+
"permissions": "readonly",
127+
"visibility": "private"
108128
}
109129
}
110130
}

tests/ut_dconfig.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,18 @@ TEST_F(ut_DConfig, isDefaultValue) {
226226
EXPECT_EQ(config.isDefaultValue("key2"), true);
227227
}
228228
}
229+
230+
TEST_F(ut_DConfig, isReadOnly) {
231+
232+
FileCopyGuard guard(":/data/dconf-example.meta.json", metaFilePath);
233+
{
234+
DConfig config(FILE_NAME);
235+
EXPECT_FALSE(config.isValid());
236+
ASSERT_EQ(config.isReadOnly("readwrite"), false);
237+
}
238+
{
239+
DConfig config(FILE_NAME);
240+
EXPECT_FALSE(config.isValid());
241+
EXPECT_EQ(config.isReadOnly("readonly"), true);
242+
}
243+
}

0 commit comments

Comments
 (0)