File indexing completed on 2024-05-12 11:56:31

0001 #pragma once
0002 
0003 #include "kpackage_debug.h"
0004 #include <KPluginMetaData>
0005 #include <QString>
0006 
0007 inline QStringList readKPackageTypes(const KPluginMetaData &metaData)
0008 {
0009     const QString structureValue = metaData.value(QStringLiteral("KPackageStructure"));
0010 
0011     // If we have the new property we can immediately return it's value, no matter if it is a structure plugin or package
0012     if (!structureValue.isEmpty()) {
0013         return QStringList(structureValue);
0014     }
0015 
0016     // Compatibility block for old keys
0017     QStringList types;
0018     const static QStringList ignoreStringList = QStringList{QStringLiteral("KPackage/PackageStructure")};
0019     const auto serviceTypes = metaData.rawData().value(QStringLiteral("KPlugin")).toObject().value(QStringLiteral("ServiceTypes")).toVariant().toStringList();
0020     if (serviceTypes == ignoreStringList) {
0021         // If the service type is set to this value we have a structure plugin, consequently we want the pluginId
0022         types << metaData.pluginId();
0023     } else {
0024         // We have a package, read the service types
0025         types << serviceTypes;
0026     }
0027     // while most package structure plugins do, they don't need to set the service types,
0028     // if we haven't found anything so far we use the plugin id if it looks like a valid package type.
0029     const QString pluginId = metaData.pluginId();
0030     if (types.isEmpty() && !metaData.fileName().endsWith(QLatin1String(".json")) && pluginId.contains(QLatin1Char('/'))) {
0031         types << pluginId;
0032     }
0033     return types;
0034 }
0035 
0036 inline KPluginMetaData structureForKPackageType(const QString &packageFormat)
0037 {
0038     const QString guessedPath = QStringLiteral("kpackage/packagestructure/") + QString(packageFormat).toLower().replace(QLatin1Char('/'), QLatin1Char('_'));
0039     KPluginMetaData guessedData(guessedPath);
0040     if (guessedData.isValid() && readKPackageTypes(guessedData).contains(packageFormat)) {
0041         return guessedData;
0042     }
0043     qCDebug(KPACKAGE_LOG) << "Could not find package structure for" << packageFormat << "by plugin path. The guessed path was" << guessedPath;
0044     QVector<KPluginMetaData> plugins =
0045         KPluginMetaData::findPlugins(QStringLiteral("kpackage/packagestructure"), [packageFormat](const KPluginMetaData &metaData) {
0046             return readKPackageTypes(metaData).contains(packageFormat);
0047         });
0048     if (!plugins.isEmpty()) {
0049         return plugins.constFirst();
0050     }
0051     return KPluginMetaData();
0052 }