File indexing completed on 2024-05-05 12:21:38

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Torben Weis <weis@kde.org>
0004     SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
0005     SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 #include <QtGlobal>
0011 QT_WARNING_PUSH
0012 QT_WARNING_DISABLE_DEPRECATED
0013 
0014 #include "kplugintrader.h"
0015 #include "ktraderparsetree_p.h"
0016 
0017 #include <KPluginLoader>
0018 #include <KPluginMetaData>
0019 #include <QVector>
0020 
0021 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 82)
0022 
0023 using namespace KTraderParse;
0024 
0025 KPluginTrader *KPluginTrader::self()
0026 {
0027     static KPluginTrader trader;
0028     return &trader;
0029 }
0030 
0031 KPluginTrader::KPluginTrader()
0032     : d(nullptr)
0033 {
0034 }
0035 
0036 KPluginTrader::~KPluginTrader()
0037 {
0038 }
0039 
0040 void KPluginTrader::applyConstraints(KPluginInfo::List &lst, const QString &constraint)
0041 {
0042     if (lst.isEmpty() || constraint.isEmpty()) {
0043         return;
0044     }
0045 
0046     const ParseTreeBase::Ptr constr = parseConstraints(constraint); // for ownership
0047     const ParseTreeBase *pConstraintTree = constr.data(); // for speed
0048 
0049     if (!constr) { // parse error
0050         lst.clear();
0051     } else {
0052         // Find all plugin information matching the constraint and remove the rest
0053         KPluginInfo::List::iterator it = lst.begin();
0054         while (it != lst.end()) {
0055             if (matchConstraintPlugin(pConstraintTree, *it, lst) != 1) {
0056                 it = lst.erase(it);
0057             } else {
0058                 ++it;
0059             }
0060         }
0061     }
0062 }
0063 
0064 KPluginInfo::List KPluginTrader::query(const QString &subDirectory, const QString &servicetype, const QString &constraint)
0065 {
0066     auto filter = [&](const KPluginMetaData &md) -> bool {
0067         const auto &types = md.serviceTypes();
0068         if (!types.isEmpty() && types.contains(servicetype)) {
0069             return true;
0070         }
0071         // handle compatibility JSON:
0072         const auto &data = md.rawData();
0073         const auto &jsonTypes = data.value(QStringLiteral("X-KDE-ServiceTypes")).toVariant().toStringList();
0074         if (!jsonTypes.isEmpty() && jsonTypes.contains(servicetype)) {
0075             return true;
0076         }
0077         return data.value(QStringLiteral("ServiceTypes")).toVariant().toStringList().contains(servicetype);
0078     };
0079     QVector<KPluginMetaData> plugins = servicetype.isEmpty() ? KPluginLoader::findPlugins(subDirectory) : KPluginLoader::findPlugins(subDirectory, filter);
0080     KPluginInfo::List lst = KPluginInfo::fromMetaData(plugins);
0081     applyConstraints(lst, constraint);
0082     return lst;
0083 }
0084 
0085 #endif