diff --git a/libsearch/plugininterface/common-defines.h b/libsearch/plugininterface/common-defines.h index 290d009..0107a89 100644 --- a/libsearch/plugininterface/common-defines.h +++ b/libsearch/plugininterface/common-defines.h @@ -3,13 +3,13 @@ namespace UkuiSearch { enum class SearchType { - File = 0, - FileContent, - Application, - Setting, - Note, - Mail, - Custom + File = 0x1 << 0, + FileContent = 0x1 << 1, + Application = 0x1 << 2, + Setting = 0x1 << 3, + Note = 0x1 << 4, + Mail = 0x1 << 5, + Custom = 0x1 << 6 }; } #endif // COMMONDEFINES_H diff --git a/libsearch/plugininterface/search-task-plugin-iface.h b/libsearch/plugininterface/search-task-plugin-iface.h index 43bd0aa..c905355 100644 --- a/libsearch/plugininterface/search-task-plugin-iface.h +++ b/libsearch/plugininterface/search-task-plugin-iface.h @@ -17,7 +17,7 @@ public: virtual QString getCustomSearchType() = 0; virtual SearchType getSearchType() = 0; //Asynchronous,multithread. - virtual void startSearch(SearchController searchController) = 0; + virtual void startSearch(std::shared_ptr searchController) = 0; virtual void stop() = 0; Q_SIGNALS: void searchFinished(size_t searchId); diff --git a/libsearch/pluginmanage/search-task-plugin-manager.cpp b/libsearch/pluginmanage/search-task-plugin-manager.cpp index 1c4bb6a..3678d26 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.cpp +++ b/libsearch/pluginmanage/search-task-plugin-manager.cpp @@ -1,5 +1,7 @@ #include "search-task-plugin-manager.h" #include + +#include "file-search-task.h" using namespace UkuiSearch; static SearchTaskPluginManager *global_instance = nullptr; SearchTaskPluginManager *SearchTaskPluginManager::getInstance() @@ -10,6 +12,14 @@ SearchTaskPluginManager *SearchTaskPluginManager::getInstance() return global_instance; } +void SearchTaskPluginManager::initPlugins(SearchType searchType) +{ + size_t type = static_cast(searchType); + if (type & static_cast(SearchType::File)) { + registerBuildinPlugin(new FileSearchTask(this)); + } +} + bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin) { //这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。 @@ -26,34 +36,34 @@ bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin) bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugin) { - m_buildinPlugin.insert(static_cast(plugin->getSearchType()), plugin); + m_buildinPlugin.insert(static_cast(plugin->getSearchType()), plugin); connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished); return true; } SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(parent) { - //register buildin plugin into m_buildinPlugin - //registerBuildinPlugin(); } -void SearchTaskPluginManager::pluginSearch(SearchType searchType, SearchController searchController) +void SearchTaskPluginManager::pluginSearch(SearchType searchType, std::shared_ptr searchController) { - int type = static_cast(searchType); + size_t type = static_cast(searchType); + qDebug() << "search type" << type; if(m_buildinPlugin.contains(type)) { if(!m_buildinPlugin.value(type)->isEnable()) { - Q_EMIT this->pluginDisable(searchController.getCurrentSearchId()); + Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId()); return; } + qDebug() << "start search"; m_buildinPlugin.value(type)->startSearch(searchController); } } -void SearchTaskPluginManager::pluginSearch(QString customSearchType, SearchController searchController) +void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared_ptr searchController) { if(m_loadedPlugin.contains(customSearchType)) { if(!m_loadedPlugin.value(customSearchType)->isEnable()) { - Q_EMIT this->pluginDisable(searchController.getCurrentSearchId()); + Q_EMIT this->pluginDisable(searchController.get()->getCurrentSearchId()); return; } m_loadedPlugin.value(customSearchType)->startSearch(searchController); diff --git a/libsearch/pluginmanage/search-task-plugin-manager.h b/libsearch/pluginmanage/search-task-plugin-manager.h index 0435b21..0c6f751 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.h +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -10,10 +10,11 @@ class SearchTaskPluginManager : public QObject Q_OBJECT public: static SearchTaskPluginManager *getInstance(); + void initPlugins(SearchType searchType); bool registerPlugin(SearchTaskPluginIface *plugin); bool registerBuildinPlugin(SearchTaskPluginIface *plugin); - void pluginSearch(SearchType searchType, SearchController searchController); - void pluginSearch(QString customSearchType, SearchController searchController); + void pluginSearch(SearchType searchType, std::shared_ptr searchController); + void pluginSearch(QString customSearchType, std::shared_ptr searchController); Q_SIGNALS: void searchFinished(size_t searchId); void pluginDisable(size_t searchId); @@ -21,7 +22,7 @@ private: explicit SearchTaskPluginManager(QObject *parent = nullptr); //这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分? - QHash m_buildinPlugin; + QHash m_buildinPlugin; QHash m_loadedPlugin; }; diff --git a/libsearch/searchinterface/result-item-private.h b/libsearch/searchinterface/result-item-private.h index 826c6ef..3c3a5d7 100644 --- a/libsearch/searchinterface/result-item-private.h +++ b/libsearch/searchinterface/result-item-private.h @@ -10,6 +10,8 @@ public: ~ResultItemPrivate(); void setSearchId(size_t searchId); void setItemKey(QString itemKey); + size_t getSearchId(); + QString getItemKey(); private: size_t m_searchId; QString m_itemKey; diff --git a/libsearch/searchinterface/result-item.cpp b/libsearch/searchinterface/result-item.cpp index 5910a50..8b6d0e5 100644 --- a/libsearch/searchinterface/result-item.cpp +++ b/libsearch/searchinterface/result-item.cpp @@ -19,6 +19,16 @@ void ResultItemPrivate::setItemKey(QString itemKey) { m_itemKey = itemKey; } + +size_t ResultItemPrivate::getSearchId() +{ + return m_searchId; +} + +QString ResultItemPrivate::getItemKey() +{ + return m_itemKey; +} ResultItem::ResultItem() : d(new ResultItemPrivate(this)) { } @@ -39,6 +49,16 @@ ResultItem::ResultItem(const size_t searchId, const QString itemKey) : d(new Res d->setItemKey(itemKey); } +size_t ResultItem::getSearchId() +{ + return d->getSearchId(); +} + +QString ResultItem::getItemKey() +{ + return d->getItemKey(); +} + ResultItem::~ResultItem() { } diff --git a/libsearch/searchinterface/result-item.h b/libsearch/searchinterface/result-item.h index be32ae3..5e159bc 100644 --- a/libsearch/searchinterface/result-item.h +++ b/libsearch/searchinterface/result-item.h @@ -12,6 +12,8 @@ public: explicit ResultItem(const size_t searchId); explicit ResultItem(const QString itemKey); explicit ResultItem(const size_t searchId, const QString itemKey); + size_t getSearchId(); + QString getItemKey(); ~ResultItem(); private: ResultItemPrivate *d; diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index aa3d47a..c6a0481 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -171,7 +171,10 @@ SearchController::SearchController(std::shared_ptr parent) : m SearchController::~SearchController() { - delete d; + if(d) { + delete d; + d = nullptr; + } } DataQueue *SearchController::refreshDataqueue() diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index 866dbad..7113061 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -19,6 +19,8 @@ class SearchController friend class SearchControllerPrivate; public: explicit SearchController(std::shared_ptr parent = nullptr); + SearchController(SearchController &) = delete; + SearchController &operator =(const SearchController &) = delete; ~SearchController(); DataQueue* refreshDataqueue(); size_t refreshSearchId(); diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index 4b9d70e..cb6a328 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -1,7 +1,13 @@ #include "file-search-task.h" +#include +#include +#include +#include using namespace UkuiSearch; -FileSearchTask::FileSearchTask(SearchController searchController): m_searchControl(searchController) +FileSearchTask::FileSearchTask(QObject *parent) { + this->setParent(parent); + qRegisterMetaType("size_t"); m_pool = new QThreadPool(this); m_pool->setMaxThreadCount(1); } @@ -21,7 +27,7 @@ QString FileSearchTask::getCustomSearchType() return "File"; } -void FileSearchTask::startSearch(SearchController searchController) +void FileSearchTask::startSearch(std::shared_ptr searchController) { FileSearchWorker *fileSearchWorker; fileSearchWorker = new FileSearchWorker(this, searchController); @@ -38,35 +44,56 @@ void FileSearchTask::sendFinishSignal(size_t searchId) Q_EMIT searchFinished(searchId); } -void FileSearchTask::run() -{ - //file search, based on index or direct search? - //1.do search - if(m_searchControl.beginSearchIdCheck(m_searchControl.getCurrentSearchId())) { - //do enqueue here - m_searchControl.finishSearchIdCheck(); - } - m_searchControl.finishSearchIdCheck(); - //finish -} - -FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, SearchController searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController) +FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController) { } void FileSearchWorker::run() { + qDebug() << "File search start"; //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(); + QQueue bfs; + bfs.enqueue(QDir::homePath()); + QFileInfoList list; + QDir dir; + if(true == m_searchController.get()->isSearchDirOnly()) { + dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); } else { - m_searchController.finishSearchIdCheck(); - return; + dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); + dir.setSorting(QDir::DirsFirst); } - QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, searchId)); + while(!bfs.empty()) { + dir.setPath(bfs.dequeue()); + list = dir.entryInfoList(); + for (auto i : list) { + if (i.isDir() && (!(i.isSymLink()))) { + bfs.enqueue(i.absoluteFilePath()); + } + bool matched = true; + for(QString word : m_searchController.get()->getKeyword()) { + if(!i.fileName().contains(word, Qt::CaseInsensitive)) { + matched = false; +// return; + } + } + if(matched) { + if((i.isDir() && true == m_searchController.get()->isSearchFileOnly()) || + (i.isFile() && true == m_searchController.get()->isSearchDirOnly())) { + continue; + } else { + ResultItem ri(m_searchController.get()->getCurrentSearchId(), i.absoluteFilePath()); + if(m_searchController.get()->beginSearchIdCheck(m_searchController.get()->getCurrentSearchId())) { + m_searchController.get()->getDataQueue()->enqueue(ri); + m_searchController.get()->finishSearchIdCheck(); + } else { + qDebug() << "Search id changed!"; + m_searchController.get()->finishSearchIdCheck(); + return; + } + } + } + } + } + QMetaObject::invokeMethod(m_FileSearchTask, "searchFinished", Q_ARG(size_t, m_searchController.get()->getCurrentSearchId())); } diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index 5323b13..9c3082b 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -15,35 +15,33 @@ class FileSearchTask : public SearchTaskPluginIface { Q_OBJECT public: - explicit FileSearchTask(SearchController searchController); + explicit FileSearchTask(QObject *parent); PluginType pluginType() {return PluginType::SearchTaskPlugin;} const QString name(); const QString description(); const QIcon icon() {return QIcon::fromTheme("folder");} - void setEnable() {} + void setEnable(bool enable) {} bool isEnable() { return true;} SearchType getSearchType() {return SearchType::File;} QString getCustomSearchType(); - void startSearch(SearchController searchController); + void startSearch(std::shared_ptr 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); + explicit FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr searchController); protected: void run(); private: FileSearchTask *m_FileSearchTask; - SearchController m_searchController; + std::shared_ptr m_searchController; }; } #endif // FILESEARCHTASK_H diff --git a/libsearch/searchinterface/ukui-search-task-private.h b/libsearch/searchinterface/ukui-search-task-private.h index 10ddd13..56a31b4 100644 --- a/libsearch/searchinterface/ukui-search-task-private.h +++ b/libsearch/searchinterface/ukui-search-task-private.h @@ -23,6 +23,7 @@ public: void setOnlySearchFile(bool onlySearchFile); void setOnlySearchDir(bool onlySearchDir); void setSearchOnlineApps(bool searchOnlineApps); + void initSearchPlugin(SearchType searchType); size_t startSearch(SearchType searchtype, QString customSearchType = QString()); void stop(); diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index acdd2b0..c5ac029 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -8,6 +8,7 @@ UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent) q(parent) { m_searchCotroller = std::shared_ptr(new SearchController()); + connect(SearchTaskPluginManager::getInstance(), &SearchTaskPluginManager::searchFinished, this, &UkuiSearchTaskPrivate::searchFinished); } UkuiSearchTaskPrivate::~UkuiSearchTaskPrivate() @@ -54,6 +55,11 @@ void UkuiSearchTaskPrivate::setSearchOnlineApps(bool searchOnlineApps) { } +void UkuiSearchTaskPrivate::initSearchPlugin(SearchType searchType) +{ + SearchTaskPluginManager::getInstance()->initPlugins(searchType); +} + size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customSearchType) { @@ -64,9 +70,9 @@ size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customS //plugin manager do async search here if(SearchType::Custom != searchtype) { - SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, SearchController(m_searchCotroller)); + SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, m_searchCotroller); } else { - SearchTaskPluginManager::getInstance()->pluginSearch(customSearchType, SearchController(m_searchCotroller)); + SearchTaskPluginManager::getInstance()->pluginSearch(customSearchType, m_searchCotroller); } @@ -127,6 +133,11 @@ void UkuiSearchTask::setSearchOnlineApps(bool searchOnlineApps) d->setSearchOnlineApps(searchOnlineApps); } +void UkuiSearchTask::initSearchPlugin(SearchType searchType) +{ + d->initSearchPlugin(searchType); +} + size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchType) { return d->startSearch(searchtype, customSearchType); diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index 6f6fc9e..8cdda5b 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -20,6 +20,7 @@ public: void setOnlySearchFile(bool onlySearchFile); void setOnlySearchDir(bool onlySearchDir); void setSearchOnlineApps(bool searchOnlineApps); + void initSearchPlugin(SearchType searchType); size_t startSearch(SearchType searchtype, QString customSearchType = QString()); void stop();