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

KDE's Doxygen guidelines are available online.