-
Notifications
You must be signed in to change notification settings - Fork 13
/
archivalversionlist.cpp
87 lines (82 loc) · 3.45 KB
/
archivalversionlist.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
#include "archivalversionlist.h"
#include <QNetworkDiskCache>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDir>
#include <QStandardPaths>
ArchivalVersionList::ArchivalVersionList(QString baseUrl) {
m_defBaseUrl = baseUrl;
m_netManager = new QNetworkAccessManager(this);
QNetworkDiskCache* cache = new QNetworkDiskCache(m_netManager);
cache->setCacheDirectory(QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)).filePath("versionCache"));
m_netManager->setCache(cache);
}
void ArchivalVersionList::downloadLists(QStringList abis, QString baseUrl) {
m_versionsnext.clear();
m_rollforwardVersionRange.clear();
m_baseUrl = baseUrl.isEmpty() ? m_defBaseUrl : baseUrl;
if (abis.size()) {
QNetworkReply* reply = m_netManager->get(QNetworkRequest(QUrl(m_baseUrl + "/versions." + abis.at(abis.size() - 1) + ".json.min")));
connect(reply, &QNetworkReply::finished, std::bind(&ArchivalVersionList::onListDownloaded, this, reply, abis.at(abis.size() - 1), abis));
} else {
m_versions = m_versionsnext;
emit versionsChanged();
}
}
void ArchivalVersionList::onListDownloaded(QNetworkReply* reply, QString abi, QStringList abis) {
QByteArray data;
QIODevice * result;
if (reply->error() != QNetworkReply::NoError) {
result = m_netManager->cache()->data(QUrl(m_baseUrl + "/versions." + abi + ".json.min"));
if (!result) {
if(!result) {
QString fileName(":/archivalversionlist/" + abi);
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) {
m_versions = m_versionsnext;
qDebug() << "Version list failed to load, entry count:" << m_versions.size();
emit versionsChanged();
return;
}
else
{
qDebug() << "Version list failed to update use embedded version";
data = file.readAll();
}
file.close();
}
} else {
data = result->readAll();
delete result;
}
} else {
data = reply->readAll();
}
QJsonDocument doc = QJsonDocument::fromJson(data);
for (QJsonValue const& el : doc.array()) {
QJsonArray ela = el.toArray();
ArchivalVersionInfo* info = new ArchivalVersionInfo(this);
info->versionCode = ela.at(0).toInt();
info->versionName = ela.at(1).toString();
info->isBeta = ela.at(2).toInt() == 1;
// Roll forward
if(ela.count() > 3 && ela.at(0).toInt() < ela.at(3).toInt()) {
RollforwardVersionRange* rollfwd = new RollforwardVersionRange(this);
rollfwd->minVersionCode = ela.at(0).toInt();
rollfwd->maxVersionCode = ela.at(3).toInt();
m_rollforwardVersionRange.push_back(rollfwd);
}
info->abi = abi;
m_versionsnext.push_front(info);
}
auto i = abis.indexOf(abi);
if(i == 0) {
m_versions = m_versionsnext;
qDebug() << "Version list loaded, entry count:" << m_versions.size();
emit versionsChanged();
} else {
QNetworkReply* reply = m_netManager->get(QNetworkRequest(QUrl(m_baseUrl + "/versions." + abis.at(i - 1) + ".json.min")));
connect(reply, &QNetworkReply::finished, std::bind(&ArchivalVersionList::onListDownloaded, this, reply, abis.at(i - 1), abis));
}
}