2022-10-26 18:01:40 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022, 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>
|
|
|
|
*
|
|
|
|
*/
|
2024-01-23 11:18:55 +08:00
|
|
|
#include "file-indexer-config.h"
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
2024-02-22 10:09:47 +08:00
|
|
|
#include <QMutexLocker>
|
2024-01-23 11:18:55 +08:00
|
|
|
|
|
|
|
static const QString INDEX_SETTINGS = QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-service.conf";
|
|
|
|
static const QString CONTENT_INDEX_TARGET_TYPE = "contentIndexTarget";
|
|
|
|
static const QString OCR_CONTENT_INDEX_TARGET_TYPE = "ocrContentIndexTarget";
|
2023-09-28 10:54:58 +08:00
|
|
|
/**
|
|
|
|
* changelog: 1.1 增加ocr开关
|
|
|
|
*/
|
|
|
|
static const QString CONFIG_VERSION = QStringLiteral("1.1");
|
2022-10-26 18:01:40 +08:00
|
|
|
static const QByteArray UKUI_SEARCH_SCHEMAS = QByteArrayLiteral("org.ukui.search.settings");
|
|
|
|
static const QString FILE_INDEX_ENABLE_KEY = QStringLiteral("fileIndexEnable");
|
|
|
|
static const QString CONTENT_INDEX_ENABLE_KEY = QStringLiteral("contentIndexEnable");
|
2023-05-31 17:59:20 +08:00
|
|
|
static const QString CONTENT_FUZZY_SEARCH_KEY = QStringLiteral("contentFuzzySearch");
|
2023-09-28 10:54:58 +08:00
|
|
|
static const QString CONTENT_INDEX_ENABLE_OCR_KEY = QStringLiteral("contentIndexEnableOcr");
|
2022-10-26 18:01:40 +08:00
|
|
|
static const QString META_DATA_INDEX_ENABLE_KEY = QStringLiteral("metaDataIndexEnable");
|
2023-08-03 09:09:18 +08:00
|
|
|
static const QString CONFIG_VERSION_KEY = QStringLiteral("version");
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
static std::once_flag flag;
|
2024-01-23 11:18:55 +08:00
|
|
|
static UkuiSearch::FileIndexerConfig *globalInstance = nullptr;
|
|
|
|
using namespace UkuiSearch;
|
2022-10-26 18:01:40 +08:00
|
|
|
FileIndexerConfig *FileIndexerConfig::getInstance()
|
|
|
|
{
|
|
|
|
std::call_once(flag, [ & ] {
|
2024-01-23 11:18:55 +08:00
|
|
|
globalInstance = new FileIndexerConfig();
|
2022-10-26 18:01:40 +08:00
|
|
|
});
|
2024-01-23 11:18:55 +08:00
|
|
|
return globalInstance;
|
2022-10-26 18:01:40 +08:00
|
|
|
}
|
|
|
|
|
2023-03-16 17:42:25 +08:00
|
|
|
FileIndexerConfig::FileIndexerConfig(QObject *parent)
|
2022-10-26 18:01:40 +08:00
|
|
|
: QObject(parent),
|
|
|
|
m_dirWatcher(DirWatcher::getDirWatcher())
|
|
|
|
{
|
|
|
|
connect(m_dirWatcher, &DirWatcher::appendIndexItem, this, &FileIndexerConfig::appendIndexDir);
|
|
|
|
connect(m_dirWatcher, &DirWatcher::removeIndexItem, this, &FileIndexerConfig::removeIndexDir);
|
|
|
|
|
|
|
|
const QByteArray id(UKUI_SEARCH_SCHEMAS);
|
|
|
|
if(QGSettings::isSchemaInstalled(id)) {
|
|
|
|
m_gsettings = new QGSettings(id, QByteArray(), this);
|
2023-08-03 09:09:18 +08:00
|
|
|
//保留旧版本配置
|
|
|
|
if(m_gsettings->keys().contains(CONFIG_VERSION_KEY)) {
|
|
|
|
QString oldVersion = m_gsettings->get(CONFIG_VERSION_KEY).toString();
|
|
|
|
if(oldVersion == "0.0") {
|
|
|
|
bool fileIndexEnable = false;
|
|
|
|
if(m_gsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) {
|
|
|
|
fileIndexEnable = m_gsettings->get(FILE_INDEX_ENABLE_KEY).toBool();
|
|
|
|
}
|
|
|
|
if(fileIndexEnable) {
|
|
|
|
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_KEY)) {
|
|
|
|
m_gsettings->set(CONTENT_INDEX_ENABLE_KEY, true);
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_OCR_KEY)) {
|
|
|
|
m_gsettings->set(CONTENT_INDEX_ENABLE_OCR_KEY, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_gsettings->set(CONFIG_VERSION_KEY, CONFIG_VERSION);
|
|
|
|
} else if (oldVersion == "1.0") {
|
|
|
|
bool contentIndex = false;
|
|
|
|
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_KEY)) {
|
|
|
|
contentIndex = m_gsettings->get(CONTENT_INDEX_ENABLE_KEY).toBool();
|
|
|
|
}
|
|
|
|
if(contentIndex) {
|
|
|
|
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_OCR_KEY)) {
|
|
|
|
m_gsettings->set(CONTENT_INDEX_ENABLE_OCR_KEY, true);
|
|
|
|
}
|
2023-08-03 09:09:18 +08:00
|
|
|
}
|
|
|
|
m_gsettings->set(CONFIG_VERSION_KEY, CONFIG_VERSION);
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 18:01:40 +08:00
|
|
|
connect(m_gsettings, &QGSettings::changed, this, [ = ](const QString &key) {
|
|
|
|
if(key == FILE_INDEX_ENABLE_KEY) {
|
|
|
|
Q_EMIT this->fileIndexEnableStatusChanged(m_gsettings->get(FILE_INDEX_ENABLE_KEY).toBool());
|
2023-08-03 09:09:18 +08:00
|
|
|
} else if(key == CONTENT_INDEX_ENABLE_KEY) {
|
|
|
|
Q_EMIT this->contentIndexEnableStatusChanged(m_gsettings->get(CONTENT_INDEX_ENABLE_KEY).toBool());
|
2023-09-28 10:54:58 +08:00
|
|
|
} else if(key == CONTENT_INDEX_ENABLE_OCR_KEY) {
|
|
|
|
Q_EMIT this->contentIndexEnableOcrStatusChanged(m_gsettings->get(CONTENT_INDEX_ENABLE_OCR_KEY).toBool());
|
2022-10-26 18:01:40 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
qWarning() << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
}
|
|
|
|
m_settings = new QSettings(INDEX_SETTINGS, QSettings::IniFormat, this);
|
2024-01-26 11:51:07 +08:00
|
|
|
sync();
|
2022-10-26 18:01:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileIndexerConfig::~FileIndexerConfig()
|
2024-01-23 11:18:55 +08:00
|
|
|
= default;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
|
|
|
QStringList FileIndexerConfig::currentIndexableDir()
|
|
|
|
{
|
|
|
|
return DirWatcher::getDirWatcher()->currentIndexableDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList FileIndexerConfig::currentBlackListOfIndex()
|
|
|
|
{
|
|
|
|
return DirWatcher::getDirWatcher()->currentBlackListOfIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileIndexerConfig::isFileIndexEnable()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2022-10-26 18:01:40 +08:00
|
|
|
if(m_gsettings) {
|
|
|
|
if(m_gsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) {
|
|
|
|
return m_gsettings->get(FILE_INDEX_ENABLE_KEY).toBool();
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig: Can not find key:" << FILE_INDEX_ENABLE_KEY << "in" << UKUI_SEARCH_SCHEMAS;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig:" << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileIndexerConfig::isContentIndexEnable()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2023-08-03 09:09:18 +08:00
|
|
|
if(m_gsettings) {
|
|
|
|
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_KEY)) {
|
|
|
|
return m_gsettings->get(CONTENT_INDEX_ENABLE_KEY).toBool();
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig: Can not find key:" << CONTENT_INDEX_ENABLE_KEY << "in" << UKUI_SEARCH_SCHEMAS;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig:" << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-26 18:01:40 +08:00
|
|
|
}
|
|
|
|
|
2023-05-31 17:59:20 +08:00
|
|
|
bool FileIndexerConfig::isFuzzySearchEnable()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2023-05-31 17:59:20 +08:00
|
|
|
if(m_gsettings) {
|
|
|
|
if(m_gsettings->keys().contains(CONTENT_FUZZY_SEARCH_KEY)) {
|
|
|
|
return m_gsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool();
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig: Can not find key:" << CONTENT_FUZZY_SEARCH_KEY << "in" << UKUI_SEARCH_SCHEMAS;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig:" << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
bool FileIndexerConfig::isOCREnable()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2023-09-28 10:54:58 +08:00
|
|
|
if(m_gsettings) {
|
|
|
|
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_OCR_KEY)) {
|
|
|
|
return m_gsettings->get(CONTENT_INDEX_ENABLE_OCR_KEY).toBool();
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig: Can not find key:" << CONTENT_INDEX_ENABLE_OCR_KEY << "in" << UKUI_SEARCH_SCHEMAS;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qWarning() << "FileIndexerConfig:" << UKUI_SEARCH_SCHEMAS << " is not found!";
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-26 18:01:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileIndexerConfig::isMetaDataIndexEnable()
|
|
|
|
{
|
|
|
|
return m_settings->value(META_DATA_INDEX_ENABLE_KEY, true).toBool();
|
|
|
|
}
|
2024-01-23 11:18:55 +08:00
|
|
|
|
|
|
|
QMap<QString, bool> FileIndexerConfig::contentIndexTarget()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2024-01-23 11:18:55 +08:00
|
|
|
return m_targetFileTypeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, bool> FileIndexerConfig::ocrContentIndexTarget()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2024-01-23 11:18:55 +08:00
|
|
|
return m_targetPhotographTypeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileIndexerConfig::sync()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2024-01-23 11:18:55 +08:00
|
|
|
m_settings->sync();
|
|
|
|
m_settings->beginGroup(CONTENT_INDEX_TARGET_TYPE);
|
|
|
|
if(m_settings->allKeys().isEmpty()) {
|
|
|
|
for(const QString &type : m_targetFileTypeMap.keys()) {
|
|
|
|
m_settings->setValue(type, m_targetFileTypeMap[type]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_targetFileTypeMap.clear();
|
|
|
|
for(const QString &type : m_settings->allKeys()) {
|
|
|
|
m_targetFileTypeMap.insert(type, m_settings->value(type).toBool());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_settings->endGroup();
|
|
|
|
m_settings->beginGroup(OCR_CONTENT_INDEX_TARGET_TYPE);
|
|
|
|
if(m_settings->allKeys().isEmpty()) {
|
|
|
|
for(const QString &type : m_targetPhotographTypeMap.keys()) {
|
|
|
|
m_settings->setValue(type, m_targetPhotographTypeMap[type]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_targetPhotographTypeMap.clear();
|
|
|
|
for(const QString &type : m_settings->allKeys()) {
|
|
|
|
m_targetPhotographTypeMap.insert(type, m_settings->value(type).toBool());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_settings->endGroup();
|
|
|
|
}
|