forked from openkylin/ukui-search
213 lines
8.6 KiB
C++
213 lines
8.6 KiB
C++
/*
|
||
* Copyright (C) 2020, KylinSoft Co., Ltd.
|
||
*
|
||
* This program is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* This program is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License
|
||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
*
|
||
* Authors: zhangzihao <zhangzihao@kylinos.cn>
|
||
* Modified by: zhangpengfei <zhangpengfei@kylinos.cn>
|
||
*
|
||
*/
|
||
#include <QtConcurrent>
|
||
#include <QPalette>
|
||
#include "global-settings.h"
|
||
|
||
using namespace UkuiSearch;
|
||
static GlobalSettings *globalInstance = nullptr;
|
||
|
||
GlobalSettings *GlobalSettings::getInstance() {
|
||
if(!globalInstance) {
|
||
globalInstance = new GlobalSettings;
|
||
}
|
||
return globalInstance;
|
||
}
|
||
|
||
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
|
||
{
|
||
//搜索黑名单过滤
|
||
m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this);
|
||
m_blockDirsSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
|
||
m_blockDirsSettings->setValue("These_are_block_dirs_conf_for_ukui_search","0");
|
||
m_blockDirsSettings->sync();
|
||
|
||
m_confWatcher = new QFileSystemWatcher(this);
|
||
m_confWatcher->addPath(BLOCK_DIRS);
|
||
connect(m_confWatcher, &QFileSystemWatcher::fileChanged, this, [ & ]() {
|
||
m_blockDirsSettings->sync();
|
||
m_confWatcher->addPath(BLOCK_DIRS);
|
||
});
|
||
//搜索历史记录
|
||
m_searchRecordSettings = new QSettings(SEARCH_HISTORY, QSettings::IniFormat, this);
|
||
m_searchRecordSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
|
||
for(QString i : m_searchRecordSettings->allKeys()) {
|
||
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
|
||
}
|
||
if(!QDBusConnection::sessionBus().connect("org.kylinssoclient.dbus",
|
||
"/org/kylinssoclient/path",
|
||
"org.freedesktop.kylinssoclient.interface",
|
||
"keyChanged",
|
||
this, SLOT(updateSearchHistory(QString))))
|
||
qWarning() << "Kylinssoclient Dbus connect fail!";
|
||
|
||
//全局页面透明度
|
||
//the default number of transparency for mainwindow is 0.7
|
||
setValue(TRANSPARENCY_KEY, 0.7);
|
||
if(QGSettings::isSchemaInstalled(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID)) {
|
||
m_transGsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this);
|
||
connect(m_transGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
|
||
if(key == TRANSPARENCY_KEY) {
|
||
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
||
qApp->paletteChanged(qApp->palette());
|
||
Q_EMIT this->transparencyChanged(m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
||
}
|
||
});
|
||
if(m_transGsettings->keys().contains(TRANSPARENCY_KEY)) {
|
||
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
||
}
|
||
}
|
||
|
||
//主题,字体大小
|
||
setValue(STYLE_NAME_KEY, "ukui-light");
|
||
setValue(FONT_SIZE_KEY, 11);
|
||
if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) {
|
||
m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
|
||
connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
|
||
if(key == STYLE_NAME_KEY) {
|
||
//当前主题改变时也发出paletteChanged信号,通知主界面刷新
|
||
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
|
||
qApp->paletteChanged(qApp->palette());
|
||
Q_EMIT this->styleChanged(m_themeGsettings->get(STYLE_NAME_KEY).toString());
|
||
} else if(key == FONT_SIZE_KEY) {
|
||
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
|
||
qApp->paletteChanged(qApp->palette());
|
||
} else if (key == ICON_THEME_KEY) {
|
||
qApp->paletteChanged(qApp->palette());
|
||
}
|
||
});
|
||
if(m_themeGsettings->keys().contains(STYLE_NAME_KEY)) {
|
||
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
|
||
}
|
||
if(m_themeGsettings->keys().contains(FONT_SIZE_KEY)) {
|
||
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
|
||
}
|
||
}
|
||
|
||
//文件索引与搜索插件相关设置
|
||
setValue(FILE_INDEX_ENABLE_KEY, false);
|
||
setValue(WEB_ENGINE_KEY, "baidu");
|
||
setValue(CONTENT_FUZZY_SEARCH_KEY, false);
|
||
if(QGSettings::isSchemaInstalled(UKUI_SEARCH_SCHEMAS)) {
|
||
m_searchGsettings = new QGSettings(UKUI_SEARCH_SCHEMAS, QByteArray(), this);
|
||
connect(m_searchGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
|
||
if(key == FILE_INDEX_ENABLE_KEY) {
|
||
bool fileSearchEnable = m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool();
|
||
setValue(FILE_INDEX_ENABLE_KEY, fileSearchEnable);
|
||
Q_EMIT fileSearchEnableChanged(fileSearchEnable);
|
||
} else if(key == WEB_ENGINE_KEY) {
|
||
QString webSearchEngine = m_searchGsettings->get(WEB_ENGINE_KEY).toString();
|
||
setValue(WEB_ENGINE_KEY, webSearchEngine);
|
||
Q_EMIT webSearchEngineChanged(webSearchEngine);
|
||
} else if (key == CONTENT_FUZZY_SEARCH_KEY) {
|
||
bool contentFuzzySearch = m_searchGsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool();
|
||
setValue(CONTENT_FUZZY_SEARCH_KEY, contentFuzzySearch);
|
||
Q_EMIT contentFuzzySearchEnableChanged(contentFuzzySearch);
|
||
}
|
||
});
|
||
if(m_searchGsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) {
|
||
setValue(FILE_INDEX_ENABLE_KEY, m_searchGsettings->get(FILE_INDEX_ENABLE_KEY).toBool());
|
||
}
|
||
if(m_searchGsettings->keys().contains(WEB_ENGINE_KEY)) {
|
||
setValue(WEB_ENGINE_KEY, m_searchGsettings->get(WEB_ENGINE_KEY).toString());
|
||
}
|
||
if(m_searchGsettings->keys().contains(CONTENT_FUZZY_SEARCH_KEY)) {
|
||
setValue(CONTENT_FUZZY_SEARCH_KEY, m_searchGsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool());
|
||
}
|
||
}
|
||
}
|
||
|
||
const QVariant GlobalSettings::getValue(const QString &key) {
|
||
|
||
m_mutex.lock();
|
||
QVariant value = m_cache.value(key);
|
||
m_mutex.unlock();
|
||
return value;
|
||
}
|
||
|
||
bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool remove) {
|
||
if(remove) {
|
||
if(path.isEmpty()) {
|
||
returnCode = PATH_EMPTY;
|
||
return false;
|
||
}
|
||
|
||
m_blockDirsSettings->remove(path);
|
||
return true;
|
||
}
|
||
|
||
//why QSetting's key can't start with "/"??
|
||
QString pathKey = path.right(path.length() - 1);
|
||
|
||
if (pathKey.endsWith(QLatin1Char('/'))) {
|
||
pathKey = pathKey.mid(0, pathKey.length() - 1);
|
||
}
|
||
|
||
QStringList blockDirs = m_blockDirsSettings->allKeys();
|
||
for(QString i : blockDirs) {
|
||
if(FileUtils::isOrUnder(pathKey, i)) {
|
||
// returnCode = QString(tr("My parent folder has been blocked!"));
|
||
returnCode = PATH_PARENT_BLOCKED;
|
||
return false;
|
||
}
|
||
|
||
if(FileUtils::isOrUnder(i, pathKey))
|
||
m_blockDirsSettings->remove(i);
|
||
}
|
||
m_blockDirsSettings->setValue(pathKey, "0");
|
||
return true;
|
||
}
|
||
|
||
QStringList GlobalSettings::getBlockDirs() {
|
||
return m_blockDirsSettings->allKeys();
|
||
}
|
||
|
||
void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) {
|
||
QStringList keys = m_searchRecordSettings->allKeys();
|
||
if(keys.contains(QString(QUrl::toPercentEncoding(word))))
|
||
m_history.removeOne(word);
|
||
m_searchRecordSettings->setValue(QString(QUrl::toPercentEncoding(word)), time.toString("yyyy-MM-dd hh:mm:ss"));
|
||
if(keys.size() >= 20)
|
||
m_searchRecordSettings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst())));
|
||
m_history.append(word);
|
||
}
|
||
|
||
QStringList GlobalSettings::getSearchRecord() {
|
||
return m_history;
|
||
}
|
||
|
||
//this method is designed for main process settings only!!
|
||
void GlobalSettings::setValue(const QString &key, const QVariant &value) {
|
||
m_mutex.lock();
|
||
m_cache.insert(key, value);
|
||
m_mutex.unlock();
|
||
}
|
||
|
||
void GlobalSettings::updateSearchHistory(QString key) {
|
||
if(key == "search") {
|
||
m_searchRecordSettings->sync();
|
||
m_history.clear();
|
||
for(QString i : m_searchRecordSettings->allKeys()) {
|
||
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
|
||
}
|
||
}
|
||
}
|