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>
|
|
|
|
*/
|
2022-04-25 11:16:25 +08:00
|
|
|
|
|
|
|
//ukui-search
|
|
|
|
#include "file-content-search-task.h"
|
|
|
|
#include "index-status-recorder.h"
|
|
|
|
#include "chinese-segmentation.h"
|
|
|
|
#include "result-item.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "file-utils.h"
|
2024-02-22 10:09:47 +08:00
|
|
|
#include "file-indexer-config.h"
|
2022-04-25 11:16:25 +08:00
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QDebug>
|
2023-04-19 15:14:36 +08:00
|
|
|
#include <QDateTime>
|
2022-04-25 11:16:25 +08:00
|
|
|
|
|
|
|
using namespace UkuiSearch;
|
|
|
|
|
|
|
|
FileContentSearchTask::FileContentSearchTask(QObject *parent)
|
|
|
|
{
|
|
|
|
this->setParent(parent);
|
|
|
|
qRegisterMetaType<size_t>("size_t");
|
|
|
|
m_pool = new QThreadPool(this);
|
|
|
|
m_pool->setMaxThreadCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileContentSearchTask::~FileContentSearchTask()
|
|
|
|
{
|
2023-03-15 16:39:38 +08:00
|
|
|
m_pool->clear();
|
|
|
|
m_pool->waitForDone();
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
void FileContentSearchTask::setController(const SearchController &searchController)
|
2023-05-06 15:41:56 +08:00
|
|
|
{
|
|
|
|
m_searchController = searchController;
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:16:25 +08:00
|
|
|
PluginInterface::PluginType FileContentSearchTask::pluginType()
|
|
|
|
{
|
|
|
|
return PluginType::SearchTaskPlugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString FileContentSearchTask::name()
|
|
|
|
{
|
2022-04-26 14:33:06 +08:00
|
|
|
return tr("File Content");
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString FileContentSearchTask::description()
|
|
|
|
{
|
2022-04-26 14:33:06 +08:00
|
|
|
return tr("File Content Search");
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QIcon FileContentSearchTask::icon()
|
|
|
|
{
|
2022-04-26 14:33:06 +08:00
|
|
|
return QIcon::fromTheme("folder", QIcon(":/unknown.svg"));
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileContentSearchTask::setEnable(bool enable)
|
|
|
|
{
|
2022-06-13 13:38:47 +08:00
|
|
|
m_enable = enable;
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileContentSearchTask::isEnable()
|
|
|
|
{
|
2024-02-22 10:09:47 +08:00
|
|
|
int state = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt();
|
|
|
|
return m_enable && state == IndexStatusRecorder::State::Ready || state == IndexStatusRecorder::State::Updating;
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString FileContentSearchTask::getCustomSearchType()
|
|
|
|
{
|
2022-04-26 14:33:06 +08:00
|
|
|
return tr("File Content");
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
SearchProperty::SearchType FileContentSearchTask::getSearchType()
|
2022-04-25 11:16:25 +08:00
|
|
|
{
|
2023-03-15 16:39:38 +08:00
|
|
|
return SearchProperty::SearchType::FileContent;
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
2023-05-06 15:41:56 +08:00
|
|
|
void FileContentSearchTask::startSearch()
|
2022-04-25 11:16:25 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
FileContentSearchWorker *worker = new FileContentSearchWorker(this, &m_searchController);
|
2022-04-25 11:16:25 +08:00
|
|
|
m_pool->start(worker);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileContentSearchTask::stop()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-19 15:15:28 +08:00
|
|
|
bool FileContentSearchTask::isSearching()
|
|
|
|
{
|
|
|
|
return m_pool->activeThreadCount() > 0;
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
FileContentSearchWorker::FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, SearchController *searchController)
|
2022-04-25 11:16:25 +08:00
|
|
|
{
|
|
|
|
m_fileContentSearchTask = fileContentSearchTask;
|
2023-07-06 15:27:23 +08:00
|
|
|
m_searchController = searchController;
|
2022-04-25 11:16:25 +08:00
|
|
|
m_currentSearchId = m_searchController->getCurrentSearchId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileContentSearchWorker::run()
|
|
|
|
{
|
|
|
|
QStringList searchDirs = m_searchController->getSearchDir();
|
|
|
|
searchDirs.removeDuplicates();
|
|
|
|
|
|
|
|
for (QString &dir : searchDirs) {
|
2023-04-07 10:20:43 +08:00
|
|
|
if (dir.endsWith("/") && dir != "/") {
|
2022-04-25 11:16:25 +08:00
|
|
|
dir = dir.mid(0, dir.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_validDirectories.append(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool finished = true;
|
2024-02-22 10:09:47 +08:00
|
|
|
if (m_fileContentSearchTask->isEnable()) {
|
|
|
|
qDebug() << "content index enable";
|
2022-04-25 11:16:25 +08:00
|
|
|
finished = execSearch();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
qWarning() << "content index incomplete";
|
2022-04-26 10:43:02 +08:00
|
|
|
sendErrorMsg(QObject::tr("Content index incomplete."));
|
|
|
|
|
|
|
|
finished = false;
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (finished) QMetaObject::invokeMethod(m_fileContentSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId));
|
|
|
|
}
|
|
|
|
|
2022-04-26 10:43:02 +08:00
|
|
|
void FileContentSearchWorker::sendErrorMsg(const QString &msg)
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(m_fileContentSearchTask, "searchError",
|
|
|
|
Q_ARG(size_t, m_currentSearchId),
|
|
|
|
Q_ARG(QString, msg));
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:16:25 +08:00
|
|
|
bool FileContentSearchWorker::execSearch()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Xapian::Database db(CONTENT_INDEX_PATH.toStdString());
|
2024-02-22 10:09:47 +08:00
|
|
|
if(FileIndexerConfig::getInstance()->isOCREnable()) {
|
|
|
|
db.add_database(Xapian::Database(OCR_CONTENT_INDEX_PATH.toStdString()));
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:16:25 +08:00
|
|
|
Xapian::Enquire enquire(db);
|
|
|
|
|
|
|
|
enquire.set_query(createQuery());
|
|
|
|
|
|
|
|
FileContentSearchFilter filter(this);
|
|
|
|
|
2024-02-22 10:09:47 +08:00
|
|
|
Xapian::MSet result = enquire.get_mset(0, m_searchController->maxResults(), nullptr, &filter);
|
2022-04-25 11:16:25 +08:00
|
|
|
|
|
|
|
for (auto it = result.begin(); it != result.end(); ++it) {
|
|
|
|
if (m_searchController->beginSearchIdCheck(m_currentSearchId)) {
|
|
|
|
QString path = QString::fromStdString(it.get_document().get_value(CONTENT_DATABASE_PATH_SLOT));
|
|
|
|
|
2024-02-22 10:09:47 +08:00
|
|
|
SearchResultProperties properties = m_searchController->getResultProperties(SearchProperty::SearchType::FileContent);
|
2023-03-15 16:39:38 +08:00
|
|
|
ResultItem resultItem(m_currentSearchId);
|
2024-02-22 10:09:47 +08:00
|
|
|
resultItem.setItemKey(path);
|
2023-03-15 16:39:38 +08:00
|
|
|
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::FilePath)) {
|
|
|
|
resultItem.setValue(SearchProperty::SearchResultProperty::FilePath, path);
|
|
|
|
}
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::FileIconName)) {
|
2023-03-16 17:42:25 +08:00
|
|
|
resultItem.setValue(SearchProperty::SearchResultProperty::FileIconName, FileUtils::getFileIcon(QUrl::fromLocalFile(path).toString()).name());
|
2023-03-15 16:39:38 +08:00
|
|
|
}
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::FileName)) {
|
|
|
|
resultItem.setValue(SearchProperty::SearchResultProperty::FileName, path.section("/", -1));
|
|
|
|
}
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::ModifiedTime)) {
|
|
|
|
resultItem.setValue(SearchProperty::SearchResultProperty::ModifiedTime,
|
|
|
|
QDateTime::fromString(QString::fromStdString(it.get_document().get_value(3)), "yyyyMMddHHmmsszzz"));
|
|
|
|
}
|
2022-04-25 11:16:25 +08:00
|
|
|
m_searchController->getDataQueue()->enqueue(resultItem);
|
2023-05-19 15:15:28 +08:00
|
|
|
if(++m_resultNum == m_searchController->informNum()) {
|
|
|
|
QMetaObject::invokeMethod(m_fileContentSearchTask, "reachInformNum");
|
|
|
|
m_resultNum = 0;
|
|
|
|
}
|
2022-04-25 11:16:25 +08:00
|
|
|
m_searchController->finishSearchIdCheck();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
qDebug() << "Search id changed!";
|
|
|
|
m_searchController->finishSearchIdCheck();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch (const Xapian::Error &e) {
|
|
|
|
qWarning() << QString::fromStdString(e.get_description());
|
2022-04-26 10:43:02 +08:00
|
|
|
sendErrorMsg("Xapian Error: " + QString::fromStdString(e.get_description()));
|
2022-04-25 11:16:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Xapian::Query FileContentSearchWorker::createQuery()
|
|
|
|
{
|
|
|
|
std::vector<Xapian::Query> v;
|
|
|
|
|
2023-03-20 15:21:58 +08:00
|
|
|
for (auto &keyword : m_searchController->getKeyword()) {
|
|
|
|
std::vector<KeyWord> sKeyWord = ChineseSegmentation::getInstance()->callSegment(keyword);
|
2022-04-25 11:16:25 +08:00
|
|
|
|
|
|
|
for(const auto & c : sKeyWord) {
|
|
|
|
v.emplace_back(c.word);
|
|
|
|
}
|
|
|
|
}
|
2024-02-22 10:09:47 +08:00
|
|
|
Xapian::Query query;
|
|
|
|
if(FileIndexerConfig::getInstance()->isFuzzySearchEnable()) {
|
|
|
|
query = Xapian::Query(Xapian::Query::OP_OR, v.begin(), v.end());
|
|
|
|
} else {
|
|
|
|
query = Xapian::Query(Xapian::Query::OP_AND, v.begin(), v.end());
|
|
|
|
}
|
2022-04-25 11:16:25 +08:00
|
|
|
|
2024-02-22 10:09:47 +08:00
|
|
|
return query;
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileContentSearchFilter::FileContentSearchFilter(FileContentSearchWorker *worker) : m_worker(worker)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileContentSearchFilter::operator()(const Xapian::Document &doc) const
|
|
|
|
{
|
|
|
|
//在此处对搜索结果进行过滤
|
|
|
|
QString path = QString::fromStdString(doc.get_value(CONTENT_DATABASE_PATH_SLOT));
|
2023-03-15 16:39:38 +08:00
|
|
|
if(!QFileInfo::exists(path)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-25 11:16:25 +08:00
|
|
|
|
|
|
|
//如果不指定搜索目录,那么搜索整个数据库
|
|
|
|
if (m_worker && !m_worker->m_validDirectories.empty()) {
|
2023-03-15 16:39:38 +08:00
|
|
|
for(const QString &dir : m_worker->m_validDirectories) {
|
|
|
|
return FileUtils::isOrUnder(path, dir);
|
|
|
|
}
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
return true;
|
2022-04-25 11:16:25 +08:00
|
|
|
}
|