Merge branch '1213-ukss' into 'ukss-dev'

Add file search task plugin frame.

See merge request kylin-desktop/ukui-search!242
This commit is contained in:
纪笑旭 2022-01-17 09:43:00 +00:00
commit 4f5e12a57d
12 changed files with 245 additions and 48 deletions

View File

@ -20,6 +20,7 @@ Q_SIGNALS:
private:
explicit SearchTaskPluginManager(QObject *parent = nullptr);
//这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分?
QHash<int, SearchTaskPluginIface*> m_buildinPlugin;
QHash<QString, SearchTaskPluginIface*> m_loadedPlugin;

View File

@ -6,10 +6,12 @@ namespace UkuiSearch {
class ResultItemPrivate
{
public:
explicit ResultItemPrivate(const QString &itemKey, ResultItem *parent);
explicit ResultItemPrivate(ResultItem *parent);
~ResultItemPrivate();
void setSearchId(size_t searchId);
void setItemKey(QString itemKey);
private:
size_t searchId;
size_t m_searchId;
QString m_itemKey;
QString m_label;
QVariant m_extral;

View File

@ -1,18 +1,42 @@
#include "result-item.h"
#include "result-item-private.h"
using namespace UkuiSearch;
ResultItemPrivate::ResultItemPrivate::ResultItemPrivate(const QString &itemKey, ResultItem *parent)
ResultItemPrivate::ResultItemPrivate::ResultItemPrivate(ResultItem *parent) : q(parent)
{
}
ResultItemPrivate::~ResultItemPrivate()
{
}
ResultItem::ResultItem(const QString &itemKey)
void ResultItemPrivate::setSearchId(size_t searchId)
{
d = new ResultItemPrivate(itemKey, this);
m_searchId = searchId;
}
void ResultItemPrivate::setItemKey(QString itemKey)
{
m_itemKey = itemKey;
}
ResultItem::ResultItem() : d(new ResultItemPrivate(this))
{
}
ResultItem::ResultItem(const size_t searchId) : d(new ResultItemPrivate(this))
{
d->setSearchId(searchId);
}
ResultItem::ResultItem(const QString itemKey) : d(new ResultItemPrivate(this))
{
d->setItemKey(itemKey);
}
ResultItem::ResultItem(const size_t searchId, const QString itemKey) : d(new ResultItemPrivate(this))
{
d->setSearchId(searchId);
d->setItemKey(itemKey);
}
ResultItem::~ResultItem()

View File

@ -8,7 +8,10 @@ class ResultItemPrivate;
class ResultItem
{
public:
explicit ResultItem(const QString &itemKey);
explicit ResultItem();
explicit ResultItem(const size_t searchId);
explicit ResultItem(const QString itemKey);
explicit ResultItem(const size_t searchId, const QString itemKey);
~ResultItem();
private:
ResultItemPrivate *d;

View File

@ -2,7 +2,6 @@
#define SEARCHCONTROLLERPRIVATE_H
#include <QMutex>
#include <QSharedPointer>
#include "search-controller.h"
namespace UkuiSearch {
@ -20,6 +19,8 @@ public:
void addKeyword(QString &keyword);
void setActiveKeywordSegmentation(bool active);
void addFileLabel(QString &label);
void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir);
size_t getCurrentSearchId();
DataQueue<ResultItem>* getDataQueue();
@ -31,19 +32,26 @@ public:
QStringList getKeyword();
bool isKeywordSegmentationActived();
QStringList getFileLabel();
bool isSearchFileOnly();
bool isSearchDirOnly();
private:
void copyData();
//TODO: 这里是否可以改为字节对齐的写法?
DataQueue<ResultItem>* m_dataQueue = nullptr ;
std::shared_ptr<DataQueue<ResultItem>> m_sharedDataueue = nullptr;
size_t m_searchId = 0;
QMutex m_searchIdMutex;
SearchController *q;
QSharedPointer<SearchController> m_formerController;
SearchController *q = nullptr;
std::shared_ptr<SearchController> m_formerController = nullptr;
QStringList m_keywords;
QStringList m_searchDirs;
QStringList m_FileLabels;
bool m_recurse = true;
bool m_activeKeywordSegmentation = false;
bool m_onlySearchFile = false;
bool m_onlySearchDir = false;
};
}

View File

@ -4,23 +4,14 @@
#include <QDebug>
using namespace UkuiSearch;
SearchControllerPrivate::SearchControllerPrivate(SearchController *parent)
: q(parent)
: q(parent),
m_formerController(q->m_parent) //如果构造参数里包含父节点智能指针,则子节点带有一个智能指针指向父节点
{
if(q->m_parent) {
m_formerController = QSharedPointer<SearchController>(q->m_parent);
m_searchId = q->m_parent->d->m_searchId;
m_dataQueue = q->m_parent->d->m_dataQueue;
m_keywords = q->m_parent->d->m_keywords;
m_searchDirs = q->m_parent->d->m_searchDirs;
m_FileLabels = q->m_parent->d->m_FileLabels;
m_recurse = q->m_parent->d->m_recurse;
m_activeKeywordSegmentation = q->m_parent->d->m_activeKeywordSegmentation;
}
copyData();
}
SearchControllerPrivate::~SearchControllerPrivate()
{
m_formerController.clear();
}
size_t SearchControllerPrivate::refreshSearchId()
@ -35,6 +26,7 @@ DataQueue<ResultItem> *SearchControllerPrivate::refreshDataqueue()
{
if(!m_dataQueue) {
m_dataQueue = new DataQueue<ResultItem>;
m_sharedDataueue = std::shared_ptr<DataQueue<ResultItem>>(m_dataQueue);
return m_dataQueue;
}
m_dataQueue->clear();
@ -45,6 +37,7 @@ DataQueue<ResultItem> *SearchControllerPrivate::initDataQueue()
{
if(!m_dataQueue) {
m_dataQueue = new DataQueue<ResultItem>;
m_sharedDataueue = std::shared_ptr<DataQueue<ResultItem>>(m_dataQueue);
return m_dataQueue;
}
return m_dataQueue;
@ -75,6 +68,16 @@ void SearchControllerPrivate::addFileLabel(QString &label)
m_FileLabels.append(label);
}
void SearchControllerPrivate::setOnlySearchFile(bool onlySearchFile)
{
m_onlySearchFile = onlySearchFile;
}
void SearchControllerPrivate::setOnlySearchDir(bool onlySearchDir)
{
m_onlySearchDir = onlySearchDir;
}
size_t SearchControllerPrivate::getCurrentSearchId()
{
return m_searchId;
@ -82,7 +85,7 @@ size_t SearchControllerPrivate::getCurrentSearchId()
DataQueue<ResultItem> *SearchControllerPrivate::getDataQueue()
{
return m_dataQueue;
return m_sharedDataueue.get();
}
bool SearchControllerPrivate::beginSearchIdCheck(size_t searchId)
@ -112,30 +115,57 @@ void SearchControllerPrivate::stop()
QStringList SearchControllerPrivate::getSearchDir()
{
return m_searchDirs;
}
bool SearchControllerPrivate::isRecurse()
{
return m_recurse;
}
QStringList SearchControllerPrivate::getKeyword()
{
return m_keywords;
}
bool SearchControllerPrivate::isKeywordSegmentationActived()
{
return m_activeKeywordSegmentation;
}
QStringList SearchControllerPrivate::getFileLabel()
{
return m_FileLabels;
}
SearchController::SearchController(SearchController *parent) : m_parent(parent), d(new SearchControllerPrivate(this))
bool SearchControllerPrivate::isSearchFileOnly()
{
return m_onlySearchFile;
}
bool SearchControllerPrivate::isSearchDirOnly()
{
return m_onlySearchDir;
}
void SearchControllerPrivate::copyData()
{
if(m_formerController.get()) {
m_searchId = m_formerController.get()->getCurrentSearchId();
//所有子节点都有一个指向根节点的队列的智能指针
m_sharedDataueue = m_formerController.get()->d->m_sharedDataueue;
m_keywords = m_formerController.get()->getKeyword();
m_searchDirs = m_formerController.get()->getSearchDir();
m_FileLabels = m_formerController.get()->getFileLabel();
m_onlySearchFile = m_formerController.get()->isSearchFileOnly();
m_onlySearchDir = m_formerController.get()->isSearchDirOnly();
m_recurse = m_formerController.get()->isRecurse();
m_activeKeywordSegmentation = m_formerController.get()->isKeywordSegmentationActived();
}
}
SearchController::SearchController(std::shared_ptr<SearchController> parent) : m_parent(parent), d(new SearchControllerPrivate(this))
{
}
@ -176,7 +206,7 @@ void SearchController::addKeyword(QString &keyword)
size_t SearchController::getCurrentSearchId()
{
d->getCurrentSearchId();
return d->getCurrentSearchId();
}
DataQueue<ResultItem> *SearchController::getDataQueue()
@ -194,6 +224,16 @@ void SearchController::addFileLabel(QString &label)
d->addFileLabel(label);
}
void SearchController::setOnlySearchFile(bool onlySearchFile)
{
d->setOnlySearchFile(onlySearchFile);
}
void SearchController::setOnlySearchDir(bool onlySearchDir)
{
d->setOnlySearchDir(onlySearchDir);
}
bool SearchController::beginSearchIdCheck(size_t searchId)
{
return d->beginSearchIdCheck(searchId);
@ -204,6 +244,41 @@ void SearchController::finishSearchIdCheck()
d->finishSearchIdCheck();
}
QStringList SearchController::getSearchDir()
{
return d->getSearchDir();
}
bool SearchController::isRecurse()
{
return d->isRecurse();
}
QStringList SearchController::getKeyword()
{
return d->getKeyword();
}
bool SearchController::isKeywordSegmentationActived()
{
return d->isKeywordSegmentationActived();
}
QStringList SearchController::getFileLabel()
{
return d->getFileLabel();
}
bool SearchController::isSearchFileOnly()
{
return d->isSearchFileOnly();
}
bool SearchController::isSearchDirOnly()
{
return d->isSearchDirOnly();
}
void SearchController::stop()
{
d->stop();

View File

@ -2,17 +2,23 @@
#define SEARCHCONTROLLER_H
#include <QStringList>
#include <memory>
#include "data-queue.h"
//todo: url parser?
namespace UkuiSearch {
class UkuiSearchTask;
class ResultItem;
class SearchControllerPrivate;
/*
*ID
*
*使
*/
class SearchController
{
friend class SearchControllerPrivate;
public:
explicit SearchController(SearchController *parent = nullptr);
explicit SearchController(std::shared_ptr<SearchController> parent = nullptr);
~SearchController();
DataQueue<ResultItem>* refreshDataqueue();
size_t refreshSearchId();
@ -24,6 +30,8 @@ public:
void addKeyword(QString &keyword);
void setActiveKeywordSegmentation(bool active);
void addFileLabel(QString &label);
void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir);
//以上方法插件请不要调用
//以下方法插件可以调用
@ -37,9 +45,12 @@ public:
QStringList getKeyword();
bool isKeywordSegmentationActived();
QStringList getFileLabel();
bool isSearchFileOnly();
bool isSearchDirOnly();
private:
SearchControllerPrivate *d;
SearchController *m_parent;
std::shared_ptr<SearchController> m_parent = nullptr;
SearchControllerPrivate *d = nullptr;
};
}

View File

@ -2,26 +2,30 @@
using namespace UkuiSearch;
FileSearchTask::FileSearchTask(SearchController searchController): m_searchControl(searchController)
{
m_pool = new QThreadPool(this);
m_pool->setMaxThreadCount(1);
}
const QString FileSearchTask::name()
{
return "File";
}
const QString FileSearchTask::description()
{
return "File search.";
}
QString FileSearchTask::getCustomSearchType()
{
return "File";
}
void FileSearchTask::startSearch(SearchController searchController)
{
FileSearchWorker *fileSearchWorker;
fileSearchWorker = new FileSearchWorker(this, searchController);
m_pool->start(fileSearchWorker);
}
void FileSearchTask::stop()
@ -29,6 +33,11 @@ void FileSearchTask::stop()
}
void FileSearchTask::sendFinishSignal(size_t searchId)
{
Q_EMIT searchFinished(searchId);
}
void FileSearchTask::run()
{
//file search, based on index or direct search?
@ -41,3 +50,23 @@ void FileSearchTask::run()
m_searchControl.finishSearchIdCheck();
//finish
}
FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController)
{
}
void FileSearchWorker::run()
{
//TODO do search here
size_t searchId = m_searchController.getCurrentSearchId();
ResultItem ri(searchId);
if(m_searchController.beginSearchIdCheck(m_searchController.getCurrentSearchId())) {
m_searchController.getDataQueue()->enqueue(ri);
m_searchController.finishSearchIdCheck();
} else {
m_searchController.finishSearchIdCheck();
return;
}
QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, searchId));
}

View File

@ -1,9 +1,16 @@
#ifndef FILESEARCHTASK_H
#define FILESEARCHTASK_H
#include <QIcon>
#include <QThreadPool>
#include <QRunnable>
#include "search-task-plugin-iface.h"
#include "search-controller.h"
#include <QIcon>
#include "result-item.h"
namespace UkuiSearch {
/*
*
*/
class FileSearchTask : public SearchTaskPluginIface
{
Q_OBJECT
@ -13,17 +20,30 @@ public:
const QString name();
const QString description();
const QIcon icon() {return QIcon::fromTheme("folder");}
void setEnable(bool enable) {}
bool isEnable() {}
void setEnable() {}
bool isEnable() { return true;}
SearchType getSearchType() {return SearchType::File;}
QString getCustomSearchType();
void startSearch(SearchController searchController);
void stop();
Q_INVOKABLE void sendFinishSignal(size_t searchId);
protected:
void run();
private:
SearchController m_searchControl;
QThreadPool *m_pool = nullptr;
};
class FileSearchWorker : public QRunnable
{
public:
explicit FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController);
protected:
void run();
private:
FileSearchTask *m_FileSearchTask;
SearchController m_searchController;
};
}
#endif // FILESEARCHTASK_H

View File

@ -4,7 +4,7 @@
#include "ukui-search-task.h"
#include <QThreadPool>
#include <QMutex>
#include <QSharedPointer>
#include <memory>
#include "data-queue.h"
#include "search-controller.h"
@ -20,12 +20,14 @@ public:
void setRecurse(bool recurse = true);
void addKeyword(QString &keyword);
void addFileLabel(QString &label);
void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir);
void setSearchOnlineApps(bool searchOnlineApps);
size_t startSearch(SearchType searchtype, QString customSearchType = QString());
void stop();
private:
SearchController* m_searchCotroller;
std::shared_ptr<SearchController> m_searchCotroller = nullptr;
size_t m_searchId = 0;
UkuiSearchTask* q = nullptr;

