-
Notifications
You must be signed in to change notification settings - Fork 6
/
updater.cpp
281 lines (252 loc) · 8.18 KB
/
updater.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <QApplication>
#include <QThread>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcess>
#include <QTemporaryDir>
#include "updater.h"
#include "globals.h"
#include "downloader.h"
#include "zipmanager.h"
namespace Yate {
Updater * Updater::instance_ = nullptr;
QThread * Updater::thread_ = nullptr;
Updater *Updater::getInstance(QObject *)
{
if (instance_ == nullptr) {
instance_ = new Updater;
thread_ = new QThread;
instance_->moveToThread(thread_);
thread_->start();
}
return instance_;
}
Updater::Updater(QObject *parent)
: QObject{parent}, manager_(new QNetworkAccessManager(this)), downloader_(new Downloader(this))
{
setStatus(UpdaterStatus::Unknown);
connect(manager_, &QNetworkAccessManager::finished, this, &Updater::onManagerFinished);
connect(downloader_, &Downloader::onDownloadFinished, this, &Updater::onDownloadFinished);
connect(downloader_, &Downloader::onDownloadFailed, this, &Updater::onDownloadFailed);
}
void Updater::setStatus(UpdaterStatus newStatus)
{
status_ = newStatus;
switch (status_) {
case UpdaterStatus::Unknown: {
emit onBusyUpdate(false);
emit updateStatusUpdate("N/A");
break;
}
case UpdaterStatus::CheckFailed: {
emit updateStatusUpdate("Check Failed");
emit onBusyUpdate(false);
break;
}
case UpdaterStatus::Checking: {
emit updateStatusUpdate("Checking");
emit onBusyUpdate(true);
break;
}
case UpdaterStatus::UpdateFailed: {
emit updateStatusUpdate("Update Failed");
emit onBusyUpdate(false);
break;
}
case UpdaterStatus::UpdateAvailable: {
emit onBusyUpdate(false);
emit updateStatusUpdate("Update Available");
emit updateAvailable();
break;
}
case UpdaterStatus::Downloading: {
emit onBusyUpdate(true);
emit updateStatusUpdate("Downloading..");
break;
}
case UpdaterStatus::Downloaded: {
emit onBusyUpdate(true);
emit updateStatusUpdate("Downloaded");
break;
}
case UpdaterStatus::Installing: {
emit onBusyUpdate(true);
emit updateStatusUpdate("Installing...");
break;
}
case UpdaterStatus::PendingRestart: {
emit onBusyUpdate(false);
emit updateStatusUpdate("Pending Restart");
break;
}
case UpdaterStatus::UpToDate: {
emit onBusyUpdate(false);
emit updateStatusUpdate("Up-to-date");
break;
}
}
}
void Updater::downloadUpdate()
{
qDebug() << "Downloading update.";
setStatus(UpdaterStatus::Downloading);
downloader_->download(downloadUrl_, downloadHash_);
}
const QString &Updater::latestVersion() const
{
return latestVersion_;
}
void Updater::setLatestVersion(const QString &newLatestVersion)
{
latestVersion_ = newLatestVersion;
}
UpdaterStatus Updater::status() const
{
return status_;
}
QVector<int> Updater::parseVersion(QString ver)
{
QVector<int> parts;
auto verDiv = ver.split(".");
while (verDiv.size() >= 4) {
verDiv.pop_back();
}
for(auto &p: verDiv) {
parts.push_back(p.toInt());
}
return parts;
}
QString Updater::getVersion() const
{
QString ver = QCoreApplication::applicationVersion();
if (ver.count(".") == 3) {
auto verDiv = ver.split(".");
verDiv.pop_back();
ver = verDiv.join(".");
}
#ifdef QT_DEBUG
ver = ver + ".d";
#endif
return ver;
}
void Updater::checkForUpdate()
{
qDebug() << "Checking for update.";
setStatus(UpdaterStatus::Checking);
QNetworkRequest request(QUrl(SETTINGS_URL_API_CHECK_VERSION));
manager_->get(request);
}
void Updater::startUpdate()
{
if (status() != UpdaterStatus::UpdateAvailable) {
return;
}
downloadUpdate();
}
void Updater::onDownloadFinished(QString filePath, QString) {
qDebug() << "Downloaded update.";
setStatus(UpdaterStatus::Downloaded);
QString selfPath = QCoreApplication::applicationFilePath();
QString selfDir = QCoreApplication::applicationDirPath();
QTemporaryDir extractionDir;
QStringList toRmList;
if (!extractionDir.isValid()) {
setStatus(UpdaterStatus::UpdateFailed);
qDebug() << "Update failed, failed to create extraction directory.";
emit errorOccurred("Update failed.");
return;
}
extractionDir.setAutoRemove(false);
ZipManager zip;
if (!zip.unzip(filePath, extractionDir.path())) {
setStatus(UpdaterStatus::UpdateFailed);
qDebug() << "Update failed, failed to unzip content.";
emit errorOccurred("Update failed.");
return;
}
QString newExec;
for(auto &f: QDir(extractionDir.path()).entryList(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot)) {
QFile extractedFile(extractionDir.path() + QDir::separator() + f);
if (f.endsWith(".exe")) {
newExec = extractedFile.fileName();
}
QString existingFilePath = selfDir + QDir::separator() + f;
QString tempPath = existingFilePath + "~";
if (QFileInfo::exists(tempPath)) {
if(!QFile::remove(tempPath)) {
setStatus(UpdaterStatus::UpdateFailed);
qDebug() << "Update failed, failed to remove existing temp path.";
emit errorOccurred("Update failed.");
return;
}
}
if (QFileInfo::exists(existingFilePath)) {
QFile existingFile(existingFilePath);
if (!existingFile.rename(tempPath)) {
setStatus(UpdaterStatus::UpdateFailed);
qDebug() << "Update failed, failed to replace existing file.";
emit errorOccurred("[1] Update failed, you may need to redownload the tool from " + SETTINGS_WEBSITE_HTTPS);
return;
}
toRmList.push_back(tempPath);
}
if (!extractedFile.rename(existingFilePath)) {
setStatus(UpdaterStatus::UpdateFailed);
qDebug() << "Update failed, failed to move extracted file.";
emit errorOccurred("[2] Update failed, you may need to redownload the tool from " + SETTINGS_WEBSITE_HTTPS);
return;
}
}
QStringList argList({"update", QString::number(QCoreApplication::applicationPid()),latestVersion_});
for(auto &rm: toRmList) {
argList.push_back(rm);
}
setStatus(UpdaterStatus::PendingRestart);
qDebug() << "Update installed, pending restart.";
QProcess::startDetached(selfPath, argList);
QCoreApplication::exit(0);
}
void Updater::onDownloadFailed(QString err) {
qDebug() << "Update download failed.";
setStatus(UpdaterStatus::UpdateFailed);
emit errorOccurred(err);
}
void Updater::onManagerFinished(QNetworkReply *reply)
{
if (reply->error()) {
setStatus(UpdaterStatus::CheckFailed);
qDebug() << "Update check failed, network error.";
emit errorOccurred("Update check failed: " + reply->errorString());
} else {
QString data = QString(reply->readAll());
QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
setLatestVersion(jsonObject.value("latestStableVersion").toString());
downloadUrl_ = jsonObject.value("download").toString();
downloadHash_ = jsonObject.value("md5").toString();
auto latestVerParts = parseVersion(latestVersion());
auto currentVerParts = parseVersion( QCoreApplication::applicationVersion());
bool isUpToDate = true;
for(int i = 0; i < latestVerParts.size(); i++) {
if(latestVerParts[i] > currentVerParts[i]) {
isUpToDate = false;
break;
} else if (currentVerParts[i] > latestVerParts[i]) {
break;
}
}
if (isUpToDate) {
qDebug() << "Current version is up-to-date.";
setStatus(UpdaterStatus::UpToDate);
} else {
qDebug() << "Update available.";
setStatus(UpdaterStatus::UpdateAvailable);
}
}
}
}