| 1 |
// SPDX-FileCopyrightText: 2016 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dsettings.h" |
| 6 |
|
| 7 |
#include <QMap> |
| 8 |
#include <QFile> |
| 9 |
#include <QJsonObject> |
| 10 |
#include <QJsonArray> |
| 11 |
#include <QJsonDocument> |
| 12 |
#include <QThread> |
| 13 |
#include <QDebug> |
| 14 |
|
| 15 |
#include "dsettingsoption.h" |
| 16 |
#include "dsettingsgroup.h" |
| 17 |
#include "dsettingsbackend.h" |
| 18 |
|
| 19 |
DCORE_BEGIN_NAMESPACE |
| 20 |
|
| 21 |
class DSettingsPrivate |
| 22 |
{ |
| 23 |
public: |
| 24 |
DSettingsPrivate(DSettings *parent) : q_ptr(parent) {} |
| 25 |
|
| 26 |
DSettingsBackend *backend = nullptr; |
| 27 |
QJsonObject meta; |
| 28 |
QMap <QString, OptionPtr> options; |
| 29 |
|
| 30 |
QMap<QString, GroupPtr> childGroups; |
| 31 |
QList<QString> childGroupKeys; |
| 32 |
|
| 33 |
DSettings *q_ptr; |
| 34 |
Q_DECLARE_PUBLIC(DSettings) |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
/*! |
| 39 |
@~english |
| 40 |
@class Dtk::Core::DSettingsBackend |
| 41 |
\inmodule dtkcore |
| 42 |
@brief DSettingsBackend is interface of DSettings storage class. |
| 43 |
|
| 44 |
Simaple example: |
| 45 |
|
| 46 |
@code |
| 47 |
{ |
| 48 |
"groups": [{ |
| 49 |
"key": "base", |
| 50 |
"name": "Basic settings", |
| 51 |
"groups": [{ |
| 52 |
"key": "open_action", |
| 53 |
"name": "Open Action", |
| 54 |
"options": [{ |
| 55 |
"key": "alway_open_on_new", |
| 56 |
"type": "checkbox", |
| 57 |
"text": "Always Open On New Windows", |
| 58 |
"default": true |
| 59 |
}, |
| 60 |
{ |
| 61 |
"key": "open_file_action", |
| 62 |
"name": "Open File:", |
| 63 |
"type": "combobox", |
| 64 |
"default": "" |
| 65 |
} |
| 66 |
] |
| 67 |
}, |
| 68 |
{ |
| 69 |
"key": "new_tab_windows", |
| 70 |
"name": "New Tab & Window", |
| 71 |
"options": [{ |
| 72 |
"key": "new_window_path", |
| 73 |
"name": "New Window Open:", |
| 74 |
"type": "combobox", |
| 75 |
"default": "" |
| 76 |
}, |
| 77 |
{ |
| 78 |
"key": "new_tab_path", |
| 79 |
"name": "New Tab Open:", |
| 80 |
"type": "combobox", |
| 81 |
"default": "" |
| 82 |
} |
| 83 |
] |
| 84 |
} |
| 85 |
] |
| 86 |
}] |
| 87 |
} |
| 88 |
@endcode |
| 89 |
|
| 90 |
How to read/write key and value: |
| 91 |
|
| 92 |
@code |
| 93 |
// init a storage backend |
| 94 |
QTemporaryFile tmpFile; |
| 95 |
tmpFile.open(); |
| 96 |
auto backend = new Dtk::Core::QSettingBackend(tmpFile.fileName()); |
| 97 |
|
| 98 |
// read settings from json |
| 99 |
auto settings = Dtk::Core::DSettings::fromJsonFile(":/resources/data/dfm-settings.json"); |
| 100 |
settings->setBackend(backend); |
| 101 |
|
| 102 |
// read value |
| 103 |
auto opt = settings->option("base.new_tab_windows.new_window_path"); |
| 104 |
qDebug() << opt->value(); |
| 105 |
|
| 106 |
// modify value |
| 107 |
opt->setValue("Test") |
| 108 |
qDebug() << opt->value(); |
| 109 |
@endcode |
| 110 |
@sa Dtk::Core::DSettingsOption |
| 111 |
@sa Dtk::Core::DSettingsGroup |
| 112 |
@sa Dtk::Core::DSettingsBackend |
| 113 |
@sa Dtk::Widget::DSettingsWidgetFactory |
| 114 |
@sa Dtk::Widget::DSettingsDialog |
| 115 |
*/ |
| 116 |
|
| 117 |
/*! |
| 118 |
@~english |
| 119 |
@fn virtual QStringList DSettingsBackend::keys() const = 0; |
| 120 |
@brief return all key of storage. |
| 121 |
*/ |
| 122 |
/*! |
| 123 |
@~english |
| 124 |
@fn virtual QVariant DSettingsBackend::getOption(const QString &key) const = 0; |
| 125 |
@brief get value by \a key. |
| 126 |
*/ |
| 127 |
/*! |
| 128 |
@~english |
| 129 |
@fn virtual void DSettingsBackend::doSync() = 0; |
| 130 |
@brief do the real sync action. |
| 131 |
*/ |
| 132 |
/*! |
| 133 |
@~english |
| 134 |
@fn virtual void DSettingsBackend::doSetOption(const QString &key, const QVariant &value) = 0; |
| 135 |
@brief write \a key / \a value to storage. |
| 136 |
*/ |
| 137 |
/*! |
| 138 |
@~english |
| 139 |
@fn void DSettingsBackend::optionChanged(const QString &key, const QVariant &value); |
| 140 |
@brief emitted when option \a value changed. |
| 141 |
*/ |
| 142 |
/*! |
| 143 |
@~english |
| 144 |
@fn void DSettingsBackend::sync(); |
| 145 |
@brief private signal, please do not use it. |
| 146 |
*/ |
| 147 |
/*! |
| 148 |
@~english |
| 149 |
@fn void DSettingsBackend::setOption(const QString &key, const QVariant &value); |
| 150 |
@brief private signal, please do not use it. |
| 151 |
|
| 152 |
\internal |
| 153 |
\a key \a value |
| 154 |
*/ |
| 155 |
|
| 156 |
/*! |
| 157 |
@~english |
| 158 |
@class Dtk::Core::DSettings |
| 159 |
\inmodule dtkcore |
| 160 |
@brief DSettings is the basic library that provides unified configuration storage and interface generation tools for Dtk applications. |
| 161 |
|
| 162 |
DSetting uses json as the description file for the application configuration. The configuration of application query is divided into two basic levels: group / key value. |
| 163 |
The standard Dtk configuration control generally contains only three levels of group / subgroup / key values. For keys with more than three levels, you can use the. |
| 164 |
DSettings's API interface reads and writes, but cannot be displayed on standard DSettingsDialogs. |
| 165 |
|
| 166 |
|
| 167 |
Simple configuration fileļ¼ |
| 168 |
@code |
| 169 |
{ |
| 170 |
"groups": [{ |
| 171 |
"key": "base", |
| 172 |
"name": "Basic settings", |
| 173 |
"groups": [{ |
| 174 |
"key": "open_action", |
| 175 |
"name": "Open Action", |
| 176 |
"options": [{ |
| 177 |
"key": "alway_open_on_new", |
| 178 |
"type": "checkbox", |
| 179 |
"text": "Always Open On New Windows", |
| 180 |
"default": true |
| 181 |
}, |
| 182 |
{ |
| 183 |
"key": "open_file_action", |
| 184 |
"name": "Open File:", |
| 185 |
"type": "combobox", |
| 186 |
"default": "" |
| 187 |
} |
| 188 |
] |
| 189 |
}, |
| 190 |
{ |
| 191 |
"key": "new_tab_windows", |
| 192 |
"name": "New Tab & Window", |
| 193 |
"options": [{ |
| 194 |
"key": "new_window_path", |
| 195 |
"name": "New Window Open:", |
| 196 |
"type": "combobox", |
| 197 |
"default": "" |
| 198 |
}, |
| 199 |
{ |
| 200 |
"key": "new_tab_path", |
| 201 |
"name": "New Tab Open:", |
| 202 |
"type": "combobox", |
| 203 |
"default": "" |
| 204 |
} |
| 205 |
] |
| 206 |
} |
| 207 |
] |
| 208 |
}] |
| 209 |
} |
| 210 |
@endcode |
| 211 |
|
| 212 |
The group contains a root group of base with two subgroups: open_action/new_tab_windows, each of which contains several options. |
| 213 |
The complete access id for the configuration "New Window Open:" is base.new_tab_windows.new_window_path. |
| 214 |
|
| 215 |
How to read/write key and value: |
| 216 |
@code |
| 217 |
// init a storage backend |
| 218 |
QTemporaryFile tmpFile; |
| 219 |
tmpFile.open(); |
| 220 |
auto backend = new Dtk::Core::QSettingBackend(tmpFile.fileName()); |
| 221 |
|
| 222 |
// read settings from json |
| 223 |
auto settings = Dtk::Core::DSettings::fromJsonFile(":/resources/data/dfm-settings.json"); |
| 224 |
settings->setBackend(backend); |
| 225 |
|
| 226 |
// read value |
| 227 |
auto opt = settings->option("base.new_tab_windows.new_window_path"); |
| 228 |
qDebug() << opt->value(); |
| 229 |
|
| 230 |
// modify value |
| 231 |
opt->setValue("Test") |
| 232 |
qDebug() << opt->value(); |
| 233 |
@endcode |
| 234 |
@sa Dtk::Core::DSettingsOption |
| 235 |
@sa Dtk::Core::DSettingsGroup |
| 236 |
@sa Dtk::Core::DSettingsBackend |
| 237 |
@sa Dtk::Widget::DSettingsWidgetFactory |
| 238 |
@sa Dtk::Widget::DSettingsDialog |
| 239 |
*/ |
| 240 |
|
| 241 |
DSettings::DSettings(QObject *parent) : |
| 242 |
QObject(parent), dd_ptr(new DSettingsPrivate(this)) |
| 243 |
{ |
| 244 |
} |
| 245 |
|
| 246 |
DSettings::~DSettings() |
| 247 |
{ |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
void DSettings::setBackend(DSettingsBackend *backend) |
| 252 |
{ |
| 253 |
Q_D(DSettings); |
| 254 |
if (nullptr == backend) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
if (d->backend != nullptr) { |
| 259 |
qWarning() << "set backend to exist " << d->backend; |
| 260 |
} |
| 261 |
|
| 262 |
d->backend = backend; |
| 263 |
|
| 264 |
|
| 265 |
auto backendWriteThread = new QThread; |
| 266 |
d->backend->moveToThread(backendWriteThread); |
| 267 |
|
| 268 |
connect(d->backend, &DSettingsBackend::optionChanged, |
| 269 |
this, [ = ](const QString & key, const QVariant & value) { |
| 270 |
option(key)->setValue(value); |
| 271 |
}); |
| 272 |
// exit and delete thread |
| 273 |
connect(this, &DSettings::destroyed, this, [backendWriteThread](){ |
| 274 |
if (backendWriteThread->isRunning()) { |
| 275 |
backendWriteThread->quit(); |
| 276 |
backendWriteThread->wait(); |
| 277 |
} |
| 278 |
backendWriteThread->deleteLater(); |
| 279 |
}); |
| 280 |
|
| 281 |
backendWriteThread->start(); |
| 282 |
|
| 283 |
// load form backend |
| 284 |
loadValue(); |
| 285 |
} |
| 286 |
|
| 287 |
/*! |
| 288 |
@~english |
| 289 |
@brief Get DSettings from \a json. The returned data needs to be manually released after use. |
| 290 |
*/ |
| 291 |
QPointer<DSettings> DSettings::fromJson(const QByteArray &json) |
| 292 |
{ |
| 293 |
auto settingsPtr = QPointer<DSettings>(new DSettings); |
| 294 |
settingsPtr->parseJson(json); |
| 295 |
return settingsPtr; |
| 296 |
} |
| 297 |
|
| 298 |
QPointer<DSettings> DSettings::fromJsonFile(const QString &filepath) |
| 299 |
{ |
| 300 |
QFile jsonFile(filepath); |
| 301 |
jsonFile.open(QIODevice::ReadOnly); |
| 302 |
auto jsonData = jsonFile.readAll(); |
| 303 |
jsonFile.close(); |
| 304 |
|
| 305 |
return DSettings::fromJson(jsonData); |
| 306 |
} |
| 307 |
|
| 308 |
QJsonObject DSettings::meta() const |
| 309 |
{ |
| 310 |
Q_D(const DSettings); |
| 311 |
return d->meta; |
| 312 |
} |
| 313 |
|
| 314 |
QStringList DSettings::keys() const |
| 315 |
{ |
| 316 |
Q_D(const DSettings); |
| 317 |
return d->options.keys(); |
| 318 |
} |
| 319 |
|
| 320 |
QPointer<DSettingsOption> DSettings::option(const QString &key) const |
| 321 |
{ |
| 322 |
Q_D(const DSettings); |
| 323 |
return d->options.value(key); |
| 324 |
} |
| 325 |
|
| 326 |
QVariant DSettings::value(const QString &key) const |
| 327 |
{ |
| 328 |
Q_D(const DSettings); |
| 329 |
auto opt = d->options.value(key); |
| 330 |
if (opt.isNull()) { |
| 331 |
return QVariant(); |
| 332 |
} |
| 333 |
|
| 334 |
return opt->value(); |
| 335 |
} |
| 336 |
|
| 337 |
QStringList DSettings::groupKeys() const |
| 338 |
{ |
| 339 |
Q_D(const DSettings); |
| 340 |
return d->childGroupKeys; |
| 341 |
} |
| 342 |
|
| 343 |
QList<QPointer<DSettingsGroup> > DSettings::groups() const |
| 344 |
{ |
| 345 |
Q_D(const DSettings); |
| 346 |
return d->childGroups.values(); |
| 347 |
} |
| 348 |
/*! |
| 349 |
@~english |
| 350 |
@brief DSettings::group will recurrence find childGroup |
| 351 |
\a key |
| 352 |
@return |
| 353 |
*/ |
| 354 |
QPointer<DSettingsGroup> DSettings::group(const QString &key) const |
| 355 |
{ |
| 356 |
Q_D(const DSettings); |
| 357 |
auto childKeylist = key.split("."); |
| 358 |
if (0 >= childKeylist.length()) { |
| 359 |
return nullptr; |
| 360 |
} |
| 361 |
|
| 362 |
auto mainGroupKey = childKeylist.value(0); |
| 363 |
if (1 >= childKeylist.length()) { |
| 364 |
return d->childGroups.value(mainGroupKey); |
| 365 |
} |
| 366 |
|
| 367 |
return d->childGroups.value(mainGroupKey)->childGroup(key); |
| 368 |
} |
| 369 |
|
| 370 |
QList<QPointer<DSettingsOption> > DSettings::options() const |
| 371 |
{ |
| 372 |
Q_D(const DSettings); |
| 373 |
return d->options.values(); |
| 374 |
} |
| 375 |
|
| 376 |
QVariant DSettings::getOption(const QString &key) const |
| 377 |
{ |
| 378 |
QPointer<DSettingsOption> optionPointer = option(key); |
| 379 |
if (optionPointer) { |
| 380 |
return optionPointer->value(); |
| 381 |
} |
| 382 |
return QVariant(); |
| 383 |
} |
| 384 |
|
| 385 |
void DSettings::setOption(const QString &key, const QVariant &value) |
| 386 |
{ |
| 387 |
option(key)->setValue(value); |
| 388 |
} |
| 389 |
|
| 390 |
void DSettings::sync() |
| 391 |
{ |
| 392 |
Q_D(DSettings); |
| 393 |
if (!d->backend) { |
| 394 |
qWarning() << "backend was not setted..!"; |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
d->backend->doSync(); |
| 399 |
} |
| 400 |
|
| 401 |
void DSettings::reset() |
| 402 |
{ |
| 403 |
Q_D(DSettings); |
| 404 |
|
| 405 |
for (auto option : d->options) { |
| 406 |
if (option->canReset()) { |
| 407 |
setOption(option->key(), option->defaultValue()); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if (!d->backend) { |
| 412 |
qWarning() << "backend was not setted..!"; |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
d->backend->sync(); |
| 417 |
} |
| 418 |
|
| 419 |
void DSettings::parseJson(const QByteArray &json) |
| 420 |
{ |
| 421 |
Q_D(DSettings); |
| 422 |
|
| 423 |
auto jsonDoc = QJsonDocument::fromJson(json); |
| 424 |
d->meta = jsonDoc.object(); |
| 425 |
auto mainGroups = d->meta.value("groups"); |
| 426 |
for (auto groupJson : mainGroups.toArray()) { |
| 427 |
auto group = DSettingsGroup::fromJson("", groupJson.toObject()); |
| 428 |
group->setParent(this); |
| 429 |
for (auto option : group->options()) { |
| 430 |
d->options.insert(option->key(), option); |
| 431 |
} |
| 432 |
d->childGroupKeys << group->key(); |
| 433 |
d->childGroups.insert(group->key(), group); |
| 434 |
} |
| 435 |
|
| 436 |
for (auto option : d->options.values()) { |
| 437 |
d->options.insert(option->key(), option); |
| 438 |
connect(option.data(), &DSettingsOption::valueChanged, |
| 439 |
this, [ = ](QVariant value) { |
| 440 |
if (d->backend) { |
| 441 |
Q_EMIT d->backend->setOption(option->key(), value); |
| 442 |
} else { |
| 443 |
qWarning() << "backend was not setted..!"; |
| 444 |
} |
| 445 |
Q_EMIT valueChanged(option->key(), value); |
| 446 |
}); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
void DSettings::loadValue() |
| 451 |
{ |
| 452 |
Q_D(DSettings); |
| 453 |
if (!d->backend) { |
| 454 |
qWarning() << "backend was not setted..!"; |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
for (auto key : d->backend->keys()) { |
| 459 |
auto value = d->backend->getOption(key); |
| 460 |
auto opt = option(key); |
| 461 |
if (!value.isValid() || opt.isNull()) { |
| 462 |
continue; |
| 463 |
} |
| 464 |
|
| 465 |
opt->blockSignals(true); |
| 466 |
opt->setValue(value); |
| 467 |
opt->blockSignals(false); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
DCORE_END_NAMESPACE |