-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathjsonparser.cpp
202 lines (167 loc) · 7.19 KB
/
jsonparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
////////////////////////////////////////////////////////////////////////////////
// This file is part of LibreELEC - http://www.libreelec.tv
// Copyright (C) 2016 Team LibreELEC
//
// LibreELEC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// LibreELEC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibreELEC. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////
#include "jsonparser.h"
#include <QDebug>
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QStandardPaths>
#include <QCollator>
#include <QRegularExpression>
#include <QVersionNumber>
#include <algorithm>
QTextStream& qStdOut()
{
static QTextStream ts( stdout );
return ts;
}
bool compareVersion(const QVariantMap &imageMap1, const QVariantMap &imageMap2)
{
QString versionStr1;
QString versionStr2;
// must compare only version not whole string
// name-8.0.2.img.gz < name-8.0.2.1.img.gz
// LibreELEC-WeTek_Hub.aarch64-8.0.2.1.img.gz
// LibreELEC-A64.arm-9.95.5-pine64-plus.img.gz
QRegularExpression regExp = QRegularExpression("-([0-9]+\\.[0-9]+\\.[0-9]+).*\\.img\\.gz");
QRegularExpressionMatch match = regExp.match(imageMap1["name"].toString());
if (match.hasMatch())
versionStr1 = match.captured(1);
QRegularExpressionMatch match2 = regExp.match(imageMap2["name"].toString());
if (match2.hasMatch())
versionStr2 = match2.captured(1);
if (versionStr1.isEmpty() || versionStr2.isEmpty())
return false; // some error
int versionCmp = QVersionNumber::compare(
QVersionNumber::fromString(versionStr1),
QVersionNumber::fromString(versionStr2));
if (versionCmp < 0)
return true;
else
return false;
}
JsonParser::JsonParser(const QByteArray &data)
{
parseAndSet(data, "");
}
void JsonParser::addExtra(const QByteArray &data, const QString label)
{
parseAndSet(data, label);
}
namespace {
bool ReadImageName(const QJsonObject& imageObject, int projectIndex, QList<QVariantMap>& imagesList, QList<ProjectData>& projectList)
{
QVariantMap imageProps = imageObject.toVariantMap();
QString imageName = imageProps["name"].toString();
if (imageName.endsWith(".img.gz"))
{
// QTextStream& textStream = qStdOut();
// textStream << "Image: " << imageName << " - " << imageProps["sha256"].toString() << " - " << imageProps["size"].toString() << "\n";
// textStream.flush();
//We need to add the full map to the list as we'll need the name, sha256 and the size.
if (projectIndex < 0) // new project
imagesList.append(std::move(imageProps));
else // existing project
projectList[projectIndex].images.append(std::move(imageProps));
return true;
}
return false;
}
} //anonymous namespace
void JsonParser::parseAndSet(const QByteArray &data, const QString label)
{
//qDebug() << "parseAndSet data:" << data;
const QJsonDocument jsonDocument = QJsonDocument::fromJson(data);
const auto jsonObject = jsonDocument.object();
// get project versions (LibreElec 7.0, 8.0, ...)
for (auto itProjectVersions = jsonObject.begin(); itProjectVersions != jsonObject.end(); ++itProjectVersions)
{
const auto project = itProjectVersions.key();
const auto projectObj = itProjectVersions.value().toObject();
const auto projectUrl = projectObj["url"].toString();
// get projects (imx6, Wetek, ...)
const auto projectVersionsNode = projectObj["project"].toObject();
for (auto itProjects = projectVersionsNode.begin(); itProjects != projectVersionsNode.end(); ++itProjects)
{
const auto projectId = itProjects.key();
const auto releasesNode = itProjects.value().toObject();
auto projectName = releasesNode["displayName"].toString();
// skip Virtual
if (projectId == "Virtual.x86_64")
continue;
if (!label.isEmpty())
projectName += QString{" - %1"}.arg(label);
QVariantMap projectData;
projectData.insert("name", projectName);
projectData.insert("id", projectId);
projectData.insert("url", projectUrl);
// get releases
for (auto itReleases = releasesNode.begin(); itReleases != releasesNode.end(); ++itReleases)
{
QList<QVariantMap> imagesList;
ProjectData projectCheck;
projectCheck.name = projectName;
int projectIndex = projectList.indexOf(projectCheck);
const auto releaseNode = itReleases.value().toObject();
for (auto itReleaseItems = releaseNode.begin(); itReleaseItems != releaseNode.end(); ++itReleaseItems)
{
const auto releaseItemsNode = itReleaseItems.value().toObject();
const auto itFile = releaseItemsNode.find("file");
if (itFile != releaseItemsNode.end())
ReadImageName(itFile.value().toObject(), projectIndex, imagesList, projectList);
const auto itImage = releaseItemsNode.find("image");
if (itImage != releaseItemsNode.end())
ReadImageName(itImage.value().toObject(), projectIndex, imagesList, projectList);
const auto itUbootsNode = releaseItemsNode.find("uboot");
if (itUbootsNode != releaseItemsNode.end())
{
QJsonArray ubootsNode = itUbootsNode.value().toArray();
for (QJsonValue uboot : ubootsNode)
{
ReadImageName(uboot.toObject(), projectIndex, imagesList, projectList);
}
}
}
if (projectIndex < 0)
{
// new project
ProjectData projectData(projectName, projectId, projectUrl, imagesList);
projectList.append(projectData);
}
}
}
}
QCollator collator;
collator.setNumericMode(true);
collator.setCaseSensitivity(Qt::CaseSensitive);
std::sort(projectList.begin(), projectList.end(),
[&collator](const ProjectData &proj1, const ProjectData &proj2)
{return collator.compare(proj1.name, proj2.name) > 0;});
for (auto& project : projectList)
{
auto& images = project.images;
std::sort(images.begin(), images.end(), compareVersion);
std::reverse(images.begin(), images.end());
}
}
QList<ProjectData> JsonParser::getProjectData() const
{
return projectList;
}