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>
|
|
|
|
*/
|
2021-12-28 15:56:41 +08:00
|
|
|
#include "file-search-task.h"
|
2022-01-24 09:44:42 +08:00
|
|
|
#include <QDir>
|
2022-07-07 16:03:12 +08:00
|
|
|
#include <QUrl>
|
2022-01-24 09:44:42 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QQueue>
|
|
|
|
#include <QDebug>
|
2023-03-15 16:39:38 +08:00
|
|
|
#include <QDateTime>
|
2022-04-28 15:49:51 +08:00
|
|
|
#include <gio/gio.h>
|
2023-03-16 17:42:25 +08:00
|
|
|
#include <QUrl>
|
2023-12-14 14:04:38 +08:00
|
|
|
#include <QTextBoundaryFinder>
|
2023-03-15 16:39:38 +08:00
|
|
|
#include "index-status-recorder.h"
|
|
|
|
#include "dir-watcher.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "file-utils.h"
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
using namespace UkuiSearch;
|
2022-01-24 09:44:42 +08:00
|
|
|
FileSearchTask::FileSearchTask(QObject *parent)
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2022-01-24 09:44:42 +08:00
|
|
|
this->setParent(parent);
|
|
|
|
qRegisterMetaType<size_t>("size_t");
|
2022-01-11 16:20:40 +08:00
|
|
|
m_pool = new QThreadPool(this);
|
|
|
|
m_pool->setMaxThreadCount(1);
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
FileSearchTask::~FileSearchTask()
|
|
|
|
{
|
|
|
|
m_pool->clear();
|
|
|
|
m_pool->waitForDone();
|
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
void FileSearchTask::setController(const SearchController &searchController)
|
2023-05-06 15:41:56 +08:00
|
|
|
{
|
|
|
|
m_searchController = searchController;
|
|
|
|
}
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
const QString FileSearchTask::name()
|
|
|
|
{
|
2022-01-11 16:20:40 +08:00
|
|
|
return "File";
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString FileSearchTask::description()
|
|
|
|
{
|
2022-01-11 16:20:40 +08:00
|
|
|
return "File search.";
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 17:38:02 +08:00
|
|
|
QString FileSearchTask::getCustomSearchType()
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2022-01-11 16:20:40 +08:00
|
|
|
return "File";
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
2023-05-06 15:41:56 +08:00
|
|
|
void FileSearchTask::startSearch()
|
2021-12-28 15:56:41 +08:00
|
|
|
{
|
2022-01-11 16:20:40 +08:00
|
|
|
FileSearchWorker *fileSearchWorker;
|
2023-07-06 15:27:23 +08:00
|
|
|
fileSearchWorker = new FileSearchWorker(this, &m_searchController);
|
2022-01-11 16:20:40 +08:00
|
|
|
m_pool->start(fileSearchWorker);
|
2021-12-28 15:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSearchTask::stop()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-19 15:15:28 +08:00
|
|
|
bool FileSearchTask::isSearching()
|
2022-01-11 16:20:40 +08:00
|
|
|
{
|
2023-05-19 15:15:28 +08:00
|
|
|
return m_pool->activeThreadCount() > 0;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, SearchController *searchController) : m_FileSearchTask(fileSarchTask)
|
2022-01-11 16:20:40 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
m_searchController = searchController;
|
2023-01-21 19:52:40 +08:00
|
|
|
m_currentSearchId = m_searchController->getCurrentSearchId();
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileSearchWorker::run()
|
|
|
|
{
|
2022-03-25 09:40:56 +08:00
|
|
|
//1.检查是否为不可搜索目录
|
|
|
|
QStringList searchDirs = m_searchController->getSearchDir();
|
|
|
|
searchDirs.removeDuplicates();
|
|
|
|
|
|
|
|
for (QString &dir : searchDirs) {
|
2022-04-28 15:49:51 +08:00
|
|
|
if (QFileInfo::exists(dir)) {
|
2023-04-07 10:20:43 +08:00
|
|
|
if (dir.endsWith("/") && dir != "/") {
|
2022-04-28 15:49:51 +08:00
|
|
|
dir.remove(dir.length() - 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList blackListTmp = DirWatcher::getDirWatcher()->blackListOfDir(dir);
|
|
|
|
if (!blackListTmp.contains(dir)) {
|
|
|
|
m_validDirectories.append(dir);
|
|
|
|
m_blackList.append(blackListTmp);
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
2022-04-28 15:49:51 +08:00
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
//过滤空标签
|
|
|
|
for (QString &label : m_searchController->getFileLabel()) {
|
|
|
|
if (!label.isEmpty()) {
|
|
|
|
m_labels.append(label);
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:09:34 +08:00
|
|
|
bool finished = true;
|
2023-03-16 17:42:25 +08:00
|
|
|
bool indexed = true;
|
|
|
|
|
|
|
|
QStringList indexedDir = DirWatcher::getDirWatcher()->currentIndexableDir();
|
2023-07-06 15:27:23 +08:00
|
|
|
if(!indexedDir.isEmpty() && IndexStatusRecorder::getInstance()->indexDatabaseEnable() && !m_searchController->searchHiddenFiles()) {
|
2023-03-15 16:39:38 +08:00
|
|
|
for(const QString &path : m_searchController->getSearchDir()) {
|
2023-03-16 17:42:25 +08:00
|
|
|
bool pathIndexed = false;
|
|
|
|
for(const QString &dir : indexedDir) {
|
|
|
|
if(FileUtils::isOrUnder(path, dir)) {
|
|
|
|
pathIndexed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!pathIndexed) {
|
2023-03-15 16:39:38 +08:00
|
|
|
indexed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 17:42:25 +08:00
|
|
|
} else {
|
|
|
|
indexed = false;
|
2023-03-15 16:39:38 +08:00
|
|
|
}
|
2023-03-16 17:42:25 +08:00
|
|
|
|
2022-03-25 09:40:56 +08:00
|
|
|
//TODO 还需要判断是否为不能建立索引的目录
|
2023-03-16 17:42:25 +08:00
|
|
|
if (indexed) {
|
2022-03-25 09:40:56 +08:00
|
|
|
qDebug() << "index ready";
|
2022-03-25 14:09:34 +08:00
|
|
|
finished = searchWithIndex();
|
2022-03-25 09:40:56 +08:00
|
|
|
} else {
|
2022-04-28 15:49:51 +08:00
|
|
|
if (m_validDirectories.empty()) {
|
|
|
|
//TODO 使用全局的默认可搜索目录
|
|
|
|
if (QFileInfo::exists(QDir::homePath())) {
|
|
|
|
sendErrorMsg(QObject::tr("Warning, Can not find home path."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_validDirectories.append(QDir::homePath());
|
|
|
|
m_blackList.append(DirWatcher::getDirWatcher()->blackListOfDir(QDir::homePath()));
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
qDebug() << "direct search";
|
2022-03-25 14:09:34 +08:00
|
|
|
finished = directSearch();
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:09:34 +08:00
|
|
|
if (finished) QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId));
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Xapian::Query FileSearchWorker::creatQueryForFileSearch() {
|
|
|
|
Xapian::Query fileOrDir = Xapian::Query::MatchAll;
|
2022-03-30 14:46:59 +08:00
|
|
|
if (m_searchController->isSearchDirOnly() && m_searchController->isSearchFileOnly()) {
|
|
|
|
//同时指定'只搜索目录'和'只搜索文件',那麼拒絕搜索
|
|
|
|
return {};
|
|
|
|
|
|
|
|
} else {
|
2022-03-25 09:40:56 +08:00
|
|
|
if (m_searchController->isSearchDirOnly()) {
|
|
|
|
fileOrDir = Xapian::Query(Xapian::Query::OP_VALUE_RANGE, 1, "1", "1");
|
|
|
|
|
|
|
|
} else if (m_searchController->isSearchFileOnly()) {
|
|
|
|
fileOrDir = Xapian::Query(Xapian::Query::OP_VALUE_RANGE, 1, "0", "0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Xapian::Query> queries;
|
2024-04-25 10:59:18 +08:00
|
|
|
if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) {
|
|
|
|
queries.emplace_back(Xapian::Query::MatchAll);
|
|
|
|
} else {
|
|
|
|
for (QString &keyword : m_searchController->getKeyword()) {
|
|
|
|
if (!keyword.isEmpty()) {
|
|
|
|
std::vector<Xapian::Query> queryOfKeyword;
|
|
|
|
QString inputKeyWord = keyword.toLower();
|
|
|
|
QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, inputKeyWord.toLower());
|
|
|
|
int start = 0;
|
|
|
|
for(; bf.position() != -1; bf.toNextBoundary()) {
|
|
|
|
int end = bf.position();
|
|
|
|
if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) {
|
|
|
|
queryOfKeyword.emplace_back(QUrl::toPercentEncoding(inputKeyWord.toLower().mid(start, end - start)).toStdString());
|
|
|
|
}
|
|
|
|
start = end;
|
2023-12-14 14:04:38 +08:00
|
|
|
}
|
2024-04-25 10:59:18 +08:00
|
|
|
queries.emplace_back(Xapian::Query::OP_PHRASE, queryOfKeyword.begin(), queryOfKeyword.end());
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {Xapian::Query::OP_AND, {Xapian::Query::OP_AND, queries.begin(), queries.end()}, fileOrDir};
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:09:34 +08:00
|
|
|
bool FileSearchWorker::searchWithIndex()
|
2022-03-25 09:40:56 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
Xapian::Database db(INDEX_PATH.toStdString());
|
|
|
|
Xapian::Enquire enquire(db);
|
|
|
|
// qDebug() << "===" << QString::fromStdString(creatQueryForFileSearch().get_description());
|
|
|
|
enquire.set_query(creatQueryForFileSearch());
|
|
|
|
FileSearchFilter fileSearchFilter(this);
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
Xapian::MSet result = enquire.get_mset(0, m_searchController->maxResults(), 0, &fileSearchFilter);
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
for (auto it = result.begin(); it != result.end(); ++it) {
|
|
|
|
if (m_searchController->beginSearchIdCheck(m_currentSearchId)) {
|
2023-03-15 16:39:38 +08:00
|
|
|
QString path = QString::fromStdString(it.get_document().get_data());
|
|
|
|
SearchResultProperties properties = m_searchController->getResultProperties(SearchProperty::SearchType::File);
|
|
|
|
ResultItem resultItem(m_currentSearchId);
|
|
|
|
resultItem.setItemKey(path);
|
|
|
|
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(2)), "yyyyMMddHHmmsszzz"));
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
m_searchController->getDataQueue()->enqueue(resultItem);
|
2023-05-19 15:15:28 +08:00
|
|
|
if(++m_resultNum == m_searchController->informNum()) {
|
|
|
|
QMetaObject::invokeMethod(m_FileSearchTask, "reachInformNum");
|
|
|
|
m_resultNum = 0;
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
m_searchController->finishSearchIdCheck();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
qDebug() << "Search id changed!";
|
|
|
|
m_searchController->finishSearchIdCheck();
|
2022-03-25 14:09:34 +08:00
|
|
|
return false;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} 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()));
|
|
|
|
return false;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
2022-03-25 14:09:34 +08:00
|
|
|
|
|
|
|
return true;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
2022-04-26 10:43:02 +08:00
|
|
|
void FileSearchWorker::sendErrorMsg(const QString &msg)
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(m_FileSearchTask, "searchError",
|
|
|
|
Q_ARG(size_t, m_currentSearchId),
|
|
|
|
Q_ARG(QString, msg));
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:09:34 +08:00
|
|
|
bool FileSearchWorker::directSearch()
|
2022-03-25 09:40:56 +08:00
|
|
|
{
|
2022-03-25 14:09:34 +08:00
|
|
|
unsigned int maxResults = m_searchController->maxResults();
|
2022-03-25 09:40:56 +08:00
|
|
|
QQueue<QString> searchPathQueue;
|
|
|
|
for (QString &dir : m_validDirectories) {
|
|
|
|
searchPathQueue.enqueue(dir);
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:44:42 +08:00
|
|
|
QDir dir;
|
2022-03-25 09:40:56 +08:00
|
|
|
QFileInfoList infoList;
|
2023-07-06 15:27:23 +08:00
|
|
|
QDir::Filters filters;
|
2022-03-25 09:40:56 +08:00
|
|
|
if (m_searchController->isSearchDirOnly()) {
|
2023-07-06 15:27:23 +08:00
|
|
|
filters = QDir::Dirs | QDir::NoDotAndDotDot;
|
2022-01-11 16:20:40 +08:00
|
|
|
} else {
|
2023-07-06 15:27:23 +08:00
|
|
|
filters = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot;
|
2022-01-24 09:44:42 +08:00
|
|
|
dir.setSorting(QDir::DirsFirst);
|
|
|
|
}
|
2023-07-06 15:27:23 +08:00
|
|
|
if(m_searchController->searchHiddenFiles()) {
|
|
|
|
filters |= QDir::Hidden;
|
|
|
|
}
|
|
|
|
dir.setFilter(filters);
|
2022-03-25 09:40:56 +08:00
|
|
|
while (!searchPathQueue.empty()) {
|
|
|
|
dir.setPath(searchPathQueue.dequeue());
|
2022-10-19 17:11:05 +08:00
|
|
|
if (!dir.exists())
|
|
|
|
continue;
|
2022-03-25 09:40:56 +08:00
|
|
|
infoList = dir.entryInfoList();
|
|
|
|
for (const auto &fileInfo : infoList) {
|
|
|
|
if (fileInfo.isDir() && !fileInfo.isSymLink()) {
|
|
|
|
QString newPath = fileInfo.absoluteFilePath();
|
2022-04-28 15:49:51 +08:00
|
|
|
|
|
|
|
bool inBlackList = std::any_of(m_blackList.begin(), m_blackList.end(), [&] (const QString &dir) {
|
|
|
|
return newPath.startsWith(dir + "/");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (inBlackList) {
|
2022-03-25 09:40:56 +08:00
|
|
|
//在黑名单的路径忽略搜索
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (m_searchController->isRecurse()) {
|
|
|
|
searchPathQueue.enqueue(newPath);
|
2022-01-24 09:44:42 +08:00
|
|
|
}
|
2023-03-15 16:39:38 +08:00
|
|
|
if (m_searchController->isSearchFileOnly()) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-24 09:44:42 +08:00
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
bool matched = false;
|
2024-04-25 10:59:18 +08:00
|
|
|
if (m_searchController->getKeyword().isEmpty() && m_searchController->getMatchAllIfNoKeyword()) {
|
|
|
|
matched = true;
|
|
|
|
} else {
|
|
|
|
//同时包含几个key为成功匹配
|
|
|
|
for (const QString &keyword: m_searchController->getKeyword()) {
|
|
|
|
if (!keyword.isEmpty()) {
|
|
|
|
//TODO 修改匹配方式,对结果进行排序
|
|
|
|
if (fileInfo.fileName().contains(keyword, Qt::CaseInsensitive)) {
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-01-24 09:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
if (matched && !m_labels.empty()) {
|
|
|
|
matched = FileSearchFilter::checkFileLabel(fileInfo.absoluteFilePath(), m_labels);
|
|
|
|
}
|
|
|
|
|
2022-03-25 09:40:56 +08:00
|
|
|
if (m_searchController->beginSearchIdCheck(m_currentSearchId)) {
|
|
|
|
if (matched) {
|
2023-03-15 16:39:38 +08:00
|
|
|
ResultItem ri(m_currentSearchId);
|
|
|
|
ri.setItemKey(fileInfo.absoluteFilePath());
|
|
|
|
SearchResultProperties properties = m_searchController->getResultProperties(SearchProperty::SearchType::File);
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::FilePath)) {
|
|
|
|
ri.setValue(SearchProperty::SearchResultProperty::FilePath, fileInfo.absoluteFilePath());
|
|
|
|
}
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::FileIconName)) {
|
2023-03-16 17:42:25 +08:00
|
|
|
ri.setValue(SearchProperty::SearchResultProperty::FileIconName, FileUtils::getFileIcon(QUrl::fromLocalFile(fileInfo.absoluteFilePath()).toString()).name());
|
2023-03-15 16:39:38 +08:00
|
|
|
}
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::FileName)) {
|
|
|
|
ri.setValue(SearchProperty::SearchResultProperty::FileName, fileInfo.fileName());
|
|
|
|
}
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::ModifiedTime)) {
|
|
|
|
ri.setValue(SearchProperty::SearchResultProperty::ModifiedTime, fileInfo.lastModified());
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
m_searchController->getDataQueue()->enqueue(ri);
|
2023-05-19 15:15:28 +08:00
|
|
|
if(++m_resultNum == m_searchController->informNum()) {
|
|
|
|
QMetaObject::invokeMethod(m_FileSearchTask, "reachInformNum");
|
|
|
|
m_resultNum = 0;
|
|
|
|
}
|
2022-03-25 14:09:34 +08:00
|
|
|
--maxResults;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
m_searchController->finishSearchIdCheck();
|
2022-03-25 14:09:34 +08:00
|
|
|
if (maxResults == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
} else {
|
|
|
|
qDebug() << "Search id changed!";
|
|
|
|
m_searchController->finishSearchIdCheck();
|
2022-03-25 14:09:34 +08:00
|
|
|
return false;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
2022-01-24 09:44:42 +08:00
|
|
|
}
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
2022-03-25 14:09:34 +08:00
|
|
|
|
|
|
|
return true;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
FileSearchFilter::FileSearchFilter(FileSearchWorker *parent) : m_parent(parent) {}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
bool FileSearchFilter::operator ()(const Xapian::Document &doc) const
|
|
|
|
{
|
2022-04-28 15:49:51 +08:00
|
|
|
if (m_parent) {
|
2022-03-25 09:40:56 +08:00
|
|
|
QString path = QString::fromStdString(doc.get_data());
|
2022-04-28 15:49:51 +08:00
|
|
|
bool isRecurse = m_parent->m_searchController->isRecurse();
|
|
|
|
bool inSearchDir = true;
|
|
|
|
|
|
|
|
if (!m_parent->m_validDirectories.empty()) {
|
|
|
|
inSearchDir = std::any_of(m_parent->m_validDirectories.begin(), m_parent->m_validDirectories.end(), [&](QString &dir) {
|
|
|
|
//限制搜索在该目录下
|
2023-06-16 09:27:33 +08:00
|
|
|
if (dir != "/" && !path.startsWith(dir + "/")) {
|
2022-04-28 15:49:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
if (!isRecurse) {
|
|
|
|
//去除搜索路径后,是否包含 "/"
|
|
|
|
return !path.midRef((dir.length() + 1), (path.length() - dir.length() - 1)).contains("/");
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
bool hasLabel = true;
|
|
|
|
if (inSearchDir && !m_parent->m_labels.empty()) {
|
|
|
|
hasLabel = FileSearchFilter::checkFileLabel(path, m_parent->m_labels);
|
|
|
|
}
|
2022-03-25 09:40:56 +08:00
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
return inSearchDir && hasLabel;
|
2022-03-25 09:40:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-01-11 16:20:40 +08:00
|
|
|
}
|
2022-04-28 15:49:51 +08:00
|
|
|
|
|
|
|
bool FileSearchFilter::checkFileLabel(const QString &path, const QStringList &labels)
|
|
|
|
{
|
|
|
|
bool hasLabel = false;
|
|
|
|
|
|
|
|
GFile *file = g_file_new_for_path(path.toUtf8().constData());
|
|
|
|
if (file) {
|
|
|
|
GFileInfo *info = g_file_query_info(file, "metadata::*," G_FILE_ATTRIBUTE_ID_FILE, G_FILE_QUERY_INFO_NONE,
|
|
|
|
nullptr, nullptr);
|
|
|
|
if (info) {
|
|
|
|
gboolean hasKey = g_file_info_has_attribute(info, "metadata::peony-file-label-ids");
|
|
|
|
if (hasKey) {
|
|
|
|
char *fileLabels = g_file_info_get_attribute_as_string(info, "metadata::peony-file-label-ids");
|
|
|
|
if (fileLabels) {
|
|
|
|
QStringList fileLabelList = QString::fromUtf8(fileLabels).split("\n");
|
|
|
|
g_free(fileLabels);
|
|
|
|
|
|
|
|
hasLabel = std::any_of(fileLabelList.begin(), fileLabelList.end(), [&](QString &fileLabel) {
|
|
|
|
return labels.contains(fileLabel);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasLabel;
|
|
|
|
}
|