[libcalamares] Port away from KPluginFactory
This commit is contained in:
parent
8f769006d6
commit
5160fdc26a
@ -4,10 +4,6 @@
|
|||||||
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
|
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
|
||||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
*
|
*
|
||||||
* Based on KPluginFactory from KCoreAddons, KDE project
|
|
||||||
* SPDX-FileCopyrightText: 2007 Matthias Kretz <kretz@kde.org>
|
|
||||||
* SPDX-FileCopyrightText: 2007 Bernhard Loos <nhuh.put@web.de>
|
|
||||||
*
|
|
||||||
* Calamares is Free Software: see the License-Identifier above.
|
* Calamares is Free Software: see the License-Identifier above.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
@ -16,81 +12,100 @@
|
|||||||
#ifndef UTILS_PLUGINFACTORY_H
|
#ifndef UTILS_PLUGINFACTORY_H
|
||||||
#define UTILS_PLUGINFACTORY_H
|
#define UTILS_PLUGINFACTORY_H
|
||||||
|
|
||||||
#include <KPluginFactory>
|
#include <QObject>
|
||||||
|
|
||||||
#define CalamaresPluginFactory_iid "io.calamares.PluginFactory"
|
#define CalamaresPluginFactory_iid "io.calamares.PluginFactory"
|
||||||
|
|
||||||
/** @brief Plugin factory for Calamares
|
/** @brief Plugin factory for Calamares
|
||||||
*
|
*
|
||||||
* Try to re-use KPluginFactory as much as possible (since the
|
* A Calamares plugin contains just one kind of plugin -- either
|
||||||
* old code for PluginFactory was a fork of an old version of
|
* a job, or a viewstep -- so the factory is straightforward.
|
||||||
* exactly that).
|
* It gets a single CreateInstanceFunction and calls that;
|
||||||
|
* the function is set when registerPlugin() is called in a subclass.
|
||||||
*
|
*
|
||||||
* 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
|
class CalamaresPluginFactory : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit CalamaresPluginFactory()
|
explicit CalamaresPluginFactory() {}
|
||||||
: KPluginFactory()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
~CalamaresPluginFactory() override;
|
~CalamaresPluginFactory() override;
|
||||||
|
|
||||||
/** @brief Create an object from the factory.
|
typedef QObject* ( *CreateInstanceFunction )( QObject* );
|
||||||
*
|
|
||||||
* Ignores all the @p args since they are not used. Calls
|
template < class T >
|
||||||
* Calamares constructors for the Jobs and ViewSteps.
|
T* create( QObject* parent = nullptr )
|
||||||
*/
|
|
||||||
template < class impl, class ParentType >
|
|
||||||
static QObject* createInstance( QWidget* parentWidget, QObject* parent, const QVariantList& args )
|
|
||||||
{
|
{
|
||||||
Q_UNUSED( parentWidget )
|
auto* op = fn ? fn( parent ) : nullptr;
|
||||||
Q_UNUSED( args )
|
if ( !op )
|
||||||
ParentType* p = nullptr;
|
|
||||||
if ( parent )
|
|
||||||
{
|
{
|
||||||
p = qobject_cast< ParentType* >( parent );
|
return nullptr;
|
||||||
Q_ASSERT( p );
|
|
||||||
}
|
}
|
||||||
return new impl( p );
|
T* tp = qobject_cast< T* >( op );
|
||||||
|
if ( !tp )
|
||||||
|
{
|
||||||
|
delete op;
|
||||||
|
}
|
||||||
|
return tp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief register a plugin
|
protected:
|
||||||
*
|
CreateInstanceFunction fn = nullptr;
|
||||||
* 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
|
/** @brief declare a Calamares Plugin Factory
|
||||||
*
|
*
|
||||||
* This would be defined as K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY,
|
* There should be one declaration -- generally alongside the
|
||||||
* except that does not actually use the base factory class that is
|
* class definition for the Job or ViewStep that the plugin is
|
||||||
* passed in.
|
* going to provide, in the header -- and one definition -- in
|
||||||
|
* the corresponding implementation.
|
||||||
*/
|
*/
|
||||||
#define CALAMARES_PLUGIN_FACTORY_DECLARATION( name ) \
|
#define CALAMARES_PLUGIN_FACTORY_DECLARATION( name ) \
|
||||||
class name : public CalamaresPluginFactory \
|
class name : public CalamaresPluginFactory \
|
||||||
{ \
|
{ \
|
||||||
Q_OBJECT \
|
Q_OBJECT \
|
||||||
Q_INTERFACES( KPluginFactory ) \
|
Q_INTERFACES( CalamaresPluginFactory ) \
|
||||||
Q_PLUGIN_METADATA( IID CalamaresPluginFactory_iid ) \
|
Q_PLUGIN_METADATA( IID CalamaresPluginFactory_iid ) \
|
||||||
public: \
|
public: \
|
||||||
explicit name(); \
|
explicit name(); \
|
||||||
~name() override; \
|
~name() override; \
|
||||||
|
template < class T > \
|
||||||
|
static QObject* createInstance( QObject* parent ) \
|
||||||
|
{ \
|
||||||
|
return new T( parent ); \
|
||||||
|
} \
|
||||||
|
template < class T > \
|
||||||
|
void registerPlugin() \
|
||||||
|
{ \
|
||||||
|
fn = createInstance< T >; \
|
||||||
|
} \
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** @brief Define a Calamares Plugin Factory
|
||||||
|
*
|
||||||
|
* This should be done exactly once, generally in the translation
|
||||||
|
* unit containing the definitions for the main class of the plugin,
|
||||||
|
* either the Job or the ViewStep definitions.
|
||||||
|
*
|
||||||
|
* The @p name must match the name used in the declaration, while
|
||||||
|
* @p pluginRegistrations should be a single call to `registerPlugin<T>()`
|
||||||
|
* where `T` is the type (subclass of Job or ViewStep) defined by the
|
||||||
|
* plugin, eg.
|
||||||
|
*
|
||||||
|
* ```
|
||||||
|
* CALAMARES_PLUGIN_FACTORY_DEFINITION( MyPlugin, registerPlugin<MyPluginJob>() )
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Leaving out the `()` will lead to generally-weird compiler warnings.
|
||||||
|
*/
|
||||||
#define CALAMARES_PLUGIN_FACTORY_DEFINITION( name, pluginRegistrations ) \
|
#define CALAMARES_PLUGIN_FACTORY_DEFINITION( name, pluginRegistrations ) \
|
||||||
K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY( name, CalamaresPluginFactory, pluginRegistrations )
|
name::name() \
|
||||||
|
: CalamaresPluginFactory() \
|
||||||
|
{ \
|
||||||
|
pluginRegistrations; \
|
||||||
|
} \
|
||||||
|
name::~name() {}
|
||||||
|
|
||||||
|
Q_DECLARE_INTERFACE( CalamaresPluginFactory, CalamaresPluginFactory_iid )
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user