From 4fdc102102dbc3f1955201286db502e65707940b Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Tue, 17 Jan 2023 17:49:07 +0800 Subject: [PATCH] Optimize the result queue logic of app search plugin. --- libsearch/appsearch/app-search-plugin.cpp | 75 ++++++++++------------- libsearch/appsearch/app-search-plugin.h | 5 -- 2 files changed, 34 insertions(+), 46 deletions(-) diff --git a/libsearch/appsearch/app-search-plugin.cpp b/libsearch/appsearch/app-search-plugin.cpp index da4efc7..e3a7bfb 100644 --- a/libsearch/appsearch/app-search-plugin.cpp +++ b/libsearch/appsearch/app-search-plugin.cpp @@ -4,8 +4,7 @@ #include #include "file-utils.h" using namespace UkuiSearch; -size_t AppSearchPlugin::uniqueSymbol = 0; -QMutex AppSearchPlugin::m_mutex; + AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearchTask(new UkuiSearchTask(this)) { SearchPluginIface::Actioninfo open { 0, tr("Open")}; @@ -21,12 +20,6 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearch m_timer = new QTimer; m_timer->setInterval(3000); m_timer->moveToThread(this); - connect(this, SIGNAL(startTimer()), m_timer, SLOT(start())); - connect(this, &AppSearchPlugin::stopTimer, m_timer, &QTimer::stop); - connect(m_timer, &QTimer::timeout, this, [ & ]{ - qWarning() << "The app-search thread stopped because of timeout."; - this->quit(); - }); m_appSearchResults = m_appSearchTask->init(); m_appSearchTask->initSearchPlugin(SearchType::Application); @@ -36,37 +29,13 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearch UkuiSearch::ApplicationIconName | UkuiSearch::ApplicationDescription | UkuiSearch::IsOnlineApplication); - - connect(m_appSearchTask, &UkuiSearchTask::searchFinished, this, [ & ] { - if (m_timer->isActive()) { - Q_EMIT this->stopTimer(); - m_timer->setInterval(3000); - } - - while(!m_appSearchResults->isEmpty()) { - ResultItem oneResult = m_appSearchResults->dequeue(); - SearchPluginIface::ResultInfo ri; - ri.actionKey = oneResult.getExtral().at(0).toString(); - ri.name = oneResult.getExtral().at(1).toString(); - ri.icon = oneResult.getExtral().at(2).value(); - SearchPluginIface::DescriptionInfo description; - description.key = QString(tr("Application Description:")); - description.value = oneResult.getExtral().at(3).toString(); - ri.description.append(description); - ri.type = oneResult.getExtral().at(4).toInt(); - m_searchResult->enqueue(ri); - } - - if(!m_timer->isActive()) { - Q_EMIT this->startTimer(); - } - }); } AppSearchPlugin::~AppSearchPlugin() { this->quit(); this->wait(); + if (m_timer) { delete m_timer; m_timer = nullptr; @@ -90,10 +59,6 @@ QString AppSearchPlugin::getPluginName() void AppSearchPlugin::KeywordSearch(QString keyword, DataQueue *searchResult) { - m_mutex.lock(); - ++uniqueSymbol; - m_mutex.unlock(); - if (!this->isRunning()) { this->start(); } @@ -108,9 +73,7 @@ void AppSearchPlugin::KeywordSearch(QString keyword, DataQueuestop(); } QList AppSearchPlugin::getActioninfo(int type) @@ -198,7 +161,36 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri) void AppSearchPlugin::run() { - exec(); + m_timer->setInterval(3000); + while (!isInterruptionRequested()) { + if (m_appSearchResults->isEmpty()) { + if (!m_timer->isActive()) { + m_timer->start(); + } + } else { + if (m_timer->isActive()) { + m_timer->stop(); + } + ResultItem oneResult = m_appSearchResults->dequeue(); + SearchPluginIface::ResultInfo ri; + ri.actionKey = oneResult.getExtral().at(0).toString(); + ri.name = oneResult.getExtral().at(1).toString(); + ri.icon = oneResult.getExtral().at(2).value(); + SearchPluginIface::DescriptionInfo description; + description.key = QString(tr("Application Description:")); + description.value = oneResult.getExtral().at(3).toString(); + ri.description.append(description); + ri.type = oneResult.getExtral().at(4).toInt(); + m_searchResult->enqueue(ri); + } + + if (m_timer->isActive() && m_timer->remainingTime() < 0.01) { + m_timer->setInterval(3000); + this->requestInterruption(); + } + + msleep(100); + } } void AppSearchPlugin::initDetailPage() @@ -308,6 +300,7 @@ bool AppSearchPlugin::launch(const QString &path) g_object_unref(desktopAppInfo); return res; } + bool AppSearchPlugin::addPanelShortcut(const QString& path) { QDBusInterface iface("com.ukui.panel.desktop", "/", diff --git a/libsearch/appsearch/app-search-plugin.h b/libsearch/appsearch/app-search-plugin.h index ee14954..9b91490 100644 --- a/libsearch/appsearch/app-search-plugin.h +++ b/libsearch/appsearch/app-search-plugin.h @@ -52,8 +52,6 @@ private: QList m_actionInfo_not_installed; // QThreadPool m_pool; QTimer *m_timer; - static size_t uniqueSymbol; - static QMutex m_mutex; UkuiSearchTask *m_appSearchTask = nullptr; DataQueue* m_appSearchResults = nullptr; @@ -80,9 +78,6 @@ private: ActionLabel *m_actionLabel4 = nullptr; QVBoxLayout * m_actionLyt = nullptr; -Q_SIGNALS: - void startTimer(); - void stopTimer(); }; //class AppSearch : public QObject, public QRunnable {