From a422c0cefaa77b96a1243a613359433e8351b127 Mon Sep 17 00:00:00 2001 From: iaom Date: Fri, 31 Dec 2021 17:38:02 +0800 Subject: [PATCH] Add plugin manager for search task. --- libsearch/libukui-search-headers.pri | 1 + libsearch/plugininterface/common-defines.h | 2 +- .../plugininterface/plugin-interface.pri | 2 +- .../search-task-plugin-iface.h | 12 ++-- libsearch/pluginmanage/plugin-manager.cpp | 6 ++ libsearch/pluginmanage/plugin-manager.h | 6 +- libsearch/pluginmanage/plugin-manager.pri | 6 +- .../search-task-plugin-manager.cpp | 61 +++++++++++++++++++ .../pluginmanage/search-task-plugin-manager.h | 29 +++++++++ .../searchinterface/result-item-private.h | 1 + libsearch/searchinterface/result-item.cpp | 9 +++ libsearch/searchinterface/search-controller.h | 5 +- .../searchtasks/file-search-task.cpp | 2 +- .../searchtasks/file-search-task.h | 5 +- .../ukui-search-task-private.h | 2 +- .../searchinterface/ukui-search-task.cpp | 21 ++++--- libsearch/searchinterface/ukui-search-task.h | 2 +- 17 files changed, 146 insertions(+), 26 deletions(-) create mode 100644 libsearch/pluginmanage/search-task-plugin-manager.cpp create mode 100644 libsearch/pluginmanage/search-task-plugin-manager.h diff --git a/libsearch/libukui-search-headers.pri b/libsearch/libukui-search-headers.pri index 5205df9..14b293e 100644 --- a/libsearch/libukui-search-headers.pri +++ b/libsearch/libukui-search-headers.pri @@ -5,4 +5,5 @@ INCLUDEPATH += $$PWD/parser INCLUDEPATH += $$PWD/pluginmanage INCLUDEPATH += $$PWD/settingsearch INCLUDEPATH += $$PWD/appsearch +INCLUDEPATH += $$PWD/searchinterface diff --git a/libsearch/plugininterface/common-defines.h b/libsearch/plugininterface/common-defines.h index 6e183c8..290d009 100644 --- a/libsearch/plugininterface/common-defines.h +++ b/libsearch/plugininterface/common-defines.h @@ -3,7 +3,7 @@ namespace UkuiSearch { enum class SearchType { - File, + File = 0, FileContent, Application, Setting, diff --git a/libsearch/plugininterface/plugin-interface.pri b/libsearch/plugininterface/plugin-interface.pri index f427c43..6d65027 100644 --- a/libsearch/plugininterface/plugin-interface.pri +++ b/libsearch/plugininterface/plugin-interface.pri @@ -1,4 +1,4 @@ -INCLUDEPATH += $$PWD \ +INCLUDEPATH += $$PWD HEADERS += \ $$PWD/action-label.h \ diff --git a/libsearch/plugininterface/search-task-plugin-iface.h b/libsearch/plugininterface/search-task-plugin-iface.h index b407473..43bd0aa 100644 --- a/libsearch/plugininterface/search-task-plugin-iface.h +++ b/libsearch/plugininterface/search-task-plugin-iface.h @@ -1,19 +1,21 @@ #ifndef SEARCHTASKPLUGINIFACE_H #define SEARCHTASKPLUGINIFACE_H -#define SearchTaskPLuginIface_iid "org.ukui.ukui-search.plugin-iface.SearchTaskPLuginIface" +#define SearchTaskPluginIface_iid "org.ukui.ukui-search.plugin-iface.SearchTaskPluginIface" #include #include #include "plugin-iface.h" -#include "search-controller.h" #include "common-defines.h" +#include "search-controller.h" + namespace UkuiSearch { -class SearchTaskPLuginIface : public QObject, public PluginInterface +class SearchTaskPluginIface : public QObject, public PluginInterface { Q_OBJECT public: - virtual QString getSearchType() = 0; + virtual QString getCustomSearchType() = 0; + virtual SearchType getSearchType() = 0; //Asynchronous,multithread. virtual void startSearch(SearchController searchController) = 0; virtual void stop() = 0; @@ -21,7 +23,7 @@ Q_SIGNALS: void searchFinished(size_t searchId); }; } -Q_DECLARE_INTERFACE(UkuiSearch::SearchTaskPLuginIface, SearchTaskPLuginIface_iid) +Q_DECLARE_INTERFACE(UkuiSearch::SearchTaskPluginIface, SearchTaskPluginIface_iid) diff --git a/libsearch/pluginmanage/plugin-manager.cpp b/libsearch/pluginmanage/plugin-manager.cpp index 49af767..88ee196 100644 --- a/libsearch/pluginmanage/plugin-manager.cpp +++ b/libsearch/pluginmanage/plugin-manager.cpp @@ -1,5 +1,6 @@ #include "plugin-manager.h" #include "search-plugin-manager.h" +#include "search-task-plugin-manager.h" using namespace UkuiSearch; @@ -46,6 +47,11 @@ PluginManager::PluginManager(QObject *parent) : QObject(parent) SearchPluginManager::getInstance()->registerPlugin(p); break; } + case PluginInterface::PluginType::SearchTaskPlugin: { + auto p = dynamic_cast(plugin); + SearchTaskPluginManager::getInstance()->registerPlugin(p); + break; + } default: break; } diff --git a/libsearch/pluginmanage/plugin-manager.h b/libsearch/pluginmanage/plugin-manager.h index 685edfe..6b0f29b 100644 --- a/libsearch/pluginmanage/plugin-manager.h +++ b/libsearch/pluginmanage/plugin-manager.h @@ -6,9 +6,9 @@ #include #include #include -#include "libsearch_global.h" -#include "plugininterface/plugin-iface.h" -#include "plugininterface/search-plugin-iface.h" +#include "plugin-iface.h" +#include "search-plugin-iface.h" +#include "search-task-plugin-iface.h" namespace UkuiSearch { class LIBSEARCH_EXPORT PluginManager : public QObject diff --git a/libsearch/pluginmanage/plugin-manager.pri b/libsearch/pluginmanage/plugin-manager.pri index 41847f8..87c7157 100644 --- a/libsearch/pluginmanage/plugin-manager.pri +++ b/libsearch/pluginmanage/plugin-manager.pri @@ -2,8 +2,10 @@ INCLUDEPATH += $$PWD HEADERS += \ $$PWD/search-plugin-manager.h \ - $$PWD/plugin-manager.h + $$PWD/plugin-manager.h \ + $$PWD/search-task-plugin-manager.h SOURCES += \ $$PWD/search-plugin-manager.cpp \ - $$PWD/plugin-manager.cpp + $$PWD/plugin-manager.cpp \ + $$PWD/search-task-plugin-manager.cpp diff --git a/libsearch/pluginmanage/search-task-plugin-manager.cpp b/libsearch/pluginmanage/search-task-plugin-manager.cpp new file mode 100644 index 0000000..1c4bb6a --- /dev/null +++ b/libsearch/pluginmanage/search-task-plugin-manager.cpp @@ -0,0 +1,61 @@ +#include "search-task-plugin-manager.h" +#include +using namespace UkuiSearch; +static SearchTaskPluginManager *global_instance = nullptr; +SearchTaskPluginManager *SearchTaskPluginManager::getInstance() +{ + if (!global_instance) { + global_instance = new SearchTaskPluginManager; + } + return global_instance; +} + +bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin) +{ + //这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。 + //但异常处理我没想好怎么加。。 + //by zpf + if(plugin->getCustomSearchType().isEmpty() || m_loadedPlugin.contains(plugin->getCustomSearchType())) { + qWarning() << "search task plugin load failed! plugin type:" << plugin->getCustomSearchType(); + return false; + } + m_loadedPlugin.insert(plugin->getCustomSearchType(), plugin); + connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished); + return true; +} + +bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *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) +{ + int type = static_cast(searchType); + if(m_buildinPlugin.contains(type)) { + if(!m_buildinPlugin.value(type)->isEnable()) { + Q_EMIT this->pluginDisable(searchController.getCurrentSearchId()); + return; + } + m_buildinPlugin.value(type)->startSearch(searchController); + } +} + +void SearchTaskPluginManager::pluginSearch(QString customSearchType, SearchController searchController) +{ + if(m_loadedPlugin.contains(customSearchType)) { + if(!m_loadedPlugin.value(customSearchType)->isEnable()) { + Q_EMIT this->pluginDisable(searchController.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 new file mode 100644 index 0000000..dcee0bb --- /dev/null +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -0,0 +1,29 @@ +#ifndef SEARCHTASKPLUGINMANAGER_H +#define SEARCHTASKPLUGINMANAGER_H + +#include +#include "search-task-plugin-iface.h" +namespace UkuiSearch { + +class SearchTaskPluginManager : public QObject +{ + Q_OBJECT +public: + static SearchTaskPluginManager *getInstance(); + bool registerPlugin(SearchTaskPluginIface *plugin); + bool registerBuildinPlugin(SearchTaskPluginIface *plugin); + void pluginSearch(SearchType searchType, SearchController searchController); + void pluginSearch(QString customSearchType, SearchController searchController); +Q_SIGNALS: + void searchFinished(size_t searchId); + void pluginDisable(size_t searchId); +private: + explicit SearchTaskPluginManager(QObject *parent = nullptr); + + QHash m_buildinPlugin; + QHash m_loadedPlugin; + +}; +} + +#endif // SEARCHTASKPLUGINMANAGER_H diff --git a/libsearch/searchinterface/result-item-private.h b/libsearch/searchinterface/result-item-private.h index f2fba0a..ae682ac 100644 --- a/libsearch/searchinterface/result-item-private.h +++ b/libsearch/searchinterface/result-item-private.h @@ -18,5 +18,6 @@ private: ResultItem *q; }; + } #endif // RESULTITEMPRIVATE_H diff --git a/libsearch/searchinterface/result-item.cpp b/libsearch/searchinterface/result-item.cpp index f962c24..79ff90e 100644 --- a/libsearch/searchinterface/result-item.cpp +++ b/libsearch/searchinterface/result-item.cpp @@ -1,6 +1,15 @@ #include "result-item.h" #include "result-item-private.h" using namespace UkuiSearch; +ResultItemPrivate::ResultItemPrivate::ResultItemPrivate(const QString &itemKey, ResultItem *parent) +{ + +} + +ResultItemPrivate::~ResultItemPrivate() +{ + +} ResultItem::ResultItem(const QString &itemKey) { d = new ResultItemPrivate(itemKey, this); diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index ad9c9f0..1d19a4b 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -17,18 +17,21 @@ public: DataQueue* refreshDataqueue(); size_t refreshSearchId(); DataQueue* initDataQueue(); + void stop(); void addSearchDir(QString &path); void setRecurse(bool recurse = true); void addKeyword(QString &keyword); void setActiveKeywordSegmentation(bool active); void addFileLabel(QString &label); + //以上方法插件请不要调用 + //以下方法插件可以调用 size_t getCurrentSearchId(); DataQueue* getDataQueue(); bool beginSearchIdCheck(size_t searchId); void finishSearchIdCheck(); - void stop(); + QStringList getSearchDir(); bool isRecurse(); QStringList getKeyword(); diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index f8ddcf0..a76aac7 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -14,7 +14,7 @@ const QString FileSearchTask::description() } -QString FileSearchTask::getSearchType() +QString FileSearchTask::getCustomSearchType() { } diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index dd2b66b..9e7f828 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -4,7 +4,7 @@ #include "search-controller.h" #include namespace UkuiSearch { -class FileSearchTask : public SearchTaskPLuginIface +class FileSearchTask : public SearchTaskPluginIface { Q_OBJECT public: @@ -16,7 +16,8 @@ public: void setEnable(bool enable) {} bool isEnable() {} - QString getSearchType(); + SearchType getSearchType() {return SearchType::File;} + QString getCustomSearchType(); void startSearch(SearchController searchController); void stop(); protected: diff --git a/libsearch/searchinterface/ukui-search-task-private.h b/libsearch/searchinterface/ukui-search-task-private.h index 2555a1d..f2bfa10 100644 --- a/libsearch/searchinterface/ukui-search-task-private.h +++ b/libsearch/searchinterface/ukui-search-task-private.h @@ -21,7 +21,7 @@ public: void addKeyword(QString &keyword); void addFileLabel(QString &label); void setSearchOnlineApps(bool searchOnlineApps); - size_t startSearch(SearchType searchtype); + size_t startSearch(SearchType searchtype, QString customSearchType = QString()); void stop(); private: diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index 46a853d..9a9e676 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -1,5 +1,6 @@ #include "ukui-search-task.h" #include "ukui-search-task-private.h" +#include "search-task-plugin-manager.h" #include using namespace UkuiSearch; UkuiSearchTaskPrivate::UkuiSearchTaskPrivate(UkuiSearchTask *parent) @@ -43,17 +44,21 @@ void UkuiSearchTaskPrivate::setSearchOnlineApps(bool searchOnlineApps) { } -size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype) +size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, QString customSearchType) { m_searchId = m_searchCotroller->refreshSearchId(); -// if(!m_dataQueue) { -// qWarning() << "Please run init first!"; -// return -1; -// } + if(m_searchCotroller->getDataQueue() == nullptr) { + qWarning() << "the date queue has not been initialized, you need run init first!"; + } //plugin manager do async search here - // plugin->search(SearchController(m_searchController)) + if(SearchType::Custom != searchtype) { + SearchTaskPluginManager::getInstance()->pluginSearch(searchtype, SearchController(m_searchCotroller)); + } else { + SearchTaskPluginManager::getInstance()->pluginSearch(customSearchType, SearchController(m_searchCotroller)); + + } return m_searchId; } @@ -102,9 +107,9 @@ void UkuiSearchTask::setSearchOnlineApps(bool searchOnlineApps) d->setSearchOnlineApps(searchOnlineApps); } -size_t UkuiSearchTask::startSearch(SearchType searchtype) +size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchTYpe) { - return d->startSearch(searchtype); + 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 2aafd36..642f671 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -18,7 +18,7 @@ public: void addKeyword(QString &keyword); void addFileLabel(QString &label); void setSearchOnlineApps(bool searchOnlineApps); - size_t startSearch(SearchType searchtype); + size_t startSearch(SearchType searchtype, QString customSearchTYpe = QString()); void stop(); Q_SIGNALS: