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..a7b9e34 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->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..706af3a 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,24 @@ public: QStringList getKeyword(); bool isKeywordSegmentationActived(); QStringList getFileLabel(); + bool isSearchFileOnly(); + bool isSearchDirOnly(); private: + void copyData(); DataQueue* m_dataQueue = 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..3d840ec 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -4,23 +4,12 @@ #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; - } } SearchControllerPrivate::~SearchControllerPrivate() { - m_formerController.clear(); } size_t SearchControllerPrivate::refreshSearchId() @@ -75,6 +64,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; @@ -112,30 +111,55 @@ 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_dataQueue = m_formerController.get()->getDataQueue(); + 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 +200,7 @@ void SearchController::addKeyword(QString &keyword) size_t SearchController::getCurrentSearchId() { - d->getCurrentSearchId(); + return d->getCurrentSearchId(); } DataQueue *SearchController::getDataQueue() @@ -194,6 +218,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 +235,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..0efc16e 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -2,6 +2,7 @@ #define SEARCHCONTROLLER_H #include +#include #include "data-queue.h" //todo: url parser? namespace UkuiSearch { @@ -12,7 +13,7 @@ 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 +25,8 @@ public: void addKeyword(QString &keyword); void setActiveKeywordSegmentation(bool active); void addFileLabel(QString &label); + void setOnlySearchFile(bool onlySearchFile); + void setOnlySearchDir(bool onlySearchDir); //以上方法插件请不要调用 //以下方法插件可以调用 @@ -37,9 +40,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..a83d610 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -1,8 +1,12 @@ #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 { @@ -13,17 +17,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..7cc6179 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,6 +112,16 @@ 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); diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index 642f671..ad15a42 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -17,6 +17,8 @@ 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();