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 >
*/
2021-05-03 00:21:36 +08:00
# include "plugin-manager.h"
# include "search-plugin-manager.h"
2021-12-31 17:38:02 +08:00
# include "search-task-plugin-manager.h"
2021-05-03 00:21:36 +08:00
2021-12-14 14:43:35 +08:00
using namespace UkuiSearch ;
2021-05-03 00:21:36 +08:00
static PluginManager * global_instance = nullptr ;
void PluginManager : : init ( )
{
PluginManager : : getInstance ( ) ;
}
PluginManager * PluginManager : : getInstance ( )
{
if ( ! global_instance ) {
global_instance = new PluginManager ;
}
return global_instance ;
}
PluginManager : : PluginManager ( QObject * parent ) : QObject ( parent )
{
QDir pluginsDir ( PLUGIN_INSTALL_DIRS ) ;
pluginsDir . setFilter ( QDir : : Files ) ;
Q_FOREACH ( QString fileName , pluginsDir . entryList ( QDir : : Files ) ) {
QPluginLoader pluginLoader ( pluginsDir . absoluteFilePath ( fileName ) ) ;
2022-03-15 14:41:37 +08:00
// version check
2022-05-27 09:42:15 +08:00
QString type = pluginLoader . metaData ( ) . value ( " MetaData " ) . toObject ( ) . value ( " type " ) . toString ( ) ;
2022-03-15 14:41:37 +08:00
QString version = pluginLoader . metaData ( ) . value ( " MetaData " ) . toObject ( ) . value ( " version " ) . toString ( ) ;
2022-05-27 09:42:15 +08:00
if ( type = = " SEARCH_PLUGIN " ) {
if ( version ! = SEARCH_PLUGIN_IFACE_VERSION ) {
qWarning ( ) < < " SEARCH_PLUGIN version check failed: " < < fileName < < " version: " < < version < < " iface version : " < < SEARCH_PLUGIN_IFACE_VERSION ;
continue ;
}
} else if ( type = = " SEARCH_TASK_PLUGIN " ) {
if ( version ! = SEARCH_TASK_PLUGIN_IFACE_VERSION ) {
qWarning ( ) < < " SEARCH_TASK_PLUGIN_IFACE_VERSION version check failed: " < < fileName < < " version: " < < version < < " iface version : " < < SEARCH_TASK_PLUGIN_IFACE_VERSION ;
continue ;
}
} else {
qWarning ( ) < < " Unsupport plugin: " < < fileName < < " type: " < < type ;
2021-05-03 00:21:36 +08:00
continue ;
2022-03-14 16:25:36 +08:00
}
2021-05-03 00:21:36 +08:00
QObject * plugin = pluginLoader . instance ( ) ;
if ( ! plugin )
continue ;
PluginInterface * piface = dynamic_cast < PluginInterface * > ( plugin ) ;
if ( ! piface )
continue ;
switch ( piface - > pluginType ( ) ) {
case PluginInterface : : PluginType : : SearchPlugin : {
auto p = dynamic_cast < SearchPluginIface * > ( plugin ) ;
2023-05-04 14:11:01 +08:00
if ( ! SearchPluginManager : : getInstance ( ) - > registerExternalPlugin ( p , pluginsDir . absoluteFilePath ( fileName ) ) ) {
delete p ;
2022-11-22 10:17:36 +08:00
}
2021-05-03 00:21:36 +08:00
break ;
}
2021-12-31 17:38:02 +08:00
case PluginInterface : : PluginType : : SearchTaskPlugin : {
auto p = dynamic_cast < SearchTaskPluginIface * > ( plugin ) ;
SearchTaskPluginManager : : getInstance ( ) - > registerPlugin ( p ) ;
2022-06-09 18:56:17 +08:00
SearchTaskPluginManager : : getInstance ( ) - > registerPluginPath ( p - > getCustomSearchType ( ) , pluginsDir . absoluteFilePath ( fileName ) ) ;
2021-12-31 17:38:02 +08:00
break ;
}
2021-05-03 00:21:36 +08:00
default :
break ;
}
}
}
PluginManager : : ~ PluginManager ( )
{
}