forked from Skarsnik/QUsb2snes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinupdatermain.cpp
106 lines (99 loc) · 3.47 KB
/
winupdatermain.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
#include <QApplication>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QNetworkReply>
#include <QFile>
#include <QProcess>
#include <QProgressBar>
#include <QLabel>
#include <QVBoxLayout>
#include <QTimer>
#include <QIcon>
const QString githubUrl = "https://api.github.com/repos/Skarsnik/QUsb2snes/";
int step = 0;
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkReply* dlReply;
QProgressBar* pb;
QLabel* label;
void requestFinished(QNetworkReply* reply)
{
QByteArray data = reply->readAll();
qDebug() << reply->error();
if (step == 0)
{
QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonArray jArr = doc.array();
QJsonArray assets = jArr.at(0).toObject().value("assets").toArray();
foreach(const QJsonValue& value, assets)
{
if (value.toObject().value("name").toString() == "QUsb2Snes.exe")
{
qDebug() << "Downloading " << value.toObject().value("browser_download_url").toString();
label->setText(QString(QObject::tr("Downloading new QUsb2Snes.exe %1")).arg(
jArr.at(0).toObject().value("tag_name").toString()));
QNetworkRequest req(QUrl(value.toObject().value("browser_download_url").toString()));
req.setRawHeader("Accept", "application/octet-stream");
dlReply = manager->get(req);
QObject::connect(dlReply, &QNetworkReply::redirected, [=] {
qDebug() << "DL reply redirected";
});
QObject::connect(dlReply, &QNetworkReply::downloadProgress, [=](qint64 bytesRcv, qint64 bytesTotal)
{
qDebug() << 20 + (bytesRcv / bytesTotal) * 80;
pb->setValue(20 + (bytesRcv / bytesTotal) * 80);
});
qDebug() << "Found QUsb2Snes.exe asset";
step = 1;
return ;
}
}
qApp->exit(1);
}
if (step == 1)
{
QFile file("QUsb2Snes.exe");
pb->setValue(100);
label->setText(QObject::tr("Writing QUsb2Snes.exe"));
if (file.open(QIODevice::WriteOnly))
{
qDebug() << data.size();
file.write(data);
qDebug() << "QUsb2Snes written";
file.flush();
file.close();
label->setText(QObject::tr("Starting QUsb2Snes"));
QProcess::startDetached(qApp->applicationDirPath() + "/QUsb2Snes.exe");
QTimer::singleShot(500, [=] {qApp->exit(0);});
} else {
qApp->exit(1);
}
}
}
void startDelayed()
{
manager->get(QNetworkRequest(QUrl(githubUrl + "releases")));
}
int main(int ac, char* ag[])
{
QApplication app(ac, ag);
QObject::connect(manager, &QNetworkAccessManager::finished, &requestFinished);
manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
QTimer::singleShot(1000, &startDelayed);
label = new QLabel();
pb = new QProgressBar();
label->setText(QObject::tr("Checking GitHub for the last release"));
pb->setTextVisible(false);
pb->setValue(20);
QVBoxLayout layout;
QWidget win;
win.setWindowTitle("QUsb2Snes Windows Updater");
win.setWindowIcon(QIcon(":/icon64x64.ico"));
win.setLayout(&layout);
layout.addWidget(label);
layout.addWidget(pb);
win.show();
app.exec();
}