应用搜索调用应用商店接口接口改为异步调用

This commit is contained in:
iaom 2023-07-14 17:22:48 +08:00 committed by 纪笑旭
parent 2619025065
commit 3d128c42b7
3 changed files with 76 additions and 47 deletions

View File

@ -60,4 +60,5 @@ enum SearchResultProperty {
typedef QVector<SearchProperty::SearchResultProperty> SearchResultProperties;
typedef QMap<SearchProperty::SearchResultProperty, QVariant> SearchResultPropertyMap;
}
Q_DECLARE_METATYPE(UkuiSearch::SearchResultProperties);
#endif // SEARCHRESULTPROPERTY_H

View File

@ -32,6 +32,7 @@ AppSearchTask::AppSearchTask(QObject *parent)
qRegisterMetaType<size_t>("size_t");
m_pool = new QThreadPool(this);
m_pool->setMaxThreadCount(3);
m_dbusCallBackThread = new QThread(this);
qDBusRegisterMetaType<QMap<QString, QString>>();
qDBusRegisterMetaType<QList<QMap<QString, QString>>>();
}
@ -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<QList<QMap<QString, QString>>> reply = *call;
size_t searchId = call->property("id").value<size_t>();
if(reply.isValid()) {
SearchResultProperties properties = call->property("prop").value<SearchResultProperties>();
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<QList<QMap<QString, QString>>> 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;
}
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 {
qWarning() << "SoftWareCenter dbus called failed!" << reply.error();
sendErrorMsg(QString("SoftWareCenter dbus called failed!") + reply.error().message());
}
}
}
QMetaObject::invokeMethod(m_appSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId));
}
}
void AppSearchWorker::sendErrorMsg(const QString &msg)

View File

@ -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