Debian Codegraph @

include/global/dconfig.h

raw
1 // SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #ifndef DCONFIG_H
6 #define DCONFIG_H
7
8 #include <dtkcore_global.h>
9 #include <DObject>
10
11 #include <QObject>
12 #include <QVariant>
13
14 DCORE_BEGIN_NAMESPACE
15 class DConfigBackend {
16 public:
17 virtual ~DConfigBackend();
18 virtual bool isValid() const = 0;
19 virtual bool load(const QString &/*appId*/) = 0;
20 virtual QStringList keyList() const = 0;
21 virtual QVariant value(const QString &/*key*/, const QVariant &/*fallback*/) const = 0;
22 virtual void setValue(const QString &/*key*/, const QVariant &/*value*/) = 0;
23 virtual void reset(const QString &key) { setValue(key, QVariant());}
24 virtual QString name() const {return QString("");}
25 virtual bool isDefaultValue(const QString &/*key*/) const { return true; }
26 };
27
28 class DConfigPrivate;
29 class LIBDTKCORESHARED_EXPORT DConfig : public QObject, public DObject
30 {
31 Q_OBJECT
32 D_DECLARE_PRIVATE(DConfig)
33
34 Q_PROPERTY(QStringList keyList READ keyList CONSTANT FINAL)
35
36 public:
37 explicit DConfig(const QString &name, const QString &subpath = QString(),
38 QObject *parent = nullptr);
39
40 explicit DConfig(DConfigBackend *backend, const QString &name, const QString &subpath = QString(),
41 QObject *parent = nullptr);
42
43 static DConfig *create(const QString &appId, const QString &name, const QString &subpath = QString(),
44 QObject *parent = nullptr);
45 static DConfig *create(DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath = QString(),
46 QObject *parent = nullptr);
47 static DConfig *createGeneric(const QString &name, const QString &subpath = QString(),
48 QObject *parent = nullptr);
49 static DConfig *createGeneric(DConfigBackend *backend, const QString &name, const QString &subpath = QString(),
50 QObject *parent = nullptr);
51
52 static void setAppId(const QString &appId);
53 static QThread *globalThread();
54
55 QString backendName() const;
56
57 QStringList keyList() const;
58
59 bool isValid() const;
60 bool isDefaultValue(const QString &key) const;
61 QVariant value(const QString &key, const QVariant &fallback = QVariant()) const;
62 void setValue(const QString &key, const QVariant &value);
63 void reset(const QString &key);
64
65 QString name() const;
66 QString subpath() const;
67
68 Q_SIGNALS:
69 void valueChanged(const QString &key);
70
71 private:
72 explicit DConfig(DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath,
73 QObject *parent = nullptr);
74 };
75
76 DCORE_END_NAMESPACE
77
78 #endif // DCONFIG_H