From 255518c24546ebcff42d03bc4df526aac85fb4a1 Mon Sep 17 00:00:00 2001 From: iaom Date: Fri, 19 May 2023 15:15:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E5=92=8C=E7=8A=B6=E6=80=81=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../search-task-plugin-iface.h | 2 + libsearch/pluginmanage/plugin-manager.cpp | 2 +- .../search-task-plugin-manager.cpp | 90 +++++-------------- .../pluginmanage/search-task-plugin-manager.h | 10 +-- .../search-controller-private.h | 3 + .../searchinterface/search-controller.cpp | 23 +++++ libsearch/searchinterface/search-controller.h | 5 +- .../searchtasks/app-search-task.cpp | 12 ++- .../searchtasks/app-search-task.h | 3 +- .../searchtasks/file-content-search-task.cpp | 9 ++ .../searchtasks/file-content-search-task.h | 4 +- .../searchtasks/file-search-task.cpp | 13 ++- .../searchtasks/file-search-task.h | 3 +- .../searchinterface/ukui-search-task.cpp | 11 +++ libsearch/searchinterface/ukui-search-task.h | 23 +++++ 15 files changed, 122 insertions(+), 91 deletions(-) diff --git a/libsearch/plugininterface/search-task-plugin-iface.h b/libsearch/plugininterface/search-task-plugin-iface.h index c525d1b..4a7f4fe 100644 --- a/libsearch/plugininterface/search-task-plugin-iface.h +++ b/libsearch/plugininterface/search-task-plugin-iface.h @@ -40,9 +40,11 @@ public: //Asynchronous,multithread. virtual void startSearch() = 0; virtual void stop() = 0; + virtual bool isSearching() = 0; Q_SIGNALS: void searchFinished(size_t searchId); void searchError(size_t searchId, QString msg = {}); + void reachInformNum(); }; } Q_DECLARE_INTERFACE(UkuiSearch::SearchTaskPluginIface, SearchTaskPluginIface_iid) diff --git a/libsearch/pluginmanage/plugin-manager.cpp b/libsearch/pluginmanage/plugin-manager.cpp index 522a249..7668293 100644 --- a/libsearch/pluginmanage/plugin-manager.cpp +++ b/libsearch/pluginmanage/plugin-manager.cpp @@ -78,8 +78,8 @@ PluginManager::PluginManager(QObject *parent) : QObject(parent) } case PluginInterface::PluginType::SearchTaskPlugin: { auto p = dynamic_cast(plugin); - SearchTaskPluginManager::getInstance()->registerPlugin(p); SearchTaskPluginManager::getInstance()->registerPluginPath(p->getCustomSearchType(), pluginsDir.absoluteFilePath(fileName)); + delete p; break; } default: diff --git a/libsearch/pluginmanage/search-task-plugin-manager.cpp b/libsearch/pluginmanage/search-task-plugin-manager.cpp index 01e9fa8..f48da76 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.cpp +++ b/libsearch/pluginmanage/search-task-plugin-manager.cpp @@ -35,78 +35,10 @@ SearchTaskPluginManager *SearchTaskPluginManager::getInstance() return global_instance; } -void SearchTaskPluginManager::initPlugins(SearchProperty::SearchType searchType) -{ - switch (searchType) { - case SearchProperty::SearchType::File: - registerBuildinPlugin(new FileSearchTask(this)); - break; - case SearchProperty::SearchType::FileContent: - registerBuildinPlugin(new FileContentSearchTask(this)); - break; - case SearchProperty::SearchType::Application: - registerBuildinPlugin(new AppSearchTask(this)); - default: - break; - } -} - -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); - connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError); - return true; -} - -bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugin) -{ - m_buildinPlugin.insert(static_cast(plugin->getSearchType()), plugin); - connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished); - connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError); - return true; -} - SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(parent) { } -void SearchTaskPluginManager::pluginSearch(SearchProperty::SearchType searchType, std::shared_ptr searchController) -{ - size_t type = static_cast(searchType); - qDebug() << "search type" << type; - if(m_buildinPlugin.contains(type)) { - if(!m_buildinPlugin.value(type)->isEnable()) { - Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(type)); - return; - } - qDebug() << "start search"; - m_buildinPlugin.value(type)->startSearch(); - } else { - Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(type)); - } -} - -void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared_ptr searchController) -{ - if(m_loadedPlugin.contains(customSearchType)) { - if(!m_loadedPlugin.value(customSearchType)->isEnable()) { - Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(customSearchType)); - return; - } - m_loadedPlugin.value(customSearchType)->startSearch(); - } else { - Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(customSearchType)); - } -} - bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType) { if (m_managedPlugins.contains(uuid)) { @@ -167,6 +99,26 @@ void SearchTaskPluginManager::registerPluginPath(const QString& customType, cons } } +bool SearchTaskPluginManager::isSearching(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType) +{ + if (m_managedPlugins.contains(uuid)) { + ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid); + + SearchTaskPluginIface *plugin = nullptr; + if (searchType == SearchProperty::SearchType::Custom) { + plugin = managedPlugin->externalPlugin(customType); + + } else { + plugin = managedPlugin->internalPlugin(searchType); + } + + if (plugin) { + return plugin->isSearching(); + } + } + return false; +} + SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, SearchProperty::SearchType searchType, const QString& customType) { if (!m_managedPlugins.contains(uuid)) { @@ -177,7 +129,6 @@ SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, S bool succeed = false; if (searchType == SearchProperty::SearchType::Custom) { succeed = m_managedPlugins.value(uuid)->insertExternalPlugin(customType, plugin); - } else { succeed = m_managedPlugins.value(uuid)->insertInternalPlugin(searchType, plugin); } @@ -197,7 +148,6 @@ void SearchTaskPluginManager::destroyPlugins(const QUuid &uuid) ManagedPlugin::~ManagedPlugin() { - } bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searchType, SearchTaskPluginIface *plugin) diff --git a/libsearch/pluginmanage/search-task-plugin-manager.h b/libsearch/pluginmanage/search-task-plugin-manager.h index d1429fa..a0f70fb 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.h +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -50,19 +50,11 @@ class SearchTaskPluginManager : public QObject Q_OBJECT public: static SearchTaskPluginManager *getInstance(); - void initPlugins(SearchProperty::SearchType searchType); SearchTaskPluginIface *initPlugins(const QUuid&, SearchProperty::SearchType, const QString& customType = QString()); - bool registerPlugin(SearchTaskPluginIface *plugin); - bool registerBuildinPlugin(SearchTaskPluginIface *plugin); - void pluginSearch(SearchProperty::SearchType searchType, std::shared_ptr searchController); - void pluginSearch(QString customSearchType, std::shared_ptr searchController); bool startSearch(const QUuid&, SearchProperty::SearchType, const QString& customType = QString()); void destroyPlugins(const QUuid& uuid); void registerPluginPath(const QString& customType, const QString& pluginPath); - -Q_SIGNALS: - void searchFinished(size_t searchId); - void searchError(size_t searchId, QString msg); + bool isSearching(const QUuid &uuid, SearchProperty::SearchType searchType, const QString& customType = QString()); private: explicit SearchTaskPluginManager(QObject *parent = nullptr); diff --git a/libsearch/searchinterface/search-controller-private.h b/libsearch/searchinterface/search-controller-private.h index 6039216..da4ef37 100644 --- a/libsearch/searchinterface/search-controller-private.h +++ b/libsearch/searchinterface/search-controller-private.h @@ -53,8 +53,10 @@ public: * @param maxResults 每次搜索结果集的数量 */ void setMaxResultNum(unsigned int maxResults); + void setInformNum(int num = 0); unsigned int maxResults() const; + int informNum() const; private: void copyData(); @@ -74,6 +76,7 @@ private: bool m_onlySearchDir = false; bool m_searchOnlineApps = false; unsigned int m_maxResults = 100; //默认取100条结果 + int m_informNum = 0; QMap m_searchType2ResultProperties; QMap m_customSearchType2ResultDataType; diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index 8a99522..93fab5b 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -184,6 +184,7 @@ void SearchControllerPrivate::copyData() m_recurse = m_formerController->d->m_recurse; m_activeKeywordSegmentation = m_formerController->d->m_activeKeywordSegmentation; m_maxResults = m_formerController->d->m_maxResults; + m_informNum = m_formerController->d->m_informNum; m_searchOnlineApps = m_formerController->d->m_searchOnlineApps; m_searchType2ResultProperties = m_formerController->d->m_searchType2ResultProperties; m_customSearchType2ResultDataType = m_formerController->d->m_customSearchType2ResultDataType; @@ -229,11 +230,23 @@ void SearchControllerPrivate::setMaxResultNum(unsigned int maxResults) m_maxResults = maxResults; } +void SearchControllerPrivate::setInformNum(int num) +{ + if(num >= 0) { + m_informNum = num; + } +} + unsigned int SearchControllerPrivate::maxResults() const { return m_maxResults; } +int SearchControllerPrivate::informNum() const +{ + return m_informNum; +} + SearchController::SearchController(std::shared_ptr parent) : m_parent(parent), d(new SearchControllerPrivate(this)) { } @@ -401,11 +414,21 @@ void SearchController::setMaxResultNum(unsigned int maxResults) d->setMaxResultNum(maxResults); } +void SearchController::setInformNum(int num) +{ + d->setInformNum(num); +} + unsigned int SearchController::maxResults() const { return d->maxResults(); } +int SearchController::informNum() const +{ + return d->informNum(); +} + bool SearchController::setResultProperties(SearchProperty::SearchType searchType, SearchResultProperties searchResultProperties) { return d->setResultProperties(searchType, searchResultProperties); diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index 524dce7..a520fc6 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -34,6 +34,8 @@ public: void setOnlySearchFile(bool onlySearchFile); void setOnlySearchDir(bool onlySearchDir); void setSearchOnlineApps(bool searchOnlineApps); + void setMaxResultNum(unsigned int maxResults); + void setInformNum(int num = 0); //以上方法插件请不要调用 //以下方法插件可以调用 @@ -57,9 +59,8 @@ public: void clearSearchDir(); void clearFileLabel(); - void setMaxResultNum(unsigned int maxResults); - unsigned int first() const; unsigned int maxResults() const; + int informNum() const; bool setResultProperties(SearchProperty::SearchType searchType, UkuiSearch::SearchResultProperties searchResultProperties); void setCustomResultDataType(QString customSearchType, QStringList dataType); diff --git a/libsearch/searchinterface/searchtasks/app-search-task.cpp b/libsearch/searchinterface/searchtasks/app-search-task.cpp index 51530af..170210d 100644 --- a/libsearch/searchinterface/searchtasks/app-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/app-search-task.cpp @@ -72,9 +72,9 @@ void AppSearchTask::stop() { } -void AppSearchTask::sendFinishSignal(size_t searchId) +bool AppSearchTask::isSearching() { - Q_EMIT searchFinished(searchId); + return m_pool->activeThreadCount() > 0; } AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask) @@ -113,6 +113,10 @@ void AppSearchWorker::run() item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, oneResult.value(ApplicationProperty::Icon).toString()); } m_controller->getDataQueue()->enqueue(item); + if(++m_resultNum == m_controller->informNum()) { + QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum"); + m_resultNum = 0; + } m_controller->finishSearchIdCheck(); } else { qDebug() << "Search id changed!"; @@ -158,6 +162,10 @@ void AppSearchWorker::run() item.setValue(SearchProperty::SearchResultProperty::IsOnlineApplication, 1); } m_controller->getDataQueue()->enqueue(item); + if(++m_resultNum == m_controller->informNum()) { + QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum"); + m_resultNum = 0; + } m_controller->finishSearchIdCheck(); } else { qDebug() << "Search id changed!"; diff --git a/libsearch/searchinterface/searchtasks/app-search-task.h b/libsearch/searchinterface/searchtasks/app-search-task.h index df73642..6eb872d 100644 --- a/libsearch/searchinterface/searchtasks/app-search-task.h +++ b/libsearch/searchinterface/searchtasks/app-search-task.h @@ -51,7 +51,7 @@ public: QString getCustomSearchType(); void startSearch(); void stop(); - Q_INVOKABLE void sendFinishSignal(size_t searchId); + bool isSearching(); private: ApplicationInfo m_appinfo; @@ -74,6 +74,7 @@ private: SearchController* m_controller = nullptr; AppSearchTask *m_appSearchTask = nullptr; size_t m_currentSearchId = 0; + int m_resultNum = 0; }; } #endif // APPSEARCHTASK_H diff --git a/libsearch/searchinterface/searchtasks/file-content-search-task.cpp b/libsearch/searchinterface/searchtasks/file-content-search-task.cpp index 3cebbd3..dd35bff 100644 --- a/libsearch/searchinterface/searchtasks/file-content-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-content-search-task.cpp @@ -103,6 +103,11 @@ void FileContentSearchTask::stop() } +bool FileContentSearchTask::isSearching() +{ + return m_pool->activeThreadCount() > 0; +} + FileContentSearchWorker::FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, std::shared_ptr searchController) { m_fileContentSearchTask = fileContentSearchTask; @@ -187,6 +192,10 @@ bool FileContentSearchWorker::execSearch() QDateTime::fromString(QString::fromStdString(it.get_document().get_value(3)), "yyyyMMddHHmmsszzz")); } m_searchController->getDataQueue()->enqueue(resultItem); + if(++m_resultNum == m_searchController->informNum()) { + QMetaObject::invokeMethod(m_fileContentSearchTask, "reachInformNum"); + m_resultNum = 0; + } m_searchController->finishSearchIdCheck(); } else { diff --git a/libsearch/searchinterface/searchtasks/file-content-search-task.h b/libsearch/searchinterface/searchtasks/file-content-search-task.h index 3f501a3..5b448c9 100644 --- a/libsearch/searchinterface/searchtasks/file-content-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-content-search-task.h @@ -51,6 +51,7 @@ public: SearchProperty::SearchType getSearchType() override; void startSearch() override; void stop() override; + bool isSearching(); private: QThreadPool *m_pool = nullptr; @@ -77,10 +78,9 @@ private: private: FileContentSearchTask *m_fileContentSearchTask = nullptr; SearchController *m_searchController = nullptr; - QStringList m_validDirectories; - size_t m_currentSearchId = 0; + int m_resultNum = 0; }; class FileContentSearchFilter : public Xapian::MatchDecider { diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index b67108e..3054630 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -78,12 +78,11 @@ void FileSearchTask::stop() } -void FileSearchTask::sendFinishSignal(size_t searchId) +bool FileSearchTask::isSearching() { - Q_EMIT searchFinished(searchId); + return m_pool->activeThreadCount() > 0; } - FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr searchController) : m_FileSearchTask(fileSarchTask) { m_searchController = new SearchController(searchController); @@ -228,6 +227,10 @@ bool FileSearchWorker::searchWithIndex() QDateTime::fromString(QString::fromStdString(it.get_document().get_value(2)), "yyyyMMddHHmmsszzz")); } m_searchController->getDataQueue()->enqueue(resultItem); + if(++m_resultNum == m_searchController->informNum()) { + QMetaObject::invokeMethod(m_FileSearchTask, "reachInformNum"); + m_resultNum = 0; + } m_searchController->finishSearchIdCheck(); } else { @@ -329,6 +332,10 @@ bool FileSearchWorker::directSearch() ri.setValue(SearchProperty::SearchResultProperty::ModifiedTime, fileInfo.lastModified()); } m_searchController->getDataQueue()->enqueue(ri); + if(++m_resultNum == m_searchController->informNum()) { + QMetaObject::invokeMethod(m_FileSearchTask, "reachInformNum"); + m_resultNum = 0; + } --maxResults; } m_searchController->finishSearchIdCheck(); diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index ce5abf0..926b0b8 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -49,7 +49,7 @@ public: QString getCustomSearchType(); void startSearch(); void stop(); - Q_INVOKABLE void sendFinishSignal(size_t searchId); + bool isSearching(); private: QThreadPool *m_pool = nullptr; @@ -85,6 +85,7 @@ private: SearchController *m_searchController = nullptr; size_t m_currentSearchId = 0; + int m_resultNum = 0; QStringList m_validDirectories; QStringList m_blackList; QStringList m_labels; diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index 74eff08..53c0ec7 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -73,6 +73,7 @@ void UkuiSearchTask::initSearchPlugin(SearchProperty::SearchType searchType, con plugin->setController(d->m_searchCotroller); connect(plugin, &SearchTaskPluginIface::searchFinished,this, &UkuiSearchTask::searchFinished); connect(plugin, &SearchTaskPluginIface::searchError,this, &UkuiSearchTask::searchError); + connect(plugin, &SearchTaskPluginIface::reachInformNum,this, &UkuiSearchTask::reachInformNum); } else { qWarning() << "The plugin has been initialized or the plugin failed to load."; } @@ -109,6 +110,11 @@ void UkuiSearchTask::stop() d->m_searchCotroller->stop(); } +bool UkuiSearchTask::isSearching(SearchProperty::SearchType searchtype, QString customSearchType) +{ + return SearchTaskPluginManager::getInstance()->isSearching(d->m_uuid, searchtype, customSearchType); +} + void UkuiSearchTask::clearAllConditions() { d->m_searchCotroller->clearAllConditions(); @@ -133,3 +139,8 @@ void UkuiSearchTask::setMaxResultNum(unsigned int maxResults) { d->m_searchCotroller->setMaxResultNum(maxResults); } + +void UkuiSearchTask::setInformNum(int num) +{ + d->m_searchCotroller->setInformNum(num); +} diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index a8c6dd3..ea7de75 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -58,14 +58,37 @@ public: void clearKeyWords(); void clearSearchDir(); void clearFileLabel(); + /** + * @brief setMaxResultNum 设置最大结果数量 + * @param maxResults + */ void setMaxResultNum(unsigned int maxResults = 99999999); + /** + * @brief setInformNum 设置搜索结果提醒数量 + * @param num + */ + void setInformNum(int num); + /** + * @brief startSearch 启动搜索 + * @param searchtype 搜索插件 + * @param customSearchType 外部插件类型,当searchType为Custom时可用 + * @return + */ size_t startSearch(SearchProperty::SearchType searchtype, QString customSearchType = QString()); + /** + * @brief stop 停止搜索 + */ void stop(); + /** + * @brief isSearching 查询某个插件是否处于搜索中 + */ + bool isSearching(SearchProperty::SearchType searchtype, QString customSearchType = {}); Q_SIGNALS: void searchFinished(size_t searchId); void searchError(size_t searchId, QString msg); + void reachInformNum(); private: UkuiSearchTaskPrivate* d = nullptr;