View File

@ -7,7 +7,7 @@ UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent)
: QObject(parent),
q(parent)
{
m_searchCotroller = new SearchController();
m_searchCotroller = std::shared_ptr<SearchController>(new SearchController());
}
UkuiSearchTaskPrivate::~UkuiSearchTaskPrivate()
@ -40,6 +40,16 @@ void UkuiSearchTaskPrivate::addFileLabel(QString &label)
m_searchCotroller->addFileLabel(label);
}
void UkuiSearchTaskPrivate::setOnlySearchFile(bool onlySearchFile)
{
m_searchCotroller->setOnlySearchFile(onlySearchFile);
}
void UkuiSearchTaskPrivate::setOnlySearchDir(bool onlySearchDir)
{
m_searchCotroller->setOnlySearchDir(onlySearchDir);
}
void UkuiSearchTaskPrivate::setSearchOnlineApps(bool searchOnlineApps)
{
}
@ -79,7 +89,7 @@ UkuiSearchTask::~UkuiSearchTask()
DataQueue<ResultItem> *UkuiSearchTask::init()
{
d->init();
return d->init();
}
void UkuiSearchTask::addSearchDir(QString &path)
@ -102,14 +112,24 @@ void UkuiSearchTask::addFileLabel(QString &label)
d->addFileLabel(label);
}
void UkuiSearchTask::setOnlySearchFile(bool onlySearchFile)
{
d->setOnlySearchFile(onlySearchFile);
}
void UkuiSearchTask::setOnlySearchDir(bool onlySearchDir)
{
d->setOnlySearchDir(onlySearchDir);
}
void UkuiSearchTask::setSearchOnlineApps(bool searchOnlineApps)
{
d->setSearchOnlineApps(searchOnlineApps);
}
size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchTYpe)
size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchType)
{
return d->startSearch(searchtype, customSearchTYpe);
return d->startSearch(searchtype, customSearchType);
}
void UkuiSearchTask::stop()

View File

@ -17,8 +17,10 @@ public:
void setRecurse(bool recurse = true);
void addKeyword(QString &keyword);
void addFileLabel(QString &label);
void setOnlySearchFile(bool onlySearchFile);
void setOnlySearchDir(bool onlySearchDir);
void setSearchOnlineApps(bool searchOnlineApps);
size_t startSearch(SearchType searchtype, QString customSearchTYpe = QString());
size_t startSearch(SearchType searchtype, QString customSearchType = QString());
void stop();
Q_SIGNALS: