diff --git a/libsearch/searchinterface/search-result-property.h b/libsearch/searchinterface/search-result-property.h index b157b89..6402210 100644 --- a/libsearch/searchinterface/search-result-property.h +++ b/libsearch/searchinterface/search-result-property.h @@ -60,4 +60,5 @@ enum SearchResultProperty { typedef QVector SearchResultProperties; typedef QMap SearchResultPropertyMap; } +Q_DECLARE_METATYPE(UkuiSearch::SearchResultProperties); #endif // SEARCHRESULTPROPERTY_H diff --git a/libsearch/searchinterface/searchtasks/app-search-task.cpp b/libsearch/searchinterface/searchtasks/app-search-task.cpp index f75d444..91cfe64 100644 --- a/libsearch/searchinterface/searchtasks/app-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/app-search-task.cpp @@ -32,6 +32,7 @@ AppSearchTask::AppSearchTask(QObject *parent) qRegisterMetaType("size_t"); m_pool = new QThreadPool(this); m_pool->setMaxThreadCount(3); + m_dbusCallBackThread = new QThread(this); qDBusRegisterMetaType>(); qDBusRegisterMetaType>>(); } @@ -40,6 +41,8 @@ AppSearchTask::~AppSearchTask() { m_pool->clear(); m_pool->waitForDone(); + m_dbusCallBackThread->quit(); + m_dbusCallBackThread->wait(); } void AppSearchTask::setController(const SearchController &searchController) @@ -64,12 +67,18 @@ QString AppSearchTask::getCustomSearchType() void AppSearchTask::startSearch() { + if(!m_dbusCallBackThread->isRunning()) { + m_dbusCallBackThread->start(); + } AppSearchWorker *appSearchWorker = new AppSearchWorker(this); m_pool->start(appSearchWorker); } void AppSearchTask::stop() { + if(m_dbusCallBackThread->isRunning()) { + m_dbusCallBackThread->exit(); + } } bool AppSearchTask::isSearching() @@ -77,6 +86,59 @@ bool AppSearchTask::isSearching() return m_pool->activeThreadCount() > 0; } +void AppSearchTask::dbusCallBack(QDBusPendingCallWatcher *call) +{ + QDBusPendingReply>> reply = *call; + size_t searchId = call->property("id").value(); + if(reply.isValid()) { + SearchResultProperties properties = call->property("prop").value(); + int resultNum = call->property("num").toInt(); + for(int i = 0; i < reply.value().size(); i++) { + if (m_searchController.beginSearchIdCheck(searchId)) { + ResultItem item(searchId); + item.setItemKey(reply.value().at(i).value("appname")); + if(properties.contains(SearchProperty::SearchResultProperty::ApplicationPkgName)) { + item.setValue(SearchProperty::SearchResultProperty::ApplicationPkgName, reply.value().at(i).value("appname")); + } + + if(properties.contains(SearchProperty::SearchResultProperty::ApplicationLocalName)) { + QString localName; + if(QLocale::system().language() == QLocale::Chinese) { + localName = reply.value().at(i).value("displayname_cn"); + } else { + localName = reply.value().at(i).value("appname"); + } + item.setValue(SearchProperty::SearchResultProperty::ApplicationLocalName, localName); + } + if(properties.contains(SearchProperty::SearchResultProperty::ApplicationIconName)) { + item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, reply.value().at(i).value("icon")); + } + if(properties.contains(SearchProperty::SearchResultProperty::ApplicationDescription)) { + item.setValue(SearchProperty::SearchResultProperty::ApplicationDescription, reply.value().at(i).value("discription")); + } + if(properties.contains(SearchProperty::SearchResultProperty::IsOnlineApplication)) { + item.setValue(SearchProperty::SearchResultProperty::IsOnlineApplication, 1); + } + m_searchController.getDataQueue()->enqueue(item); + if(++resultNum == m_searchController.informNum()) { + Q_EMIT reachInformNum(); + resultNum = 0; + } + m_searchController.finishSearchIdCheck(); + } else { + qDebug() << "Search id changed!"; + m_searchController.finishSearchIdCheck(); + return; + } + } + Q_EMIT searchFinished(searchId); + } else { + qWarning() << "SoftWareCenter dbus called failed!" << reply.error(); + Q_EMIT searchError(searchId, "SoftWareCenter dbus called failed!"); + } + call->deleteLater(); +} + AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask) { m_controller = &m_appSearchTask->m_searchController; @@ -132,55 +194,17 @@ void AppSearchWorker::run() QStringLiteral("com.kylin.getsearchresults"), QStringLiteral("get_search_result")); msg.setArguments({keyword}); - QDBusReply>> reply = QDBusConnection::sessionBus().call(msg, QDBus::Block, 1500); - if(reply.isValid()) { -// qDebug() << reply.value(); - for(int i = 0; i < reply.value().size(); i++) { - if (m_controller->beginSearchIdCheck(m_currentSearchId)) { - ResultItem item(m_currentSearchId); - item.setItemKey(reply.value().at(i).value("appname")); - if(properties.contains(SearchProperty::SearchResultProperty::ApplicationPkgName)) { - item.setValue(SearchProperty::SearchResultProperty::ApplicationPkgName, reply.value().at(i).value("appname")); - } - - if(properties.contains(SearchProperty::SearchResultProperty::ApplicationLocalName)) { - QString localName; - if(QLocale::system().language() == QLocale::Chinese) { - localName = reply.value().at(i).value("displayname_cn"); - } else { - localName = reply.value().at(i).value("appname"); - } - item.setValue(SearchProperty::SearchResultProperty::ApplicationLocalName, localName); - } - if(properties.contains(SearchProperty::SearchResultProperty::ApplicationIconName)) { - item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, reply.value().at(i).value("icon")); - } - if(properties.contains(SearchProperty::SearchResultProperty::ApplicationDescription)) { - item.setValue(SearchProperty::SearchResultProperty::ApplicationDescription, reply.value().at(i).value("discription")); - } - if(properties.contains(SearchProperty::SearchResultProperty::IsOnlineApplication)) { - item.setValue(SearchProperty::SearchResultProperty::IsOnlineApplication, 1); - } - m_controller->getDataQueue()->enqueue(item); - if(++m_resultNum == m_controller->informNum()) { - QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum"); - m_resultNum = 0; - } - m_controller->finishSearchIdCheck(); - } else { - qDebug() << "Search id changed!"; - m_controller->finishSearchIdCheck(); - return; - } - } - } else { - qWarning() << "SoftWareCenter dbus called failed!" << reply.error(); - sendErrorMsg(QString("SoftWareCenter dbus called failed!") + reply.error().message()); - } + QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(msg); + auto *watcher = new QDBusPendingCallWatcher(call); + watcher->setProperty("id", QVariant::fromValue(m_currentSearchId)); + watcher->setProperty("prop", QVariant::fromValue(properties)); + watcher->setProperty("num", m_resultNum); + watcher->moveToThread(m_appSearchTask->m_dbusCallBackThread); + QObject::connect(watcher, &QDBusPendingCallWatcher::finished, m_appSearchTask, &AppSearchTask::dbusCallBack, Qt::DirectConnection); } + } else { + QMetaObject::invokeMethod(m_appSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId)); } - - QMetaObject::invokeMethod(m_appSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId)); } void AppSearchWorker::sendErrorMsg(const QString &msg) diff --git a/libsearch/searchinterface/searchtasks/app-search-task.h b/libsearch/searchinterface/searchtasks/app-search-task.h index e2b9461..c7d98b7 100644 --- a/libsearch/searchinterface/searchtasks/app-search-task.h +++ b/libsearch/searchinterface/searchtasks/app-search-task.h @@ -53,10 +53,14 @@ public: void stop(); bool isSearching(); +private Q_SLOTS: + void dbusCallBack(QDBusPendingCallWatcher *call); + private: ApplicationInfo m_appinfo; SearchController m_searchController; QThreadPool *m_pool = nullptr; + QThread *m_dbusCallBackThread = nullptr; }; class AppSearchWorker : public QRunnable