ukui-search/libsearch/index/file-indexer-config.cpp

110 lines
3.6 KiB
C++

/*
* 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>
*
*/
#include "file-indexer-config.h"\
#include <mutex>
#include <QDebug>
#include <QDir>
#define INDEX_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-service.conf"
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");
static const QString OCR_ENABLE_KEY = QStringLiteral("ocrEnable");
static const QString META_DATA_INDEX_ENABLE_KEY = QStringLiteral("metaDataIndexEnable");
static std::once_flag flag;
static FileIndexerConfig *global_intance = nullptr;
FileIndexerConfig *FileIndexerConfig::getInstance()
{
std::call_once(flag, [ & ] {
global_intance = new FileIndexerConfig();
});
return global_intance;
}
FileIndexerConfig::FileIndexerConfig(QObject *parent)
: 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);
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());
}
});
} else {
qWarning() << UKUI_SEARCH_SCHEMAS << " is not found!";
}
m_settings = new QSettings(INDEX_SETTINGS, QSettings::IniFormat, this);
}
FileIndexerConfig::~FileIndexerConfig()
{
}
QStringList FileIndexerConfig::currentIndexableDir()
{
return DirWatcher::getDirWatcher()->currentIndexableDir();
}
QStringList FileIndexerConfig::currentBlackListOfIndex()
{
return DirWatcher::getDirWatcher()->currentBlackListOfIndex();
}
bool FileIndexerConfig::isFileIndexEnable()
{
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()
{
return m_settings->value(CONTENT_INDEX_ENABLE_KEY, true).toBool();
}
bool FileIndexerConfig::isOCREnable()
{
return m_settings->value(OCR_ENABLE_KEY, true).toBool();
}
bool FileIndexerConfig::isMetaDataIndexEnable()
{
return m_settings->value(META_DATA_INDEX_ENABLE_KEY, true).toBool();
}