-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqmldownloader.cpp
232 lines (203 loc) · 7.65 KB
/
qmldownloader.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
/* Unvanquished Updater
* Copyright (C) Unvanquished Developers
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QDir>
#include <QApplication>
#include <QDebug>
#include <QMetaObject>
#include "qmldownloader.h"
#include "system.h"
static const QString UPDATER_BASE_URL("https://github.com/Unvanquished/updater/releases/download");
QmlDownloader::QmlDownloader(QString ariaLogFilename, QString connectUrl, Settings& settings) :
ariaLogFilename_(ariaLogFilename),
connectUrl_(connectUrl),
settings_(settings),
downloadSpeed_(0),
uploadSpeed_(0),
eta_(0),
totalSize_(0),
completedSize_(0),
worker_(nullptr),
state_(IDLE) {}
QmlDownloader::~QmlDownloader()
{
stopAria();
}
int QmlDownloader::downloadSpeed() const {
return downloadSpeed_;
}
int QmlDownloader::uploadSpeed() const {
return uploadSpeed_;
}
int QmlDownloader::eta() const {
return eta_.count();
}
int QmlDownloader::totalSize() const {
return totalSize_;
}
int QmlDownloader::completedSize() const {
return completedSize_;
}
void QmlDownloader::setDownloadSpeed(int speed) {
downloadSpeed_ = speed;
downloadTime_.addSpeed(speed);
emit downloadSpeedChanged(speed);
}
void QmlDownloader::setUploadSpeed(int speed) {
uploadSpeed_ = speed;
emit uploadSpeedChanged(speed);
}
void QmlDownloader::setTotalSize(int size) {
totalSize_ = size;
emit totalSizeChanged(size);
}
void QmlDownloader::setCompletedSize(int size) {
completedSize_ = size;
emit completedSizeChanged(size);
eta_ = std::chrono::seconds(downloadTime_.getTime(totalSize_ - completedSize_));
emit etaChanged(eta_.count());
}
void QmlDownloader::onDownloadEvent(int event)
{
switch (event) {
case aria2::EVENT_ON_BT_DOWNLOAD_COMPLETE:
if (state() != COMPLETED) {
qDebug() << "installUpdater in" << settings_.installPath();
if (!Sys::installUpdater(settings_.installPath())) {
emit fatalMessage("Error installing launcher");
return;
}
qDebug() << "Calling Sys::install";
if (Sys::installShortcuts()) {
emit statusMessage("Up to date");
} else {
emit statusMessage("Error installing shortcuts");
}
setState(COMPLETED);
setDownloadSpeed(0);
setUploadSpeed(0);
setCompletedSize(totalSize_);
stopAria();
}
break;
case aria2::EVENT_ON_DOWNLOAD_COMPLETE:
emit statusMessage("Torrent downloaded");
break;
case aria2::EVENT_ON_DOWNLOAD_ERROR:
emit fatalMessage("Error received while downloading");
break;
case aria2::EVENT_ON_DOWNLOAD_PAUSE:
emit statusMessage("Download paused");
break;
case aria2::EVENT_ON_DOWNLOAD_START:
emit statusMessage("Download started");
break;
case aria2::EVENT_ON_DOWNLOAD_STOP:
emit statusMessage("Download stopped");
break;
case DownloadWorker::ERROR_EXTRACTING:
emit fatalMessage("Error extracting update");
break;
}
}
void QmlDownloader::startUpdate(const QString& selectedInstallPath)
{
qDebug() << "Selected install path:" << selectedInstallPath;
if (!Sys::validateInstallPath(selectedInstallPath)) {
emit fatalMessage("You are running as root, which may cause the installation to"
" work incorrectly. Restart the program without using 'sudo'.");
return;
}
QDir dir(selectedInstallPath);
if (!dir.exists()) {
if (!dir.mkpath(dir.path())) {
emit fatalMessage(dir.path() + " does not exist and could not be created");
return;
}
}
if (!QFileInfo(selectedInstallPath).isWritable()) {
emit fatalMessage("Install dir not writable. Please select another");
return;
}
// Persist the install path only now that download has been initiated and we know the path is good
emit statusMessage("Installing to " + dir.canonicalPath());
if (settings_.installPath() != selectedInstallPath) {
qDebug() << "Clearing installed version because path was changed";
settings_.setInstalledVersion("");
}
settings_.setInstallPath(selectedInstallPath);
setState(DOWNLOADING);
worker_ = new DownloadWorker(ariaLogFilename_);
worker_->setDownloadDirectory(dir.canonicalPath().toStdString());
worker_->addTorrent("https://cdn.unvanquished.net/current.torrent");
worker_->moveToThread(&thread_);
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));
connect(worker_, SIGNAL(downloadSpeedChanged(int)), this, SLOT(setDownloadSpeed(int)));
connect(worker_, SIGNAL(uploadSpeedChanged(int)), this, SLOT(setUploadSpeed(int)));
connect(worker_, SIGNAL(totalSizeChanged(int)), this, SLOT(setTotalSize(int)));
connect(worker_, SIGNAL(completedSizeChanged(int)), this, SLOT(setCompletedSize(int)));
connect(&thread_, SIGNAL(started()), worker_, SLOT(download()));
thread_.start();
}
void QmlDownloader::toggleDownload(QString installPath)
{
qDebug() << "QmlDownloader::toggleDownload called";
if (state() == COMPLETED) return;
if (!worker_) {
startUpdate(installPath);
return;
}
QMetaObject::invokeMethod(worker_, "toggle");
setState(state() == DOWNLOADING ? PAUSED : DOWNLOADING);
}
void QmlDownloader::stopAria()
{
if (worker_) {
qDebug() << "Stopping downloader thread";
QMetaObject::invokeMethod(worker_, "stop");
thread_.quit();
thread_.wait();
worker_ = nullptr;
}
}
void QmlDownloader::startUpdaterUpdate(QString version)
{
QString url = UPDATER_BASE_URL + "/" + version + "/" + Sys::updaterArchiveName();
temp_dir_.reset(new QTemporaryDir());
worker_ = new DownloadWorker(ariaLogFilename_);
worker_->setDownloadDirectory(QDir(temp_dir_->path()).canonicalPath().toStdString());
worker_->setConnectUrl(connectUrl_);
worker_->addUpdaterUri(url.toStdString());
worker_->moveToThread(&thread_);
connect(&thread_, SIGNAL(finished()), worker_, SLOT(deleteLater()));
connect(worker_, SIGNAL(onDownloadEvent(int)), this, SLOT(onDownloadEvent(int)));
connect(worker_, SIGNAL(downloadSpeedChanged(int)), this, SLOT(setDownloadSpeed(int)));
connect(worker_, SIGNAL(uploadSpeedChanged(int)), this, SLOT(setUploadSpeed(int)));
connect(worker_, SIGNAL(totalSizeChanged(int)), this, SLOT(setTotalSize(int)));
connect(worker_, SIGNAL(completedSizeChanged(int)), this, SLOT(setCompletedSize(int)));
connect(&thread_, SIGNAL(started()), worker_, SLOT(download()));
thread_.start();
}
QmlDownloader::DownloadState QmlDownloader::state() const
{
return state_;
}
void QmlDownloader::setState(DownloadState state)
{
state_ = state;
emit stateChanged(state);
}