Optimezed function of app search.

This commit is contained in:
iaom 2021-05-30 11:28:04 +08:00
parent 823130fa8b
commit f36c3b0bf7
3 changed files with 30 additions and 19 deletions

View File

@ -46,6 +46,7 @@ AppMatch::AppMatch(QObject *parent) : QThread(parent)
if(!m_interFace->isValid()) { if(!m_interFace->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
} }
m_interFace->setTimeout(200);
qDebug() << "AppMatch is new"; qDebug() << "AppMatch is new";
} }
@ -277,10 +278,10 @@ void AppMatch::appNameMatch(QString appname, QMap<NameString, QStringList> &inst
} }
void AppMatch::softWareCenterSearch(QMap<NameString, QStringList> &softwarereturn) { void AppMatch::softWareCenterSearch(QMap<NameString, QStringList> &softwarereturn) {
if(m_interFace->timeout() != -1) { // if(m_interFace->timeout() != -1) {
qWarning() << "softWareCente Dbus is timeout !"; // qWarning() << "softWareCente Dbus is timeout !";
return; // return;
} // }
slotDBusCallFinished(softwarereturn); slotDBusCallFinished(softwarereturn);
qDebug() << "softWareCenter match app is successful!"; qDebug() << "softWareCenter match app is successful!";
} }

View File

@ -5,21 +5,32 @@ size_t uniqueSymbol = 0;
QMutex m_mutex; QMutex m_mutex;
SearchAppThread::SearchAppThread(QObject *parent) : QObject(parent) { SearchAppThread::SearchAppThread(QObject *parent) : QObject(parent) {
m_pool.setMaxThreadCount(1); m_pool.setMaxThreadCount(2);
m_pool.setExpiryTimeout(1000); m_pool.setExpiryTimeout(1000);
} }
void SearchAppThread::startSearch(const QString & keyword) { void SearchAppThread::startSearch(const QString & keyword) {
m_mutex.lock();
++uniqueSymbol;
m_mutex.unlock();
SearchApp *appsearch; SearchApp *appsearch;
appsearch = new SearchApp(keyword); appsearch = new SearchApp(keyword,uniqueSymbol,this);
// appsearch->setKeyword(keyword); // appsearch->setKeyword(keyword);
connect(appsearch, &SearchApp::searchResultApp, this, &SearchAppThread::searchResultApp); // connect(appsearch, &SearchApp::searchResultApp, this, &SearchAppThread::searchResultApp);
m_pool.start(appsearch); m_pool.start(appsearch);
} }
void SearchAppThread::sendResult(const QVector<QStringList> result)
{
Q_EMIT this->searchResultApp(result);
}
SearchApp::SearchApp(const QString& keyword, QObject * parent) : QObject(parent) {
SearchApp::SearchApp(const QString& keyword, size_t uniqueSymbol, QObject * parent) : QObject(parent) {
this->setAutoDelete(true);
m_searchappThread = qobject_cast<SearchAppThread *>(parent);
m_keyword = keyword; m_keyword = keyword;
m_uniqueSymbol = uniqueSymbol;
} }
SearchApp::~SearchApp() { SearchApp::~SearchApp() {
@ -35,11 +46,6 @@ SearchApp::~SearchApp() {
//} //}
void SearchApp::run() { void SearchApp::run() {
m_mutex.lock();
size_t tmp_uniqueSymbol;
uniqueSymbol++;
tmp_uniqueSymbol = uniqueSymbol;
m_mutex.unlock();
//nameList:应用名pathList:已安装的是.desktop路径未安装为空iconList:已安装的是图标名,未安装的是图标路径 //nameList:应用名pathList:已安装的是.desktop路径未安装为空iconList:已安装的是图标名,未安装的是图标路径
QStringList nameList, pathList, iconList, descList; QStringList nameList, pathList, iconList, descList;
QVector<QStringList> appVector; QVector<QStringList> appVector;
@ -70,8 +76,9 @@ void SearchApp::run() {
appVector.append(iconList); appVector.append(iconList);
appVector.append(descList); appVector.append(descList);
m_mutex.lock(); m_mutex.lock();
if (tmp_uniqueSymbol == uniqueSymbol) { if (m_uniqueSymbol == uniqueSymbol) {
Q_EMIT this->searchResultApp(appVector); QMetaObject::invokeMethod(m_searchappThread, "sendResult", Q_ARG(const QVector<QStringList>, appVector));
// Q_EMIT this->searchResultApp(appVector);
} }
m_mutex.unlock(); m_mutex.unlock();
m_installed_apps.clear(); m_installed_apps.clear();

View File

@ -13,27 +13,30 @@ public:
SearchAppThread(QObject * parent = nullptr); SearchAppThread(QObject * parent = nullptr);
~SearchAppThread() = default; ~SearchAppThread() = default;
void startSearch(const QString&); void startSearch(const QString&);
Q_INVOKABLE void sendResult(const QVector<QStringList> result);
private: private:
QThreadPool m_pool; QThreadPool m_pool;
Q_SIGNALS: Q_SIGNALS:
void searchResultApp(const QVector<QStringList>&); void searchResultApp(const QVector<QStringList>);
}; };
class SearchApp : public QObject, public QRunnable { class SearchApp : public QObject, public QRunnable {
Q_OBJECT Q_OBJECT
public: public:
SearchApp(const QString& keyword, QObject * parent = nullptr); SearchApp(const QString& keyword, size_t uniqueSymbol, QObject * parent = nullptr);
~SearchApp(); ~SearchApp();
// void setKeyword(const QString&); // void setKeyword(const QString&);
protected: protected:
void run() override; void run() override;
private: private:
SearchAppThread *m_searchappThread = nullptr;
QString m_keyword; QString m_keyword;
size_t m_uniqueSymbol;
QMap<NameString, QStringList> m_installed_apps; QMap<NameString, QStringList> m_installed_apps;
QMap<NameString, QStringList> m_uninstalled_apps; QMap<NameString, QStringList> m_uninstalled_apps;
Q_SIGNALS: //Q_SIGNALS:
void searchResultApp(const QVector<QStringList>&); // void searchResultApp(const QVector<QStringList>&);
}; };
} }