Optimize the structure of app-search plugin.

This commit is contained in:
JunjieBai 2022-06-23 10:13:58 +08:00
parent 57df997039
commit e7cfd7bd83
3 changed files with 146 additions and 87 deletions

View File

@ -6,7 +6,7 @@
using namespace UkuiSearch; using namespace UkuiSearch;
size_t AppSearchPlugin::uniqueSymbol = 0; size_t AppSearchPlugin::uniqueSymbol = 0;
QMutex AppSearchPlugin::m_mutex; QMutex AppSearchPlugin::m_mutex;
AppSearchPlugin::AppSearchPlugin(QObject *parent) : QObject(parent), m_appSearchTask(new UkuiSearchTask(this)) AppSearchPlugin::AppSearchPlugin(QObject *parent) : QThread(parent), m_appSearchTask(new UkuiSearchTask(this))
{ {
SearchPluginIface::Actioninfo open { 0, tr("Open")}; SearchPluginIface::Actioninfo open { 0, tr("Open")};
SearchPluginIface::Actioninfo addtoDesktop { 1, tr("Add Shortcut to Desktop")}; SearchPluginIface::Actioninfo addtoDesktop { 1, tr("Add Shortcut to Desktop")};
@ -14,10 +14,20 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QObject(parent), m_appSearch
SearchPluginIface::Actioninfo install { 0, tr("Install")}; SearchPluginIface::Actioninfo install { 0, tr("Install")};
m_actionInfo_installed << open << addtoDesktop << addtoPanel; m_actionInfo_installed << open << addtoDesktop << addtoPanel;
m_actionInfo_not_installed << install; m_actionInfo_not_installed << install;
m_pool.setMaxThreadCount(1); // m_pool.setMaxThreadCount(1);
m_pool.setExpiryTimeout(1000); // m_pool.setExpiryTimeout(1000);
initDetailPage(); initDetailPage();
m_timer = new QTimer(this);
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()<<"-------------->stopped by itself";
this->quit();
});
m_appSearchResults = m_appSearchTask->init(); m_appSearchResults = m_appSearchTask->init();
m_appSearchTask->initSearchPlugin(SearchType::Application); m_appSearchTask->initSearchPlugin(SearchType::Application);
m_appSearchTask->setSearchOnlineApps(true); m_appSearchTask->setSearchOnlineApps(true);
@ -26,6 +36,31 @@ AppSearchPlugin::AppSearchPlugin(QObject *parent) : QObject(parent), m_appSearch
UkuiSearch::ApplicationIconName | UkuiSearch::ApplicationIconName |
UkuiSearch::ApplicationDescription | UkuiSearch::ApplicationDescription |
UkuiSearch::IsOnlineApplication); 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();
}
});
} }
const QString AppSearchPlugin::name() const QString AppSearchPlugin::name()
@ -48,8 +83,17 @@ void AppSearchPlugin::KeywordSearch(QString keyword, DataQueue<SearchPluginIface
m_mutex.lock(); m_mutex.lock();
++uniqueSymbol; ++uniqueSymbol;
m_mutex.unlock(); m_mutex.unlock();
AppSearch *appsearch = new AppSearch(searchResult, m_appSearchResults, m_appSearchTask, keyword, uniqueSymbol);
m_pool.start(appsearch); if (!this->isRunning()) {
this->start();
}
m_searchResult = searchResult;
m_appSearchTask->clearKeyWords();
m_appSearchTask->addKeyword(keyword);
m_appSearchTask->startSearch(SearchType::Application);
// AppSearch *appsearch = new AppSearch(searchResult, m_appSearchResults, m_appSearchTask, keyword, uniqueSymbol);
// m_pool.start(appsearch);
} }
void AppSearchPlugin::stopSearch() void AppSearchPlugin::stopSearch()
@ -142,6 +186,11 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri)
return m_detailPage; return m_detailPage;
} }
void AppSearchPlugin::run()
{
exec();
}
void AppSearchPlugin::initDetailPage() void AppSearchPlugin::initDetailPage()
{ {
m_detailPage = new QWidget(); m_detailPage = new QWidget();
@ -273,72 +322,75 @@ bool AppSearchPlugin::installAppAction(const QString & name) {
} }
} }
AppSearch::AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol) //AppSearch::AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol)
{ //{
this->setAutoDelete(true); // this->setAutoDelete(true);
m_search_result = searchResult; // m_search_result = searchResult;
m_appSearchResults = appSearchResults; // m_appSearchResults = appSearchResults;
m_appSearchTask = appSearchTask; // m_appSearchTask = appSearchTask;
m_appSearchTask->clearKeyWords(); // m_appSearchTask->clearKeyWords();
m_appSearchTask->addKeyword(keyword); // m_appSearchTask->addKeyword(keyword);
m_uniqueSymbol = uniqueSymbol; // m_uniqueSymbol = uniqueSymbol;
} //}
AppSearch::~AppSearch() //AppSearch::~AppSearch()
{ //{
} //}
void AppSearch::run() //void AppSearch::run()
{ //{
m_appSearchTask->startSearch(SearchType::Application); //// m_appSearchTask->startSearch(SearchType::Application);
QTimer timer; // QTimer timer;
timer.setInterval(3000); // timer.setInterval(3000);
bool is_empty; // bool is_empty;
while(1) { // while(1) {
is_empty = false; // is_empty = false;
if(!m_appSearchResults->isEmpty()) { // if(!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();
if (isUniqueSymbolChanged()) {
m_appSearchResults->clear();
break;
}
m_search_result->enqueue(ri);
} else {
is_empty = true;
}
if (isUniqueSymbolChanged()) {
break;
}
if(timer.isActive() && timer.remainingTime() < 0.01) {
qWarning()<<"-------------->stopped by itself";
break;
}
if(is_empty && !timer.isActive()) {
timer.start();
} else if(!is_empty) {
timer.stop();
} else {
QThread::msleep(100);
}
}
}
bool AppSearch::isUniqueSymbolChanged() // ResultItem oneResult = m_appSearchResults->dequeue();
{
QMutexLocker locker(&AppSearchPlugin::m_mutex); // SearchPluginIface::ResultInfo ri;
if (m_uniqueSymbol != AppSearchPlugin::uniqueSymbol) { // ri.actionKey = oneResult.getExtral().at(0).toString();
qDebug() << "uniqueSymbol changged, app search finished!"; // ri.name = oneResult.getExtral().at(1).toString();
return true; // ri.icon = oneResult.getExtral().at(2).value<QIcon>();
} else { // SearchPluginIface::DescriptionInfo description;
return false; // description.key = QString(tr("Application Description:"));
} // description.value = oneResult.getExtral().at(3).toString();
} // ri.description.append(description);
// ri.type = oneResult.getExtral().at(4).toInt();
//// if (isUniqueSymbolChanged()) {
//// m_appSearchResults->clear();
//// break;
//// }
// m_search_result->enqueue(ri);
// } else {
// is_empty = true;
// }
// if (isUniqueSymbolChanged()) {
// break;
// }
// if(timer.isActive() && timer.remainingTime() < 0.01) {
// qWarning()<<"-------------->stopped by itself";
// break;
// }
// if(is_empty && !timer.isActive()) {
// timer.start();
// } else if(!is_empty) {
// timer.stop();
// } else {
// QThread::msleep(100);
// }
// }
//}
//bool AppSearch::isUniqueSymbolChanged()
//{
// QMutexLocker locker(&AppSearchPlugin::m_mutex);
// if (m_uniqueSymbol != AppSearchPlugin::uniqueSymbol) {
// qDebug() << "uniqueSymbol changged, app search finished!";
// return true;
// } else {
// return false;
// }
//}

