2023-04-11 10:19:35 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023, 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: iaom <zhangpengfei@kylinos.cn>
|
|
|
|
*/
|
2023-04-06 10:39:10 +08:00
|
|
|
#include "config.h"
|
|
|
|
#include <QDir>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <mutex>
|
2023-04-08 11:45:59 +08:00
|
|
|
#include <QFile>
|
2023-04-06 10:39:10 +08:00
|
|
|
|
|
|
|
static const QString HOME_PATH = QDir::homePath();
|
|
|
|
static const QString CURRENT_INDEXABLE_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf";
|
|
|
|
static const QString INDEXABLE_DIR_KEY = "IndexableDir"; //兼容历史版本
|
|
|
|
static const QString CONFIG_VERSION_KEY = "ConfigVersion";
|
|
|
|
static const QString CONFIG_VERSION = "1.0";
|
|
|
|
static const QString GLOBAL_BLACK_LIST_KEY = "GlobalBlackList";
|
|
|
|
static const QStringList GLOBAL_BLACK_LIST{"/proc", "/sys", "/dev", "/tmp", "/run"};
|
2023-04-08 11:45:59 +08:00
|
|
|
static const QString GLOBAL_SETTINGS_GROUP = "GlobalSettings";
|
|
|
|
static const QString SEARCH_DIRS_GROUP = "SearchDirs";
|
|
|
|
|
2023-04-06 10:39:10 +08:00
|
|
|
static std::once_flag flag;
|
2023-04-08 11:45:59 +08:00
|
|
|
static Config *global_instance = nullptr;
|
2023-04-06 10:39:10 +08:00
|
|
|
Config *Config::self()
|
|
|
|
{
|
|
|
|
std::call_once(flag, [ & ] {
|
2023-04-08 11:45:59 +08:00
|
|
|
global_instance = new Config();
|
2023-04-06 10:39:10 +08:00
|
|
|
});
|
2023-04-08 11:45:59 +08:00
|
|
|
return global_instance;
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::addDir(const SearchDir &dir)
|
|
|
|
{
|
2023-04-08 11:45:59 +08:00
|
|
|
m_searchDirs.append(dir);
|
|
|
|
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
|
|
|
m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList());
|
|
|
|
m_settings->endGroup();
|
2023-04-06 10:39:10 +08:00
|
|
|
m_settings->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Config::removeDir(const SearchDir &dir)
|
|
|
|
{
|
|
|
|
QString url = QUrl::fromLocalFile(dir.getPath()).toString();
|
2023-04-08 11:45:59 +08:00
|
|
|
QStringList blackDirs = m_settings->value(SEARCH_DIRS_GROUP + "/" + url).toStringList();
|
|
|
|
removeDir(url);
|
|
|
|
m_searchDirs.removeOne(dir);
|
2023-04-06 10:39:10 +08:00
|
|
|
return blackDirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Config::isCompatibilityMode()
|
|
|
|
{
|
|
|
|
return m_compatibilityMode;
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:45:59 +08:00
|
|
|
QStringList Config::searchDirs()
|
2023-04-06 10:39:10 +08:00
|
|
|
{
|
|
|
|
QStringList tmp;
|
|
|
|
for(const SearchDir& dir : m_searchDirs) {
|
2023-04-08 11:45:59 +08:00
|
|
|
if(QFile::exists(dir.getPath())) {
|
|
|
|
tmp.append(dir.getPath());
|
|
|
|
} else {
|
|
|
|
m_searchDirs.removeOne(dir);
|
|
|
|
removeDir(QUrl::fromLocalFile(dir.getPath()).toString());
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:45:59 +08:00
|
|
|
QStringList Config::blackDirs()
|
2023-04-06 10:39:10 +08:00
|
|
|
{
|
|
|
|
QStringList tmp;
|
|
|
|
for(const SearchDir& dir : m_searchDirs) {
|
2023-04-08 11:45:59 +08:00
|
|
|
if(QFile::exists(dir.getPath())) {
|
|
|
|
tmp.append(dir.getBlackList());
|
|
|
|
} else {
|
|
|
|
m_searchDirs.removeOne(dir);
|
|
|
|
removeDir(QUrl::fromLocalFile(dir.getPath()).toString());
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Config::globalBlackList() const
|
|
|
|
{
|
2023-04-08 11:45:59 +08:00
|
|
|
return m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + GLOBAL_BLACK_LIST_KEY).toStringList();
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 11:45:59 +08:00
|
|
|
QVector<SearchDir> Config::searchDirObjects()
|
2023-04-06 10:39:10 +08:00
|
|
|
{
|
2023-04-08 11:45:59 +08:00
|
|
|
for(const SearchDir& dir : m_searchDirs) {
|
|
|
|
if(!QFile::exists(dir.getPath())) {
|
|
|
|
m_searchDirs.removeOne(dir);
|
|
|
|
removeDir(QUrl::fromLocalFile(dir.getPath()).toString());
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
return m_searchDirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::Config()
|
|
|
|
{
|
|
|
|
m_settings = new QSettings(CURRENT_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
|
2023-04-08 16:54:01 +08:00
|
|
|
if(!(m_settings->value(GLOBAL_SETTINGS_GROUP + "/" + CONFIG_VERSION_KEY).toString() == CONFIG_VERSION)) {
|
2023-04-06 10:39:10 +08:00
|
|
|
m_compatibilityMode = true;
|
2023-04-08 11:45:59 +08:00
|
|
|
for(const QString& path : m_settings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) {
|
|
|
|
if(QFile::exists(path)) {
|
|
|
|
SearchDir dir(path, false);
|
|
|
|
m_compatibleCache.append(dir);
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
|
|
|
m_settings->remove(INDEXABLE_DIR_KEY);
|
2023-04-08 11:45:59 +08:00
|
|
|
m_settings->beginGroup(GLOBAL_SETTINGS_GROUP);
|
2023-04-06 10:39:10 +08:00
|
|
|
m_settings->setValue(CONFIG_VERSION_KEY, CONFIG_VERSION);
|
2023-04-08 11:45:59 +08:00
|
|
|
m_settings->endGroup();
|
2023-04-06 10:39:10 +08:00
|
|
|
} else {
|
2023-04-08 11:45:59 +08:00
|
|
|
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
2023-04-06 10:39:10 +08:00
|
|
|
for(const QString& urlStr : m_settings->allKeys()) {
|
2023-04-08 11:45:59 +08:00
|
|
|
QString path = QUrl(urlStr).toLocalFile();
|
|
|
|
if(QFile::exists(path)) {
|
|
|
|
SearchDir dir(path, false);
|
|
|
|
dir.setBlackList(m_settings->value(urlStr).toStringList());
|
|
|
|
m_searchDirs.append(dir);
|
|
|
|
} else {
|
|
|
|
removeDir(urlStr);
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
m_settings->endGroup();
|
2023-04-06 10:39:10 +08:00
|
|
|
}
|
2023-04-08 16:54:01 +08:00
|
|
|
|
2023-04-08 11:45:59 +08:00
|
|
|
m_settings->beginGroup(GLOBAL_SETTINGS_GROUP);
|
2023-04-06 10:39:10 +08:00
|
|
|
if(m_settings->value(GLOBAL_BLACK_LIST_KEY).isNull()) {
|
|
|
|
m_settings->setValue(GLOBAL_BLACK_LIST_KEY, GLOBAL_BLACK_LIST);
|
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
m_settings->endGroup();
|
|
|
|
|
|
|
|
m_settings->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::removeDir(const QString &url)
|
|
|
|
{
|
|
|
|
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
|
|
|
m_settings->remove(url);
|
|
|
|
m_settings->endGroup();
|
2023-04-06 10:39:10 +08:00
|
|
|
m_settings->sync();
|
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
|
|
|
|
void Config::processCompatibleCache()
|
|
|
|
{
|
|
|
|
if(this->isCompatibilityMode()) {
|
|
|
|
m_settings->beginGroup(SEARCH_DIRS_GROUP);
|
2023-04-08 16:54:01 +08:00
|
|
|
if (m_compatibleCache.isEmpty()) {
|
|
|
|
SearchDir defaultDir(QDir::homePath());
|
|
|
|
if (defaultDir.error() == SearchDir::ErrorInfo::Successful) {
|
|
|
|
m_searchDirs.append(defaultDir);
|
|
|
|
m_settings->setValue(QUrl::fromLocalFile(defaultDir.getPath()).toString(), defaultDir.getBlackList());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (SearchDir dir : m_compatibleCache) {
|
|
|
|
dir.generateBlackList();
|
|
|
|
if (dir.error() == SearchDir::ErrorInfo::Successful) {
|
|
|
|
m_searchDirs.append(dir);
|
|
|
|
m_settings->setValue(QUrl::fromLocalFile(dir.getPath()).toString(), dir.getBlackList());
|
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_settings->endGroup();
|
|
|
|
|
|
|
|
m_settings->sync();
|
|
|
|
m_compatibleCache.clear();
|
|
|
|
m_compatibilityMode = false;
|
|
|
|
}
|
|
|
|
}
|