ukui-search/libsearch/global-settings.cpp

213 lines
8.6 KiB
C++
Raw Normal View History

2021-01-29 11:43:07 +08:00
/*
* 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"
2021-12-14 14:43:35 +08:00
using namespace UkuiSearch;
static GlobalSettings *globalInstance = nullptr;
2021-04-26 15:06:47 +08:00
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();
2021-05-17 20:54:48 +08:00
m_confWatcher = new QFileSystemWatcher(this);
m_confWatcher->addPath(BLOCK_DIRS);
connect(m_confWatcher, &QFileSystemWatcher::fileChanged, this, [ & ]() {
m_blockDirsSettings->sync();
2021-05-17 20:54:48 +08:00
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()) {
2021-03-30 09:23:57 +08:00
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
}
if(!QDBusConnection::sessionBus().connect("org.kylinssoclient.dbus",
2021-04-26 15:06:47 +08:00
"/org/kylinssoclient/path",
"org.freedesktop.kylinssoclient.interface",
"keyChanged",
this, SLOT(updateSearchHistory(QString))))
qWarning() << "Kylinssoclient Dbus connect fail!";
2021-03-30 09:23:57 +08:00
//全局页面透明度
//the default number of transparency for mainwindow is 0.7
setValue(TRANSPARENCY_KEY, 0.7);
2021-04-26 15:06:47 +08:00
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) {
2021-04-26 15:06:47 +08:00
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);
2021-04-26 15:06:47 +08:00
if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) {
m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
2021-04-26 15:06:47 +08:00
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());
2021-05-12 15:58:38 +08:00
} 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).toBool());
}
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;
}
2021-04-26 15:06:47 +08:00
bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool remove) {
if(remove) {
if(path.isEmpty()) {
returnCode = PATH_EMPTY;
return false;
}
m_blockDirsSettings->remove(path);
2021-01-10 09:23:02 +08:00
return true;
}
//why QSetting's key can't start with "/"??
2021-04-26 15:06:47 +08:00
QString pathKey = path.right(path.length() - 1);
if (pathKey.endsWith(QLatin1Char('/'))) {
pathKey = pathKey.mid(0, pathKey.length() - 1);
}
QStringList blockDirs = m_blockDirsSettings->allKeys();
2021-04-26 15:06:47 +08:00
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;
}
2021-04-26 15:06:47 +08:00
QStringList GlobalSettings::getBlockDirs() {
return m_blockDirsSettings->allKeys();
}
2021-04-26 15:06:47 +08:00
void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) {
QStringList keys = m_searchRecordSettings->allKeys();
2021-03-30 09:23:57 +08:00
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"));
2021-03-30 09:23:57 +08:00
if(keys.size() >= 20)
m_searchRecordSettings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst())));
2021-03-30 09:23:57 +08:00
m_history.append(word);
2021-03-20 15:37:54 +08:00
}
2021-04-26 15:06:47 +08:00
QStringList GlobalSettings::getSearchRecord() {
2021-03-30 09:23:57 +08:00
return m_history;
2021-03-20 15:37:54 +08:00
}
//this method is designed for main process settings only!!
2021-04-26 15:06:47 +08:00
void GlobalSettings::setValue(const QString &key, const QVariant &value) {
m_mutex.lock();
m_cache.insert(key, value);
m_mutex.unlock();
}
2021-03-30 09:23:57 +08:00
2021-04-26 15:06:47 +08:00
void GlobalSettings::updateSearchHistory(QString key) {
if(key == "search") {
m_searchRecordSettings->sync();
2021-03-30 09:23:57 +08:00
m_history.clear();
for(QString i : m_searchRecordSettings->allKeys()) {
2021-03-30 09:23:57 +08:00
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
}
}
}