1/*
2 SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <KConfigGroup>
8#include <KDesktopFile>
9#include <KPluginMetaData>
10#include <QJsonArray>
11#include <QJsonObject>
12#include <QString>
13
14template<class T = QString>
15inline void copyIfExists(const KConfigGroup &grp, QJsonObject &obj, const char *key, const T &t = QString())
16{
17 copyAndRenameIfExists(grp, obj, key, key, t);
18}
19
20template<class T>
21inline void copyAndRenameIfExists(const KConfigGroup &grp, QJsonObject &obj, const char *oldKey, const char *key, const T &t)
22{
23 if (grp.hasKey(key: oldKey)) {
24 obj.insert(QLatin1String(key), grp.readEntry(oldKey, t));
25 }
26}
27inline KPluginMetaData parseMetaDataFromDesktopFile(const QString &fileName)
28{
29 KDesktopFile file(fileName);
30 const KConfigGroup grp = file.desktopGroup();
31
32 QJsonObject kplugin;
33 copyIfExists(grp, obj&: kplugin, key: "Name");
34 copyIfExists(grp, obj&: kplugin, key: "Icon");
35 copyAndRenameIfExists(grp, obj&: kplugin, oldKey: "X-KDE-PluginInfo-Name", key: "Id", t: QString());
36 copyAndRenameIfExists(grp, obj&: kplugin, oldKey: "Comment", key: "Description", t: QString());
37 copyAndRenameIfExists(grp, obj&: kplugin, oldKey: "X-KDE-PluginInfo-EnabledByDefault", key: "EnabledByDefault", t: false);
38 QJsonObject root;
39 root.insert(key: QLatin1String("KPlugin"), value: kplugin);
40
41 copyIfExists(grp, obj&: root, key: "X-Plasma-DBusRunner-Service");
42 copyIfExists(grp, obj&: root, key: "X-Plasma-DBusRunner-Path");
43 copyIfExists(grp, obj&: root, key: "X-Plasma-Runner-Unique-Results", t: false);
44 copyIfExists(grp, obj&: root, key: "X-Plasma-Runner-Weak-Results", t: false);
45 copyIfExists(grp, obj&: root, key: "X-Plasma-API");
46 copyIfExists(grp, obj&: root, key: "X-Plasma-Request-Actions-Once", t: false);
47 copyIfExists(grp, obj&: root, key: "X-Plasma-Runner-Min-Letter-Count", t: 0);
48 copyIfExists(grp, obj&: root, key: "X-Plasma-Runner-Match-Regex");
49 root.insert(key: QLatin1String("X-Plasma-Runner-Syntaxes"), value: QJsonArray::fromStringList(list: grp.readEntry(key: "X-Plasma-Runner-Syntaxes", aDefault: QStringList())));
50 root.insert(key: QLatin1String("X-Plasma-Runner-Syntax-Descriptions"),
51 value: QJsonArray::fromStringList(list: grp.readEntry(key: "X-Plasma-Runner-Syntax-Descriptions", aDefault: QStringList())));
52 QJsonObject author;
53 author.insert(key: QLatin1String("Name"), value: grp.readEntry(key: "X-KDE-PluginInfo-Author"));
54 author.insert(key: QLatin1String("Email"), value: grp.readEntry(key: "X-KDE-PluginInfo-Email"));
55 author.insert(key: QLatin1String("Website"), value: grp.readEntry(key: "X-KDE-PluginInfo-Website"));
56
57 return KPluginMetaData(root, fileName);
58}
59

source code of krunner/src/kpluginmetadata_utils_p.h