-
Notifications
You must be signed in to change notification settings - Fork 7
/
selfupdater.cpp
187 lines (128 loc) · 4.8 KB
/
selfupdater.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
#include "selfupdater.h"
#include "ui_selfupdater.h"
#include "mainwindow.h"
#include <QUrl>
#include <QNetworkRequest>
#include <QMessageBox>
#include <QNetworkReply>
#include <QCloseEvent>
SelfUpdater::SelfUpdater(bool silent, QWidget *parent) :
QDialog(parent),
ui(new Ui::SelfUpdater), selfUpdateNetworkManager(this) {
ui->setupUi(this);
this->silent = silent;
allowClose = true;
}
SelfUpdater::~SelfUpdater() {
delete ui;
}
int SelfUpdater::execUpdate() {
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(1);
ui->label->setText("Checking for updates...");
connect(&selfUpdateNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadVersionFinished(QNetworkReply*)));
QNetworkRequest request(MainWindow::selfUpdateUrl);
selfUpdateNetworkManager.get(request);
return exec();
}
void SelfUpdater::silentCheck() {
connect(&selfUpdateNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadVersionFinished(QNetworkReply*)));
QNetworkRequest request(MainWindow::selfUpdateUrl);
selfUpdateNetworkManager.get(request);
//reply->
}
void SelfUpdater::closeEvent(QCloseEvent* event) {
if (!allowClose)
event->ignore();
else
QDialog::closeEvent(event);
}
void SelfUpdater::downloadVersionFinished(QNetworkReply* reply) {
ui->progressBar->setValue(1);
disconnect(&selfUpdateNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadVersionFinished(QNetworkReply*)));
if (reply->error() != QNetworkReply::NoError) {
if (!silent)
QMessageBox::warning(this, "ERROR", reply->errorString());
done(0);
return;
}
QString data = reply->readAll();
if (!data.contains(";")) {
if (!silent)
QMessageBox::warning(this, "ERROR", "Could not check for newest version, unknown format!");
done(0);
return;
}
QStringList list = data.split(";");
if (list.size() < 2) {
if (!silent)
QMessageBox::warning(this, "ERROR", "Could not check for newest version, unknown format!");
done(0);
return;
}
if (list.at(1).toDouble() <= MainWindow::version.toDouble()) {
if (!silent)
QMessageBox::information(this, "Version", "Launcher already on latest version");
//emit finished(0);
done(0);
return;
} else {
if (silent) {
if (QMessageBox::question(this, "Update", "A new version of the launchpad has been found, do you want to update?") != QMessageBox::Yes)
return;
}
}
ui->label->setText("Downloading new version...");
ui->progressBar->setValue(0);
ui->progressBar->setMaximum(0);
connect(&selfUpdateNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadNewVersionFinished(QNetworkReply*)));
QNetworkReply* newReply = selfUpdateNetworkManager.get(QNetworkRequest(list.at(0)));
connect(newReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
if (silent)
exec();
}
void SelfUpdater::downloadNewVersionFinished(QNetworkReply* reply) {
ui->progressBar->setValue(ui->progressBar->maximum());
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::warning(this, "ERROR", reply->errorString());
done(0);
return;
}
qDebug() << "finished downloading new version";
int bufferSize = 8192 * 2;
char* buffer = (char*) malloc(bufferSize);
QFile fileObject("setup.exe");
//fileObject.set
if (!fileObject.open(QIODevice::WriteOnly)) {
if (!silent)
QMessageBox::warning(this, "ERROR", "Could not open to write downloaded file to disk!");
free(buffer);
done(0);
return;
}
int read = 0;
while ((read = reply->read(buffer, bufferSize)) > 0) {
if (fileObject.write(buffer, read) == -1) {
if (!silent)
QMessageBox::warning(this, "ERROR", "Could not write downloaded file to disk!");
free(buffer);
done(0);
return;
}
}
fileObject.close();
free(buffer);
bool startRes = QProcess::startDetached("setup.exe");
if (startRes)
QApplication::quit();
done(1);
}
void SelfUpdater::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
//qDebug() << " progress " << ui->progressBar->value() << " " << bytesReceived << " " << bytesTotal;
if (bytesTotal <= 0)
return;
if (ui->progressBar->value() == 0) {
ui->progressBar->setMaximum(bytesTotal);
}
ui->progressBar->setValue(bytesReceived);
}