| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "settings/backend/gsettingsbackend.h" |
| 6 |
|
| 7 |
//#include <QDebug> |
| 8 |
#include <QFile> |
| 9 |
#include <QJsonDocument> |
| 10 |
#include <QJsonObject> |
| 11 |
#include <QVariant> |
| 12 |
|
| 13 |
#if QT_HAS_INCLUDE(<QGSettings/QGSettings>) |
| 14 |
#include <QGSettings/QGSettings> |
| 15 |
#else |
| 16 |
#include <QGSettings> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <DSettings> |
| 20 |
|
| 21 |
DCORE_BEGIN_NAMESPACE |
| 22 |
|
| 23 |
QString unqtifyName(const QString &name) |
| 24 |
{ |
| 25 |
QString ret; |
| 26 |
for (auto p : name) { |
| 27 |
const QChar c(p); |
| 28 |
if (c.isUpper()) { |
| 29 |
ret.append("-"); |
| 30 |
ret.append(c.toLower().toLatin1()); |
| 31 |
} else { |
| 32 |
ret.append(c); |
| 33 |
} |
| 34 |
} |
| 35 |
return ret; |
| 36 |
} |
| 37 |
|
| 38 |
QString qtifyName(const QString &key) |
| 39 |
{ |
| 40 |
return QString(key).replace(".", "-").replace("_", "-"); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
class GSettingsBackendPrivate |
| 45 |
{ |
| 46 |
public: |
| 47 |
GSettingsBackendPrivate(GSettingsBackend *parent) : q_ptr(parent) {} |
| 48 |
|
| 49 |
QGSettings *gsettings; |
| 50 |
QMap<QString, QString> keyMap; |
| 51 |
|
| 52 |
GSettingsBackend *q_ptr; |
| 53 |
Q_DECLARE_PUBLIC(GSettingsBackend) |
| 54 |
}; |
| 55 |
|
| 56 |
/*! |
| 57 |
@~english |
| 58 |
@class Dtk::Core::GSettingsBackend |
| 59 |
\inmodule dtkcore |
| 60 |
@brief Storage backend of DSettings use gsettings. |
| 61 |
|
| 62 |
You should generate gsetting schema with /usr/lib/x86_64-linux-gnu/libdtk-$$VERSION/DCore/bin/dtk-settings. |
| 63 |
|
| 64 |
You can find this tool from libdtkcore-bin. use /usr/lib/x86_64-linux-gnu/libdtk-$$VERSION/DCore/bin/dtk-settings -h for help. |
| 65 |
*/ |
| 66 |
|
| 67 |
GSettingsBackend::GSettingsBackend(DSettings *settings, QObject *parent) : |
| 68 |
DSettingsBackend(parent), d_ptr(new GSettingsBackendPrivate(this)) |
| 69 |
{ |
| 70 |
Q_D(GSettingsBackend); |
| 71 |
|
| 72 |
QJsonObject settingsMeta = settings->meta(); |
| 73 |
auto gsettingsMeta = settingsMeta.value("gsettings").toObject(); |
| 74 |
auto id = gsettingsMeta.value("id").toString(); |
| 75 |
auto path = gsettingsMeta.value("path").toString(); |
| 76 |
|
| 77 |
for (QString key : settings->keys()) { |
| 78 |
auto gsettingsKey = QString(key).replace(".", "-").replace("_", "-"); |
| 79 |
d->keyMap.insert(gsettingsKey, key); |
| 80 |
} |
| 81 |
|
| 82 |
d->gsettings = new QGSettings(id.toUtf8(), path.toUtf8(), this); |
| 83 |
|
| 84 |
connect(d->gsettings, &QGSettings::changed, this, [ = ](const QString & key) { |
| 85 |
auto dk = d->keyMap.value(unqtifyName(key)); |
| 86 |
// qDebug() << "gsetting change" << key << d->gsettings->get(key); |
| 87 |
Q_EMIT optionChanged(dk, d->gsettings->get(key)); |
| 88 |
}); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
GSettingsBackend::~GSettingsBackend() |
| 93 |
{ |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
/*! |
| 98 |
@~english |
| 99 |
@brief List all gsettings keys. |
| 100 |
@return Return all gsettings keys. |
| 101 |
*/ |
| 102 |
QStringList GSettingsBackend::keys() const |
| 103 |
{ |
| 104 |
Q_D(const GSettingsBackend); |
| 105 |
return d->gsettings->keys(); |
| 106 |
} |
| 107 |
|
| 108 |
/*! |
| 109 |
@~english |
| 110 |
@brief Get value of key. |
| 111 |
@return Return the value of the given \a key. |
| 112 |
*/ |
| 113 |
QVariant GSettingsBackend::getOption(const QString &key) const |
| 114 |
{ |
| 115 |
Q_D(const GSettingsBackend); |
| 116 |
return d->gsettings->get(qtifyName(key)); |
| 117 |
} |
| 118 |
|
| 119 |
/*! |
| 120 |
@~english |
| 121 |
@brief Set value to gsettings |
| 122 |
Use the \a key to save the \a value. |
| 123 |
*/ |
| 124 |
void GSettingsBackend::doSetOption(const QString &key, const QVariant &value) |
| 125 |
{ |
| 126 |
Q_D(GSettingsBackend); |
| 127 |
if (value != d->gsettings->get(qtifyName(key))) { |
| 128 |
// qDebug() << "doSetOption" << key << d->gsettings->get(qtifyName(key)); |
| 129 |
d->gsettings->set(qtifyName(key), value); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/*! |
| 134 |
@~english |
| 135 |
@brief Trigger DSettings to sync option to storage. |
| 136 |
*/ |
| 137 |
void GSettingsBackend::doSync() |
| 138 |
{ |
| 139 |
Q_EMIT sync(); |
| 140 |
} |
| 141 |
|
| 142 |
DCORE_END_NAMESPACE |