From a6a5b7846af66911ea9c399d996989a0f52ab6db Mon Sep 17 00:00:00 2001 From: iaom Date: Sat, 21 Jan 2023 11:37:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=81=B6=E7=8E=B0=E7=9A=84?= =?UTF-8?q?=E7=94=B1=E4=BA=8E=E9=98=9F=E5=88=97=E5=A4=84=E7=90=86=E4=B8=8D?= =?UTF-8?q?=E5=BD=93=E5=AF=BC=E8=87=B4=E7=9A=84=E6=90=9C=E7=B4=A2=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/model/search-result-manager.cpp | 67 +++++++++---------- frontend/model/search-result-manager.h | 13 ++-- libsearch/appsearch/app-search-plugin.cpp | 20 ++---- libsearch/plugininterface/data-queue.h | 8 +++ .../plugininterface/search-plugin-iface.h | 9 +++ .../searchinterface/result-item-private.h | 2 +- 6 files changed, 63 insertions(+), 56 deletions(-) diff --git a/frontend/model/search-result-manager.cpp b/frontend/model/search-result-manager.cpp index a8efa63..b12a3c0 100644 --- a/frontend/model/search-result-manager.cpp +++ b/frontend/model/search-result-manager.cpp @@ -23,21 +23,21 @@ using namespace UkuiSearch; SearchResultManager::SearchResultManager(const QString& plugin_id, QObject *parent) : QObject(parent) { - m_plugin_id = plugin_id; - m_result_queue = new DataQueue; - m_get_result_thread = new ReceiveResultThread(m_result_queue, this); + m_pluginId = plugin_id; + m_resultQueue = new DataQueue; + m_getResultThread = new ReceiveResultThread(m_resultQueue, this); initConnections(); } void SearchResultManager::startSearch(const QString &keyword) { - qDebug()<isRunning()) { - m_get_result_thread->start(); + qDebug()<isRunning()) { + m_getResultThread->start(); } - m_result_queue->clear(); - SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_plugin_id); - plugin->KeywordSearch(keyword, m_result_queue); + m_resultQueue->clear(); + SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_pluginId); + plugin->KeywordSearch(keyword, m_resultQueue); } /** @@ -45,54 +45,49 @@ void SearchResultManager::startSearch(const QString &keyword) */ void SearchResultManager::stopSearch() { - if(m_get_result_thread->isRunning()) { - qDebug()<stop(); - SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_plugin_id); + if(m_getResultThread->isRunning()) { + m_getResultThread->stop(); + SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_pluginId); plugin->stopSearch(); -// m_get_result_thread->quit(); + qDebug() << m_pluginId << "stopped"; } } void SearchResultManager::initConnections() { - connect(m_get_result_thread, &ReceiveResultThread::gotResultInfo, this, &SearchResultManager::gotResultInfo); + connect(m_getResultThread, &ReceiveResultThread::gotResultInfo, this, &SearchResultManager::gotResultInfo); } -ReceiveResultThread::ReceiveResultThread(DataQueue * result_queue, QObject *parent) +ReceiveResultThread::ReceiveResultThread(DataQueue * resultQueue, QObject *parent): QThread(parent) { - m_result_queue = result_queue; + m_resultQueue = resultQueue; } void ReceiveResultThread::stop() { this->requestInterruption(); this->wait(); - this->quit(); } void ReceiveResultThread::run() { - QTimer *m_timer = new QTimer; - m_timer->setInterval(3000); - bool is_empty; - while(!isInterruptionRequested()) { - is_empty = false; - if(!m_result_queue->isEmpty()) { - Q_EMIT this->gotResultInfo(m_result_queue->dequeue()); + QTimer *timer = new QTimer; + timer->setInterval(3000); - } else { - is_empty = true; - } - if(m_timer->isActive() && m_timer->remainingTime() < 0.01) { - this->requestInterruption(); - } - if(is_empty && !m_timer->isActive()) { - m_timer->start(); - } else if(!is_empty) { - m_timer->stop(); - } else { + while(!isInterruptionRequested()) { + SearchPluginIface::ResultInfo oneResult = m_resultQueue->tryDequeue(); + if(oneResult.name.isEmpty()) { + if(!timer->isActive()) { + timer->start(); + } msleep(100); + } else { + timer->stop(); + Q_EMIT gotResultInfo(oneResult); + } + + if(timer->isActive() && timer->remainingTime() < 0.01 && m_resultQueue->isEmpty()) { + this->requestInterruption(); } } delete m_timer; diff --git a/frontend/model/search-result-manager.h b/frontend/model/search-result-manager.h index 0c82525..3c92cb7 100644 --- a/frontend/model/search-result-manager.h +++ b/frontend/model/search-result-manager.h @@ -25,21 +25,22 @@ #include #include #include -#include "pluginmanage/search-plugin-manager.h" +#include "search-plugin-manager.h" namespace UkuiSearch { class ReceiveResultThread : public QThread { Q_OBJECT public: - ReceiveResultThread(DataQueue * result_queue, QObject * parent = nullptr); + ReceiveResultThread(DataQueue * resultQueue, QObject * parent = nullptr); ~ReceiveResultThread() = default; void stop(); protected: void run() override; private: - DataQueue * m_result_queue; + DataQueue * m_resultQueue; + QTimer *m_timer = nullptr; Q_SIGNALS: void gotResultInfo(const SearchPluginIface::ResultInfo&); @@ -59,9 +60,9 @@ public Q_SLOTS: private: void initConnections(); - QString m_plugin_id; - DataQueue * m_result_queue; - ReceiveResultThread * m_get_result_thread = nullptr; + QString m_pluginId; + DataQueue * m_resultQueue; + ReceiveResultThread * m_getResultThread = nullptr; Q_SIGNALS: void gotResultInfo(const SearchPluginIface::ResultInfo&); diff --git a/libsearch/appsearch/app-search-plugin.cpp b/libsearch/appsearch/app-search-plugin.cpp index e3a7bfb..13328d5 100644 --- a/libsearch/appsearch/app-search-plugin.cpp +++ b/libsearch/appsearch/app-search-plugin.cpp @@ -33,7 +33,6 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearch AppSearchPlugin::~AppSearchPlugin() { - this->quit(); this->wait(); if (m_timer) { @@ -161,17 +160,15 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri) void AppSearchPlugin::run() { - m_timer->setInterval(3000); - while (!isInterruptionRequested()) { - if (m_appSearchResults->isEmpty()) { - if (!m_timer->isActive()) { + while(!isInterruptionRequested()) { + ResultItem oneResult = m_appSearchResults->tryDequeue(); + if(oneResult.getSearchId() == 0 && oneResult.getItemKey().isEmpty() && oneResult.getExtral().isEmpty()) { + if(!m_timer->isActive()) { m_timer->start(); } + msleep(100); } else { - if (m_timer->isActive()) { - m_timer->stop(); - } - ResultItem oneResult = m_appSearchResults->dequeue(); + m_timer->stop(); SearchPluginIface::ResultInfo ri; ri.actionKey = oneResult.getExtral().at(0).toString(); ri.name = oneResult.getExtral().at(1).toString(); @@ -184,12 +181,9 @@ void AppSearchPlugin::run() m_searchResult->enqueue(ri); } - if (m_timer->isActive() && m_timer->remainingTime() < 0.01) { - m_timer->setInterval(3000); + if(m_timer->isActive() && m_timer->remainingTime() < 0.01 && m_appSearchResults->isEmpty()) { this->requestInterruption(); } - - msleep(100); } } diff --git a/libsearch/plugininterface/data-queue.h b/libsearch/plugininterface/data-queue.h index 64c4fe8..452af7e 100644 --- a/libsearch/plugininterface/data-queue.h +++ b/libsearch/plugininterface/data-queue.h @@ -27,6 +27,14 @@ public: QMutexLocker locker(&m_mutex); return QList::isEmpty(); } + inline T tryDequeue() { + QMutexLocker locker(&m_mutex); + if(QList::isEmpty()) { + return T(); + } else { + return QList::takeFirst(); + } + } private: QMutex m_mutex; }; diff --git a/libsearch/plugininterface/search-plugin-iface.h b/libsearch/plugininterface/search-plugin-iface.h index 1eb1a13..e4089f7 100644 --- a/libsearch/plugininterface/search-plugin-iface.h +++ b/libsearch/plugininterface/search-plugin-iface.h @@ -42,6 +42,15 @@ public: QVector description; QString actionKey; int type; + ResultInfo(const QIcon &iconToSet = QIcon(), const QString &nameToSet = QString(), + const QVector &descriptionToSet = QVector(), + const QString &actionKeyToSet = QString(), const int &typeToSet = 0) { + icon = iconToSet; + name = nameToSet; + description = descriptionToSet; + actionKey = actionKeyToSet; + type = typeToSet; + } }; virtual ~SearchPluginIface() {} diff --git a/libsearch/searchinterface/result-item-private.h b/libsearch/searchinterface/result-item-private.h index 7c4b00f..4bcf5ee 100644 --- a/libsearch/searchinterface/result-item-private.h +++ b/libsearch/searchinterface/result-item-private.h @@ -16,7 +16,7 @@ public: QVariantList getExtral(); private: - size_t m_searchId; + size_t m_searchId = 0; QString m_itemKey; QVariantList m_extral; //and something else...