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>
|
|
|
|
|
*
|
|
|
|
|
*/
|
2021-01-03 16:58:26 +08:00
|
|
|
|
#include <QtConcurrent>
|
2021-01-04 14:21:45 +08:00
|
|
|
|
#include <QPalette>
|
|
|
|
|
#include "global-settings.h"
|
2023-04-19 10:31:51 +08:00
|
|
|
|
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
|
2020-12-28 20:29:13 +08:00
|
|
|
|
|
2021-12-14 14:43:35 +08:00
|
|
|
|
using namespace UkuiSearch;
|
2022-11-28 18:11:55 +08:00
|
|
|
|
static GlobalSettings *globalInstance = nullptr;
|
2021-01-03 16:58:26 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
GlobalSettings *GlobalSettings::getInstance() {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
if(!globalInstance) {
|
|
|
|
|
globalInstance = new GlobalSettings;
|
2021-01-03 16:58:26 +08:00
|
|
|
|
}
|
2022-11-28 18:11:55 +08:00
|
|
|
|
return globalInstance;
|
2021-01-03 16:58:26 +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();
|
|
|
|
|
|
2021-05-17 20:54:48 +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();
|
2021-05-17 20:54:48 +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()) {
|
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
|
|
|
|
|
2022-11-28 18:11:55 +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)) {
|
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) {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(key == TRANSPARENCY_KEY) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
setValue(TRANSPARENCY_KEY, m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
2021-01-04 14:21:45 +08:00
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
2022-11-28 18:11:55 +08:00
|
|
|
|
Q_EMIT this->transparencyChanged(m_transGsettings->get(TRANSPARENCY_KEY).toDouble());
|
2021-01-04 14:21:45 +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());
|
|
|
|
|
}
|
2021-04-13 15:40:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
//主题,字体大小
|
|
|
|
|
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)) {
|
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) {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(key == STYLE_NAME_KEY) {
|
2021-04-13 15:40:19 +08:00
|
|
|
|
//当前主题改变时也发出paletteChanged信号,通知主界面刷新
|
2022-11-28 18:11:55 +08:00
|
|
|
|
setValue(STYLE_NAME_KEY, m_themeGsettings->get(STYLE_NAME_KEY).toString());
|
2021-04-20 16:24:07 +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());
|
2021-06-11 11:45:07 +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());
|
2021-06-11 11:45:07 +08:00
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
2021-05-12 15:58:38 +08:00
|
|
|
|
} else if (key == ICON_THEME_KEY) {
|
|
|
|
|
qApp->paletteChanged(qApp->palette());
|
2021-04-13 15:40:19 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
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());
|
|
|
|
|
}
|
2021-01-04 14:21:45 +08:00
|
|
|
|
}
|
2021-01-03 16:58:26 +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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-03 16:58:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
const QVariant GlobalSettings::getValue(const QString &key) {
|
2021-01-03 16:58:26 +08:00
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_mutex.lock();
|
|
|
|
|
QVariant value = m_cache.value(key);
|
|
|
|
|
m_mutex.unlock();
|
|
|
|
|
return value;
|
2021-01-03 16:58:26 +08:00
|
|
|
|
}
|
2020-12-28 20:29:13 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
bool GlobalSettings::setBlockDirs(const QString &path, int &returnCode, bool remove) {
|
|
|
|
|
if(remove) {
|
|
|
|
|
if(path.isEmpty()) {
|
2021-02-04 20:01:21 +08:00
|
|
|
|
returnCode = PATH_EMPTY;
|
2021-01-27 10:22:56 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->remove(path);
|
2021-01-10 09:23:02 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-01-12 16:07:50 +08:00
|
|
|
|
|
|
|
|
|
//why QSetting's key can't start with "/"??
|
2021-04-26 15:06:47 +08:00
|
|
|
|
QString pathKey = path.right(path.length() - 1);
|
2021-01-12 16:07:50 +08:00
|
|
|
|
|
2021-07-06 16:53:32 +08:00
|
|
|
|
if (pathKey.endsWith(QLatin1Char('/'))) {
|
|
|
|
|
pathKey = pathKey.mid(0, pathKey.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
QStringList blockDirs = m_blockDirsSettings->allKeys();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
for(QString i : blockDirs) {
|
2021-07-06 16:53:32 +08:00
|
|
|
|
if(FileUtils::isOrUnder(pathKey, i)) {
|
2021-02-04 20:01:21 +08:00
|
|
|
|
// returnCode = QString(tr("My parent folder has been blocked!"));
|
|
|
|
|
returnCode = PATH_PARENT_BLOCKED;
|
2021-01-08 19:08:24 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-06 16:53:32 +08:00
|
|
|
|
if(FileUtils::isOrUnder(i, pathKey))
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->remove(i);
|
2021-01-08 19:08:24 +08:00
|
|
|
|
}
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_blockDirsSettings->setValue(pathKey, "0");
|
2021-01-08 19:08:24 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
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;
|
2021-01-03 16:58:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void GlobalSettings::setSearchRecord(const QString &word, const QDateTime &time) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
QStringList keys = m_searchRecordSettings->allKeys();
|
2021-03-30 09:23:57 +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"));
|
2021-03-30 09:23:57 +08:00
|
|
|
|
if(keys.size() >= 20)
|
2022-11-28 18:11:55 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 11:17:07 +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) {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_mutex.lock();
|
2021-01-03 16:58:26 +08:00
|
|
|
|
m_cache.insert(key, value);
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_mutex.unlock();
|
2020-12-28 20:29:13 +08:00
|
|
|
|
}
|
2021-03-30 09:23:57 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void GlobalSettings::updateSearchHistory(QString key) {
|
|
|
|
|
if(key == "search") {
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_searchRecordSettings->sync();
|
2021-03-30 09:23:57 +08:00
|
|
|
|
m_history.clear();
|
2022-11-28 18:11:55 +08:00
|
|
|
|
for(QString i : m_searchRecordSettings->allKeys()) {
|
2021-03-30 09:23:57 +08:00
|
|
|
|
m_history.append(QUrl::fromPercentEncoding(i.toLocal8Bit()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|