1 | /* |
2 | SPDX-FileCopyrightText: 2020 Alexander Lohnau <alexander.lohnau@gmx.de> |
3 | SPDX-License-Identifier: LGPL-2.0-or-later |
4 | */ |
5 | |
6 | #include <QCoreApplication> |
7 | #include <QFile> |
8 | #include <QFileInfo> |
9 | #include <QJsonArray> |
10 | #include <QJsonDocument> |
11 | #include <QJsonObject> |
12 | #include <QString> |
13 | |
14 | static QStringList jsonArrayToStringList(const QJsonArray &array) |
15 | { |
16 | QStringList list; |
17 | for (const QJsonValue &value : array) { |
18 | list.append(t: value.toString()); |
19 | } |
20 | return list; |
21 | } |
22 | |
23 | static void writeKeyValue(QFile *file, const QString &key, const QString &value) |
24 | { |
25 | file->write(data: (key + u'=' + value + u'\n').toUtf8()); |
26 | } |
27 | |
28 | static void writeKeyValue(QFile *file, const QString &key, const QStringList &list) |
29 | { |
30 | writeKeyValue(file, key, value: list.join(sep: u';')); |
31 | } |
32 | |
33 | int main(int argc, char **argv) |
34 | { |
35 | Q_ASSERT(argc == 3); |
36 | // Use QCoreApplication to parse arguments to handle encoding correctly |
37 | QCoreApplication app(argc, argv); |
38 | QString fileName = app.arguments().at(i: 1); |
39 | |
40 | QFile file(fileName); |
41 | bool isOpen = file.open(flags: QIODevice::ReadOnly); |
42 | if (!isOpen) { |
43 | qCritical() << "Could not open file" << fileName; |
44 | exit(status: 1); |
45 | } |
46 | QJsonDocument doc = QJsonDocument::fromJson(json: file.readAll()); |
47 | Q_ASSERT(doc.isObject()); |
48 | |
49 | const QJsonObject metadata = doc.object(); |
50 | const QJsonObject kplugin = metadata.value(key: QLatin1String("KPlugin" )).toObject(); |
51 | const QLatin1String namePrefix("Name" ); |
52 | |
53 | QFile out(app.arguments().at(i: 2)); |
54 | out.open(flags: QIODevice::WriteOnly); |
55 | out.write(data: "[Desktop Entry]\n" ); |
56 | out.write(data: "Type=Application\n" ); |
57 | out.write(data: "NoDisplay=true\n" ); |
58 | out.write(data: "X-KDE-AliasFor=systemsettings\n" ); |
59 | |
60 | const QString showOnlyOnQtPlatformsKey = QStringLiteral("X-KDE-OnlyShowOnQtPlatforms" ); |
61 | if (auto it = metadata.find(key: showOnlyOnQtPlatformsKey); it != metadata.end()) { |
62 | writeKeyValue(file: &out, key: showOnlyOnQtPlatformsKey, list: jsonArrayToStringList(array: it->toArray())); |
63 | } |
64 | |
65 | QString executableProgram = QStringLiteral("systemsettings " ); |
66 | if (!metadata.contains(key: QLatin1String("X-KDE-System-Settings-Parent-Category" ))) { |
67 | executableProgram = QStringLiteral("kcmshell6 " ); |
68 | } |
69 | |
70 | const QString exec = QLatin1String("Exec=" ) + executableProgram + QFileInfo(fileName).baseName() + QLatin1Char('\n'); |
71 | out.write(data: exec.toUtf8()); |
72 | const QString icon = QLatin1String("Icon=" ) + kplugin.value(key: QLatin1String("Icon" )).toString() + QLatin1Char('\n'); |
73 | out.write(data: icon.toUtf8()); |
74 | |
75 | for (auto it = kplugin.begin(), end = kplugin.end(); it != end; ++it) { |
76 | const QString key = it.key(); |
77 | if (key.startsWith(s: namePrefix)) { |
78 | writeKeyValue(file: &out, key, value: it.value().toString()); |
79 | } |
80 | } |
81 | } |
82 | |