2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2015-09-09 18:42:44 +02:00
|
|
|
*
|
|
|
|
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
|
2018-06-15 11:59:11 +02:00
|
|
|
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
2015-09-09 18:42:44 +02:00
|
|
|
*
|
|
|
|
* Based on KPluginFactory from KCoreAddons, KDE project
|
|
|
|
* Copyright 2007, Matthias Kretz <kretz@kde.org>
|
|
|
|
* Copyright 2007, Bernhard Loos <nhuh.put@web.de>
|
|
|
|
*
|
|
|
|
* Calamares 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.
|
|
|
|
*
|
|
|
|
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PluginFactory.h"
|
|
|
|
#include "PluginFactory_p.h"
|
|
|
|
|
|
|
|
#include <QObjectCleanupHandler>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
Q_GLOBAL_STATIC( QObjectCleanupHandler, factorycleanup )
|
2015-09-09 18:42:44 +02:00
|
|
|
|
|
|
|
namespace Calamares
|
|
|
|
{
|
|
|
|
|
|
|
|
PluginFactory::PluginFactory()
|
2018-01-16 12:00:58 +01:00
|
|
|
: pd_ptr( new PluginFactoryPrivate )
|
2015-09-09 18:42:44 +02:00
|
|
|
{
|
2018-01-16 12:00:58 +01:00
|
|
|
pd_ptr->q_ptr = this;
|
2015-09-09 18:42:44 +02:00
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
factorycleanup()->add( this );
|
2015-09-09 18:42:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
PluginFactory::PluginFactory( PluginFactoryPrivate& d )
|
2018-01-16 12:00:58 +01:00
|
|
|
: pd_ptr( &d )
|
2015-09-09 18:42:44 +02:00
|
|
|
{
|
2017-09-18 15:10:13 +02:00
|
|
|
factorycleanup()->add( this );
|
2015-09-09 18:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PluginFactory::~PluginFactory()
|
|
|
|
{
|
2018-01-16 12:00:58 +01:00
|
|
|
delete pd_ptr;
|
2015-09-09 18:42:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
void PluginFactory::doRegisterPlugin( const QString& keyword, const QMetaObject* metaObject, CreateInstanceFunction instanceFunction )
|
2015-09-09 18:42:44 +02:00
|
|
|
{
|
2017-09-18 15:10:13 +02:00
|
|
|
Q_ASSERT( metaObject );
|
2015-09-09 18:42:44 +02:00
|
|
|
|
|
|
|
// we allow different interfaces to be registered without keyword
|
2017-09-18 15:10:13 +02:00
|
|
|
if ( !keyword.isEmpty() )
|
|
|
|
{
|
2018-01-16 12:00:58 +01:00
|
|
|
if ( pd_ptr->createInstanceHash.contains( keyword ) )
|
2015-09-09 18:42:44 +02:00
|
|
|
qWarning() << "A plugin with the keyword" << keyword << "was already registered. A keyword must be unique!";
|
2018-01-16 12:00:58 +01:00
|
|
|
pd_ptr->createInstanceHash.insert( keyword, PluginFactoryPrivate::Plugin( metaObject, instanceFunction ) );
|
2017-09-18 15:10:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-16 12:00:58 +01:00
|
|
|
const QList<PluginFactoryPrivate::Plugin> clashes( pd_ptr->createInstanceHash.values( keyword ) );
|
2017-09-18 15:10:13 +02:00
|
|
|
const QMetaObject* superClass = metaObject->superClass();
|
|
|
|
if ( superClass )
|
|
|
|
{
|
|
|
|
for ( const PluginFactoryPrivate::Plugin& plugin : clashes )
|
|
|
|
{
|
|
|
|
for ( const QMetaObject* otherSuper = plugin.first->superClass(); otherSuper;
|
|
|
|
otherSuper = otherSuper->superClass() )
|
|
|
|
{
|
|
|
|
if ( superClass == otherSuper )
|
2015-09-09 18:42:44 +02:00
|
|
|
qWarning() << "Two plugins with the same interface(" << superClass->className() << ") were registered. Use keywords to identify the plugins.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 15:10:13 +02:00
|
|
|
for ( const PluginFactoryPrivate::Plugin& plugin : clashes )
|
|
|
|
{
|
2015-09-09 18:42:44 +02:00
|
|
|
superClass = plugin.first->superClass();
|
2017-09-18 15:10:13 +02:00
|
|
|
if ( superClass )
|
|
|
|
{
|
|
|
|
for ( const QMetaObject* otherSuper = metaObject->superClass(); otherSuper;
|
|
|
|
otherSuper = otherSuper->superClass() )
|
|
|
|
{
|
|
|
|
if ( superClass == otherSuper )
|
2015-09-09 18:42:44 +02:00
|
|
|
qWarning() << "Two plugins with the same interface(" << superClass->className() << ") were registered. Use keywords to identify the plugins.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 12:00:58 +01:00
|
|
|
pd_ptr->createInstanceHash.insertMulti( keyword, PluginFactoryPrivate::Plugin( metaObject, instanceFunction ) );
|
2015-09-09 18:42:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
QObject* PluginFactory::create( const char* iface, QWidget* parentWidget, QObject* parent, const QString& keyword )
|
2015-09-09 18:42:44 +02:00
|
|
|
{
|
2017-09-20 15:16:15 +02:00
|
|
|
QObject* obj = nullptr;
|
2015-09-09 18:42:44 +02:00
|
|
|
|
2018-01-16 12:00:58 +01:00
|
|
|
const QList<PluginFactoryPrivate::Plugin> candidates( pd_ptr->createInstanceHash.values( keyword ) );
|
2015-09-09 18:42:44 +02:00
|
|
|
// for !keyword.isEmpty() candidates.count() is 0 or 1
|
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
for ( const PluginFactoryPrivate::Plugin& plugin : candidates )
|
|
|
|
{
|
|
|
|
for ( const QMetaObject* current = plugin.first; current; current = current->superClass() )
|
|
|
|
{
|
|
|
|
if ( 0 == qstrcmp( iface, current->className() ) )
|
|
|
|
{
|
|
|
|
if ( obj )
|
2015-09-09 18:42:44 +02:00
|
|
|
qWarning() << "ambiguous interface requested from a DSO containing more than one plugin";
|
2017-09-18 15:10:13 +02:00
|
|
|
obj = plugin.second( parentWidget, parent );
|
2015-09-09 18:42:44 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:10:13 +02:00
|
|
|
if ( obj )
|
|
|
|
emit objectCreated( obj );
|
2015-09-09 18:42:44 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|