| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "configsetter.h" |
| 22 |
#include <QDebug> |
| 23 |
|
| 24 |
#include <QDir> |
| 25 |
#include <QFileInfo> |
| 26 |
#include <QProcess> |
| 27 |
|
| 28 |
const QString CONFIG_PATH = QDir::homePath() + |
| 29 |
"/.config/deepin/deepin-image-viewer/config.conf"; |
| 30 |
const QString DB_PATH = QDir::homePath() + |
| 31 |
"/.local/share/deepin/deepin-image-viewer/deepinimageviewer.db"; |
| 32 |
|
| 33 |
ConfigSetter::ConfigSetter(QObject *parent) : QObject(parent) |
| 34 |
, m_settings(nullptr) |
| 35 |
{ |
| 36 |
if (!QFileInfo(CONFIG_PATH).exists()) |
| 37 |
QProcess::startDetached(QString("rm %1").arg(DB_PATH)); |
| 38 |
|
| 39 |
m_settings = new QSettings(CONFIG_PATH, QSettings::IniFormat, this); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
ConfigSetter *ConfigSetter::m_setter = nullptr; |
| 44 |
ConfigSetter *ConfigSetter::instance() |
| 45 |
{ |
| 46 |
if (! m_setter) { |
| 47 |
m_setter = new ConfigSetter(); |
| 48 |
} |
| 49 |
|
| 50 |
return m_setter; |
| 51 |
} |
| 52 |
|
| 53 |
void ConfigSetter::setValue(const QString &group, const QString &key, |
| 54 |
const QVariant &value) |
| 55 |
{ |
| 56 |
// QMutexLocker locker(&m_mutex); |
| 57 |
|
| 58 |
m_settings->beginGroup(group); |
| 59 |
m_settings->setValue(key, value); |
| 60 |
m_settings->endGroup(); |
| 61 |
|
| 62 |
emit valueChanged(group, key, value); |
| 63 |
} |
| 64 |
|
| 65 |
QVariant ConfigSetter::value(const QString &group, const QString &key, |
| 66 |
const QVariant &defaultValue) |
| 67 |
{ |
| 68 |
QMutexLocker locker(&m_mutex); |
| 69 |
|
| 70 |
QVariant value; |
| 71 |
m_settings->beginGroup(group); |
| 72 |
if (key != "") |
| 73 |
value = m_settings->value(key, defaultValue); |
| 74 |
m_settings->endGroup(); |
| 75 |
|
| 76 |
return value; |
| 77 |
} |
| 78 |
|
| 79 |
//QStringList ConfigSetter::keys(const QString &group) |
| 80 |
//{ |
| 81 |
// QStringList v; |
| 82 |
// m_settings->beginGroup(group); |
| 83 |
// v = m_settings->childKeys(); |
| 84 |
// m_settings->endGroup(); |
| 85 |
|
| 86 |
// return v; |
| 87 |
//} |