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-18 10:46:14 +08:00
|
|
|
#include "search-task-plugin-manager.h"
|
|
|
|
#include <QDebug>
|
2022-10-21 11:21:35 +08:00
|
|
|
#include <QPluginLoader>
|
2022-06-18 10:46:14 +08:00
|
|
|
|
|
|
|
#include "file-search-task.h"
|
|
|
|
#include "file-content-search-task.h"
|
2022-10-21 11:21:35 +08:00
|
|
|
#include "app-search-task.h"
|
2022-06-18 10:46:14 +08:00
|
|
|
|
|
|
|
using namespace UkuiSearch;
|
|
|
|
static SearchTaskPluginManager *global_instance = nullptr;
|
|
|
|
SearchTaskPluginManager *SearchTaskPluginManager::getInstance()
|
|
|
|
{
|
|
|
|
if (!global_instance) {
|
|
|
|
global_instance = new SearchTaskPluginManager;
|
|
|
|
}
|
|
|
|
return global_instance;
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
void SearchTaskPluginManager::initPlugins(SearchProperty::SearchType searchType)
|
2022-06-18 10:46:14 +08:00
|
|
|
{
|
|
|
|
switch (searchType) {
|
2023-03-15 16:39:38 +08:00
|
|
|
case SearchProperty::SearchType::File:
|
2022-06-18 10:46:14 +08:00
|
|
|
registerBuildinPlugin(new FileSearchTask(this));
|
|
|
|
break;
|
2023-03-15 16:39:38 +08:00
|
|
|
case SearchProperty::SearchType::FileContent:
|
2022-06-18 10:46:14 +08:00
|
|
|
registerBuildinPlugin(new FileContentSearchTask(this));
|
|
|
|
break;
|
2023-03-15 16:39:38 +08:00
|
|
|
case SearchProperty::SearchType::Application:
|
2022-10-21 11:21:35 +08:00
|
|
|
registerBuildinPlugin(new AppSearchTask(this));
|
2022-06-18 10:46:14 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchTaskPluginManager::registerPlugin(SearchTaskPluginIface *plugin)
|
|
|
|
{
|
|
|
|
//这里把内部插件和外部插件分开写是考虑到异常处理,因为内部插件我们可以保证质量,但外部插件可能有各种各样的问题。
|
|
|
|
//但异常处理我没想好怎么加。。
|
|
|
|
//by zpf
|
|
|
|
if(plugin->getCustomSearchType().isEmpty() || m_loadedPlugin.contains(plugin->getCustomSearchType())) {
|
|
|
|
qWarning() << "search task plugin load failed! plugin type:" << plugin->getCustomSearchType();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_loadedPlugin.insert(plugin->getCustomSearchType(), plugin);
|
|
|
|
connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished);
|
|
|
|
connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SearchTaskPluginManager::registerBuildinPlugin(SearchTaskPluginIface *plugin)
|
|
|
|
{
|
|
|
|
m_buildinPlugin.insert(static_cast<size_t>(plugin->getSearchType()), plugin);
|
|
|
|
connect(plugin, &SearchTaskPluginIface::searchFinished, this, &SearchTaskPluginManager::searchFinished);
|
|
|
|
connect(plugin, &SearchTaskPluginIface::searchError, this, &SearchTaskPluginManager::searchError);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
void SearchTaskPluginManager::pluginSearch(SearchProperty::SearchType searchType, std::shared_ptr<SearchController> searchController)
|
2022-06-18 10:46:14 +08:00
|
|
|
{
|
|
|
|
size_t type = static_cast<size_t>(searchType);
|
|
|
|
qDebug() << "search type" << type;
|
|
|
|
if(m_buildinPlugin.contains(type)) {
|
|
|
|
if(!m_buildinPlugin.value(type)->isEnable()) {
|
|
|
|
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(type));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug() << "start search";
|
2023-05-06 15:41:56 +08:00
|
|
|
m_buildinPlugin.value(type)->startSearch();
|
2022-06-18 10:46:14 +08:00
|
|
|
} else {
|
|
|
|
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchTaskPluginManager::pluginSearch(QString customSearchType, std::shared_ptr<SearchController> searchController)
|
|
|
|
{
|
|
|
|
if(m_loadedPlugin.contains(customSearchType)) {
|
|
|
|
if(!m_loadedPlugin.value(customSearchType)->isEnable()) {
|
|
|
|
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is disabled!").arg(customSearchType));
|
|
|
|
return;
|
|
|
|
}
|
2023-05-06 15:41:56 +08:00
|
|
|
m_loadedPlugin.value(customSearchType)->startSearch();
|
2022-06-18 10:46:14 +08:00
|
|
|
} else {
|
|
|
|
Q_EMIT this->searchError(searchController.get()->getCurrentSearchId(), tr("plugin type: %1, is not registered!").arg(customSearchType));
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 11:21:35 +08:00
|
|
|
|
2023-05-06 15:41:56 +08:00
|
|
|
bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType)
|
2022-10-21 11:21:35 +08:00
|
|
|
{
|
|
|
|
if (m_managedPlugins.contains(uuid)) {
|
|
|
|
ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid);
|
|
|
|
|
|
|
|
SearchTaskPluginIface *plugin = nullptr;
|
2023-03-15 16:39:38 +08:00
|
|
|
if (searchType == SearchProperty::SearchType::Custom) {
|
2022-10-21 11:21:35 +08:00
|
|
|
plugin = managedPlugin->externalPlugin(customType);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
plugin = managedPlugin->internalPlugin(searchType);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plugin) {
|
2023-05-06 15:41:56 +08:00
|
|
|
plugin->startSearch();
|
2022-10-21 11:21:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
SearchTaskPluginIface *SearchTaskPluginManager::getPlugin(SearchProperty::SearchType searchType, const QString& customType)
|
2022-10-21 11:21:35 +08:00
|
|
|
{
|
|
|
|
SearchTaskPluginIface *searchPlugin = nullptr;
|
2023-03-15 16:39:38 +08:00
|
|
|
if (searchType == SearchProperty::SearchType::Custom) {
|
2022-10-21 11:21:35 +08:00
|
|
|
if (m_loadedPluginPath.contains(customType)) {
|
|
|
|
QPluginLoader pluginLoader(m_loadedPluginPath.value(customType));
|
|
|
|
QObject *plugin = pluginLoader.instance();
|
|
|
|
if (plugin) {
|
|
|
|
searchPlugin = dynamic_cast<SearchTaskPluginIface*>(plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
switch (searchType) {
|
2023-03-15 16:39:38 +08:00
|
|
|
case SearchProperty::SearchType::File:
|
2022-10-21 11:21:35 +08:00
|
|
|
searchPlugin = new FileSearchTask(this);
|
|
|
|
break;
|
2023-03-15 16:39:38 +08:00
|
|
|
case SearchProperty::SearchType::FileContent:
|
2022-10-21 11:21:35 +08:00
|
|
|
searchPlugin = new FileContentSearchTask(this);
|
|
|
|
break;
|
2023-03-15 16:39:38 +08:00
|
|
|
case SearchProperty::SearchType::Application:
|
2022-10-21 11:21:35 +08:00
|
|
|
searchPlugin = new AppSearchTask(this);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchPlugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchTaskPluginManager::registerPluginPath(const QString& customType, const QString &pluginPath)
|
|
|
|
{
|
|
|
|
if (!m_loadedPluginPath.contains(customType)) {
|
|
|
|
m_loadedPluginPath.insert(customType, pluginPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, SearchProperty::SearchType searchType, const QString& customType)
|
2022-10-21 11:21:35 +08:00
|
|
|
{
|
|
|
|
if (!m_managedPlugins.contains(uuid)) {
|
|
|
|
m_managedPlugins.insert(uuid, new ManagedPlugin(nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchTaskPluginIface *plugin = getPlugin(searchType, customType);
|
|
|
|
bool succeed = false;
|
2023-03-15 16:39:38 +08:00
|
|
|
if (searchType == SearchProperty::SearchType::Custom) {
|
2022-10-21 11:21:35 +08:00
|
|
|
succeed = m_managedPlugins.value(uuid)->insertExternalPlugin(customType, plugin);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
succeed = m_managedPlugins.value(uuid)->insertInternalPlugin(searchType, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return succeed ? plugin : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchTaskPluginManager::destroyPlugins(const QUuid &uuid)
|
|
|
|
{
|
|
|
|
if (m_managedPlugins.contains(uuid)) {
|
|
|
|
ManagedPlugin *managedPlugin = m_managedPlugins.value(uuid);
|
|
|
|
m_managedPlugins.remove(uuid);
|
|
|
|
|
|
|
|
delete managedPlugin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ManagedPlugin::~ManagedPlugin()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searchType, SearchTaskPluginIface *plugin)
|
2022-10-21 11:21:35 +08:00
|
|
|
{
|
|
|
|
if (plugin) {
|
|
|
|
auto type = static_cast<size_t>(searchType);
|
|
|
|
plugin->setParent(this);
|
|
|
|
|
|
|
|
if (!m_internalPlugins.contains(type)) {
|
|
|
|
m_internalPlugins.insert(type, plugin);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPluginIface *plugin)
|
|
|
|
{
|
|
|
|
if (plugin) {
|
|
|
|
plugin->setParent(this);
|
|
|
|
if (!customType.isEmpty() && !m_externalPlugin.contains(customType)) {
|
|
|
|
m_externalPlugin.insert(customType, plugin);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-15 16:39:38 +08:00
|
|
|
inline SearchTaskPluginIface *ManagedPlugin::internalPlugin(const SearchProperty::SearchType &searchType)
|
2022-10-21 11:21:35 +08:00
|
|
|
{
|
|
|
|
auto type = static_cast<size_t>(searchType);
|
|
|
|
if (m_internalPlugins.contains(type)) {
|
|
|
|
return m_internalPlugins.value(type);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SearchTaskPluginIface *ManagedPlugin::externalPlugin(const QString &customType)
|
|
|
|
{
|
|
|
|
if (m_externalPlugin.contains(customType)) {
|
|
|
|
return m_externalPlugin.value(customType);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|