1818#include < QMessageBox>
1919#include < QNetworkRequest>
2020#include < QProcess>
21- #include < QSettings>
2221#include < QStringBuilder>
2322#include < QTemporaryFile>
2423#include < QUrlQuery>
2524
2625Updater::Updater (QObject *parent) : QObject(parent), url(QStringLiteral(" https://openrct2.org/altapi/" ))
2726{
2827 connect (&net, &QNetworkAccessManager::sslErrors, [](QNetworkReply * reply, const QList<QSslError> & errors){Q_UNUSED (reply); qDebug () << errors;});
28+ }
29+
30+ void Updater::download () {
31+ if (update != nullptr ) update->abort (), update = nullptr ;
32+ if (api != nullptr ) api->abort (), api = nullptr ;
33+ if (bundle != nullptr ) bundle->abort (), bundle = nullptr ;
2934
3035 QUrlQuery query;
3136 query.addQueryItem (QStringLiteral (" command" ), QStringLiteral (" get-latest-download" ));
3237 query.addQueryItem (QStringLiteral (" flavourId" ), QStringLiteral (OPENRCT2_FLAVOR));
33- query.addQueryItem (QStringLiteral (" gitBranch" ), QStringLiteral (" develop" ));
38+
39+ QVariant stableVar = settings.value (QStringLiteral (" stable" ));
40+ bool stable = stableVar.isValid () && stableVar.toBool ();
41+ query.addQueryItem (QStringLiteral (" gitBranch" ), stable ? QStringLiteral (" master" ) : QStringLiteral (" develop" ));
3442
3543 url.setQuery (query.query ());
36- }
3744
38- void Updater::download () {
3945 QNetworkRequest urequest (QStringLiteral (" https://api.github.com/repos/LRFLEW/OpenRCT2Launcher/releases/latest" ));
40- QNetworkReply *ureply = net.get (urequest);
41- connect (ureply , &QNetworkReply::finished, [ this , ureply]() { receivedUpdate (ureply); } );
46+ update = net.get (urequest);
47+ connect (update , &QNetworkReply::finished, this , &Updater:: receivedUpdate);
4248
4349 QNetworkRequest request (url);
44- QNetworkReply *reply = net.get (request);
45- connect (reply , &QNetworkReply::finished, [ this , reply]() { receivedAPI (reply); } );
50+ api = net.get (request);
51+ connect (api , &QNetworkReply::finished, this , &Updater:: receivedAPI);
4652}
4753
48- void Updater::receivedUpdate (QNetworkReply *reply ) {
49- if (reply ->error () != QNetworkReply::NoError) {
50- emit error (reply ->errorString ());
54+ void Updater::receivedUpdate () {
55+ if (update ->error () != QNetworkReply::NoError) {
56+ emit error (update ->errorString ());
5157 return ;
5258 }
5359
54- QByteArray data = reply->readAll ();
55- reply->close ();
56- reply->deleteLater ();
60+ QByteArray data = update->readAll ();
61+ update->close ();
62+ update->deleteLater ();
63+ update = nullptr ;
5764
5865 QJsonDocument response = QJsonDocument::fromJson (data);
5966 QJsonObject robj = response.object ();
@@ -87,15 +94,16 @@ void Updater::receivedUpdate(QNetworkReply *reply) {
8794 }
8895}
8996
90- void Updater::receivedAPI (QNetworkReply *reply ) {
91- if (reply ->error () != QNetworkReply::NoError) {
92- emit error (reply ->errorString ());
97+ void Updater::receivedAPI () {
98+ if (api ->error () != QNetworkReply::NoError) {
99+ emit error (api ->errorString ());
93100 return ;
94101 }
95102
96- QByteArray data = reply->readAll ();
97- reply->close ();
98- reply->deleteLater ();
103+ QByteArray data = api->readAll ();
104+ api->close ();
105+ api->deleteLater ();
106+ api = nullptr ;
99107
100108 QJsonDocument response = QJsonDocument::fromJson (data);
101109 QJsonObject robj = response.object ();
@@ -104,7 +112,6 @@ void Updater::receivedAPI(QNetworkReply *reply) {
104112 return ;
105113 }
106114
107- QSettings settings;
108115 QString have = settings.value (QStringLiteral (" downloadId" )).toString ();
109116 version = robj[QStringLiteral (" downloadId" )].toString ();
110117
@@ -116,21 +123,22 @@ void Updater::receivedAPI(QNetworkReply *reply) {
116123 githash = QByteArray::fromHex (robj[QStringLiteral (" gitHash" )].toString ().toLatin1 ());
117124
118125 QNetworkRequest request (robj[QStringLiteral (" url" )].toString ());
119- QNetworkReply *dreply = net.get (request);
120- connect (dreply , &QNetworkReply::finished, [ this , dreply]() { this -> receivedBundle (dreply); } );
121- connect (dreply , &QNetworkReply::downloadProgress, this , &Updater::downloadProgress);
126+ bundle = net.get (request);
127+ connect (bundle , &QNetworkReply::finished, this , &Updater:: receivedBundle);
128+ connect (bundle , &QNetworkReply::downloadProgress, this , &Updater::downloadProgress);
122129 }
123130}
124131
125- void Updater::receivedBundle (QNetworkReply *reply ) {
126- if (reply ->error () != QNetworkReply::NoError) {
127- emit error (reply ->errorString ());
132+ void Updater::receivedBundle () {
133+ if (bundle ->error () != QNetworkReply::NoError) {
134+ emit error (bundle ->errorString ());
128135 return ;
129136 }
130137
131- QByteArray data = reply->readAll ();
132- reply->close ();
133- reply->deleteLater ();
138+ QByteArray data = bundle->readAll ();
139+ bundle->close ();
140+ bundle->deleteLater ();
141+ bundle = nullptr ;
134142
135143 if (data.size () != size) {
136144 emit error (tr (" Invalid Download" ));
@@ -161,9 +169,9 @@ void Updater::receivedBundle(QNetworkReply *reply) {
161169 if (extract (data, bin)) {
162170 emit installed ();
163171
164- QSettings settings;
165172 settings.setValue (QStringLiteral (" downloadId" ), version);
166173 settings.setValue (QStringLiteral (" gitHash" ), githash);
174+ settings.sync ();
167175 } else {
168176 emit error (tr (" Error extracting archive" ));
169177 }
0 commit comments