KService

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