File indexing completed on 2024-04-28 07:45:39

0001 /*
0002     SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KConfigGroup>
0008 #include <KDesktopFile>
0009 #include <KPluginMetaData>
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 #include <QString>
0013 
0014 template<class T = QString>
0015 inline void copyIfExists(const KConfigGroup &grp, QJsonObject &obj, const char *key, const T &t = QString())
0016 {
0017     copyAndRenameIfExists(grp, obj, key, key, t);
0018 }
0019 
0020 template<class T>
0021 inline void copyAndRenameIfExists(const KConfigGroup &grp, QJsonObject &obj, const char *oldKey, const char *key, const T &t)
0022 {
0023     if (grp.hasKey(oldKey)) {
0024         obj.insert(QLatin1String(key), grp.readEntry(oldKey, t));
0025     }
0026 }
0027 inline KPluginMetaData parseMetaDataFromDesktopFile(const QString &fileName)
0028 {
0029     KDesktopFile file(fileName);
0030     const KConfigGroup grp = file.desktopGroup();
0031 
0032     QJsonObject kplugin;
0033     copyIfExists(grp, kplugin, "Name");
0034     copyIfExists(grp, kplugin, "Icon");
0035     copyAndRenameIfExists(grp, kplugin, "X-KDE-PluginInfo-Name", "Id", QString());
0036     copyAndRenameIfExists(grp, kplugin, "Comment", "Description", QString());
0037     copyAndRenameIfExists(grp, kplugin, "X-KDE-PluginInfo-EnabledByDefault", "EnabledByDefault", false);
0038     QJsonObject root;
0039     root.insert(QLatin1String("KPlugin"), kplugin);
0040 
0041     copyIfExists(grp, root, "X-Plasma-DBusRunner-Service");
0042     copyIfExists(grp, root, "X-Plasma-DBusRunner-Path");
0043     copyIfExists(grp, root, "X-Plasma-Runner-Unique-Results", false);
0044     copyIfExists(grp, root, "X-Plasma-Runner-Weak-Results", false);
0045     copyIfExists(grp, root, "X-Plasma-API");
0046     copyIfExists(grp, root, "X-Plasma-Request-Actions-Once", false);
0047     copyIfExists(grp, root, "X-Plasma-Runner-Min-Letter-Count", 0);
0048     copyIfExists(grp, root, "X-Plasma-Runner-Match-Regex");
0049     root.insert(QLatin1String("X-Plasma-Runner-Syntaxes"), QJsonArray::fromStringList(grp.readEntry("X-Plasma-Runner-Syntaxes", QStringList())));
0050     root.insert(QLatin1String("X-Plasma-Runner-Syntax-Descriptions"),
0051                 QJsonArray::fromStringList(grp.readEntry("X-Plasma-Runner-Syntax-Descriptions", QStringList())));
0052     QJsonObject author;
0053     author.insert(QLatin1String("Name"), grp.readEntry("X-KDE-PluginInfo-Author"));
0054     author.insert(QLatin1String("Email"), grp.readEntry("X-KDE-PluginInfo-Email"));
0055     author.insert(QLatin1String("Website"), grp.readEntry("X-KDE-PluginInfo-Website"));
0056 
0057     return KPluginMetaData(root, fileName);
0058 }