2021-05-22 21:29:43 +08:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2020, 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: zhangjiaping <zhangjiaping@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "search-result-manager.h"
|
2023-05-06 15:41:56 +08:00
|
|
|
|
#include <QDeadlineTimer>
|
2021-05-22 21:29:43 +08:00
|
|
|
|
|
2021-12-14 14:43:35 +08:00
|
|
|
|
using namespace UkuiSearch;
|
2021-05-22 21:29:43 +08:00
|
|
|
|
SearchResultManager::SearchResultManager(const QString& plugin_id, QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
2023-01-21 11:37:39 +08:00
|
|
|
|
m_pluginId = plugin_id;
|
|
|
|
|
m_resultQueue = new DataQueue<SearchPluginIface::ResultInfo>;
|
|
|
|
|
m_getResultThread = new ReceiveResultThread(m_resultQueue, this);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
initConnections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SearchResultManager::startSearch(const QString &keyword)
|
|
|
|
|
{
|
2023-01-21 11:37:39 +08:00
|
|
|
|
qDebug()<<m_pluginId<<"started";
|
|
|
|
|
if(! m_getResultThread->isRunning()) {
|
|
|
|
|
m_getResultThread->start();
|
2021-05-25 19:42:40 +08:00
|
|
|
|
}
|
2023-01-21 11:37:39 +08:00
|
|
|
|
m_resultQueue->clear();
|
|
|
|
|
SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_pluginId);
|
|
|
|
|
plugin->KeywordSearch(keyword, m_resultQueue);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief SearchResultManager::stopSearch 停止搜索,开始一次新搜索前或主界面退出时执行
|
|
|
|
|
*/
|
|
|
|
|
void SearchResultManager::stopSearch()
|
|
|
|
|
{
|
2023-01-21 11:37:39 +08:00
|
|
|
|
if(m_getResultThread->isRunning()) {
|
|
|
|
|
m_getResultThread->stop();
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
2023-06-14 14:08:55 +08:00
|
|
|
|
SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_pluginId);
|
|
|
|
|
plugin->stopSearch();
|
|
|
|
|
qDebug() << m_pluginId << "stopped";
|
2023-09-01 11:07:57 +08:00
|
|
|
|
m_resultQueue->clear();
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SearchResultManager::initConnections()
|
|
|
|
|
{
|
2023-01-21 11:37:39 +08:00
|
|
|
|
connect(m_getResultThread, &ReceiveResultThread::gotResultInfo, this, &SearchResultManager::gotResultInfo);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 11:37:39 +08:00
|
|
|
|
ReceiveResultThread::ReceiveResultThread(DataQueue<SearchPluginIface::ResultInfo> * resultQueue, QObject *parent): QThread(parent)
|
2021-05-22 21:29:43 +08:00
|
|
|
|
{
|
2023-01-21 11:37:39 +08:00
|
|
|
|
m_resultQueue = resultQueue;
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 19:42:40 +08:00
|
|
|
|
void ReceiveResultThread::stop()
|
|
|
|
|
{
|
|
|
|
|
this->requestInterruption();
|
2022-08-24 10:34:00 +08:00
|
|
|
|
this->wait();
|
2021-05-25 19:42:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 21:29:43 +08:00
|
|
|
|
void ReceiveResultThread::run()
|
|
|
|
|
{
|
2023-07-20 16:42:29 +08:00
|
|
|
|
QDeadlineTimer deadline(25000);
|
2023-01-21 11:37:39 +08:00
|
|
|
|
while(!isInterruptionRequested()) {
|
|
|
|
|
SearchPluginIface::ResultInfo oneResult = m_resultQueue->tryDequeue();
|
|
|
|
|
if(oneResult.name.isEmpty()) {
|
2023-05-06 15:41:56 +08:00
|
|
|
|
if(deadline.remainingTime()) {
|
|
|
|
|
msleep(100);
|
|
|
|
|
} else {
|
|
|
|
|
this->requestInterruption();
|
2023-01-21 11:37:39 +08:00
|
|
|
|
}
|
2021-05-22 21:29:43 +08:00
|
|
|
|
} else {
|
2023-07-20 16:42:29 +08:00
|
|
|
|
deadline.setRemainingTime(25000);
|
2023-01-21 11:37:39 +08:00
|
|
|
|
Q_EMIT gotResultInfo(oneResult);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-01 11:07:57 +08:00
|
|
|
|
m_resultQueue->clear();
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|