| 1 |
// SPDX-FileCopyrightText: 2016 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "settings/backend/qsettingbackend.h" |
| 6 |
|
| 7 |
#include <QDebug> |
| 8 |
#include <QMutex> |
| 9 |
#include <QSettings> |
| 10 |
|
| 11 |
DCORE_BEGIN_NAMESPACE |
| 12 |
|
| 13 |
class QSettingBackendPrivate |
| 14 |
{ |
| 15 |
public: |
| 16 |
QSettingBackendPrivate(QSettingBackend *parent) : q_ptr(parent) {} |
| 17 |
|
| 18 |
QSettings *settings = nullptr; |
| 19 |
QMutex writeLock; |
| 20 |
|
| 21 |
QSettingBackend *q_ptr; |
| 22 |
Q_DECLARE_PUBLIC(QSettingBackend) |
| 23 |
}; |
| 24 |
|
| 25 |
/*! |
| 26 |
@~english |
| 27 |
@class Dtk::Core::QSettingBackend |
| 28 |
\inmodule dtkcore |
| 29 |
@brief Storage DSetttings to an QSettings. |
| 30 |
*/ |
| 31 |
|
| 32 |
/*! |
| 33 |
@~english |
| 34 |
@brief Save data to filepath with QSettings::NativeFormat format. |
| 35 |
\a filepath is path to storage data. |
| 36 |
\a parent |
| 37 |
*/ |
| 38 |
QSettingBackend::QSettingBackend(const QString &filepath, QObject *parent) : |
| 39 |
DSettingsBackend(parent), d_ptr(new QSettingBackendPrivate(this)) |
| 40 |
{ |
| 41 |
Q_D(QSettingBackend); |
| 42 |
|
| 43 |
d->settings = new QSettings(filepath, QSettings::NativeFormat, this); |
| 44 |
qDebug() << "create config" << d->settings->fileName(); |
| 45 |
} |
| 46 |
|
| 47 |
QSettingBackend::~QSettingBackend() |
| 48 |
{ |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/*! |
| 53 |
@~english |
| 54 |
@brief List all keys of QSettings |
| 55 |
@return |
| 56 |
*/ |
| 57 |
QStringList QSettingBackend::keys() const |
| 58 |
{ |
| 59 |
Q_D(const QSettingBackend); |
| 60 |
return d->settings->childGroups(); |
| 61 |
} |
| 62 |
|
| 63 |
/*! |
| 64 |
@~english |
| 65 |
@brief Get value of key from QSettings |
| 66 |
\a key |
| 67 |
@return |
| 68 |
*/ |
| 69 |
QVariant QSettingBackend::getOption(const QString &key) const |
| 70 |
{ |
| 71 |
Q_D(const QSettingBackend); |
| 72 |
d->settings->beginGroup(key); |
| 73 |
auto value = d->settings->value("value"); |
| 74 |
d->settings->endGroup(); |
| 75 |
return value; |
| 76 |
} |
| 77 |
|
| 78 |
/*! |
| 79 |
@~english |
| 80 |
@brief Set value of key to QSettings |
| 81 |
\a key |
| 82 |
\a value |
| 83 |
*/ |
| 84 |
void QSettingBackend::doSetOption(const QString &key, const QVariant &value) |
| 85 |
{ |
| 86 |
Q_D(QSettingBackend); |
| 87 |
d->writeLock.lock(); |
| 88 |
d->settings->beginGroup(key); |
| 89 |
auto oldValue = d->settings->value("value"); |
| 90 |
if (oldValue != value) { |
| 91 |
d->settings->setValue("value", value); |
| 92 |
} |
| 93 |
d->settings->endGroup(); |
| 94 |
d->settings->sync(); |
| 95 |
d->writeLock.unlock(); |
| 96 |
} |
| 97 |
|
| 98 |
/*! |
| 99 |
@~english |
| 100 |
@brief Trigger DSettings to save option value to QSettings |
| 101 |
*/ |
| 102 |
void QSettingBackend::doSync() |
| 103 |
{ |
| 104 |
Q_D(QSettingBackend); |
| 105 |
d->settings->sync(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
DCORE_END_NAMESPACE |