KCoreAddons

kpluginloader.h
1 /*
2  This file is part of the KDE project
3 
4  SPDX-FileCopyrightText: 2007 Bernhard Loos <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 #ifndef KPLUGINLOADER_H
9 #define KPLUGINLOADER_H
10 
11 #include <kexportplugin.h>
12 
13 #include <QPluginLoader>
14 
15 #include <functional>
16 
17 class KPluginFactory;
18 class KPluginMetaData;
19 
20 class KPluginLoaderPrivate;
21 class KPluginName;
22 
60 class KCOREADDONS_EXPORT KPluginLoader : public QObject
61 {
62  Q_OBJECT
63  Q_PROPERTY(QString fileName READ fileName)
64  Q_PROPERTY(QLibrary::LoadHints loadHints READ loadHints WRITE setLoadHints)
65  Q_PROPERTY(QString pluginName READ pluginName)
66  Q_PROPERTY(quint32 pluginVersion READ pluginVersion)
67 public:
79  explicit KPluginLoader(const QString &plugin, QObject *parent = nullptr);
80 
91  explicit KPluginLoader(const KPluginName &name, QObject *parent = nullptr);
92 
99  ~KPluginLoader();
100 
110  KPluginFactory *factory();
111 
122  QString pluginName() const;
123 
132  quint32 pluginVersion();
133 
156  static QString findPlugin(const QString &name);
157 
165  QString errorString() const;
166 
178  QString fileName() const;
179 
190  QObject *instance();
191 
199  bool isLoaded() const;
200 
215  bool load();
216 
227  QLibrary::LoadHints loadHints() const;
228 
236  QJsonObject metaData() const;
237 
248  void setLoadHints(QLibrary::LoadHints loadHints);
249 
261  bool unload();
262 
305  static QList<QObject *> instantiatePlugins(const QString &directory,
306  std::function<bool(const KPluginMetaData &)> filter = std::function<bool(const KPluginMetaData &)>(),
307  QObject* parent = nullptr);
308 
325  static QVector<KPluginMetaData> findPlugins(const QString &directory,
326  std::function<bool(const KPluginMetaData &)> filter = std::function<bool(const KPluginMetaData &)>());
327 
343  static QVector<KPluginMetaData> findPluginsById(const QString &directory, const QString &pluginId);
344 
364  static void forEachPlugin(const QString &directory,
365  std::function<void(const QString &)> callback = std::function<void(const QString &)>());
366 private:
367  Q_DECLARE_PRIVATE(KPluginLoader)
369 
370  KPluginLoaderPrivate *const d_ptr;
371 };
372 
381 // NOTE: this class is all inline, as it mainly exists for typing reasons
382 // (ie: to prevent the issues that would be caused by adding an
383 // operator QString() method to KService)
384 class KCOREADDONS_EXPORT KPluginName
385 {
386 public:
396  inline explicit KPluginName(const QString &name);
397 
404  inline QString name() const;
405 
415  inline bool isValid() const;
416 
423  inline QString errorString() const;
424 
433  static inline KPluginName fromErrorString(const QString &errorString);
434 
435 private:
436  inline KPluginName(const QString &name, bool isError);
437 
438  QString m_name;
439  bool m_isError;
440 };
441 
443  : m_name(name), m_isError(false)
444 {}
445 inline KPluginName::KPluginName(const QString &name, bool isError)
446  : m_name(name), m_isError(isError)
447 {}
449 {
450  return m_isError ? QString() : m_name;
451 }
452 inline bool KPluginName::isValid() const
453 {
454  return !m_isError;
455 }
457 {
458  return m_isError ? m_name : QString();
459 }
461 {
462  return KPluginName(errorString, true);
463 }
464 
465 #endif
QString name() const
The name of the plugin.
Represents the name of a plugin intended for KPluginLoader.
This class behaves largely like QPluginLoader (and, indeed, uses it internally), but additionally rea...
Definition: kpluginloader.h:60
typedef LoadHints
static KPluginName fromErrorString(const QString &errorString)
Construct an invalid plugin name with an error message.
QString errorString() const
The error string if no name could be determined.
Q_PROPERTY(...)
Q_DISABLE_COPY(Class)
bool isValid() const
Whether the name is valid.
KPluginName(const QString &name)
Construct a (valid) plugin name from a string.
This class allows easily accessing some standardized values from the JSON metadata that can be embedd...
KPluginFactory provides a convenient way to provide factory-style plugins.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 22:52:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.