KService

kplugintrader.h
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2000 Torben Weis <[email protected]>
4  SPDX-FileCopyrightText: 2006 David Faure <[email protected]>
5  SPDX-FileCopyrightText: 2013 Sebastian K├╝gler <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-only
8 */
9 
10 #ifndef __kplugintrader_h__
11 #define __kplugintrader_h__
12 
13 #include "kplugininfo.h"
14 class KPluginTraderPrivate;
78 class KSERVICE_EXPORT KPluginTrader
79 {
80 public:
84  ~KPluginTrader();
85 
145  KPluginInfo::List query(const QString &subDirectory, const QString &serviceType = QString(),
146  const QString &constraint = QString());
147 
156  static KPluginTrader *self();
157 
178  template <class T>
179  static T *createInstanceFromQuery(const QString &subDirectory,
180  const QString &serviceType = QString(),
181  const QString &constraint = QString(),
182  QObject *parent = nullptr,
183  const QVariantList &args = QVariantList(),
184  QString *error = nullptr)
185  {
186  return createInstanceFromQuery<T>(subDirectory, serviceType, constraint, parent, nullptr, args, error);
187  }
188 
207  template <class T>
208  static T *createInstanceFromQuery(const QString &subDirectory,
209  const QString &serviceType,
210  const QString &constraint,
211  QObject *parent,
212  QWidget *parentWidget,
213  const QVariantList &args = QVariantList(),
214  QString *error = nullptr)
215  {
216  Q_UNUSED(parentWidget)
217  Q_UNUSED(args)
218  if (error) {
219  error->clear();
220  }
221  const KPluginInfo::List offers = self()->query(subDirectory, serviceType, constraint);
222 
223  for (const KPluginInfo &info : offers) {
224  KPluginLoader loader(info.libraryPath());
225  const QVariantList argsWithMetaData = QVariantList() << loader.metaData().toVariantMap();
226  KPluginFactory *factory = loader.factory();
227  if (factory) {
228  T *component = factory->create<T>(parent, argsWithMetaData);
229  if (component) {
230  return component;
231  }
232  }
233  }
234  if (error && error->isEmpty()) {
235  *error = QCoreApplication::translate("", "No service matching the requirements was found");
236  }
237  return nullptr;
238  }
239 
240  static void applyConstraints(KPluginInfo::List &lst,
241  const QString &constraint);
242 
243 private:
247  KPluginTrader();
248 
249  // disallow copy ctor and assignment operator
250  KPluginTrader(const KPluginTrader &other);
251  KPluginTrader &operator=(const KPluginTrader &rhs);
252 
253  KPluginTraderPrivate *const d;
254 };
255 
256 #endif
Information about a plugin.
Definition: kplugininfo.h:32
A trader interface which provides a way to query specific subdirectories in the Qt plugin paths for p...
Definition: kplugintrader.h:78
T * create(QObject *parent=nullptr, const QVariantList &args=QVariantList())
QVariantMap toVariantMap() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
static T * createInstanceFromQuery(const QString &subDirectory, const QString &serviceType, const QString &constraint, QObject *parent, QWidget *parentWidget, const QVariantList &args=QVariantList(), QString *error=nullptr)
Get a plugin from a trader query.
QJsonObject metaData() const
static T * createInstanceFromQuery(const QString &subDirectory, const QString &serviceType=QString(), const QString &constraint=QString(), QObject *parent=nullptr, const QVariantList &args=QVariantList(), QString *error=nullptr)
Get a plugin from a trader query.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Sep 23 2020 22:47:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.