Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
jsontools.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include <QDir>
5#include <QJsonArray>
6#include <QJsonObject>
7
8#include "jsontools.h"
9#include "common.h"
10
11#include <iostream>
12#include <optional>
13
14namespace JsonTools {
15
16bool savePreloadFile(QSet<PreloadEntry> preload, QString destFile)
17{
18
19 QJsonArray jsonArray;
20 for (const PreloadEntry &entry : preload) {
21 QJsonObject obj;
22 obj["source"] = entry.source;
23 obj["destination"] = entry.destination;
24 jsonArray.append(obj);
25 }
26 QJsonDocument doc(jsonArray);
27
28 QFile outFile(destFile);
29 if (outFile.exists()) {
30 if (!outFile.remove()) {
31 std::cout << "ERROR: Failed to delete old file: " << outFile.fileName().toStdString()
32 << std::endl;
33 return false;
34 }
35 }
36 if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
37 std::cout << "ERROR: Failed to open file for writing:" << outFile.fileName().toStdString()
38 << std::endl;
39 return false;
40 }
41 if (outFile.write(doc.toJson(QJsonDocument::Indented)) == -1) {
42 std::cout << "ERROR: Failed writing into file :" << outFile.fileName().toStdString()
43 << std::endl;
44 return false;
45 }
46 if (!outFile.flush()) {
47 std::cout << "ERROR: Failed flushing the file :" << outFile.fileName().toStdString()
48 << std::endl;
49 return false;
50 }
51 outFile.close();
52 return true;
53}
54
56{
57 QString qtLibPath = "$QTDIR/lib";
58 QString qtQmlPath = "$QTDIR/qml";
59 QString qtDeployQmlPath = "/qt/qml";
60 QSet<PreloadEntry> res;
61 auto addImport = [&res](const PreloadEntry &entry) {
62 // qDebug() << "adding " << entry.source << "" << entry.destination;
63 res.insert(entry);
64 };
65
66 QJsonParseError parseError;
67 QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8(), &parseError);
68
69 if (parseError.error != QJsonParseError::NoError) {
70 std::cout << "ERROR: QmlImport JSON parse error: " << parseError.errorString().toStdString()
71 << std::endl;
72 return std::nullopt;
73 }
74 if (!doc.isArray()) {
75 std::cout << "ERROR: QmlImport JSON is not an array." << std::endl;
76 return std::nullopt;
77 }
78
79 QJsonArray jsonArray = doc.array();
80 for (const QJsonValue &value : jsonArray) {
81 if (value.isObject()) {
82 QJsonObject obj = value.toObject();
83 auto relativePath = obj["relativePath"].toString();
84 auto plugin = obj["plugin"].toString();
85 if (plugin.isEmpty() || relativePath.isEmpty()) {
86 continue;
87 }
88 auto pluginFilename = "lib" + plugin + ".so";
89 addImport(PreloadEntry{
90 QDir::cleanPath(qtQmlPath + "/" + relativePath + "/" + pluginFilename),
91 QDir::cleanPath(qtDeployQmlPath + "/" + relativePath + "/" + pluginFilename) });
92 addImport(PreloadEntry{
93 QDir::cleanPath(qtQmlPath + "/" + relativePath + "/" + "qmldir"),
94 QDir::cleanPath(qtDeployQmlPath + "/" + relativePath + "/" + "qmldir") });
95 }
96 }
97
98 return res;
99}
100
101}; // namespace JsonTools
bool savePreloadFile(QSet< PreloadEntry > preload, QString destFile)
Definition jsontools.cpp:16
std::optional< QSet< PreloadEntry > > getPreloadsFromQmlImportScannerOutput(QString output)
Definition jsontools.cpp:55