KService

kplugintrader.cpp
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 #include "kplugintrader.h"
11 #include "ktraderparsetree_p.h"
12 
13 #include <QVector>
14 #include <KPluginLoader>
15 #include <KPluginMetaData>
16 
17 using namespace KTraderParse;
18 
20 {
21  static KPluginTrader trader;
22  return &trader;
23 }
24 
25 KPluginTrader::KPluginTrader()
26  : d(nullptr)
27 {
28 }
29 
31 {
32 }
33 
34 void KPluginTrader::applyConstraints(KPluginInfo::List &lst, const QString &constraint)
35 {
36  if (lst.isEmpty() || constraint.isEmpty()) {
37  return;
38  }
39 
40  const ParseTreeBase::Ptr constr = parseConstraints(constraint); // for ownership
41  const ParseTreeBase *pConstraintTree = constr.data(); // for speed
42 
43  if (!constr) { // parse error
44  lst.clear();
45  } else {
46  // Find all plugin information matching the constraint and remove the rest
48  while (it != lst.end()) {
49  if (matchConstraintPlugin(pConstraintTree, *it, lst) != 1) {
50  it = lst.erase(it);
51  } else {
52  ++it;
53  }
54  }
55  }
56 }
57 
58 KPluginInfo::List KPluginTrader::query(const QString &subDirectory, const QString &servicetype, const QString &constraint)
59 {
60  auto filter = [&](const KPluginMetaData &md) -> bool
61  {
62  const auto &types = md.serviceTypes();
63  if (!types.isEmpty() && types.contains(servicetype)) {
64  return true;
65  }
66  // handle compatibility JSON:
67  const auto &data = md.rawData();
68  const auto &jsonTypes = data.value(QStringLiteral("X-KDE-ServiceTypes")).toVariant().toStringList();
69  if (!jsonTypes.isEmpty() && jsonTypes.contains(servicetype)) {
70  return true;
71  }
72  return data.value(QStringLiteral("ServiceTypes")).toVariant().toStringList().contains(servicetype);
73  };
74  QVector<KPluginMetaData> plugins = servicetype.isEmpty() ?
75  KPluginLoader::findPlugins(subDirectory) : KPluginLoader::findPlugins(subDirectory, filter);
77  applyConstraints(lst, constraint);
78  return lst;
79 }
void clear()
A trader interface which provides a way to query specific subdirectories in the Qt plugin paths for p...
Definition: kplugintrader.h:78
static KPluginInfo fromMetaData(const KPluginMetaData &meta)
QList::iterator erase(QList::iterator pos)
static KPluginTrader * self()
This is a static pointer to the KPluginTrader singleton.
~KPluginTrader()
Standard destructor.
bool isEmpty() const const
bool isEmpty() const const
QList::iterator end()
KPluginInfo::List query(const QString &subDirectory, const QString &serviceType=QString(), const QString &constraint=QString())
The main function in the KPluginTrader class.
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter=std::function< bool(const KPluginMetaData &)>())
QList::iterator begin()
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.