File indexing completed on 2024-05-12 15:59:03

0001 /*
0002  * SPDX-FileCopyrightText: 2006-2016 Boudewijn Rempt (boud@valdyas.org)
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KO_PLUGIN_LOADER_H
0008 #define KO_PLUGIN_LOADER_H
0009 
0010 #include <QObject>
0011 #include <QStringList>
0012 
0013 #include "kritaplugin_export.h"
0014 
0015 #ifndef Q_MOC_RUN
0016 /**
0017  * The pluginloader singleton is responsible for loading the plugins
0018  * that it's asked to load. It keeps track of which servicetypes it
0019  * has seen and doesn't reload them. The plugins need to inherit
0020  * a QObject with a default constructor. Inside the default
0021  * constructor you can create whatever object you want and add it to
0022  * whatever registry you prefer. After having been constructed, your plugin
0023  * will be deleted, so do all you need in the constructor.  Things like
0024  * adding a factory to a registry make sense there.
0025  * Example header file;
0026 @code
0027 #include <QObject>
0028 
0029 class MyPlugin : public QObject {
0030     Q_OBJECT
0031 public:
0032     MyPlugin(QObject *parent, const QVariantList & );
0033     ~MyPlugin() {}
0034 };
0035 @endcode
0036  * Example cpp file;
0037 @code
0038 #include "MyPlugin.h"
0039 #include <kpluginfactory.h>
0040 
0041 K_PLUGIN_FACTORY_WITH_JSON(MyPluginFactory, "myplugin.json", registerPlugin<MyPlugin>();)
0042 
0043 MyPlugin::MyPlugin( QObject *parent, const QVariantList& ) : QObject(parent) {
0044     // do stuff like creating a factory and adding it to the
0045     // registry instance.
0046 }
0047 #include <MyPlugin.moc>
0048 @endcode
0049  */
0050 #endif
0051 class KRITAPLUGIN_EXPORT KoPluginLoader : public QObject
0052 {
0053 
0054     Q_OBJECT
0055 
0056 public:
0057     /**
0058      * Config object for load()
0059      * It is possible to limit which plugins will be loaded in the KConfig configuration file by
0060      * stating explicitly which plugins are wanted.
0061      */
0062     struct PluginsConfig {
0063         PluginsConfig() : group(0), whiteList(0), blacklist(0) {}
0064         /**
0065          * The properties are retrieved from the config using the following construct;
0066          * /code
0067          *  KConfigGroup configGroup =  KSharedConfig::openConfig()->group(config.group);
0068          * /endcode
0069          * For most cases you can pass the string "krita" into this variable.
0070          */
0071         const char * group;
0072         /// This contains the variable name for the list of plugins (by library name) the user wants to load
0073         const char * whiteList;
0074         /// This contains the variable name for the list of plugins (by library name) that will not be loaded
0075         const char * blacklist;
0076         /// A registry can state it wants to load a default set of plugins instead of all plugins
0077         /// when the application starts the first time.  Append all such plugin (library) names to this list.
0078         QStringList defaults;
0079     };
0080 
0081     ~KoPluginLoader() override;
0082 
0083     /**
0084      * Return an instance of the KoPluginLoader
0085      * Creates an instance if that has never happened before and returns the singleton instance.
0086      */
0087     static KoPluginLoader * instance();
0088 
0089     /**
0090      * Load all plugins that conform to the versiontype and versionstring,
0091      * for instance:
0092      * KoPluginLoader::instance()->load("Krita/Flake", "([X-Flake-PluginVersion] == 28)");
0093      * This method allows you to optionally limit the plugins that are loaded by version, but also
0094      * using a user configurable set of config options.
0095      * If you pass a PluginsConfig struct only those plugins are loaded that are specified in the
0096      * application config file.  New plugins found since last start will be automatically loaded.
0097      * @param serviceType The string used to identify the plugins.
0098      * @param versionString A string match that allows you to check for a specific version
0099      * @param config when passing a valid config only the wanted plugins are actually loaded
0100      * #param owner if 0, the plugin will be deleted after instantiation, if not, the given qobject will own the plugin in its qobject hierarchy
0101      * @param cache: if true, the plugin will only be loaded once
0102      * @return a list of services (by library name) that were not know in the config
0103      */
0104     void load(const QString & serviceType, const QString & versionString = QString(), const PluginsConfig &config = PluginsConfig(), QObject* owner = 0, bool cache = true);
0105 
0106 public:
0107     /// DO NOT USE! Use instance() instead
0108     // TODO: turn KoPluginLoader into namespace and do not expose object at all
0109     KoPluginLoader();
0110 private:
0111     KoPluginLoader(const KoPluginLoader&);
0112     KoPluginLoader operator=(const KoPluginLoader&);
0113 
0114 private:
0115     class Private;
0116     Private * const d;
0117 };
0118 
0119 #endif // KO_PLUGIN_LOADER_H