Skip to content

Commit 3efc25d

Browse files
sync: from linuxdeepin/dtkcore
Synchronize source files from linuxdeepin/dtkcore. Source-pull-request: linuxdeepin/dtkcore#523
1 parent 7fdc769 commit 3efc25d

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,14 @@ 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_TRUE(config.isValid());
236+
ASSERT_EQ(config.isReadOnly("readwrite"), false);
237+
EXPECT_EQ(config.isReadOnly("readonly"), true);
238+
}
239+
}

0 commit comments

Comments
 (0)