KCoreAddons

desktoptojson.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Sebastian K├╝gler <[email protected]>
3  SPDX-FileCopyrightText: 2014 Alex Richardson <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 
8 #include "desktoptojson.h"
9 #include "../lib/plugin/desktopfileparser_p.h"
10 
11 
12 #include <QFile>
13 #include <QFileInfo>
14 #include <QJsonDocument>
15 #include <QJsonObject>
16 #include <QJsonArray>
17 
18 DesktopToJson::DesktopToJson(QCommandLineParser *parser, const QCommandLineOption &i,
19  const QCommandLineOption &o, const QCommandLineOption &v,
20  const QCommandLineOption &c, const QCommandLineOption &s)
21  : m_parser(parser),
22  input(i),
23  output(o),
24  verbose(v),
25  compat(c),
26  serviceTypesOption(s)
27 {
28 }
29 
30 bool DesktopFileParser::s_verbose = false;
31 bool DesktopFileParser::s_compatibilityMode = false;
32 
33 
34 int DesktopToJson::runMain()
35 {
36  if (!m_parser->isSet(input)) {
37  m_parser->showHelp(1);
38  return 1;
39  }
40  if (m_parser->isSet(verbose)) {
41  DesktopFileParser::s_verbose = true;
42  }
43  if (m_parser->isSet(compat)) {
44  DesktopFileParser::s_compatibilityMode = true;
45  }
46  if (!resolveFiles()) {
47  qCCritical(DESKTOPPARSER) << "Failed to resolve filenames" << m_inFile << m_outFile;
48  return 1;
49  }
50 
51 #pragma message("TODO: make it an error if one of the service type files is invalid or not found")
52  const QStringList serviceTypes = m_parser->values(serviceTypesOption);
53  return convert(m_inFile, m_outFile, serviceTypes) ? EXIT_SUCCESS : EXIT_FAILURE;
54 }
55 
56 bool DesktopToJson::resolveFiles()
57 {
58  if (m_parser->isSet(input)) {
59  m_inFile = m_parser->value(input);
60  const QFileInfo fi(m_inFile);
61  if (!fi.exists()) {
62  qCCritical(DESKTOPPARSER) << "File not found: " << m_inFile;
63  return false;
64  }
65  if (!fi.isAbsolute()) {
66  m_inFile = fi.absoluteFilePath();
67  }
68  }
69 
70  if (m_parser->isSet(output)) {
71  m_outFile = m_parser->value(output);
72  } else if (!m_inFile.isEmpty()) {
73  m_outFile = m_inFile;
74  m_outFile.replace(QStringLiteral(".desktop"), QStringLiteral(".json"));
75  }
76 
77  return m_inFile != m_outFile && !m_inFile.isEmpty() && !m_outFile.isEmpty();
78 }
79 
80 void DesktopFileParser::convertToCompatibilityJson(const QString &key, const QString &value, QJsonObject &json, int lineNr)
81 {
82  // XXX: Hidden=true doesn't make sense with json plugins since the metadata is inside the .so
83  static const QStringList boolkeys = QStringList {
84  QStringLiteral("Hidden"),
85  QStringLiteral("X-KDE-PluginInfo-EnabledByDefault"),
86  };
87  static const QStringList stringlistkeys = QStringList {
88  QStringLiteral("X-KDE-ServiceTypes"),
89  QStringLiteral("X-KDE-PluginInfo-Depends"),
90  };
91  if (boolkeys.contains(key)) {
92  // should only be lower case, but be tolerant here
93  if (value.toLower() == QLatin1String("true")) {
94  json[key] = true;
95  } else {
96  if (value.toLower() != QLatin1String("false")) {
97  qCWarning(DESKTOPPARSER).nospace() << "Expected boolean value for key \"" << key
98  << "\" at line " << lineNr << "but got \"" << value << "\" instead.";
99  }
100  json[key] = false;
101  }
102  } else if (stringlistkeys.contains(key)) {
103  json[key] = QJsonArray::fromStringList(DesktopFileParser::deserializeList(value));
104  } else {
105  json[key] = value;
106  }
107 }
108 
109 bool DesktopToJson::convert(const QString &src, const QString &dest, const QStringList& serviceTypes)
110 {
111 
112  QJsonObject json;
113  DesktopFileParser::convert(src, serviceTypes, json, nullptr);
114 
115  if (DesktopFileParser::s_compatibilityMode) {
116  Q_ASSERT(json.value(QStringLiteral("KPlugin")).toObject().isEmpty());
117  json.remove(QStringLiteral("KPlugin"));
118  }
119  QJsonDocument jdoc;
120  jdoc.setObject(json);
121 
122  QFile file(dest);
123  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
124  qCCritical(DESKTOPPARSER) << "Failed to open " << dest;
125  return false;
126  }
127 
128  file.write(jdoc.toJson());
129  qCDebug(DESKTOPPARSER) << "Generated " << dest;
130  return true;
131 }
T convert(const QVariant &value)
QByteArray toJson() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QJsonObject toObject() const const
QString toLower() const const
void remove(const QString &key)
void setObject(const QJsonObject &object)
QJsonValue value(const QString &key) const const
QJsonArray fromStringList(const QStringList &list)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 23:08:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.