KService

kplugintrader.h
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Torben Weis <[email protected]>
3  Copyright (C) 2006 David Faure <[email protected]>
4  Copyright 2013 Sebastian K├╝gler <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 #ifndef __kplugintrader_h__
21 #define __kplugintrader_h__
22 
23 #include "kplugininfo.h"
24 class KPluginTraderPrivate;
88 class KSERVICE_EXPORT KPluginTrader
89 {
90 public:
94  ~KPluginTrader();
95 
155  KPluginInfo::List query(const QString &subDirectory, const QString &serviceType = QString(),
156  const QString &constraint = QString());
157 
166  static KPluginTrader *self();
167 
188  template <class T>
189  static T *createInstanceFromQuery(const QString &subDirectory,
190  const QString &serviceType = QString(),
191  const QString &constraint = QString(),
192  QObject *parent = nullptr,
193  const QVariantList &args = QVariantList(),
194  QString *error = nullptr)
195  {
196  return createInstanceFromQuery<T>(subDirectory, serviceType, constraint, parent, nullptr, args, error);
197  }
198 
217  template <class T>
218  static T *createInstanceFromQuery(const QString &subDirectory,
219  const QString &serviceType,
220  const QString &constraint,
221  QObject *parent,
222  QWidget *parentWidget,
223  const QVariantList &args = QVariantList(),
224  QString *error = nullptr)
225  {
226  Q_UNUSED(parentWidget)
227  Q_UNUSED(args)
228  if (error) {
229  error->clear();
230  }
231  const KPluginInfo::List offers = self()->query(subDirectory, serviceType, constraint);
232 
233  for (const KPluginInfo &info : offers) {
234  KPluginLoader loader(info.libraryPath());
235  const QVariantList argsWithMetaData = QVariantList() << loader.metaData().toVariantMap();
236  KPluginFactory *factory = loader.factory();
237  if (factory) {
238  T *component = factory->create<T>(parent, argsWithMetaData);
239  if (component) {
240  return component;
241  }
242  }
243  }
244  if (error && error->isEmpty()) {
245  *error = QCoreApplication::translate("", "No service matching the requirements was found");
246  }
247  return nullptr;
248  }
249 
250  static void applyConstraints(KPluginInfo::List &lst,
251  const QString &constraint);
252 
253 private:
257  KPluginTrader();
258 
259  // disallow copy ctor and assignment operator
260  KPluginTrader(const KPluginTrader &other);
261  KPluginTrader &operator=(const KPluginTrader &rhs);
262 
263  KPluginTraderPrivate *const d;
264 };
265 
266 #endif
Information about a plugin.
Definition: kplugininfo.h:44
A trader interface which provides a way to query specific subdirectories in the Qt plugin paths for p...
Definition: kplugintrader.h:88
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 Sun May 31 2020 22:55:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.