2021-01-03 16:58:26 +08:00
|
|
|
#include <QtConcurrent>
|
2021-01-04 14:21:45 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QPalette>
|
|
|
|
#include "global-settings.h"
|
2020-12-28 20:29:13 +08:00
|
|
|
|
2021-01-03 16:58:26 +08:00
|
|
|
static GlobalSettings *global_instance = nullptr;
|
|
|
|
|
|
|
|
GlobalSettings *GlobalSettings::getInstance()
|
|
|
|
{
|
|
|
|
if (!global_instance) {
|
|
|
|
global_instance = new GlobalSettings;
|
|
|
|
}
|
|
|
|
return global_instance;
|
|
|
|
}
|
|
|
|
|
2020-12-28 20:29:13 +08:00
|
|
|
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2021-01-04 18:55:18 +08:00
|
|
|
m_settings = new QSettings("org.ukui", "ukui-search", this);
|
2021-01-04 14:21:45 +08:00
|
|
|
//the default number of transparency in mainwindow is 0.7
|
|
|
|
//if someone changes the num in mainwindow, here should be modified too
|
|
|
|
m_cache.insert(TRANSPARENCY_KEY, 0.7);
|
|
|
|
if (QGSettings::isSchemaInstalled(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID)) {
|
|
|
|
m_gsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this);
|
|
|
|
connect(m_gsettings, &QGSettings::changed, this, [=](const QString& key) {
|
|
|
|
if (key == TRANSPARENCY_KEY) {
|
|
|
|
m_cache.remove(TRANSPARENCY_KEY);
|
|
|
|
m_cache.insert(TRANSPARENCY_KEY, m_gsettings->get(TRANSPARENCY_KEY).toDouble());
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
m_cache.remove(TRANSPARENCY_KEY);
|
|
|
|
m_cache.insert(TRANSPARENCY_KEY, m_gsettings->get(TRANSPARENCY_KEY).toDouble());
|
2021-01-03 16:58:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalSettings::~GlobalSettings()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const QVariant GlobalSettings::getValue(const QString &key)
|
|
|
|
{
|
|
|
|
return m_cache.value(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalSettings::isExist(const QString &key)
|
|
|
|
{
|
|
|
|
return !m_cache.value(key).isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSettings::reset(const QString &key)
|
|
|
|
{
|
|
|
|
m_cache.remove(key);
|
|
|
|
QtConcurrent::run([=]() {
|
|
|
|
if (m_mutex.tryLock(1000)) {
|
|
|
|
m_settings->remove(key);
|
|
|
|
m_settings->sync();
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Q_EMIT this->valueChanged(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSettings::resetAll()
|
|
|
|
{
|
|
|
|
QStringList tmp = m_cache.keys();
|
|
|
|
m_cache.clear();
|
|
|
|
for (auto key : tmp) {
|
|
|
|
Q_EMIT this->valueChanged(key);
|
|
|
|
}
|
|
|
|
QtConcurrent::run([=]() {
|
|
|
|
if (m_mutex.tryLock(1000)) {
|
|
|
|
m_settings->clear();
|
|
|
|
m_settings->sync();
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-28 20:29:13 +08:00
|
|
|
|
2021-01-03 16:58:26 +08:00
|
|
|
QList<QString> GlobalSettings::getBlockDirs()
|
|
|
|
{
|
|
|
|
return m_cache.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSettings::setValue(const QString &key, const QVariant &value)
|
|
|
|
{
|
|
|
|
m_cache.insert(key, value);
|
|
|
|
QtConcurrent::run([=]() {
|
|
|
|
if (m_mutex.tryLock(1000)) {
|
|
|
|
m_settings->setValue(key, value);
|
|
|
|
m_settings->sync();
|
|
|
|
m_mutex.unlock();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSettings::forceSync(const QString &key)
|
|
|
|
{
|
|
|
|
m_settings->sync();
|
|
|
|
if (key.isNull()) {
|
|
|
|
m_cache.clear();
|
|
|
|
for (auto key : m_settings->allKeys()) {
|
|
|
|
m_cache.insert(key, m_settings->value(key));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_cache.remove(key);
|
|
|
|
m_cache.insert(key, m_settings->value(key));
|
|
|
|
}
|
2020-12-28 20:29:13 +08:00
|
|
|
}
|