2022-06-18 10:46:14 +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"
|
|
|
|
|
|
|
|
|
|
using namespace UkuiSearch;
|
2022-11-28 18:11:55 +08:00
|
|
|
|
static GlobalSettings *globalInstance = nullptr;
|
2022-06-18 10:46:14 +08:00
|
|
|
|
|
|
|
|
|
GlobalSettings *GlobalSettings::getInstance() {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
if(!globalInstance) {
|
|
|
|
|
globalInstance = new GlobalSettings;
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
2022-11-28 18:11:55 +08:00
|
|
|
|
return globalInstance;
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
//搜索黑名单过滤
|
|
|
|
|
m_blockDirsSettings = new QSettings(BLOCK_DIRS, QSettings::IniFormat, this);
|
|
|
|
|
m_blockDirsSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
|
2023-04-12 15:05:25 +08:00
|
|
|
|
if (!QFile::exists(BLOCK_DIRS)) {
|
|
|
|
|
QFile file(BLOCK_DIRS);
|
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->sync();
|
|
|
|
|
|
2022-06-18 10:46:14 +08:00
|
|
|
|
m_confWatcher = new QFileSystemWatcher(this);
|
|
|
|
|
m_confWatcher->addPath(BLOCK_DIRS);
|
|
|
|
|
connect(m_confWatcher, &QFileSystemWatcher::fileChanged, this, [ & ]() {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->sync();
|
2022-06-18 10:46:14 +08:00
|
|
|
|
m_confWatcher->addPath(BLOCK_DIRS);
|
|
|
|
|
});
|
2022-11-28 18:11:55 +08:00
|
|
|
|
//搜索历史记录
|
|
|
|
|
m_searchRecordSettings = new QSettings(SEARCH_HISTORY, QSettings::IniFormat, this);
|
|
|
|
|
m_searchRecordSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
|
|
|
|
|
for(QString i : m_searchRecordSettings->allKeys()) {
|
2022-06-18 10:46:14 +08:00
|
|
|
|
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!";
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
//全局页面透明度
|
|
|
|
|
//the default number of transparency for mainwindow is 0.7
|
|
|
|
|
setValue(TRANSPARENCY_KEY, 0.7);
|
2022-06-18 10:46:14 +08:00
|
|
|
|
if(QGSettings::isSchemaInstalled(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID)) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_transGsettings = new QGSettings(CONTROL_CENTER_PERSONALISE_GSETTINGS_ID, QByteArray(), this);
|
|
|
|
|
connect(m_transGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
|
2022-06-18 10:46:14 +08:00
|
|
|
|
if(key == TRANSPARENCY_KEY) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
2022-06-18 10:46:14 +08:00
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
2022-11-28 18:11:55 +08:00
|
|
|
|
Q_EMIT this->transparencyChanged(m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-11-28 18:11:55 +08:00
|
|
|
|
if(m_transGsettings->keys().contains(TRANSPARENCY_KEY)) {
|
|
|
|
|
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
|
|
|
|
}
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
//主题,字体大小
|
|
|
|
|
setValue(STYLE_NAME_KEY, "ukui-light");
|
|
|
|
|
setValue(FONT_SIZE_KEY, 11);
|
2022-06-18 10:46:14 +08:00
|
|
|
|
if(QGSettings::isSchemaInstalled(THEME_GSETTINGS_ID)) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_themeGsettings = new QGSettings(THEME_GSETTINGS_ID, QByteArray(), this);
|
|
|
|
|
connect(m_themeGsettings, &QGSettings::changed, this, [ = ](const QString & key) {
|
2022-06-18 10:46:14 +08:00
|
|
|
|
if(key == STYLE_NAME_KEY) {
|
|
|
|
|
//当前主题改变时也发出paletteChanged信号,通知主界面刷新
|
2022-11-28 18:11:55 +08:00
|
|
|
|
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
|
2022-06-18 10:46:14 +08:00
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
2022-11-28 18:11:55 +08:00
|
|
|
|
Q_EMIT this->styleChanged(m_themeGsettings->get(STYLE_NAME_KEY).toString());
|
2022-06-18 10:46:14 +08:00
|
|
|
|
} else if(key == FONT_SIZE_KEY) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
setValue(FONT_SIZE_KEY, m_themeGsettings->get(FONT_SIZE_KEY).toDouble());
|
2022-06-18 10:46:14 +08:00
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
|
|
|
|
} else if (key == ICON_THEME_KEY) {
|
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-11-28 18:11:55 +08:00
|
|
|
|
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());
|
|
|
|
|
}
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
//文件索引与搜索插件相关设置
|
|
|
|
|
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)) {
|
2022-12-09 18:00:24 +08:00
|
|
|
|
setValue(WEB_ENGINE_KEY, m_searchGsettings->get(WEB_ENGINE_KEY).toString());
|
2022-11-28 18:11:55 +08:00
|
|
|
|
}
|
|
|
|
|
if(m_searchGsettings->keys().contains(CONTENT_FUZZY_SEARCH_KEY)) {
|
|
|
|
|
setValue(CONTENT_FUZZY_SEARCH_KEY, m_searchGsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool());
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
const QVariant GlobalSettings::getValue(const QString &key) {
|
2022-06-18 10:46:14 +08:00
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_mutex.lock();
|
|
|
|
|
QVariant value = m_cache.value(key);
|
|
|
|
|
m_mutex.unlock();
|
|
|
|
|
return value;
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool remove) {
|
|
|
|
|
if(remove) {
|
|
|
|
|
if(path.isEmpty()) {
|
|
|
|
|
returnCode = PATH_EMPTY;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->remove(path);
|
2022-06-18 10:46:14 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
QStringList blockDirs = m_blockDirsSettings->allKeys();
|
2022-06-18 10:46:14 +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))
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->remove(i);
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->setValue(pathKey, "0");
|
2022-06-18 10:46:14 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList GlobalSettings::getBlockDirs() {
|
2023-04-12 15:05:25 +08:00
|
|
|
|
QStringList blockList;
|
|
|
|
|
QStringList tmp = m_blockDirsSettings->allKeys();
|
|
|
|
|
for (const QString& blockDir : tmp) {
|
|
|
|
|
QString wholePath = "/" + blockDir;
|
|
|
|
|
if (QFile::exists(wholePath)) {
|
|
|
|
|
blockList.append(wholePath);
|
|
|
|
|
} else {
|
|
|
|
|
m_blockDirsSettings->remove(blockDir);
|
|
|
|
|
m_blockDirsSettings->sync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return blockList;
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
QStringList keys = m_searchRecordSettings->allKeys();
|
2022-06-18 10:46:14 +08:00
|
|
|
|
if(keys.contains(QString(QUrl::toPercentEncoding(word))))
|
|
|
|
|
m_history.removeOne(word);
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_searchRecordSettings->setValue(QString(QUrl::toPercentEncoding(word)), time.toString("yyyy-MM-dd hh:mm:ss"));
|
2022-06-18 10:46:14 +08:00
|
|
|
|
if(keys.size() >= 20)
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_searchRecordSettings->remove(QString(QUrl::toPercentEncoding(m_history.takeFirst())));
|
2022-06-18 10:46:14 +08:00
|
|
|
|
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) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_mutex.lock();
|
2022-06-18 10:46:14 +08:00
|
|
|
|
m_cache.insert(key, value);
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_mutex.unlock();
|
2022-06-18 10:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GlobalSettings::updateSearchHistory(QString key) {
|
|
|
|
|
if(key == "search") {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_searchRecordSettings->sync();
|
2022-06-18 10:46:14 +08:00
|
|
|
|
m_history.clear();
|
2022-11-28 18:11:55 +08:00
|
|
|
|
for(QString i : m_searchRecordSettings->allKeys()) {
|
2022-06-18 10:46:14 +08:00
|
|
|
|
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|