View File

@ -17,7 +17,7 @@
#include "libsearch_global.h" #include "libsearch_global.h"
#include "ukui-search-task.h" #include "ukui-search-task.h"
namespace UkuiSearch { namespace UkuiSearch {
class LIBSEARCH_EXPORT AppSearchPlugin : public QObject, public SearchPluginIface class LIBSEARCH_EXPORT AppSearchPlugin : public QThread, public SearchPluginIface
{ {
friend class AppSearch; friend class AppSearch;
friend class AppMatch; friend class AppMatch;
@ -39,6 +39,7 @@ public:
// bool isPreviewEnable(QString key, int type); // bool isPreviewEnable(QString key, int type);
// QWidget *previewPage(QString key, int type, QWidget *parent); // QWidget *previewPage(QString key, int type, QWidget *parent);
QWidget *detailPage(const ResultInfo &ri); QWidget *detailPage(const ResultInfo &ri);
void run() override;
private: private:
void initDetailPage(); void initDetailPage();
bool launch(const QString &path); bool launch(const QString &path);
@ -48,12 +49,14 @@ private:
bool m_enable = true; bool m_enable = true;
QList<SearchPluginIface::Actioninfo> m_actionInfo_installed; QList<SearchPluginIface::Actioninfo> m_actionInfo_installed;
QList<SearchPluginIface::Actioninfo> m_actionInfo_not_installed; QList<SearchPluginIface::Actioninfo> m_actionInfo_not_installed;
QThreadPool m_pool; // QThreadPool m_pool;
QTimer *m_timer;
static size_t uniqueSymbol; static size_t uniqueSymbol;
static QMutex m_mutex; static QMutex m_mutex;
UkuiSearchTask *m_appSearchTask = nullptr; UkuiSearchTask *m_appSearchTask = nullptr;
DataQueue<ResultItem>* m_appSearchResults = nullptr; DataQueue<ResultItem>* m_appSearchResults = nullptr;
DataQueue<SearchPluginIface::ResultInfo> *m_searchResult = nullptr;
QString m_currentActionKey; QString m_currentActionKey;
QWidget *m_detailPage; QWidget *m_detailPage;
@ -76,25 +79,28 @@ private:
ActionLabel *m_actionLabel4 = nullptr; ActionLabel *m_actionLabel4 = nullptr;
QVBoxLayout * m_actionLyt = nullptr; QVBoxLayout * m_actionLyt = nullptr;
Q_SIGNALS:
void startTimer();
void stopTimer();
}; };
class AppSearch : public QObject, public QRunnable { //class AppSearch : public QObject, public QRunnable {
Q_OBJECT // Q_OBJECT
public: //public:
AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol); // AppSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, DataQueue<ResultItem>* appSearchResults, UkuiSearchTask *appSearchTask, QString keyword, size_t uniqueSymbol);
~AppSearch(); // ~AppSearch();
protected: //protected:
void run() override; // void run() override;
private: //private:
bool isUniqueSymbolChanged(); // bool isUniqueSymbolChanged();
size_t m_uniqueSymbol; // size_t m_uniqueSymbol;
UkuiSearchTask *m_appSearchTask = nullptr; // UkuiSearchTask *m_appSearchTask = nullptr;
DataQueue<ResultItem>* m_appSearchResults = nullptr; // DataQueue<ResultItem>* m_appSearchResults = nullptr;
DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr; // DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr;
}; //};
} }
#endif // APPSEARCHPLUGIN_H #endif // APPSEARCHPLUGIN_H

View File

@ -86,6 +86,7 @@ size_t UkuiSearchTaskPrivate::startSearch(SearchType searchtype, const QString&
qWarning() << "the date queue has not been initialized, you need run init first!"; qWarning() << "the date queue has not been initialized, you need run init first!";
} }
m_searchCotroller->refreshDataqueue();
//plugin manager do async search here //plugin manager do async search here
if (!SearchTaskPluginManager::getInstance()->startSearch(m_uuid, m_searchCotroller, searchtype, customSearchType)) { if (!SearchTaskPluginManager::getInstance()->startSearch(m_uuid, m_searchCotroller, searchtype, customSearchType)) {
Q_EMIT searchError(m_searchCotroller->getCurrentSearchId(), tr("Current task uuid error or an unregistered plugin is used!")); Q_EMIT searchError(m_searchCotroller->getCurrentSearchId(), tr("Current task uuid error or an unregistered plugin is used!"));