[libcalamares] Switch to using KPluginFactory

- this is not entirely straightfoward, since  we need
   different constructor arguments for the objects
   Calamares creates (no QVariantList& args, in particular).
   Implement our own registerPlugin() and createInstance()
   for that.
 - work around a bug in K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY
main
Adriaan de Groot 5 years ago
parent 9098f8d741
commit 0716a46eb2

@ -44,6 +44,7 @@ set( libSources
utils/CommandList.cpp
utils/Dirs.cpp
utils/Logger.cpp
utils/PluginFactory.cpp
utils/Retranslator.cpp
utils/String.cpp
utils/UMask.cpp

@ -0,0 +1,11 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019, Adriaan de Groot <groot@kde.org>
*
*/
#include "PluginFactory.h"
CalamaresPluginFactory::~CalamaresPluginFactory()
{
}

@ -28,10 +28,75 @@
#define CalamaresPluginFactory_iid "io.calamares.PluginFactory"
/** @brief Plugin factory for Calamares
*
* Try to re-use KPluginFactory as much as possible (since the
* old code for PluginFactory was a fork of an old version of
* exactly that).
*
* The current createInstance() method passes more arguments
* to the job and viewstep constructors than we want; chasing
* that change means modifying each Calamares module. This class
* implements a version of createInstance() with fewer arguments
* and overloads registerPlugin() to use that.
*/
class CalamaresPluginFactory : public KPluginFactory
{
Q_OBJECT
public:
explicit CalamaresPluginFactory() : KPluginFactory() {}
~CalamaresPluginFactory() override;
/** @brief Create an object from the factory.
*
* Ignores all the @p args since they are not used. Calls
* Calamares constructors for the Jobs and ViewSteps.
*/
template<class impl, class ParentType>
static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
{
Q_UNUSED(parentWidget);
Q_UNUSED(args);
ParentType *p = nullptr;
if (parent) {
p = qobject_cast<ParentType *>(parent);
Q_ASSERT(p);
}
return new impl(p);
}
/** @brief register a plugin
*
* The Calamares version doesn't accept keywords, and uses
* the Calamares createInstance() version which ignores
* the QVariantList of arguments.
*/
template<class T>
void registerPlugin()
{
KPluginFactory::registerPlugin<T>(QString(), &createInstance<T, QObject>);
}
};
/** @brief declare a Calamares Plugin Factory
*
* This would be defined as K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY,
* except that does not actually use the base factory class that is
* passed in.
*/
#define CALAMARES_PLUGIN_FACTORY_DECLARATION(name) \
K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name,KPluginFactory,Q_PLUGIN_METADATA(IID CalamaresPluginFactory_iid))
class name : public CalamaresPluginFactory \
{ \
Q_OBJECT \
Q_INTERFACES(KPluginFactory) \
Q_PLUGIN_METADATA(IID CalamaresPluginFactory_iid) \
public: \
explicit name(); \
~name(); \
};
#define CALAMARES_PLUGIN_FACTORY_DEFINITION(name, pluginRegistrations) \
K_PLUGIN_FACTORY_DEFINITION(name, pluginRegistrations )
K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name,CalamaresPluginFactory,pluginRegistrations)
// Q_DECLARE_INTERFACE( Calamares::PluginFactory, CalamaresPluginFactory_iid )

@ -49,7 +49,7 @@ CppJobModule::loadSelf()
{
if ( m_loader )
{
PluginFactory* pf = qobject_cast< PluginFactory* >( m_loader->instance() );
CalamaresPluginFactory* pf = qobject_cast< CalamaresPluginFactory* >( m_loader->instance() );
if ( !pf )
{
cDebug() << Q_FUNC_INFO << m_loader->errorString();

@ -50,7 +50,7 @@ ViewModule::loadSelf()
{
if ( m_loader )
{
PluginFactory* pf = qobject_cast< PluginFactory* >( m_loader->instance() );
CalamaresPluginFactory* pf = qobject_cast< CalamaresPluginFactory* >( m_loader->instance() );
if ( !pf )
{
cWarning() << Q_FUNC_INFO << "No factory:" << m_loader->errorString();

Loading…
Cancel
Save