Optimize the result queue logic of app search plugin.

This commit is contained in:
JunjieBai 2023-01-17 17:49:07 +08:00
parent e73366e7f0
commit 4fdc102102
2 changed files with 34 additions and 46 deletions

View File

@ -4,8 +4,7 @@
#include <QLabel>
#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<QIcon>();
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<SearchPluginIface::ResultInfo> *searchResult)
{
m_mutex.lock();
++uniqueSymbol;
m_mutex.unlock();
if (!this->isRunning()) {
this->start();
}
@ -108,9 +73,7 @@ void AppSearchPlugin::KeywordSearch(QString keyword, DataQueue<SearchPluginIface
void AppSearchPlugin::stopSearch()
{
m_mutex.lock();
++uniqueSymbol;
m_mutex.unlock();
m_appSearchTask->stop();
}
QList<SearchPluginIface::Actioninfo> 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<QIcon>();
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",
"/",

View File

@ -52,8 +52,6 @@ private:
QList<SearchPluginIface::Actioninfo> m_actionInfo_not_installed;
// QThreadPool m_pool;
QTimer *m_timer;
static size_t uniqueSymbol;
static QMutex m_mutex;
UkuiSearchTask *m_appSearchTask = nullptr;
DataQueue<ResultItem>* 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 {