12 #include <QMessageBox>
13 #include <QNetworkReply>
14 #include <QDesktopServices>
15 #include <QNetworkAccessManager>
26 m_ui =
new Ui::Downloader;
30 m_manager =
new QNetworkAccessManager();
36 m_useCustomProcedures =
false;
37 m_mandatoryUpdate =
false;
40 m_downloadDir.setPath(QDir::homePath() +
"/Downloads/");
43 setWindowIcon(QIcon());
44 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
47 m_ui->openButton->setEnabled(
false);
48 m_ui->openButton->setVisible(
false);
49 connect(m_ui->stopButton, SIGNAL(clicked()),
this, SLOT(cancelDownload()));
50 connect(m_ui->openButton, SIGNAL(clicked()),
this, SLOT(installUpdate()));
53 setFixedSize(minimumSizeHint());
70 return m_useCustomProcedures;
91 m_ui->progressBar->setValue(0);
92 m_ui->stopButton->setText(tr(
"Stop"));
93 m_ui->downloadLabel->setText(tr(
"Downloading updates"));
94 m_ui->timeLabel->setText(tr(
"Time remaining") +
": " + tr(
"unknown"));
97 QNetworkRequest request(url);
98 if (!m_userAgentString.isEmpty())
99 request.setRawHeader(
"User-Agent", m_userAgentString.toUtf8());
102 m_reply = m_manager->get(request);
103 m_startTime = QDateTime::currentDateTime().toSecsSinceEpoch();
106 if (!m_downloadDir.exists())
107 m_downloadDir.mkpath(
".");
110 QFile::remove(m_downloadDir.filePath(m_fileName));
111 QFile::remove(m_downloadDir.filePath(m_fileName +
PARTIAL_DOWN));
114 connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(updateProgress(qint64, qint64)));
115 connect(m_reply, SIGNAL(finished()),
this, SLOT(finished()));
116 connect(m_reply, SIGNAL(redirected(QUrl)),
this, SLOT(
startDownload(QUrl)));
128 if (m_fileName.isEmpty())
129 m_fileName =
"QSU_Update.bin";
137 m_userAgentString = agent;
140 void Downloader::finished()
143 QFile::rename(m_downloadDir.filePath(m_fileName +
PARTIAL_DOWN), m_downloadDir.filePath(m_fileName));
159 void Downloader::openDownload()
161 if (!m_fileName.isEmpty())
163 QDesktopServices::openUrl(QUrl::fromLocalFile(m_downloadDir.filePath(m_fileName)));
167 QMessageBox::critical(
this, tr(
"Error"), tr(
"Cannot find downloaded update!"), QMessageBox::Close);
179 void Downloader::installUpdate()
185 m_ui->stopButton->setText(tr(
"Close"));
186 m_ui->downloadLabel->setText(tr(
"Download complete!"));
187 m_ui->timeLabel->setText(tr(
"The installer will open separately") +
"...");
191 box.setIcon(QMessageBox::Question);
192 box.setDefaultButton(QMessageBox::Ok);
193 box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
194 box.setInformativeText(tr(
"Click \"OK\" to begin installing the update"));
196 QString text = tr(
"In order to install the update, you may need to "
197 "quit the application.");
199 if (m_mandatoryUpdate)
200 text = tr(
"In order to install the update, you may need to "
201 "quit the application. This is a mandatory update, exiting now will close the application");
203 box.setText(
"<h3>" + text +
"</h3>");
206 if (box.exec() == QMessageBox::Ok)
214 if (m_mandatoryUpdate)
216 QString installerFile = m_downloadDir.filePath(m_fileName);
217 QMessageBox::information(
this,
"", tr(
"Please install from <i>%1</i> mannually!").arg(installerFile));
218 QApplication::quit();
221 m_ui->openButton->setEnabled(
true);
222 m_ui->openButton->setVisible(
true);
223 m_ui->timeLabel->setText(tr(
"Click the \"Open\" button to "
224 "apply the update"));
232 void Downloader::cancelDownload()
234 if (!m_reply->isFinished())
237 box.setWindowTitle(tr(
"Updater"));
238 box.setIcon(QMessageBox::Question);
239 box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
241 QString text = tr(
"Are you sure you want to cancel the download?");
242 if (m_mandatoryUpdate)
244 text = tr(
"Are you sure you want to cancel the download? This is a mandatory update, exiting now will close "
249 if (box.exec() == QMessageBox::Yes)
253 if (m_mandatoryUpdate)
254 QApplication::quit();
259 if (m_mandatoryUpdate)
260 QApplication::quit();
269 void Downloader::saveFile(qint64 received, qint64 total)
275 QUrl url = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
283 QFile file(m_downloadDir.filePath(m_fileName +
PARTIAL_DOWN));
286 file.write(m_reply->readAll());
296 void Downloader::calculateSizes(qint64 received, qint64 total)
299 QString receivedSize;
302 totalSize = tr(
"%1 bytes").arg(total);
304 else if (total < 1048576)
305 totalSize = tr(
"%1 KB").arg(round(total / 1024));
308 totalSize = tr(
"%1 MB").arg(round(total / 1048576));
311 receivedSize = tr(
"%1 bytes").arg(received);
313 else if (received < 1048576)
314 receivedSize = tr(
"%1 KB").arg(received / 1024);
317 receivedSize = tr(
"%1 MB").arg(received / 1048576);
319 m_ui->downloadLabel->setText(tr(
"Downloading updates") +
" (" + receivedSize +
" " + tr(
"of") +
" " + totalSize
327 void Downloader::updateProgress(qint64 received, qint64 total)
331 m_ui->progressBar->setMinimum(0);
332 m_ui->progressBar->setMaximum(100);
333 m_ui->progressBar->setValue((received * 100) / total);
335 calculateSizes(received, total);
336 calculateTimeRemaining(received, total);
337 saveFile(received, total);
342 m_ui->progressBar->setMinimum(0);
343 m_ui->progressBar->setMaximum(0);
344 m_ui->progressBar->setValue(-1);
345 m_ui->downloadLabel->setText(tr(
"Downloading Updates") +
"...");
346 m_ui->timeLabel->setText(QString(
"%1: %2").arg(tr(
"Time Remaining")).arg(tr(
"Unknown")));
358 void Downloader::calculateTimeRemaining(qint64 received, qint64 total)
360 uint difference = QDateTime::currentDateTime().toSecsSinceEpoch() - m_startTime;
365 qreal timeRemaining = (total - received) / (received / difference);
367 if (timeRemaining > 7200)
369 timeRemaining /= 3600;
370 int hours = int(timeRemaining + 0.5);
373 timeString = tr(
"about %1 hours").arg(hours);
375 timeString = tr(
"about one hour");
378 else if (timeRemaining > 60)
381 int minutes = int(timeRemaining + 0.5);
384 timeString = tr(
"%1 minutes").arg(minutes);
386 timeString = tr(
"1 minute");
389 else if (timeRemaining <= 60)
391 int seconds = int(timeRemaining + 0.5);
394 timeString = tr(
"%1 seconds").arg(
seconds);
396 timeString = tr(
"1 second");
399 m_ui->timeLabel->setText(tr(
"Time remaining") +
": " + timeString);
406 qreal Downloader::round(
const qreal &input)
408 return static_cast<qreal
>(roundf(
static_cast<float>(input) * 100) / 100);
413 return m_downloadDir.absolutePath();
428 m_mandatoryUpdate = mandatory_update;
440 m_useCustomProcedures = custom;
static const QString PARTIAL_DOWN(".part")
Downloader(QWidget *parent=nullptr)
void setUserAgentString(const QString &agent)
bool useCustomInstallProcedures() const
void downloadFinished(const QString &url, const QString &filepath)
QString downloadDir() const
void startDownload(const QUrl &url)
void setDownloadDir(const QString &downloadDir)
void setFileName(const QString &file)
void setMandatoryUpdate(const bool mandatory_update)
void setUseCustomInstallProcedures(const bool custom)
void setUrlId(const QString &url)
Tensor Append(const Tensor &self, const Tensor &other, const utility::optional< int64_t > &axis)
Appends the two tensors, along the given axis into a new tensor. Both the tensors must have same data...