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

192 lines
8.3 KiB
C++
Raw Normal View History

2023-04-11 10:19:35 +08:00
/*
*
* 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(3);
qDBusRegisterMetaType<QMap<QString, QString>>();
qDBusRegisterMetaType<QList<QMap<QString, QString>>>();
}
AppSearchTask::~AppSearchTask()
{
m_pool->clear();
m_pool->waitForDone();
}
void AppSearchTask::setController(const SearchController &searchController)
{
m_searchController = searchController;
}
const QString AppSearchTask::name()
{
return tr("Application");
}
const QString AppSearchTask::description()
{
return tr("Application search.");
}
QString AppSearchTask::getCustomSearchType()
{
return "Application";
}
void AppSearchTask::startSearch()
{
AppSearchWorker *appSearchWorker = new AppSearchWorker(this);
m_pool->start(appSearchWorker);
}
void AppSearchTask::stop()
{
}
bool AppSearchTask::isSearching()
{
return m_pool->activeThreadCount() > 0;
}
AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask)
{
m_controller = &m_appSearchTask->m_searchController;
m_currentSearchId = m_controller->getCurrentSearchId();
}
void AppSearchWorker::run()
{
ApplicationProperties applicationProperties;
SearchResultProperties properties = m_controller->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_controller->getKeyword(), ApplicationPropertyMap{{ApplicationProperty::DontDisplay, 0}, {ApplicationProperty::AutoStart, 0}});
for (const QString &desktop : data.keys()) {
if (m_controller->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_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;
}
}
if (m_controller->isSearchOnlineApps()) {
//online app search
for (auto keyword : m_controller->getKeyword()) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.kylin.softwarecenter.getsearchresults"),
QStringLiteral("/com/kylin/softwarecenter/getsearchresults"),
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;
}
}
} 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)
{
QMetaObject::invokeMethod(m_appSearchTask, "searchError",
Q_ARG(size_t, m_currentSearchId),
Q_ARG(QString, msg));
}