-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathwinupdatermain.cpp
220 lines (205 loc) · 7.96 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (c) 2018 Sylvain "Skarsnik" Colinet.
*
* This file is part of the QUsb2Snes project.
* (see https://github.com/Skarsnik/QUsb2snes).
*
* QUsb2Snes 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 3 of the License, or
* (at your option) any later version.
*
* QUsb2Snes 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 QUsb2Snes. If not, see <https://www.gnu.org/licenses/>.
*/
#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>
#include <QMessageBox>
const QString githubUrl = "https://api.github.com/repos/Skarsnik/QUsb2snes/";
int step = 0;
QNetworkAccessManager* manager = new QNetworkAccessManager();
QNetworkReply* dlReply;
QProgressBar* pb;
QLabel* label;
QList<QNetworkRequest> listReq;
QStringList listReqExe;
QStringList exeToDL = {"QUsb2Snes.exe", "QFile2Snes.exe"};
QString newQUsbVersion;
QMap<QString, QString> locations;
static QTextStream logfile;
static QTextStream cout(stdout);
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QTextStream* log = &logfile;
#ifdef QT_NO_DEBUG
QString logString = QString("%6 %5 - %7: %1").arg(localMsg.constData()).arg(context.category, 20).arg(QDateTime::currentDateTime().toString(Qt::ISODate));
#else
QString logString = QString("%6 %5 - %7: %1 \t(%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function).arg(context.category, 20).arg(QDateTime::currentDateTime().toString(Qt::ISODate));
#endif
switch (type)
{
case QtDebugMsg:
*log << logString.arg("Debug");
break;
case QtCriticalMsg:
*log << logString.arg("Critical");
break;
case QtWarningMsg:
*log << logString.arg("Warning");
break;
case QtFatalMsg:
*log << logString.arg("Fatal");
*log<< "\n"; log->flush();
QMessageBox::critical(nullptr, QObject::tr("Critical error"), msg);
qApp->exit(1);
break;
case QtInfoMsg:
*log << logString.arg("Info");
break;
}
*log << "\n";
log->flush();
cout << QString("%1 : %2").arg(context.category, 20).arg(msg) << "\n";
cout.flush();
}
void startQUsb2Snes()
{
qDebug() << "Starting QUsb2Snes";
label->setText(QObject::tr("Starting QUsb2Snes"));
QProcess::startDetached(qApp->applicationDirPath() + "/QUsb2Snes.exe", QStringList() << "-updated" << newQUsbVersion);
qDebug() << "starting exit";
QTimer::singleShot(1000, [=] {qApp->exit(0);});
}
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();
newQUsbVersion = jArr.at(0).toObject().value("tag_name").toString();
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");
listReq.append(req);
listReqExe.append("QUsb2Snes.exe");
qDebug() << "Found QUsb2Snes.exe asset";
}
if (value.toObject().value("name").toString() == "QFile2Snes.exe")
{
QNetworkRequest req(QUrl(value.toObject().value("browser_download_url").toString()));
req.setRawHeader("Accept", "application/octet-stream");
qDebug() << "Found QFile2Snes.exe asset";
listReq.append(req);
listReqExe.append("QFile2Snes.exe");
}
}
step = 1;
label->setText(QString(QObject::tr("Downloading new %1")).arg(listReqExe.first()));
dlReply = manager->get(listReq.takeFirst());
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);
});
return ;
}
if (step >= 1)
{
QString fileStr = listReqExe.takeFirst();
QFile file(locations[fileStr] + "/" + fileStr);
qDebug() << "Creating " << file.fileName();
pb->setValue(100);
label->setText(QString(QObject::tr("Writing %1")).arg(fileStr));
if (file.open(QIODevice::WriteOnly))
{
qDebug() << data.size();
file.write(data);
qDebug() << fileStr << " written";
file.flush();
file.close();
} else {
qDebug() << "Can't write file";
QMessageBox::critical(nullptr, QString(QObject::tr("Error writing %1")).arg(fileStr),
QString(QObject::tr("There was an error writing %1, check if %1 is not running and restart the update (or the updater)")).arg(fileStr));
qApp->exit(1);
}
if (listReq.isEmpty()) {
startQUsb2Snes();
return ;
}
pb->setValue(0);
dlReply = manager->get(listReq.takeFirst());
label->setText(QString(QObject::tr("Downloading new %1")).arg(listReqExe.first()));
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);
});
}
}
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(1500, &startDelayed);
locations["QUsb2Snes.exe"] = qApp->applicationDirPath();
locations["QFile2Snes.exe"] = qApp->applicationDirPath() + "/apps/QFile2Snes/";
QFile mlog(qApp->applicationDirPath() + "/updaterlog.txt");
if (mlog.open(QIODevice::WriteOnly | QIODevice::Text))
{
logfile.setDevice(&mlog);
qInstallMessageHandler(myMessageOutput);
}
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();
}