File indexing completed on 2024-04-21 03:56:32

0001 // SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 #include <KPackage/Package>
0004 #include <QMap>
0005 
0006 class KConfigGroup;
0007 class KDesktopFile;
0008 namespace KPackagePrivate
0009 {
0010 template<typename DesktopFile = KDesktopFile, typename ConfigGroup = KConfigGroup>
0011 /**
0012  * @param package KPackage which will have the desktop file metadata set to (if present)
0013  * @param customValueTypes Additional keys and their types that should be read from the desktop file
0014  */
0015 void convertCompatMetaDataDesktopFile(KPackage::Package *package, const QMap<QString, QMetaType::Type> &customValueTypes = {})
0016 {
0017     if (const QString legacyPath = package->filePath("metadata"); !legacyPath.isEmpty() && legacyPath.endsWith(QLatin1String(".desktop"))) {
0018         DesktopFile file(legacyPath);
0019         const ConfigGroup grp = file.desktopGroup();
0020         QJsonObject kplugin{
0021             {QLatin1String("Name"), grp.readEntry("Name")},
0022             {QLatin1String("Description"), grp.readEntry("Comment")},
0023             {QLatin1String("Version"), grp.readEntry("X-KDE-PluginInfo-Version")},
0024             {QLatin1String("Id"), grp.readEntry("X-KDE-PluginInfo-Name")},
0025         };
0026         QJsonObject obj{
0027             {QLatin1String("KPlugin"), kplugin},
0028             {QLatin1String("KPackageStructure"), grp.readEntry("X-KDE-ServiceTypes")},
0029         };
0030         for (auto it = customValueTypes.begin(), end = customValueTypes.end(); it != end; ++it) {
0031             if (const QString value = grp.readEntry(it.key()); !value.isEmpty()) {
0032                 if (QVariant variant(value); variant.convert(QMetaType(it.value()))) { // Make sure the type in resulting json is what the API caller needs
0033                     obj.insert(it.key(), QJsonValue::fromVariant(variant));
0034                 }
0035             }
0036         }
0037         package->setMetadata(KPluginMetaData(obj, legacyPath));
0038     }
0039 }
0040 };