diff --git a/libsearch/pluginmanage/search-task-plugin-manager.h b/libsearch/pluginmanage/search-task-plugin-manager.h index dcee0bb..0435b21 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.h +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -20,6 +20,7 @@ Q_SIGNALS: private: explicit SearchTaskPluginManager(QObject *parent = nullptr); + //这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分? QHash m_buildinPlugin; QHash m_loadedPlugin; diff --git a/libsearch/searchinterface/result-item-private.h b/libsearch/searchinterface/result-item-private.h index ae682ac..826c6ef 100644 --- a/libsearch/searchinterface/result-item-private.h +++ b/libsearch/searchinterface/result-item-private.h @@ -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; diff --git a/libsearch/searchinterface/result-item.cpp b/libsearch/searchinterface/result-item.cpp index 79ff90e..5910a50 100644 --- a/libsearch/searchinterface/result-item.cpp +++ b/libsearch/searchinterface/result-item.cpp @@ -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() diff --git a/libsearch/searchinterface/result-item.h b/libsearch/searchinterface/result-item.h index 3689692..be32ae3 100644 --- a/libsearch/searchinterface/result-item.h +++ b/libsearch/searchinterface/result-item.h @@ -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; diff --git a/libsearch/searchinterface/search-controller-private.h b/libsearch/searchinterface/search-controller-private.h index c39b41a..7fae89c 100644 --- a/libsearch/searchinterface/search-controller-private.h +++ b/libsearch/searchinterface/search-controller-private.h @@ -2,7 +2,6 @@ #define SEARCHCONTROLLERPRIVATE_H #include -#include #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* getDataQueue(); @@ -31,19 +32,26 @@ public: QStringList getKeyword(); bool isKeywordSegmentationActived(); QStringList getFileLabel(); + bool isSearchFileOnly(); + bool isSearchDirOnly(); private: + void copyData(); + //TODO: 这里是否可以改为字节对齐的写法? DataQueue* m_dataQueue = nullptr ; + std::shared_ptr> m_sharedDataueue = nullptr; size_t m_searchId = 0; QMutex m_searchIdMutex; - SearchController *q; - QSharedPointer m_formerController; + SearchController *q = nullptr; + std::shared_ptr 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; }; } diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index 8ee243e..aa3d47a 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -4,23 +4,14 @@ #include using namespace UkuiSearch; SearchControllerPrivate::SearchControllerPrivate(SearchController *parent) - : q(parent) + : q(parent), + m_formerController(q->m_parent) //如果构造参数里包含父节点智能指针,则子节点带有一个智能指针指向父节点 { - if(q->m_parent) { - m_formerController = QSharedPointer(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 *SearchControllerPrivate::refreshDataqueue() { if(!m_dataQueue) { m_dataQueue = new DataQueue; + m_sharedDataueue = std::shared_ptr>(m_dataQueue); return m_dataQueue; } m_dataQueue->clear(); @@ -45,6 +37,7 @@ DataQueue *SearchControllerPrivate::initDataQueue() { if(!m_dataQueue) { m_dataQueue = new DataQueue; + m_sharedDataueue = std::shared_ptr>(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 *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 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 *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); @@ -201,7 +241,42 @@ bool SearchController::beginSearchIdCheck(size_t searchId) void SearchController::finishSearchIdCheck() { - d->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() diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index 1d19a4b..866dbad 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -2,17 +2,23 @@ #define SEARCHCONTROLLER_H #include +#include #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 parent = nullptr); ~SearchController(); DataQueue* 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 m_parent = nullptr; + SearchControllerPrivate *d = nullptr; + }; } diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index a76aac7..4b9d70e 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -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)); +} diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index 9e7f828..5323b13 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -1,9 +1,16 @@ #ifndef FILESEARCHTASK_H #define FILESEARCHTASK_H + +#include +#include +#include #include "search-task-plugin-iface.h" #include "search-controller.h" -#include +#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 diff --git a/libsearch/searchinterface/ukui-search-task-private.h b/libsearch/searchinterface/ukui-search-task-private.h index f2bfa10..10ddd13 100644 --- a/libsearch/searchinterface/ukui-search-task-private.h +++ b/libsearch/searchinterface/ukui-search-task-private.h @@ -4,7 +4,7 @@ #include "ukui-search-task.h" #include #include -#include +#include #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 m_searchCotroller = nullptr; size_t m_searchId = 0; UkuiSearchTask* q = nullptr; diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index 9a9e676..acdd2b0 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -7,7 +7,7 @@ UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent) : QObject(parent), q(parent) { - m_searchCotroller = new SearchController(); + m_searchCotroller = std::shared_ptr(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 *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() diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index 642f671..6f6fc9e 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -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: