-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathdconfig.cpp
More file actions
891 lines (751 loc) · 24.2 KB
/
Copy pathdconfig.cpp
File metadata and controls
891 lines (751 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dconfig.h"
#ifndef D_DISABLE_DCONFIG
#include "dconfigfile.h"
#ifndef D_DISABLE_DBUS_CONFIG
#include "configmanager_interface.h"
#include "manager_interface.h"
#endif
#else
#include <QSettings>
#endif
#include "dobject_p.h"
#include <DSGApplication>
#include <QLoggingCategory>
#include <QCoreApplication>
#include <QThread>
#ifdef Q_OS_LINUX
#include <unistd.h>
#endif
// https://gitlabwh.uniontech.com/wuhan/se/deepin-specifications/-/issues/3
DCORE_BEGIN_NAMESPACE
#ifdef D_DISABLE_DCONFIG
Q_LOGGING_CATEGORY(cfLog, "dtk.dsg.config")
#else
Q_DECLARE_LOGGING_CATEGORY(cfLog)
#endif
static QString NoAppId;
/*!
@~english
@class Dtk::Core::DConfigBackend
\inmodule dtkcore
@brief Configure the abstract interface of the backend.
All configuration backends used by DConfig inherit this class, and users can inherit this class to implement their own configuration backends.
*/
/*!
@~english
@fn bool DConfigBackend::load(const QString &) = 0
@brief Initialize the backend
\a appId Managed configuration information key value, the default is the application name.
*/
/*!
@~english
@fn bool DConfigBackend::isValid() const = 0
@sa DConfig::isValid().
*/
/*!
@~english
@fn QStringList DConfigBackend::keyList() const = 0
@sa DConfig::keyList()
*/
/*!
@~english
@fn QVariant DConfigBackend::value(const QString &key, const QVariant &fallback = QVariant()) const = 0
@sa DConfig::value()
*/
/*!
@~english
@fn void DConfigBackend::setValue(const QString &key, const QVariant &value) = 0
@sa DConfig::setValue()
*/
/*!
@~english
@fn void DConfigBackend::reset(const QString &key)
@sa DConfig::reset()
*/
/*!
@~english
@fn bool DConfigBackend::isReadOnly(const QString &key) const = 0
@sa DConfig::isReadOnly()
*/
/*!
@~english
@fn QString DConfigBackend::name() const = 0
@brief The unique identity of the backend configuration
*/
/*!
@~english
@fn bool DConfigBackend::isDefaultValue(const QString &key) const = 0
@sa DConfig::isDefaultValue()
*/
DConfigBackend::~DConfigBackend()
{
}
static QString _globalAppId;
class Q_DECL_HIDDEN DConfigPrivate : public DObjectPrivate
{
public:
explicit DConfigPrivate(DConfig *qq,
const QString &appId,
const QString &name,
const QString &subpath)
: DObjectPrivate(qq)
, appId(appId)
, name(name)
, subpath(subpath)
{
}
virtual ~DConfigPrivate() override;
inline bool invalid() const
{
const bool valid = backend && backend->isValid();
if (!valid)
qCWarning(cfLog, "DConfig is invalid of appid=%s name=%s, subpath=%s",
qPrintable(appId), qPrintable(name), qPrintable(subpath));
return !valid;
}
DConfigBackend *getOrCreateBackend();
DConfigBackend *createBackendByEnv();
QString appId;
QString name;
QString subpath;
QScopedPointer<DConfigBackend> backend;
D_DECLARE_PUBLIC(DConfig)
};
namespace {
#ifndef D_DISABLE_DCONFIG
class Q_DECL_HIDDEN FileBackend : public DConfigBackend
{
public:
explicit FileBackend(DConfigPrivate *o)
: owner(o)
{
}
virtual ~FileBackend() override;
virtual bool isValid() const override
{
return configFile && configFile->isValid();
}
virtual bool load(const QString &/*appId*/) override
{
if (configFile)
return true;
configFile.reset(new DConfigFile(owner->appId,owner->name, owner->subpath));
configCache.reset(configFile->createUserCache(getuid()));
const QString &prefix = localPrefix();
if (!configFile->load(prefix) || !configCache->load(prefix))
return false;
// generic config doesn't need to fallback to generic configration.
if (owner->appId == NoAppId)
return true;
std::unique_ptr<DConfigFile> file(new DConfigFile(NoAppId, owner->name, owner->subpath));
const bool canFallbackToGeneric = !file->meta()->metaPath(prefix).isEmpty();
if (canFallbackToGeneric) {
std::unique_ptr<DConfigCache> cache(file->createUserCache(getuid()));
if (file->load(prefix) && cache->load(prefix)) {
genericConfigFile.reset(file.release());
genericConfigCache.reset(cache.release());
}
}
return true;
}
virtual QStringList keyList() const override
{
return configFile->meta()->keyList();
}
virtual QVariant value(const QString &key, const QVariant &fallback) const override
{
const QVariant &vc = configFile->cacheValue(configCache.get(), key);
if (vc.isValid())
return vc;
// fallback to generic configuration, and use itself's configuration if generic isn't set.
if (genericConfigFile) {
const auto &tmp = genericConfigFile->cacheValue(genericConfigCache.get(), key);
if (tmp.isValid())
return tmp;
}
const QVariant &v = configFile->value(key);
if (v.isValid())
return v;
// fallback to default value of generic configuration.
const QVariant &vg = genericConfigFile->value(key);
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
{
// setValue's callerAppid is itself instead of config's appId.
if (configFile->setValue(key, value, DSGApplication::id(), configCache.get())) {
Q_EMIT owner->q_func()->valueChanged(key);
}
}
virtual void reset(const QString &key) override
{
setValue(key, QVariant());
}
virtual bool isReadOnly(const QString &key) const override
{
const auto vc = configFile->meta()->permissions(key);
return vc == DConfigFile::ReadOnly;
}
virtual QString name() const override
{
return QString("FileBackend");
}
private:
QString localPrefix() const
{
if (!envLocalPrefix.isEmpty()) {
return QString::fromLocal8Bit(envLocalPrefix);
}
return QString();
}
private:
QScopedPointer<DConfigFile> configFile;
QScopedPointer<DConfigCache> configCache;
QScopedPointer<DConfigFile> genericConfigFile;
QScopedPointer<DConfigCache> genericConfigCache;
DConfigPrivate* owner;
const QByteArray envLocalPrefix = qgetenv("DSG_DCONFIG_FILE_BACKEND_LOCAL_PREFIX");
};
FileBackend::~FileBackend()
{
const QString &prefix = localPrefix();
if (configCache) {
configCache->save(prefix);
configCache.reset();
}
if (configFile) {
configFile->save(prefix);
configFile.reset();
}
if (genericConfigCache) {
genericConfigCache->save(prefix);
genericConfigCache.reset();
}
if (genericConfigFile) {
genericConfigFile->save(prefix);
genericConfigFile.reset();
}
}
#ifndef D_DISABLE_DBUS_CONFIG
#define DSG_CONFIG "org.desktopspec.ConfigManager"
#define DSG_CONFIG_MANAGER "org.desktopspec.ConfigManager"
class Q_DECL_HIDDEN DBusBackend : public DConfigBackend
{
public:
explicit DBusBackend(DConfigPrivate* o):
owner(o),
config(nullptr)
{
}
virtual ~DBusBackend() override;
static bool isServiceRegistered()
{
return QDBusConnection::systemBus().interface()->isServiceRegistered(DSG_CONFIG);
}
static bool isServiceActivatable()
{
const QDBusReply<QStringList> activatableNames = QDBusConnection::systemBus().interface()->
callWithArgumentList(QDBus::AutoDetect,
QLatin1String("ListActivatableNames"),
QList<QVariant>());
// qInfo() << activatableNames.value() << activatableNames.value().contains(DSG_CONFIG);
return activatableNames.value().contains(DSG_CONFIG);
}
virtual bool isValid() const override
{
return config && config->isValid();
}
/*!
@~english
\internal
Initialize the DBus connection, the call acquireManager dynamically obtains a configuration connection,
The configuration file is then accessed through this configuration connection.
*/
virtual bool load(const QString &/*appId*/) override
{
if (config)
return true;
qCDebug(cfLog, "Try acquire config manager object form DBus");
DSGConfig dsg_config(DSG_CONFIG, "/", QDBusConnection::systemBus());
QDBusPendingReply<QDBusObjectPath> dbus_reply = dsg_config.acquireManager(owner->appId, owner->name, owner->subpath);
const QDBusObjectPath dbus_path = dbus_reply.value();
const auto path = dbus_path.path(); // 显式拷贝,避免其它线程共用systemBus连接而修改dbus数据
if (dbus_reply.isError() || path.isEmpty()) {
qCWarning(cfLog, "Can't acquire config manager. error:\"%s\"", qPrintable(dbus_reply.error().message()));
return false;
} else {
qCDebug(cfLog, "dbus path=\"%s\"", qPrintable(path));
config = new DSGConfigManager(DSG_CONFIG_MANAGER, path,
QDBusConnection::systemBus(), owner->q_func());
if (!config->isValid()) {
qCWarning(cfLog, "Can't acquire config path=\"%s\"", qPrintable(path));
config->deleteLater();
config = nullptr;
return false;
} else {
QObject::connect(config, &DSGConfigManager::valueChanged, owner->q_func(), &DConfig::valueChanged);
}
}
return true;
}
virtual QStringList keyList() const override
{
return config->keyList();
}
static QVariant decodeQDBusArgument(const QVariant &v)
{
if (v.canConvert<QDBusArgument>()) {
// we use QJsonValue to resolve all data type in DConfigInfo class, so it's type is equal QJsonValue::Type,
// now we parse Map and Array type to QVariant explicitly.
const QDBusArgument &complexType = v.value<QDBusArgument>();
switch (complexType.currentType()) {
case QDBusArgument::MapType: {
QVariantMap list;
complexType >> list;
QVariantMap res;
for (auto iter = list.begin(); iter != list.end(); iter++) {
res[iter.key()] = decodeQDBusArgument(iter.value());
}
return res;
}
case QDBusArgument::ArrayType: {
QVariantList list;
complexType >> list;
QVariantList res;
res.reserve(list.size());
for (const auto &item : std::as_const(list)) {
res << decodeQDBusArgument(item);
}
return res;
}
default:
qWarning("Can't parse the type, it maybe need user to do it, "
"QDBusArgument::ElementType: %d.", complexType.currentType());
}
}
return v;
}
virtual QVariant value(const QString &key, const QVariant &fallback) const override
{
auto reply = config->value(key);
reply.waitForFinished();
if (reply.isError()) {
qWarning() << "value error key:" << key << ", error message:" << reply.error().message();
return fallback;
}
return decodeQDBusArgument(reply.value().variant());
}
virtual bool isDefaultValue(const QString &key) const override
{
auto reply = config->isDefaultValue(key);
reply.waitForFinished();
if (reply.isError()) {
qWarning() << "Failed to call `isDefaultValue`, key:" << key
<< ", error message:" << reply.error().message();
return false;
}
return reply.value();
}
virtual void setValue(const QString &key, const QVariant &value) override
{
auto reply = config->setValue(key, QDBusVariant(value));
reply.waitForFinished();
if (reply.isError())
qCWarning(cfLog) << "Failed to setValue for the key:" << key
<< ", error message:" << reply.error();
}
virtual void reset(const QString &key) override
{
auto reply = config->reset(key);
reply.waitForFinished();
if (reply.isError())
qCWarning(cfLog) << "Failed to reset for the key:" << key
<< ", error message:" << reply.error();
}
virtual bool isReadOnly(const QString &key) const override
{
auto reply = config->permissions(key);
reply.waitForFinished();
if (reply.isError()) {
qWarning() << "Failed to call `permissions`, key:" << key
<< ", error message:" << reply.error().message();
return false;
}
return reply.value() == QLatin1String("readonly");
}
virtual QString name() const override
{
return QString("DBusBackend");
}
private:
DSGConfigManager *config;
DConfigPrivate* owner;
};
DBusBackend::~DBusBackend()
{
if (config) {
config->release();
}
}
#endif //D_DISABLE_DBUS_CONFIG
#else
class Q_DECL_HIDDEN QSettingBackend : public DConfigBackend
{
public:
explicit QSettingBackend(DConfigPrivate* o):
owner(o)
{
}
virtual ~QSettingBackend() override;
virtual bool isValid() const override
{
return settings;
}
virtual bool load(const QString &appid) override
{
Q_UNUSED(appid);
if (settings)
return true;
settings = new QSettings(owner->name, QSettings::IniFormat, owner->q_func());
settings->beginGroup(owner->subpath);
return true;
}
virtual QStringList keyList() const override
{
return settings->childKeys();
}
virtual QVariant value(const QString &key, const QVariant &fallback) const override
{
return settings->value(key, fallback);
}
virtual void setValue(const QString &key, const QVariant &value) override
{
settings->setValue(key, value);
}
virtual QString name() const override
{
return QString("QSettingBackend");
}
private:
QSettings *settings = nullptr;
DConfigPrivate* owner;
};
QSettingBackend::~QSettingBackend()
{
}
#endif //D_DISABLE_DCONFIG
}
DConfigPrivate::~DConfigPrivate()
{
backend.reset();
}
/*!
@~english
\internal
@brief Create a configuration backend
The default configuration backend preferentially selects the D-Bus interface in the configuration center or the file configuration backend interface based on environment variables.
If this environment variable is not configured, the configuration center service or file configuration backend interface will be selected according to whether the configuration center provides D-Bus services
*/
DConfigBackend *DConfigPrivate::getOrCreateBackend()
{
if (backend) {
return backend.data();
}
if (auto backendEnv = createBackendByEnv()) {
backend.reset(backendEnv);
return backend.data();
}
#ifndef D_DISABLE_DCONFIG
#ifndef D_DISABLE_DBUS_CONFIG
if (DBusBackend::isServiceRegistered() || DBusBackend::isServiceActivatable()) {
qCDebug(cfLog, "Fallback to DBus mode");
backend.reset(new DBusBackend(this));
}
#else
backend.reset(new FileBackend(this));
#endif //D_DISABLE_DBUS_CONFIG
#else
qCDebug(cfLog, "Fallback to QSettings mode");
backend.reset(new QSettingBackend(this));
#endif //D_DISABLE_DCONFIG
return backend.data();
}
/*!
@~english
\internal
@brief Create a configuration backend
Try to choose between configuring the D-Bus interface in the center or the file configuration backend interface based on the environment variables.
*/
DConfigBackend *DConfigPrivate::createBackendByEnv()
{
const QByteArray &envBackend = qgetenv("DSG_DCONFIG_BACKEND_TYPE");
if (!envBackend.isEmpty()) {
if (envBackend == "DBusBackend") {
#ifndef D_DISABLE_DCONFIG
#ifndef D_DISABLE_DBUS_CONFIG
if (DBusBackend::isServiceRegistered() || DBusBackend::isServiceActivatable()) {
qCDebug(cfLog, "Fallback to DBus mode");
return new DBusBackend(this);
}
#endif //D_DISABLE_DBUS_CONFIG
#endif //D_DISABLE_DCONFIG
} else if (envBackend == "FileBackend") {
#ifndef D_DISABLE_DCONFIG
qCDebug(cfLog, "Fallback to DConfigFile mode");
return new FileBackend(this);
#endif //D_DISABLE_DCONFIG
} else {
#ifndef D_DISABLE_DCONFIG
#else
qCDebug(cfLog, "Fallback to QSettings mode");
return new QSettingBackend(this);
#endif //D_DISABLE_DCONFIG
}
}
return nullptr;
}
/*!
@~english
@class Dtk::Core::DConfig
\inmodule dtkcore
@brief Configure the interface class provided by the policy
This interface specification defines the relevant interfaces provided by the development library for reading and writing configuration files,
If the application uses a development library that implements this specification, the application should use the interfaces provided by the development library first.
*/
/*!
@~english
* @brief Constructs the objects provided by the configuration policy
* \a name Configuration File Name
* \a subpath Subdirectory corresponding to the configuration file
* \a parent Parent object
*/
DConfig::DConfig(const QString &name, const QString &subpath, QObject *parent)
: DConfig(nullptr, name, subpath, parent)
{
}
DConfig::DConfig(DConfigBackend *backend, const QString &name, const QString &subpath, QObject *parent)
: DConfig(backend, _globalAppId.isEmpty() ? DSGApplication::id() : _globalAppId, name, subpath, parent)
{
}
/*!
@~english
* @brief Constructs the object provided by the configuration policy, specifying the application Id to which the configuration belongs.
* \a appId
* \a name
* \a subpath
* \a parent
* @return The constructed configuration policy object, which is released by the caller
* @note \a appId is not empty.
*/
DConfig *DConfig::create(const QString &appId, const QString &name, const QString &subpath, QObject *parent)
{
Q_ASSERT(appId != NoAppId);
return new DConfig(nullptr, appId, name, subpath, parent);
}
DConfig *DConfig::create(DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath, QObject *parent)
{
Q_ASSERT(appId != NoAppId);
return new DConfig(backend, appId, name, subpath, parent);
}
/*!
* \brief Constructs the object, and which is application.
* \param name
* \param subpath
* \param parent
* \return Dconfig object, which is released by the caller
* @note It's usually used for application independent, we should use DConfig::create if the configuration is a specific application.
*/
DConfig *DConfig::createGeneric(const QString &name, const QString &subpath, QObject *parent)
{
return new DConfig(nullptr, NoAppId, name, subpath, parent);
}
DConfig *DConfig::createGeneric(DConfigBackend *backend, const QString &name, const QString &subpath, QObject *parent)
{
return new DConfig(backend, NoAppId, name, subpath, parent);
}
/*!
* \brief Explicitly specify application Id for config.
* \param appId
* @note It's should be called before QCoreApplication constructed.
*/
void DConfig::setAppId(const QString &appId)
{
if (!_globalAppId.isEmpty()) {
qCWarning(cfLog, "`setAppId`should only be called once.");
}
_globalAppId = appId;
qCDebug(cfLog, "Explicitly specify application Id as appId=%s for config.", qPrintable(appId));
}
class DConfigThread : public QThread
{
public:
DConfigThread() {
setObjectName("DConfigGlobalThread");
start();
}
~DConfigThread() override {
if (isRunning()) {
quit();
wait();
}
}
};
Q_GLOBAL_STATIC(DConfigThread, _globalThread)
QThread *DConfig::globalThread()
{
return _globalThread;
}
/*!
@~english
* @brief Use custom configuration policy backend to construct objects
* \a backend The caller inherits the configuration policy backend of DConfigBackend
* \a appId The application Id of the configuration file. If it is blank, it will be the application Id by default
* \a name Configuration File Name
* \a subpath Subdirectory corresponding to the configuration file
* \a parent Parent object
* @note The caller only constructs backend, which is released by DConfig.
*/
DConfig::DConfig(DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath, QObject *parent)
: QObject(parent)
, DObject(*new DConfigPrivate(this, appId, name, subpath))
{
D_D(DConfig);
qCDebug(cfLog, "Load config of appid=%s name=%s, subpath=%s",
qPrintable(d->appId), qPrintable(d->name), qPrintable(d->subpath));
if (backend) {
d->backend.reset(backend);
}
if (auto backend = d->getOrCreateBackend()) {
backend->load(d->appId);
}
}
/*!
@~english
* @brief DConfig::backendName
* @return Configure policy backend name
* @note The caller can only access the DConfigBackend object with DConfig, so the DConfigBackend object is not returned.
*/
QString DConfig::backendName() const
{
D_DC(DConfig);
if (d->invalid())
return QString();
return d->backend->name();
}
/*!
@~english
* @brief Get all available configuration item names
* @return Configuration item name collection
*/
QStringList DConfig::keyList() const
{
D_DC(DConfig);
if (d->invalid())
return QStringList();
return d->backend->keyList();
}
/*!
@~english
* @brief Check whether the backend is available
* @return
*/
bool DConfig::isValid() const
{
D_DC(DConfig);
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
* @brief Get the corresponding value according to the configuration item name
* @param key Configuration Item Name
* @param fallback The default value provided after the configuration item value is not obtained
* @return
*/
QVariant DConfig::value(const QString &key, const QVariant &fallback) const
{
D_DC(DConfig);
if (d->invalid())
return fallback;
return d->backend->value(key, fallback);
}
/*!
@~english
* @brief Set the value according to the configuration item name
* @param key Configuration Item Name
* @param value Values that need to be updated
*/
void DConfig::setValue(const QString &key, const QVariant &value)
{
D_D(DConfig);
if (d->invalid())
return;
d->backend->setValue(key, value);
}
/*!
@~english
* @brief Set the default value corresponding to its configuration item. This value is overridden by the override mechanism. It is not necessarily the value defined in the meta in this configuration file
* @param key Configuration Item Name
*/
void DConfig::reset(const QString &key)
{
D_D(DConfig);
if (d->invalid())
return;
d->backend->reset(key);
}
/*!
* @~english
* @brief Check whether the configuration item is read-only
* @param key Configuration Item Name
* @return Return `true` if the configuration item is read-only, otherwise return `false`
*/
bool DConfig::isReadOnly(const QString &key) const
{
D_DC(DConfig);
if (d->invalid())
return false;
return d->backend->isReadOnly(key);
}
/*!
@~english
* @brief Return configuration file name
* @return
*/
QString DConfig::name() const
{
D_DC(DConfig);
return d->name;
}
/*!
@~english
* @brief Return the subdirectory corresponding to the configuration file
* @return
*/
QString DConfig::subpath() const
{
D_DC(DConfig);
return d->subpath;
}
DCORE_END_NAMESPACE