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"
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
m_plugin_id = plugin_id;
|
2021-05-28 10:16:28 +08:00
|
|
|
|
m_result_queue = new DataQueue<SearchPluginIface::ResultInfo>;
|
2021-07-21 17:01:34 +08:00
|
|
|
|
m_get_result_thread = new ReceiveResultThread(m_result_queue, this);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
initConnections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SearchResultManager::startSearch(const QString &keyword)
|
|
|
|
|
{
|
2022-02-12 14:20:55 +08:00
|
|
|
|
qDebug()<<m_plugin_id<<"started";
|
2021-05-25 19:42:40 +08:00
|
|
|
|
if(! m_get_result_thread->isRunning()) {
|
|
|
|
|
m_get_result_thread->start();
|
|
|
|
|
}
|
2021-05-22 21:29:43 +08:00
|
|
|
|
m_result_queue->clear();
|
|
|
|
|
SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_plugin_id);
|
2021-05-28 10:16:28 +08:00
|
|
|
|
plugin->KeywordSearch(keyword, m_result_queue);
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief SearchResultManager::stopSearch 停止搜索,开始一次新搜索前或主界面退出时执行
|
|
|
|
|
*/
|
|
|
|
|
void SearchResultManager::stopSearch()
|
|
|
|
|
{
|
|
|
|
|
if(m_get_result_thread->isRunning()) {
|
2022-02-12 14:20:55 +08:00
|
|
|
|
qDebug()<<m_plugin_id<<"stopped";
|
2021-05-25 19:42:40 +08:00
|
|
|
|
m_get_result_thread->stop();
|
2022-02-12 14:20:55 +08:00
|
|
|
|
SearchPluginIface *plugin = SearchPluginManager::getInstance()->getPlugin(m_plugin_id);
|
|
|
|
|
plugin->stopSearch();
|
2021-05-25 19:42:40 +08:00
|
|
|
|
// m_get_result_thread->quit();
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SearchResultManager::initConnections()
|
|
|
|
|
{
|
|
|
|
|
connect(m_get_result_thread, &ReceiveResultThread::gotResultInfo, this, &SearchResultManager::gotResultInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 10:16:28 +08:00
|
|
|
|
ReceiveResultThread::ReceiveResultThread(DataQueue<SearchPluginIface::ResultInfo> * result_queue, QObject *parent)
|
2021-05-22 21:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
m_result_queue = result_queue;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
this->quit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 21:29:43 +08:00
|
|
|
|
void ReceiveResultThread::run()
|
|
|
|
|
{
|
|
|
|
|
QTimer * m_timer = new QTimer;
|
|
|
|
|
m_timer->setInterval(3000);
|
|
|
|
|
bool is_empty;
|
|
|
|
|
while(!isInterruptionRequested()) {
|
|
|
|
|
is_empty = false;
|
|
|
|
|
if(!m_result_queue->isEmpty()) {
|
|
|
|
|
Q_EMIT this->gotResultInfo(m_result_queue->dequeue());
|
2021-09-27 17:25:06 +08:00
|
|
|
|
|
2021-05-22 21:29:43 +08:00
|
|
|
|
} else {
|
|
|
|
|
is_empty = true;
|
|
|
|
|
}
|
|
|
|
|
if(m_timer->isActive() && m_timer->remainingTime() < 0.01) {
|
|
|
|
|
this->requestInterruption();
|
2022-02-12 14:20:55 +08:00
|
|
|
|
qWarning()<<"-------------->stopped by itself";
|
2021-05-22 21:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
if(is_empty && !m_timer->isActive()) {
|
|
|
|
|
m_timer->start();
|
|
|
|
|
} else if(!is_empty) {
|
|
|
|
|
m_timer->stop();
|
|
|
|
|
} else {
|
|
|
|
|
msleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|