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>
|
|
|
|
*/
|
2022-06-13 13:38:47 +08:00
|
|
|
#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);
|
2023-05-06 15:41:56 +08:00
|
|
|
m_pool->setMaxThreadCount(3);
|
2023-03-15 16:39:38 +08:00
|
|
|
qDBusRegisterMetaType<QMap<QString, QString>>();
|
|
|
|
qDBusRegisterMetaType<QList<QMap<QString, QString>>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
AppSearchTask::~AppSearchTask()
|
|
|
|
{
|
|
|
|
m_pool->clear();
|
|
|
|
m_pool->waitForDone();
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 15:27:23 +08:00
|
|
|
void AppSearchTask::setController(const SearchController &searchController)
|
2023-05-06 15:41:56 +08:00
|
|
|
{
|
|
|
|
m_searchController = searchController;
|
|
|
|
}
|
|
|
|
|
2022-06-13 13:38:47 +08:00
|
|
|
const QString AppSearchTask::name()
|
|
|
|
{
|
|
|
|
return tr("Application");
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString AppSearchTask::description()
|
|
|
|
{
|
|
|
|
return tr("Application search.");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AppSearchTask::getCustomSearchType()
|
|
|
|
{
|
|
|
|
return "Application";
|
|
|
|
}
|
|
|
|
|
2023-05-06 15:41:56 +08:00
|
|
|
void AppSearchTask::startSearch()
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
2023-05-06 15:41:56 +08:00
|
|
|
AppSearchWorker *appSearchWorker = new AppSearchWorker(this);
|
2022-06-13 13:38:47 +08:00
|
|
|
m_pool->start(appSearchWorker);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppSearchTask::stop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-19 15:15:28 +08:00
|
|
|
bool AppSearchTask::isSearching()
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
2023-05-19 15:15:28 +08:00
|
|
|
return m_pool->activeThreadCount() > 0;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
AppSearchWorker::AppSearchWorker(AppSearchTask *AppSarchTask): m_appSearchTask(AppSarchTask)
|
2022-06-13 13:38:47 +08:00
|
|
|
{
|
2023-07-06 15:27:23 +08:00
|
|
|
m_controller = &m_appSearchTask->m_searchController;
|
2023-05-06 15:41:56 +08:00
|
|
|
m_currentSearchId = m_controller->getCurrentSearchId();
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppSearchWorker::run()
|
|
|
|
{
|
2023-03-15 16:39:38 +08:00
|
|
|
ApplicationProperties applicationProperties;
|
2023-05-06 15:41:56 +08:00
|
|
|
SearchResultProperties properties = m_controller->getResultProperties(SearchProperty::SearchType::Application);
|
2023-03-15 16:39:38 +08:00
|
|
|
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);
|
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
ApplicationInfoMap data = m_appSearchTask->m_appinfo.searchApp(applicationProperties, m_controller->getKeyword(), ApplicationPropertyMap{{ApplicationProperty::DontDisplay, 0}, {ApplicationProperty::AutoStart, 0}});
|
2023-03-15 16:39:38 +08:00
|
|
|
for (const QString &desktop : data.keys()) {
|
2023-05-06 15:41:56 +08:00
|
|
|
if (m_controller->beginSearchIdCheck(m_currentSearchId)) {
|
2023-03-15 16:39:38 +08:00
|
|
|
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());
|
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
m_controller->getDataQueue()->enqueue(item);
|
2023-05-19 15:15:28 +08:00
|
|
|
if(++m_resultNum == m_controller->informNum()) {
|
|
|
|
QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum");
|
|
|
|
m_resultNum = 0;
|
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
m_controller->finishSearchIdCheck();
|
2022-06-13 13:38:47 +08:00
|
|
|
} else {
|
|
|
|
qDebug() << "Search id changed!";
|
2023-05-06 15:41:56 +08:00
|
|
|
m_controller->finishSearchIdCheck();
|
2023-01-16 15:56:58 +08:00
|
|
|
return;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
if (m_controller->isSearchOnlineApps()) {
|
2022-06-13 13:38:47 +08:00
|
|
|
//online app search
|
2023-05-06 15:41:56 +08:00
|
|
|
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);
|
2022-06-13 13:38:47 +08:00
|
|
|
if(reply.isValid()) {
|
2023-05-06 15:41:56 +08:00
|
|
|
// qDebug() << reply.value();
|
2022-06-13 13:38:47 +08:00
|
|
|
for(int i = 0; i < reply.value().size(); i++) {
|
2023-05-06 15:41:56 +08:00
|
|
|
if (m_controller->beginSearchIdCheck(m_currentSearchId)) {
|
2023-03-15 16:39:38 +08:00
|
|
|
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"));
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
2023-03-15 16:39:38 +08:00
|
|
|
|
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::ApplicationLocalName)) {
|
|
|
|
QString localName;
|
|
|
|
if(QLocale::system().language() == QLocale::Chinese) {
|
|
|
|
localName = reply.value().at(i).value("displayname_cn");
|
2022-06-13 13:38:47 +08:00
|
|
|
} else {
|
2023-03-15 16:39:38 +08:00
|
|
|
localName = reply.value().at(i).value("appname");
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
2023-03-15 16:39:38 +08:00
|
|
|
item.setValue(SearchProperty::SearchResultProperty::ApplicationLocalName, localName);
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
2023-03-15 16:39:38 +08:00
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::ApplicationIconName)) {
|
|
|
|
item.setValue(SearchProperty::SearchResultProperty::ApplicationIconName, reply.value().at(i).value("icon"));
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
2023-03-15 16:39:38 +08:00
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::ApplicationDescription)) {
|
|
|
|
item.setValue(SearchProperty::SearchResultProperty::ApplicationDescription, reply.value().at(i).value("discription"));
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
2023-03-15 16:39:38 +08:00
|
|
|
if(properties.contains(SearchProperty::SearchResultProperty::IsOnlineApplication)) {
|
|
|
|
item.setValue(SearchProperty::SearchResultProperty::IsOnlineApplication, 1);
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
m_controller->getDataQueue()->enqueue(item);
|
2023-05-19 15:15:28 +08:00
|
|
|
if(++m_resultNum == m_controller->informNum()) {
|
|
|
|
QMetaObject::invokeMethod(m_appSearchTask, "reachInformNum");
|
|
|
|
m_resultNum = 0;
|
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
m_controller->finishSearchIdCheck();
|
2022-06-13 13:38:47 +08:00
|
|
|
} else {
|
|
|
|
qDebug() << "Search id changed!";
|
2023-05-06 15:41:56 +08:00
|
|
|
m_controller->finishSearchIdCheck();
|
2023-01-16 15:56:58 +08:00
|
|
|
return;
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-21 15:16:01 +08:00
|
|
|
qWarning() << "SoftWareCenter dbus called failed!" << reply.error();
|
2023-01-16 15:56:58 +08:00
|
|
|
sendErrorMsg(QString("SoftWareCenter dbus called failed!") + reply.error().message());
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
QMetaObject::invokeMethod(m_appSearchTask, "searchFinished", Q_ARG(size_t, m_currentSearchId));
|
2022-06-13 13:38:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppSearchWorker::sendErrorMsg(const QString &msg)
|
|
|
|
{
|
2023-03-15 16:39:38 +08:00
|
|
|
QMetaObject::invokeMethod(m_appSearchTask, "searchError",
|
2022-06-13 13:38:47 +08:00
|
|
|
Q_ARG(size_t, m_currentSearchId),
|
|
|
|
Q_ARG(QString, msg));
|
|
|
|
}
|