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 
113  KPluginFactory *factory();
114 
125  QString pluginName() const;
126 
135  quint32 pluginVersion();
136 
159  static QString findPlugin(const QString &name);
160 
168  QString errorString() const;
169 
181  QString fileName() const;
182 
193  QObject *instance();
194 
202  bool isLoaded() const;
203 
218  bool load();
219 
230  QLibrary::LoadHints loadHints() const;
231 
239  QJsonObject metaData() const;
240 
251  void setLoadHints(QLibrary::LoadHints loadHints);
252 
264  bool unload();
265 
308  static QList<QObject *> instantiatePlugins(const QString &directory,
309  std::function<bool(const KPluginMetaData &)> filter = std::function<bool(const KPluginMetaData &)>(),
310  QObject* parent = nullptr);
311 
328  static QVector<KPluginMetaData> findPlugins(const QString &directory,
329  std::function<bool(const KPluginMetaData &)> filter = std::function<bool(const KPluginMetaData &)>());
330 
346  static QVector<KPluginMetaData> findPluginsById(const QString &directory, const QString &pluginId);
347 
367  static void forEachPlugin(const QString &directory,
368  std::function<void(const QString &)> callback = std::function<void(const QString &)>());
369 private:
370  Q_DECLARE_PRIVATE(KPluginLoader)
372 
373  KPluginLoaderPrivate *const d_ptr;
374 };
375 
384 // NOTE: this class is all inline, as it mainly exists for typing reasons
385 // (ie: to prevent the issues that would be caused by adding an
386 // operator QString() method to KService)
387 class KCOREADDONS_EXPORT KPluginName
388 {
389 public:
399  inline explicit KPluginName(const QString &name);
400 
407  inline QString name() const;
408 
418  inline bool isValid() const;
419 
426  inline QString errorString() const;
427 
436  static inline KPluginName fromErrorString(const QString &errorString);
437 
438 private:
439  inline KPluginName(const QString &name, bool isError);
440 
441  QString m_name;
442  bool m_isError;
443 };
444 
446  : m_name(name), m_isError(false)
447 {}
448 inline KPluginName::KPluginName(const QString &name, bool isError)
449  : m_name(name), m_isError(isError)
450 {}
452 {
453  return m_isError ? QString() : m_name;
454 }
455 inline bool KPluginName::isValid() const
456 {
457  return !m_isError;
458 }
460 {
461  return m_isError ? m_name : QString();
462 }
464 {
465  return KPluginName(errorString, true);
466 }
467 
468 #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-2021 The KDE developers.
Generated on Sat Jan 23 2021 04:00:31 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.