From f9f6fecd1d50b678ecf884a9fe2ecef3ef0ee49d Mon Sep 17 00:00:00 2001 From: iaom Date: Sat, 6 May 2023 15:41:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=9A=E5=BC=80=E6=9C=BA?= =?UTF-8?q?=E6=97=B6=E5=BA=94=E7=94=A8=E6=90=9C=E7=B4=A2=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=81=B6=E7=8E=B0=E5=8D=A1=E6=AD=BB=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=8F=92=E4=BB=B6=E6=8E=A5=E5=8F=A3=EF=BC=9B=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=E9=98=9F=E5=88=97?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/model/search-result-manager.cpp | 19 +++--- libsearch/appsearch/app-search-plugin.cpp | 30 ++++----- libsearch/appsearch/app-search-plugin.h | 4 +- .../search-task-plugin-iface.h | 5 +- .../search-task-plugin-manager.cpp | 8 +-- .../pluginmanage/search-task-plugin-manager.h | 5 +- .../search-controller-private.h | 3 +- .../searchinterface/search-controller.cpp | 23 ++++--- .../searchtasks/app-search-task.cpp | 63 ++++++++++--------- .../searchtasks/app-search-task.h | 10 +-- .../searchtasks/file-content-search-task.cpp | 19 +++++- .../searchtasks/file-content-search-task.h | 8 ++- .../searchtasks/file-search-task.cpp | 20 +++++- .../searchtasks/file-search-task.h | 8 ++- .../searchinterface/ukui-search-task.cpp | 3 +- 15 files changed, 126 insertions(+), 102 deletions(-) diff --git a/frontend/model/search-result-manager.cpp b/frontend/model/search-result-manager.cpp index b12a3c0..b0811f8 100644 --- a/frontend/model/search-result-manager.cpp +++ b/frontend/model/search-result-manager.cpp @@ -19,6 +19,7 @@ * */ #include "search-result-manager.h" +#include using namespace UkuiSearch; SearchResultManager::SearchResultManager(const QString& plugin_id, QObject *parent) : QObject(parent) @@ -71,24 +72,18 @@ void ReceiveResultThread::stop() void ReceiveResultThread::run() { - QTimer *timer = new QTimer; - timer->setInterval(3000); - + QDeadlineTimer deadline(3000); while(!isInterruptionRequested()) { SearchPluginIface::ResultInfo oneResult = m_resultQueue->tryDequeue(); if(oneResult.name.isEmpty()) { - if(!timer->isActive()) { - timer->start(); + if(deadline.remainingTime()) { + msleep(100); + } else { + this->requestInterruption(); } - msleep(100); } else { - timer->stop(); + deadline.setRemainingTime(3000); Q_EMIT gotResultInfo(oneResult); } - - if(timer->isActive() && timer->remainingTime() < 0.01 && m_resultQueue->isEmpty()) { - this->requestInterruption(); - } } - delete m_timer; } diff --git a/libsearch/appsearch/app-search-plugin.cpp b/libsearch/appsearch/app-search-plugin.cpp index 3174d3d..55de443 100644 --- a/libsearch/appsearch/app-search-plugin.cpp +++ b/libsearch/appsearch/app-search-plugin.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "file-utils.h" using namespace UkuiSearch; @@ -36,11 +37,6 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearch // m_pool.setMaxThreadCount(1); // m_pool.setExpiryTimeout(1000); initDetailPage(); - - m_timer = new QTimer; - m_timer->setInterval(3000); - m_timer->moveToThread(this); - m_appSearchResults = m_appSearchTask->init(); m_appSearchTask->initSearchPlugin(SearchProperty::SearchType::Application); m_appSearchTask->setSearchOnlineApps(true); @@ -56,11 +52,6 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearch AppSearchPlugin::~AppSearchPlugin() { this->wait(); - - if (m_timer) { - delete m_timer; - m_timer = nullptr; - } } const QString AppSearchPlugin::name() @@ -182,18 +173,21 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri) void AppSearchPlugin::run() { + QDeadlineTimer deadline(3000); while(!isInterruptionRequested()) { ResultItem oneResult = m_appSearchResults->tryDequeue(); SearchResultPropertyMap data = oneResult.getAllValue(); if(oneResult.getSearchId() == 0 && oneResult.getItemKey().isEmpty() && data.isEmpty()) { - if(!m_timer->isActive()) { - m_timer->start(); + if(deadline.remainingTime()) { + msleep(100); + } else { + this->requestInterruption(); } - msleep(100); + } else { - m_timer->stop(); + deadline.setRemainingTime(3000); SearchPluginIface::ResultInfo ri; - ri.actionKey = data.value(SearchProperty::SearchResultProperty::ApplicationDesktopPath).toString(); + ri.name = data.value(SearchProperty::SearchResultProperty::ApplicationLocalName).toString(); ri.icon = XdgIcon::fromTheme(data.value(SearchProperty::SearchResultProperty::ApplicationIconName).toString(), QIcon(":/res/icons/unknown.svg")); SearchPluginIface::DescriptionInfo description; @@ -203,13 +197,11 @@ void AppSearchPlugin::run() ri.type = data.value(SearchProperty::SearchResultProperty::IsOnlineApplication).toInt(); if (ri.type == 1) { ri.actionKey = data.value(SearchProperty::SearchResultProperty::ApplicationPkgName).toString(); + } else { + ri.actionKey = data.value(SearchProperty::SearchResultProperty::ApplicationDesktopPath).toString(); } m_searchResult->enqueue(ri); } - - if(m_timer->isActive() && m_timer->remainingTime() < 0.01 && m_appSearchResults->isEmpty()) { - this->requestInterruption(); - } } } diff --git a/libsearch/appsearch/app-search-plugin.h b/libsearch/appsearch/app-search-plugin.h index 12a4488..b03526e 100644 --- a/libsearch/appsearch/app-search-plugin.h +++ b/libsearch/appsearch/app-search-plugin.h @@ -59,7 +59,7 @@ public: // bool isPreviewEnable(QString key, int type); // QWidget *previewPage(QString key, int type, QWidget *parent); QWidget *detailPage(const ResultInfo &ri); - void run() override; + void run(); private: void initDetailPage(); bool launch(const QString &path); @@ -69,8 +69,6 @@ private: bool m_enable = true; QList m_actionInfo_installed; QList m_actionInfo_not_installed; -// QThreadPool m_pool; - QTimer *m_timer; UkuiSearchTask *m_appSearchTask = nullptr; DataQueue* m_appSearchResults = nullptr; diff --git a/libsearch/plugininterface/search-task-plugin-iface.h b/libsearch/plugininterface/search-task-plugin-iface.h index 59264c9..c525d1b 100644 --- a/libsearch/plugininterface/search-task-plugin-iface.h +++ b/libsearch/plugininterface/search-task-plugin-iface.h @@ -20,7 +20,7 @@ #ifndef SEARCHTASKPLUGINIFACE_H #define SEARCHTASKPLUGINIFACE_H #define SearchTaskPluginIface_iid "org.ukui.ukui-search.plugin-iface.SearchTaskPluginIface" -#define SEARCH_TASK_PLUGIN_IFACE_VERSION "1.0.0" +#define SEARCH_TASK_PLUGIN_IFACE_VERSION "1.1.0" #include #include @@ -34,10 +34,11 @@ class SearchTaskPluginIface : public QObject, public PluginInterface { Q_OBJECT public: + virtual void setController(const std::shared_ptr &searchController) = 0; virtual QString getCustomSearchType() = 0; virtual SearchProperty::SearchType getSearchType() = 0; //Asynchronous,multithread. - virtual void startSearch(std::shared_ptr searchController) = 0; + virtual void startSearch() = 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 91e0cba..01e9fa8 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.cpp +++ b/libsearch/pluginmanage/search-task-plugin-manager.cpp @@ -88,7 +88,7 @@ void SearchTaskPluginManager::pluginSearch(SearchProperty::SearchType searchType return; } qDebug() << "start search"; - m_buildinPlugin.value(type)->startSearch(searchController); + m_buildinPlugin.value(type)->startSearch(); } else { Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(type)); } @@ -101,13 +101,13 @@ void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(customSearchType)); return; } - m_loadedPlugin.value(customSearchType)->startSearch(searchController); + 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, std::shared_ptr searchController, SearchProperty::SearchType searchType, const QString &customType) +bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType) { if (m_managedPlugins.contains(uuid)) { ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid); @@ -121,7 +121,7 @@ bool SearchTaskPluginManager::startSearch(const QUuid &uuid, std::shared_ptrstartSearch(std::move(searchController)); + plugin->startSearch(); return true; } } diff --git a/libsearch/pluginmanage/search-task-plugin-manager.h b/libsearch/pluginmanage/search-task-plugin-manager.h index 5b8c685..d1429fa 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.h +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -56,10 +56,8 @@ public: 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&, std::shared_ptr, SearchProperty::SearchType, const QString& customType = QString()); + bool startSearch(const QUuid&, SearchProperty::SearchType, const QString& customType = QString()); void destroyPlugins(const QUuid& uuid); - - SearchTaskPluginIface *getPlugin(SearchProperty::SearchType searchType, const QString& customType = QString()); void registerPluginPath(const QString& customType, const QString& pluginPath); Q_SIGNALS: @@ -68,6 +66,7 @@ Q_SIGNALS: private: explicit SearchTaskPluginManager(QObject *parent = nullptr); + SearchTaskPluginIface *getPlugin(SearchProperty::SearchType searchType, const QString& customType = QString()); //这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分? QHash m_buildinPlugin; diff --git a/libsearch/searchinterface/search-controller-private.h b/libsearch/searchinterface/search-controller-private.h index c63e030..6039216 100644 --- a/libsearch/searchinterface/search-controller-private.h +++ b/libsearch/searchinterface/search-controller-private.h @@ -58,8 +58,7 @@ public: private: void copyData(); - //TODO: 这里是否可以改为字节对齐的写法? -// DataQueue* m_dataQueue = nullptr ; + std::shared_ptr> m_sharedDataQueue = nullptr; size_t m_searchId = 0; QMutex m_searchIdMutex; diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index 29e16b9..8a99522 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -174,16 +174,19 @@ bool SearchControllerPrivate::isSearchOnlineApps() void SearchControllerPrivate::copyData() { if(m_formerController.get()) { - m_searchId = m_formerController.get()->getCurrentSearchId(); - //所有子节点都有一个指向根节点的队列的智能指针 - m_sharedDataQueue = m_formerController.get()->d->m_sharedDataQueue; - 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(); + m_searchId = m_formerController->d->m_searchId; + m_sharedDataQueue = m_formerController->d->m_sharedDataQueue; + m_keywords = m_formerController->d->m_keywords; + m_searchDirs = m_formerController->d->m_searchDirs; + m_FileLabels = m_formerController->d->m_FileLabels; + m_onlySearchFile = m_formerController->d->m_onlySearchFile; + m_onlySearchDir = m_formerController->d->m_onlySearchDir; + m_recurse = m_formerController->d->m_recurse; + m_activeKeywordSegmentation = m_formerController->d->m_activeKeywordSegmentation; + m_maxResults = m_formerController->d->m_maxResults; + m_searchOnlineApps = m_formerController->d->m_searchOnlineApps; + m_searchType2ResultProperties = m_formerController->d->m_searchType2ResultProperties; + m_customSearchType2ResultDataType = m_formerController->d->m_customSearchType2ResultDataType; } } diff --git a/libsearch/searchinterface/searchtasks/app-search-task.cpp b/libsearch/searchinterface/searchtasks/app-search-task.cpp index 24155d6..51530af 100644 --- a/libsearch/searchinterface/searchtasks/app-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/app-search-task.cpp @@ -31,7 +31,7 @@ AppSearchTask::AppSearchTask(QObject *parent) this->setParent(parent); qRegisterMetaType("size_t"); m_pool = new QThreadPool(this); - m_pool->setMaxThreadCount(1); + m_pool->setMaxThreadCount(3); qDBusRegisterMetaType>(); qDBusRegisterMetaType>>(); } @@ -42,6 +42,11 @@ AppSearchTask::~AppSearchTask() m_pool->waitForDone(); } +void AppSearchTask::setController(const std::shared_ptr &searchController) +{ + m_searchController = searchController; +} + const QString AppSearchTask::name() { return tr("Application"); @@ -57,11 +62,9 @@ QString AppSearchTask::getCustomSearchType() return "Application"; } -void AppSearchTask::startSearch(std::shared_ptr searchController) +void AppSearchTask::startSearch() { - m_searchController = searchController; - AppSearchWorker *appSearchWorker; - appSearchWorker = new AppSearchWorker(this); + AppSearchWorker *appSearchWorker = new AppSearchWorker(this); m_pool->start(appSearchWorker); } @@ -74,23 +77,16 @@ void AppSearchTask::sendFinishSignal(size_t searchId) Q_EMIT searchFinished(searchId); } - AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask) { - m_interFace = new QDBusInterface("com.kylin.softwarecenter.getsearchresults", "/com/kylin/softwarecenter/getsearchresults", - "com.kylin.getsearchresults", - QDBusConnection::sessionBus()); - if(!m_interFace->isValid()) { - qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); - } - m_interFace->setTimeout(1500); - m_currentSearchId = m_appSearchTask->m_searchController->getCurrentSearchId(); + m_controller = new SearchController(m_appSearchTask->m_searchController); + m_currentSearchId = m_controller->getCurrentSearchId(); } void AppSearchWorker::run() { ApplicationProperties applicationProperties; - SearchResultProperties properties = m_appSearchTask->m_searchController->getResultProperties(SearchProperty::SearchType::Application); + SearchResultProperties properties = m_controller->getResultProperties(SearchProperty::SearchType::Application); if(properties.contains(SearchProperty::SearchResultProperty::ApplicationDesktopPath)) { applicationProperties.append(ApplicationProperty::DesktopFilePath); } @@ -100,9 +96,9 @@ void AppSearchWorker::run() if(properties.contains(SearchProperty::SearchResultProperty::ApplicationIconName)) { applicationProperties.append(ApplicationProperty::Icon); } - ApplicationInfoMap data = m_appSearchTask->m_appinfo.searchApp(applicationProperties, m_appSearchTask->m_searchController->getKeyword(), ApplicationPropertyMap{{ApplicationProperty::DontDisplay, 0}, {ApplicationProperty::AutoStart, 0}}); + ApplicationInfoMap data = m_appSearchTask->m_appinfo.searchApp(applicationProperties, m_controller->getKeyword(), ApplicationPropertyMap{{ApplicationProperty::DontDisplay, 0}, {ApplicationProperty::AutoStart, 0}}); for (const QString &desktop : data.keys()) { - if (m_appSearchTask->m_searchController->beginSearchIdCheck(m_currentSearchId)) { + if (m_controller->beginSearchIdCheck(m_currentSearchId)) { ResultItem item(desktop); item.setSearchId(m_currentSearchId); @@ -116,21 +112,27 @@ void AppSearchWorker::run() if(oneResult.contains(ApplicationProperty::Icon)) { item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, oneResult.value(ApplicationProperty::Icon).toString()); } - m_appSearchTask->m_searchController->getDataQueue()->enqueue(item); - m_appSearchTask->m_searchController->finishSearchIdCheck(); + m_controller->getDataQueue()->enqueue(item); + m_controller->finishSearchIdCheck(); } else { qDebug() << "Search id changed!"; - m_appSearchTask->m_searchController->finishSearchIdCheck(); + m_controller->finishSearchIdCheck(); return; } } - if (m_appSearchTask->m_searchController->isSearchOnlineApps()) { + if (m_controller->isSearchOnlineApps()) { //online app search - for (auto keyword : m_appSearchTask->m_searchController->getKeyword()) { - QDBusReply>> reply = m_interFace->call("get_search_result", keyword); //阻塞,直到远程方法调用完成。 + for (auto keyword : m_controller->getKeyword()) { + QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.kylin.softwarecenter.getsearchresults"), + QStringLiteral("/com/kylin/softwarecenter/getsearchresults"), + QStringLiteral("com.kylin.getsearchresults"), + QStringLiteral("get_search_result")); + msg.setArguments({keyword}); + QDBusReply>> reply = QDBusConnection::sessionBus().call(msg, QDBus::Block, 1500); if(reply.isValid()) { +// qDebug() << reply.value(); for(int i = 0; i < reply.value().size(); i++) { - if (m_appSearchTask->m_searchController->beginSearchIdCheck(m_currentSearchId)) { + if (m_controller->beginSearchIdCheck(m_currentSearchId)) { ResultItem item(m_currentSearchId); item.setItemKey(reply.value().at(i).value("appname")); if(properties.contains(SearchProperty::SearchResultProperty::ApplicationPkgName)) { @@ -155,11 +157,11 @@ void AppSearchWorker::run() if(properties.contains(SearchProperty::SearchResultProperty::IsOnlineApplication)) { item.setValue(SearchProperty::SearchResultProperty::IsOnlineApplication, 1); } - m_appSearchTask->m_searchController->getDataQueue()->enqueue(item); - m_appSearchTask->m_searchController->finishSearchIdCheck(); + m_controller->getDataQueue()->enqueue(item); + m_controller->finishSearchIdCheck(); } else { qDebug() << "Search id changed!"; - m_appSearchTask->m_searchController->finishSearchIdCheck(); + m_controller->finishSearchIdCheck(); return; } } @@ -175,9 +177,10 @@ void AppSearchWorker::run() AppSearchWorker::~AppSearchWorker() { - if (m_interFace) - delete m_interFace; - m_interFace = nullptr; + if(m_controller) { + delete m_controller; + m_controller = nullptr; + } } void AppSearchWorker::sendErrorMsg(const QString &msg) diff --git a/libsearch/searchinterface/searchtasks/app-search-task.h b/libsearch/searchinterface/searchtasks/app-search-task.h index dee7024..df73642 100644 --- a/libsearch/searchinterface/searchtasks/app-search-task.h +++ b/libsearch/searchinterface/searchtasks/app-search-task.h @@ -37,8 +37,9 @@ class AppSearchTask : public SearchTaskPluginIface friend class AppSearchWorker; Q_OBJECT public: - explicit AppSearchTask(QObject *parent); + explicit AppSearchTask(QObject *parent = nullptr); ~AppSearchTask(); + void setController(const std::shared_ptr &searchController); PluginType pluginType() {return PluginType::SearchTaskPlugin;} const QString name(); const QString description(); @@ -48,7 +49,7 @@ public: SearchProperty::SearchType getSearchType() {return SearchProperty::SearchType::Application;} QString getCustomSearchType(); - void startSearch(std::shared_ptr searchController); + void startSearch(); void stop(); Q_INVOKABLE void sendFinishSignal(size_t searchId); @@ -62,17 +63,16 @@ class AppSearchWorker : public QRunnable { public: explicit AppSearchWorker(AppSearchTask *AppSarchTask); - + ~AppSearchWorker(); protected: void run(); private: - ~AppSearchWorker(); void sendErrorMsg(const QString &msg); private: + SearchController* m_controller = nullptr; AppSearchTask *m_appSearchTask = nullptr; - QDBusInterface *m_interFace = nullptr; size_t m_currentSearchId = 0; }; } diff --git a/libsearch/searchinterface/searchtasks/file-content-search-task.cpp b/libsearch/searchinterface/searchtasks/file-content-search-task.cpp index 15c1211..e639182 100644 --- a/libsearch/searchinterface/searchtasks/file-content-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-content-search-task.cpp @@ -47,6 +47,11 @@ FileContentSearchTask::~FileContentSearchTask() m_pool->waitForDone(); } +void FileContentSearchTask::setController(const std::shared_ptr &searchController) +{ + m_searchController = searchController; +} + PluginInterface::PluginType FileContentSearchTask::pluginType() { return PluginType::SearchTaskPlugin; @@ -87,9 +92,9 @@ SearchProperty::SearchType FileContentSearchTask::getSearchType() return SearchProperty::SearchType::FileContent; } -void FileContentSearchTask::startSearch(std::shared_ptr searchController) +void FileContentSearchTask::startSearch() { - FileContentSearchWorker *worker = new FileContentSearchWorker(this, searchController); + FileContentSearchWorker *worker = new FileContentSearchWorker(this, m_searchController); m_pool->start(worker); } @@ -101,10 +106,18 @@ void FileContentSearchTask::stop() FileContentSearchWorker::FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, std::shared_ptr searchController) { m_fileContentSearchTask = fileContentSearchTask; - m_searchController = std::move(searchController); + m_searchController = new SearchController(searchController); m_currentSearchId = m_searchController->getCurrentSearchId(); } +FileContentSearchWorker::~FileContentSearchWorker() +{ + if(m_searchController) { + delete m_searchController; + m_searchController = nullptr; + } +} + void FileContentSearchWorker::run() { QStringList searchDirs = m_searchController->getSearchDir(); diff --git a/libsearch/searchinterface/searchtasks/file-content-search-task.h b/libsearch/searchinterface/searchtasks/file-content-search-task.h index f80500a..3f501a3 100644 --- a/libsearch/searchinterface/searchtasks/file-content-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-content-search-task.h @@ -39,7 +39,7 @@ public: explicit FileContentSearchTask(QObject *parent = nullptr); ~FileContentSearchTask() override; - + void setController(const std::shared_ptr &searchController) override; PluginType pluginType() override; const QString name() override; const QString description() override; @@ -49,12 +49,13 @@ public: QString getCustomSearchType() override; SearchProperty::SearchType getSearchType() override; - void startSearch(std::shared_ptr searchController) override; + void startSearch() override; void stop() override; private: QThreadPool *m_pool = nullptr; bool m_enable = true; + std::shared_ptr m_searchController; }; class FileContentSearchWorker : public QRunnable @@ -63,6 +64,7 @@ class FileContentSearchWorker : public QRunnable public: explicit FileContentSearchWorker(FileContentSearchTask *fileContentSearchTask, std::shared_ptr searchController); + ~FileContentSearchWorker(); void run() override; @@ -74,7 +76,7 @@ private: private: FileContentSearchTask *m_fileContentSearchTask = nullptr; - std::shared_ptr m_searchController; + SearchController *m_searchController = nullptr; QStringList m_validDirectories; diff --git a/libsearch/searchinterface/searchtasks/file-search-task.cpp b/libsearch/searchinterface/searchtasks/file-search-task.cpp index 9842962..b67108e 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-search-task.cpp @@ -46,6 +46,11 @@ FileSearchTask::~FileSearchTask() m_pool->waitForDone(); } +void FileSearchTask::setController(const std::shared_ptr &searchController) +{ + m_searchController = searchController; +} + const QString FileSearchTask::name() { return "File"; @@ -61,10 +66,10 @@ QString FileSearchTask::getCustomSearchType() return "File"; } -void FileSearchTask::startSearch(std::shared_ptr searchController) +void FileSearchTask::startSearch() { FileSearchWorker *fileSearchWorker; - fileSearchWorker = new FileSearchWorker(this, searchController); + fileSearchWorker = new FileSearchWorker(this, m_searchController); m_pool->start(fileSearchWorker); } @@ -79,11 +84,20 @@ void FileSearchTask::sendFinishSignal(size_t searchId) } -FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr searchController) : m_FileSearchTask(fileSarchTask), m_searchController(searchController) +FileSearchWorker::FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr searchController) : m_FileSearchTask(fileSarchTask) { + m_searchController = new SearchController(searchController); m_currentSearchId = m_searchController->getCurrentSearchId(); } +FileSearchWorker::~FileSearchWorker() +{ + if(m_searchController) { + delete m_searchController; + m_searchController = nullptr; + } +} + void FileSearchWorker::run() { //1.检查是否为不可搜索目录 diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index b3be0d9..ce5abf0 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -36,6 +36,8 @@ class FileSearchTask : public SearchTaskPluginIface public: explicit FileSearchTask(QObject *parent); ~FileSearchTask(); + + void setController(const std::shared_ptr &searchController); PluginType pluginType() {return PluginType::SearchTaskPlugin;} const QString name(); const QString description(); @@ -45,12 +47,13 @@ public: SearchProperty::SearchType getSearchType() {return SearchProperty::SearchType::File;} QString getCustomSearchType(); - void startSearch(std::shared_ptr searchController); + void startSearch(); void stop(); Q_INVOKABLE void sendFinishSignal(size_t searchId); private: QThreadPool *m_pool = nullptr; + std::shared_ptr m_searchController; }; class FileSearchWorker : public QRunnable @@ -59,6 +62,7 @@ class FileSearchWorker : public QRunnable public: explicit FileSearchWorker(FileSearchTask *fileSarchTask, std::shared_ptr searchController); + ~FileSearchWorker(); protected: void run(); @@ -78,7 +82,7 @@ private: private: FileSearchTask *m_FileSearchTask; - std::shared_ptr m_searchController; + SearchController *m_searchController = nullptr; size_t m_currentSearchId = 0; QStringList m_validDirectories; diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index a4d719f..74eff08 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -70,6 +70,7 @@ void UkuiSearchTask::initSearchPlugin(SearchProperty::SearchType searchType, con { SearchTaskPluginIface *plugin = SearchTaskPluginManager::getInstance()->initPlugins(d->m_uuid, searchType, customSearchType); if (plugin) { + plugin->setController(d->m_searchCotroller); connect(plugin, &SearchTaskPluginIface::searchFinished,this, &UkuiSearchTask::searchFinished); connect(plugin, &SearchTaskPluginIface::searchError,this, &UkuiSearchTask::searchError); } else { @@ -96,7 +97,7 @@ size_t UkuiSearchTask::startSearch(SearchProperty::SearchType searchtype, QStrin d->m_searchCotroller->refreshDataqueue(); //plugin manager do async search here - if (!SearchTaskPluginManager::getInstance()->startSearch(d->m_uuid, d->m_searchCotroller, searchtype, customSearchType)) { + if (!SearchTaskPluginManager::getInstance()->startSearch(d->m_uuid, searchtype, customSearchType)) { Q_EMIT searchError(d->m_searchCotroller->getCurrentSearchId(), tr("Current task uuid error or an unregistered plugin is used!")); }