2018-09-29 15:56:04 +03:00
|
|
|
/*
|
2019-01-26 07:51:58 +03:00
|
|
|
* Copyright (C) 2018-2019 QuasarApp.
|
2018-09-29 15:56:04 +03:00
|
|
|
* Distributed under the lgplv3 software license, see the accompanying
|
|
|
|
* Everyone is permitted to copy and distribute verbatim copies
|
|
|
|
* of this license document, but changing it is not allowed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
#include <QSettings>
|
2019-09-23 18:36:29 +03:00
|
|
|
#include <QCoreApplication>
|
2018-09-29 15:56:04 +03:00
|
|
|
|
|
|
|
using namespace QuasarAppUtils;
|
|
|
|
|
2019-09-28 16:41:43 +03:00
|
|
|
Settings::Settings(SettingsSaveMode mode) {
|
2019-09-23 18:36:29 +03:00
|
|
|
auto name = QCoreApplication::applicationName();
|
2019-09-27 20:35:37 +03:00
|
|
|
auto company = QCoreApplication::organizationName();
|
2019-09-23 18:36:29 +03:00
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = "QuasarAppUtils";
|
|
|
|
}
|
|
|
|
|
2019-09-27 20:35:37 +03:00
|
|
|
if (company.isEmpty()) {
|
|
|
|
company = "QuasarApp";
|
|
|
|
}
|
|
|
|
|
|
|
|
_settings = new QSettings(QSettings::IniFormat, QSettings::Scope::UserScope, company, name);
|
2019-09-28 16:41:43 +03:00
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsSaveMode Settings::getMode() const
|
|
|
|
{
|
|
|
|
return _mode;
|
|
|
|
}
|
2019-09-27 20:35:37 +03:00
|
|
|
|
2019-09-28 16:41:43 +03:00
|
|
|
void Settings::setMode(const SettingsSaveMode &mode)
|
|
|
|
{
|
|
|
|
_mode = mode;
|
2018-09-29 15:56:04 +03:00
|
|
|
}
|
|
|
|
|
2019-09-28 16:41:43 +03:00
|
|
|
Settings *Settings::initSettings(SettingsSaveMode mode) {
|
|
|
|
static Settings* res = new Settings(mode);
|
2018-09-29 15:56:04 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-09-23 18:36:29 +03:00
|
|
|
Settings *Settings::get() {
|
|
|
|
return initSettings();
|
|
|
|
}
|
|
|
|
|
2018-09-29 15:56:04 +03:00
|
|
|
const Settings *Settings::getConst() {
|
2019-09-23 18:36:29 +03:00
|
|
|
return initSettings();
|
2018-09-29 15:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Settings::getValue(const QString &key, const QVariant &def) {
|
|
|
|
return _settings->value(key, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Settings::getStrValue(const QString &key, const QString &def) {
|
|
|
|
return getValue(key, QVariant(def)).toString();
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:41:43 +03:00
|
|
|
void Settings::sync() {
|
|
|
|
_settings->sync();
|
|
|
|
}
|
|
|
|
|
2018-09-29 15:56:04 +03:00
|
|
|
void Settings::setValue(const QString key, const QVariant &value) {
|
|
|
|
_settings->setValue(key, value);
|
|
|
|
|
|
|
|
emit valueChanged(key, value);
|
|
|
|
emit valueStrChanged(key, value.toString());
|
|
|
|
|
2019-09-28 16:41:43 +03:00
|
|
|
if (_mode == SettingsSaveMode::Auto) {
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
|
2018-09-29 15:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::setStrValue(const QString &key, const QString &value) {
|
|
|
|
setValue(key, value);
|
|
|
|
}
|