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 <mutex>
|
2023-04-08 11:45:59 +08:00
|
|
|
|
#include <QFile>
|
2023-04-11 20:53:28 +08:00
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QVariant>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonArray>
|
2023-04-06 10:39:10 +08:00
|
|
|
|
|
|
|
|
|
static const QString HOME_PATH = QDir::homePath();
|
2023-04-11 20:53:28 +08:00
|
|
|
|
static const QString OLD_INDEXABLE_DIR_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf";
|
|
|
|
|
static const QString SEARCH_DIRS_SETTINGS = HOME_PATH + "/.config/org.ukui/ukui-search/ukui-search-dirs.json";
|
|
|
|
|
static const QString SEARCH_DIRS_SETTINGS_DIR = HOME_PATH + "/.config/org.ukui/ukui-search";
|
2023-04-06 10:39:10 +08:00
|
|
|
|
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-11 20:53:28 +08:00
|
|
|
|
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
|
|
|
|
QJsonArray blackList;
|
|
|
|
|
for(const QString& path : dir.getBlackList()) {
|
|
|
|
|
blackList.append(path);
|
|
|
|
|
}
|
|
|
|
|
searchDirData.insert(dir.getPath(), blackList);
|
|
|
|
|
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
|
|
|
|
save(m_settingsData);
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Config::removeDir(const SearchDir &dir)
|
|
|
|
|
{
|
2023-04-11 20:53:28 +08:00
|
|
|
|
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
|
|
|
|
QStringList blackDirs = searchDirData.take(dir.getPath()).toVariant().toStringList();
|
|
|
|
|
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
|
|
|
|
save(m_settingsData);
|
2023-04-06 10:39:10 +08:00
|
|
|
|
return blackDirs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Config::isCompatibilityMode()
|
|
|
|
|
{
|
|
|
|
|
return m_compatibilityMode;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 20:53:28 +08:00
|
|
|
|
void Config::save(const QJsonObject &jsonDocData)
|
|
|
|
|
{
|
|
|
|
|
QDir dir;
|
|
|
|
|
QString configFileDir(SEARCH_DIRS_SETTINGS_DIR);
|
|
|
|
|
if (!dir.exists(configFileDir)) {
|
|
|
|
|
if (!dir.mkdir(configFileDir)) {
|
|
|
|
|
qWarning() << "Unable to create settings config file.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile settingsFile(SEARCH_DIRS_SETTINGS);
|
|
|
|
|
if(!settingsFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
|
|
|
qWarning() << "Fail to open file " << SEARCH_DIRS_SETTINGS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settingsFile.write(QJsonDocument(jsonDocData).toJson()) == -1) {
|
|
|
|
|
qWarning() << "Error saving configuration file.";
|
|
|
|
|
}
|
|
|
|
|
settingsFile.flush();
|
|
|
|
|
settingsFile.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Config::initSettingsData(QJsonObject &data)
|
|
|
|
|
{
|
|
|
|
|
//全局配置
|
|
|
|
|
QJsonObject globalSettings;
|
|
|
|
|
|
|
|
|
|
QJsonArray blackList;
|
|
|
|
|
for(const QString& path : GLOBAL_BLACK_LIST) {
|
|
|
|
|
blackList.append(path);
|
|
|
|
|
}
|
|
|
|
|
globalSettings.insert(GLOBAL_BLACK_LIST_KEY, blackList);
|
|
|
|
|
|
|
|
|
|
data.insert(CONFIG_VERSION_KEY, CONFIG_VERSION);
|
|
|
|
|
data.insert(GLOBAL_SETTINGS_GROUP, globalSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 11:45:59 +08:00
|
|
|
|
QStringList Config::searchDirs()
|
2023-04-06 10:39:10 +08:00
|
|
|
|
{
|
|
|
|
|
QStringList tmp;
|
2023-04-11 20:53:28 +08:00
|
|
|
|
bool needSave = false;
|
|
|
|
|
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
|
|
|
|
for(const QString& path : searchDirData.keys()) {
|
|
|
|
|
if(QFile::exists(path)) {
|
|
|
|
|
tmp.append(path);
|
2023-04-08 11:45:59 +08:00
|
|
|
|
} else {
|
2023-04-11 20:53:28 +08:00
|
|
|
|
searchDirData.remove(path);
|
|
|
|
|
needSave = true;
|
2023-04-08 11:45:59 +08:00
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-11 20:53:28 +08:00
|
|
|
|
if(needSave) {
|
|
|
|
|
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
|
|
|
|
save(m_settingsData);
|
|
|
|
|
}
|
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;
|
2023-04-11 20:53:28 +08:00
|
|
|
|
bool needSave = false;
|
|
|
|
|
QJsonObject searchDir = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
|
|
|
|
for(const QString& path : searchDir.keys()) {
|
|
|
|
|
if(QFile::exists(path)) {
|
|
|
|
|
tmp.append(searchDir.value(path).toVariant().toStringList());
|
2023-04-08 11:45:59 +08:00
|
|
|
|
} else {
|
2023-04-11 20:53:28 +08:00
|
|
|
|
searchDir.remove(path);
|
|
|
|
|
needSave = true;
|
2023-04-08 11:45:59 +08:00
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-11 20:53:28 +08:00
|
|
|
|
if(needSave) {
|
|
|
|
|
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDir);
|
|
|
|
|
save(m_settingsData);
|
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Config::globalBlackList() const
|
|
|
|
|
{
|
2023-04-11 20:53:28 +08:00
|
|
|
|
return m_settingsData.value(GLOBAL_SETTINGS_GROUP).toObject().value(GLOBAL_BLACK_LIST_KEY).toVariant().toStringList();
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Config::Config()
|
|
|
|
|
{
|
2023-04-11 20:53:28 +08:00
|
|
|
|
m_oldSettings = new QSettings(OLD_INDEXABLE_DIR_SETTINGS, QSettings::IniFormat);
|
|
|
|
|
QFile settingsFile(SEARCH_DIRS_SETTINGS);
|
|
|
|
|
|
|
|
|
|
if(!settingsFile.exists()) {
|
2023-04-06 10:39:10 +08:00
|
|
|
|
m_compatibilityMode = true;
|
2023-04-11 20:53:28 +08:00
|
|
|
|
qDebug() << m_oldSettings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList();
|
|
|
|
|
for(const QString& path : m_oldSettings->value(INDEXABLE_DIR_KEY + "/" + INDEXABLE_DIR_KEY).toStringList()) {
|
2023-04-08 11:45:59 +08:00
|
|
|
|
if(QFile::exists(path)) {
|
|
|
|
|
SearchDir dir(path, false);
|
2023-04-11 20:53:28 +08:00
|
|
|
|
qDebug() << "Found old config path" << path;
|
2023-04-08 11:45:59 +08:00
|
|
|
|
m_compatibleCache.append(dir);
|
|
|
|
|
}
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-11 20:53:28 +08:00
|
|
|
|
|
|
|
|
|
//老版本配置会在更新后被清空,如果allKeys为空说明已经运行过新版本,此时如果新版配置不存在则添加家目录作为默认搜索目录
|
|
|
|
|
if(m_compatibleCache.isEmpty() && m_oldSettings->allKeys().isEmpty()) {
|
|
|
|
|
SearchDir dir(HOME_PATH, false);
|
|
|
|
|
m_compatibleCache.append(dir);
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-11 20:53:28 +08:00
|
|
|
|
m_oldSettings->clear();
|
|
|
|
|
m_oldSettings->sync();
|
|
|
|
|
initSettingsData(m_settingsData);
|
|
|
|
|
save(m_settingsData);
|
|
|
|
|
return;
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-08 16:54:01 +08:00
|
|
|
|
|
2023-04-11 20:53:28 +08:00
|
|
|
|
if (!settingsFile.open(QFile::ReadOnly)) {
|
|
|
|
|
qWarning() << "SettingsManagerPrivate: configuration file " << settingsFile.fileName() << "open failed !";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QByteArray byteArray = settingsFile.readAll();
|
|
|
|
|
settingsFile.close();
|
|
|
|
|
QJsonParseError errRpt;
|
|
|
|
|
QJsonDocument jsonDocument(QJsonDocument::fromJson(byteArray, &errRpt));
|
|
|
|
|
if (errRpt.error != QJsonParseError::NoError) {
|
|
|
|
|
qWarning() << "Incorrect configuration files. JSON parse error";
|
|
|
|
|
initSettingsData(m_settingsData);
|
|
|
|
|
save(m_settingsData);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 读取配置文件
|
|
|
|
|
QJsonObject settingsData = jsonDocument.object();
|
|
|
|
|
QString version = settingsData.value(CONFIG_VERSION_KEY).toString();
|
|
|
|
|
if (version != QString(CONFIG_VERSION)) {
|
|
|
|
|
qWarning() << "Settings version check failed, old: " << version << " new:" << CONFIG_VERSION;
|
|
|
|
|
//TODO:这里做一些兼容处理
|
|
|
|
|
} else {
|
|
|
|
|
m_settingsData.swap(settingsData);
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
|
|
2023-04-11 20:53:28 +08:00
|
|
|
|
QStringList pathToRemove;
|
|
|
|
|
for(const QString& path : m_settingsData.value(SEARCH_DIRS_GROUP).toObject().keys()) {
|
|
|
|
|
if(!QFile::exists(path)) {
|
|
|
|
|
pathToRemove.append(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!pathToRemove.isEmpty()) {
|
|
|
|
|
removeDir(pathToRemove);
|
|
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 20:53:28 +08:00
|
|
|
|
void Config::removeDir(const QStringList &paths)
|
2023-04-08 11:45:59 +08:00
|
|
|
|
{
|
2023-04-11 20:53:28 +08:00
|
|
|
|
QJsonObject searchDirData = m_settingsData.value(SEARCH_DIRS_GROUP).toObject();
|
|
|
|
|
for(const QString& path : paths) {
|
|
|
|
|
searchDirData.remove(path);
|
|
|
|
|
}
|
|
|
|
|
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirData);
|
|
|
|
|
save(m_settingsData);
|
2023-04-06 10:39:10 +08:00
|
|
|
|
}
|
2023-04-08 11:45:59 +08:00
|
|
|
|
|
|
|
|
|
void Config::processCompatibleCache()
|
|
|
|
|
{
|
2023-04-11 20:53:28 +08:00
|
|
|
|
if(this->isCompatibilityMode() && !m_compatibleCache.isEmpty()) {
|
|
|
|
|
QJsonObject searchDirsdata;
|
|
|
|
|
for (SearchDir dir : m_compatibleCache) {
|
|
|
|
|
dir.generateBlackList();
|
|
|
|
|
if (dir.error() == SearchDir::ErrorInfo::Successful) {
|
|
|
|
|
QJsonArray blackList;
|
|
|
|
|
for(const QString& path : dir.getBlackList()) {
|
|
|
|
|
blackList.append(path);
|
2023-04-08 16:54:01 +08:00
|
|
|
|
}
|
2023-04-11 20:53:28 +08:00
|
|
|
|
searchDirsdata.insert(dir.getPath(), blackList);
|
|
|
|
|
m_settingsData.insert(SEARCH_DIRS_GROUP, searchDirsdata);
|
2023-04-08 11:45:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-11 20:53:28 +08:00
|
|
|
|
save(m_settingsData);
|
2023-04-08 11:45:59 +08:00
|
|
|
|
m_compatibleCache.clear();
|
|
|
|
|
m_compatibilityMode = false;
|
|
|
|
|
}
|
|
|
|
|
}
|