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
|
|
|
|
#ifndef FILESEARCHTASK_H
|
|
|
|
|
#define FILESEARCHTASK_H
|
2022-01-11 16:20:40 +08:00
|
|
|
|
|
|
|
|
|
#include <QIcon>
|
|
|
|
|
#include <QThreadPool>
|
|
|
|
|
#include <QRunnable>
|
2022-03-25 09:40:56 +08:00
|
|
|
|
#include <xapian.h>
|
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
|
#include "search-task-plugin-iface.h"
|
|
|
|
|
#include "search-controller.h"
|
2022-01-11 16:20:40 +08:00
|
|
|
|
#include "result-item.h"
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
|
namespace UkuiSearch {
|
2021-12-31 17:38:02 +08:00
|
|
|
|
class FileSearchTask : public SearchTaskPluginIface
|
2021-12-28 15:56:41 +08:00
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2023-07-06 15:27:23 +08:00
|
|
|
|
explicit FileSearchTask(QObject *parent = nullptr);
|
2023-03-15 16:39:38 +08:00
|
|
|
|
~FileSearchTask();
|
2023-05-06 15:41:56 +08:00
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
|
void setController(const SearchController &searchController);
|
2021-12-28 15:56:41 +08:00
|
|
|
|
PluginType pluginType() {return PluginType::SearchTaskPlugin;}
|
|
|
|
|
const QString name();
|
|
|
|
|
const QString description();
|
|
|
|
|
const QIcon icon() {return QIcon::fromTheme("folder");}
|
2023-04-24 14:07:56 +08:00
|
|
|
|
void setEnable(bool enable) {Q_UNUSED(enable)}
|
2022-01-11 16:20:40 +08:00
|
|
|
|
bool isEnable() { return true;}
|
2021-12-28 15:56:41 +08:00
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
|
SearchProperty::SearchType getSearchType() {return SearchProperty::SearchType::File;}
|
2021-12-31 17:38:02 +08:00
|
|
|
|
QString getCustomSearchType();
|
2023-05-06 15:41:56 +08:00
|
|
|
|
void startSearch();
|
2021-12-28 15:56:41 +08:00
|
|
|
|
void stop();
|
2023-05-19 15:15:28 +08:00
|
|
|
|
bool isSearching();
|
2022-01-24 09:44:42 +08:00
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
|
private:
|
2022-01-11 16:20:40 +08:00
|
|
|
|
QThreadPool *m_pool = nullptr;
|
2023-07-06 15:27:23 +08:00
|
|
|
|
SearchController m_searchController;
|
2022-01-11 16:20:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class FileSearchWorker : public QRunnable
|
|
|
|
|
{
|
2022-03-25 09:40:56 +08:00
|
|
|
|
friend class FileSearchFilter;
|
|
|
|
|
|
2022-01-11 16:20:40 +08:00
|
|
|
|
public:
|
2023-07-06 15:27:23 +08:00
|
|
|
|
explicit FileSearchWorker(FileSearchTask *fileSarchTask, SearchController *searchController);
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
2022-01-11 16:20:40 +08:00
|
|
|
|
protected:
|
|
|
|
|
void run();
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2022-03-25 14:09:34 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 通过索引进行搜索,如果搜索过程正常,返回true
|
|
|
|
|
* 如果搜索被打断,返回false.
|
|
|
|
|
* 搜索被打断是指用户使用同一个task发起多次搜索,导致searchId发生变化,那么上一次搜索即被打断。
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool searchWithIndex();
|
|
|
|
|
//同上
|
|
|
|
|
bool directSearch();
|
2022-03-25 09:40:56 +08:00
|
|
|
|
Xapian::Query creatQueryForFileSearch();
|
2022-04-26 10:43:02 +08:00
|
|
|
|
void sendErrorMsg(const QString &msg);
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
2022-01-11 16:20:40 +08:00
|
|
|
|
private:
|
|
|
|
|
FileSearchTask *m_FileSearchTask;
|
2023-05-06 15:41:56 +08:00
|
|
|
|
SearchController *m_searchController = nullptr;
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
|
|
size_t m_currentSearchId = 0;
|
2023-05-19 15:15:28 +08:00
|
|
|
|
int m_resultNum = 0;
|
2022-03-25 09:40:56 +08:00
|
|
|
|
QStringList m_validDirectories;
|
|
|
|
|
QStringList m_blackList;
|
2022-04-28 15:49:51 +08:00
|
|
|
|
QStringList m_labels;
|
2021-12-28 15:56:41 +08:00
|
|
|
|
};
|
2022-03-25 09:40:56 +08:00
|
|
|
|
|
|
|
|
|
class FileSearchFilter : public Xapian::MatchDecider {
|
|
|
|
|
public:
|
|
|
|
|
explicit FileSearchFilter(FileSearchWorker *parent);
|
|
|
|
|
bool operator ()(const Xapian::Document &doc) const;
|
|
|
|
|
|
2022-04-28 15:49:51 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 检查path对应的文件是否包含labels中的标签
|
|
|
|
|
* @param path
|
|
|
|
|
* @param labels
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static bool checkFileLabel(const QString &path, const QStringList &labels);
|
|
|
|
|
|
2022-03-25 09:40:56 +08:00
|
|
|
|
private:
|
2022-04-28 15:49:51 +08:00
|
|
|
|
FileSearchWorker *m_parent = nullptr;
|
2022-03-25 09:40:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-28 15:56:41 +08:00
|
|
|
|
}
|
|
|
|
|
#endif // FILESEARCHTASK_H
|