ukui-search/libsearch/searchinterface/searchtasks/app-search-task.cpp

189 lines
8.1 KiB
C++

/*
*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Authors: iaom <zhangpengfei@kylinos.cn>
*/
#include "app-search-task.h"
#include "index-status-recorder.h"
#include "common.h"
#include <QDir>
#include <QFile>
#include <QQueue>
#include <QDebug>
using namespace UkuiSearch;
AppSearchTask::AppSearchTask(QObject *parent)
{
this->setParent(parent);
qRegisterMetaType<size_t>("size_t");
m_pool = new QThreadPool(this);
m_pool->setMaxThreadCount(1);
qDBusRegisterMetaType<QMap<QString, QString>>();
qDBusRegisterMetaType<QList<QMap<QString, QString>>>();
}
AppSearchTask::~AppSearchTask()
{
m_pool->clear();
m_pool->waitForDone();
}
const QString AppSearchTask::name()
{
return tr("Application");
}
const QString AppSearchTask::description()
{
return tr("Application search.");
}
QString AppSearchTask::getCustomSearchType()
{
return "Application";
}
void AppSearchTask::startSearch(std::shared_ptr<SearchController> searchController)
{
m_searchController = searchController;
AppSearchWorker *appSearchWorker;
appSearchWorker = new AppSearchWorker(this);
m_pool->start(appSearchWorker);
}
void AppSearchTask::stop()
{
}
void AppSearchTask::sendFinishSignal(size_t searchId)
{
Q_EMIT searchFinished(searchId);
}
AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask)
{
m_interFace = new QDBusInterface("com.kylin.softwarecenter.getsearchresults", "/com/kylin/softwarecenter/getsearchresults",
"com.kylin.getsearchresults",
QDBusConnection::sessionBus());
if(!m_interFace->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
m_interFace->setTimeout(1500);
m_currentSearchId = m_appSearchTask->m_searchController->getCurrentSearchId();
}
void AppSearchWorker::run()
{
ApplicationProperties applicationProperties;
SearchResultProperties properties = m_appSearchTask->m_searchController->getResultProperties(SearchProperty::SearchType::Application);
if(properties.contains(SearchProperty::SearchResultProperty::ApplicationDesktopPath)) {
applicationProperties.append(ApplicationProperty::DesktopFilePath);
}
if(properties.contains(SearchProperty::SearchResultProperty::ApplicationLocalName)) {
applicationProperties.append(ApplicationProperty::LocalName);
}
if(properties.contains(SearchProperty::SearchResultProperty::ApplicationIconName)) {
applicationProperties.append(ApplicationProperty::Icon);
}
ApplicationInfoMap data = m_appSearchTask->m_appinfo.searchApp(applicationProperties, m_appSearchTask->m_searchController->getKeyword(), ApplicationPropertyMap{{ApplicationProperty::DontDisplay, 0}, {ApplicationProperty::AutoStart, 0}});
for (const QString &desktop : data.keys()) {
if (m_appSearchTask->m_searchController->beginSearchIdCheck(m_currentSearchId)) {
ResultItem item(desktop);
item.setSearchId(m_currentSearchId);
ApplicationPropertyMap oneResult = data.value(desktop);
if(oneResult.contains(ApplicationProperty::DesktopFilePath)) {
item.setValue(SearchProperty::SearchResultProperty::ApplicationDesktopPath, oneResult.value(ApplicationProperty::DesktopFilePath).toString());
}
if(oneResult.contains(ApplicationProperty::LocalName)) {
item.setValue(SearchProperty::SearchResultProperty::ApplicationLocalName, oneResult.value(ApplicationProperty::LocalName).toString());
}
if(oneResult.contains(ApplicationProperty::Icon)) {
item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, oneResult.value(ApplicationProperty::Icon).toString());
}
m_appSearchTask->m_searchController->getDataQueue()->enqueue(item);
m_appSearchTask->m_searchController->finishSearchIdCheck();
} else {
qDebug() << "Search id changed!";
m_appSearchTask->m_searchController->finishSearchIdCheck();
return;
}
}
if (m_appSearchTask->m_searchController->isSearchOnlineApps()) {
//online app search
for (auto keyword : m_appSearchTask->m_searchController->getKeyword()) {
QDBusReply<QList<QMap<QString, QString>>> reply = m_interFace->call("get_search_result", keyword); //阻塞,直到远程方法调用完成。
if(reply.isValid()) {
for(int i = 0; i < reply.value().size(); i++) {
if (m_appSearchTask->m_searchController->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_appSearchTask->m_searchController->getDataQueue()->enqueue(item);
m_appSearchTask->m_searchController->finishSearchIdCheck();
} else {
qDebug() << "Search id changed!";
m_appSearchTask->m_searchController->finishSearchIdCheck();
return;
}
}
} 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));
}
AppSearchWorker::~AppSearchWorker()
{
if (m_interFace)
delete m_interFace;
m_interFace = nullptr;
}
void AppSearchWorker::sendErrorMsg(const QString &msg)
{
QMetaObject::invokeMethod(m_appSearchTask, "searchError",
Q_ARG(size_t, m_currentSearchId),
Q_ARG(QString, msg));
}