Small improvements

- Minor UI changes
- Implemented a mechanism to calculate the download time remaining
- Fixed a bug where the updater icon would not be displayed.
This commit is contained in:
Alex Spataru 2014-11-08 20:24:12 -06:00
parent 2ad0c796fc
commit bbe47f426a
7 changed files with 153 additions and 106 deletions

View File

@ -27,8 +27,7 @@ win32* {
LIBS += -L$$PWD/dependencies/win32/ -llibeay32 LIBS += -L$$PWD/dependencies/win32/ -llibeay32
} }
RESOURCES += \ RESOURCES += $$PWD/res/qsu_resources.qrc
$$PWD/res/res.qrc
FORMS += \ FORMS += \
$$PWD/src/dialogs/download_dialog.ui $$PWD/src/dialogs/download_dialog.ui

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.6 KiB

View File

@ -1,5 +1,5 @@
<RCC> <RCC>
<qresource prefix="/icons"> <qresource prefix="/icons">
<file>internet_icon.png</file> <file alias="update.png">update.png</file>
</qresource> </qresource>
</RCC> </RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -42,10 +42,12 @@ void DownloadDialog::beginDownload(const QUrl &url)
ui->progressBar->setValue(0); ui->progressBar->setValue(0);
ui->installButton->setEnabled(false); ui->installButton->setEnabled(false);
ui->cancelButton->setText(tr("Cancel")); ui->cancelButton->setText(tr("Cancel"));
ui->downloadLabel->setText(tr("Downloading update...")); ui->downloadLabel->setText(tr("Downloading updates"));
ui->timeLabel->setText(tr("Time remaining") + ": " + tr("unknown"));
// Begin the download // Begin the download
m_reply = m_manager->get(QNetworkRequest(url)); m_reply = m_manager->get(QNetworkRequest(url));
m_start_time = QDateTime::currentDateTime().toTime_t();
// Update the progress bar value automatically // Update the progress bar value automatically
connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)),
@ -80,21 +82,25 @@ void DownloadDialog::openDownload()
void DownloadDialog::cancelDownload() void DownloadDialog::cancelDownload()
{ {
// Cancel download QMessageBox _message;
m_reply->abort(); _message.setWindowTitle(tr("Updater"));
_message.setIcon(QMessageBox::Question);
_message.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
_message.setText(tr("Are you sure you want to cancel the download?"));
// Close the dialog if (_message.exec() == QMessageBox::Yes) {
close(); hide();
m_reply->abort();
}
} }
void DownloadDialog::downloadFinished() void DownloadDialog::downloadFinished()
{ {
// Setup the UI ui->timeLabel->setText(NULL);
ui->cancelButton->setText(tr("Close"));
ui->installButton->setEnabled(true); ui->installButton->setEnabled(true);
ui->cancelButton->setText(tr("Close"));
ui->downloadLabel->setText(tr("Download complete!")); ui->downloadLabel->setText(tr("Download complete!"));
// Write the file
QByteArray data = m_reply->readAll(); QByteArray data = m_reply->readAll();
if (!data.isEmpty()) { if (!data.isEmpty()) {
@ -102,18 +108,15 @@ void DownloadDialog::downloadFinished()
QStringList list = m_reply->url().toString().split("/"); QStringList list = m_reply->url().toString().split("/");
QFile file(QDir::tempPath() + "/" + list.at(list.count() - 1)); QFile file(QDir::tempPath() + "/" + list.at(list.count() - 1));
// Write download data to the opened file
if (file.open(QIODevice::WriteOnly)) { if (file.open(QIODevice::WriteOnly)) {
file.write(data); file.write(data);
m_path = file.fileName(); m_path = file.fileName();
} }
// Show a warning if we cannot open the file for writting
else { else {
qWarning() << "QSimpleUpdater: cannot write downloaded data!"; qWarning() << "QSimpleUpdater: cannot write downloaded data!";
} }
// Close the file
file.close(); file.close();
} }
@ -125,24 +128,19 @@ void DownloadDialog::downloadFinished()
void DownloadDialog::updateProgress(qint64 received, qint64 total) void DownloadDialog::updateProgress(qint64 received, qint64 total)
{ {
// We know the size of the download, so we can calculate the progress.... // We know the size of the download, so we can calculate the progress....
if (total > 0) { if (total > 0 && received > 0) {
ui->progressBar->setMinimum(0); ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(100); ui->progressBar->setMaximum(100);
// Calculate and show download progress
int _progress = (int) ((received * 100) / total); int _progress = (int) ((received * 100) / total);
ui->progressBar->setValue(_progress); ui->progressBar->setValue(_progress);
ui->downloadLabel->setText(tr("Downloading update (%1%)...").arg(_progress));
// Get size information
QString _total_string; QString _total_string;
QString _received_string; QString _received_string;
float _total = total; float _total = total;
float _received = received; float _received = received;
// Calculate the lenght unit for
// the total size of the download
if (_total < 1024) { if (_total < 1024) {
_total_string = tr("%1 bytes").arg(_total); _total_string = tr("%1 bytes").arg(_total);
} else if (_total < 1024 * 1024) { } else if (_total < 1024 * 1024) {
@ -153,8 +151,6 @@ void DownloadDialog::updateProgress(qint64 received, qint64 total)
_total_string = tr("%1 MB").arg(_total); _total_string = tr("%1 MB").arg(_total);
} }
// Calculate the lenght unit for
// the received data of the download
if (_received < 1024) { if (_received < 1024) {
_received_string = tr("%1 bytes").arg(_received); _received_string = tr("%1 bytes").arg(_received);
} else if (received < 1024 * 1024) { } else if (received < 1024 * 1024) {
@ -165,19 +161,41 @@ void DownloadDialog::updateProgress(qint64 received, qint64 total)
_received_string = tr("%1 MB").arg(_received); _received_string = tr("%1 MB").arg(_received);
} }
// Update the progress label ui->downloadLabel->setText(tr("Downloading updates") + " (" +
ui->progressLabel->setText(_received_string + " " + tr("of") + " " + _total_string); _received_string + " " + tr("of") + " " +
_total_string + ")");
uint _diff = QDateTime::currentDateTime().toTime_t() - m_start_time;
if (_diff > 0) {
QString _time_string;
float _time_remaining = total / (received / _diff);
if (_time_remaining > 7200) {
_time_remaining /= 3600;
_time_string = tr("About %1 hours").arg(int(_time_remaining + 0.5));
}
else if (_time_remaining > 60) {
_time_remaining /= 60;
_time_string = tr("About %1 minutes").arg(int(_time_remaining + 0.5));
}
else if (_time_remaining <= 60) {
_time_string = tr("%1 seconds").arg(int(_time_remaining + 0.5));
}
ui->timeLabel->setText(tr("Time remaining") + ": " + _time_string);
}
} }
// We do not know the size of the download, so we improvise... // We do not know the size of the download, so we improvise...
else { else {
// Enable a marquee animation
ui->progressBar->setValue(-1); ui->progressBar->setValue(-1);
ui->progressBar->setMinimum(0); ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(0); ui->progressBar->setMaximum(0);
ui->downloadLabel->setText(tr("Downloading updates"));
ui->downloadLabel->setText(tr("Downloading update...")); ui->timeLabel->setText(tr("Time remaining") + ": " + tr("Unknown"));
} }
} }
@ -188,6 +206,5 @@ void DownloadDialog::ignoreSslErrors (QNetworkReply *reply, const QList<QSslErro
float DownloadDialog::roundNumber(const float &input) float DownloadDialog::roundNumber(const float &input)
{ {
// Round the input number to two decimal places
return roundf(input * 100) / 100; return roundf(input * 100) / 100;
} }

View File

@ -2,8 +2,11 @@
#define DOWNLOAD_DIALOG_H #define DOWNLOAD_DIALOG_H
#include <QDir> #include <QDir>
#include <QIcon>
#include <QDebug> #include <QDebug>
#include <QWidget> #include <QWidget>
#include <QDateTime>
#include <QMessageBox>
#include <QNetworkReply> #include <QNetworkReply>
#include <QDesktopServices> #include <QDesktopServices>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
@ -39,6 +42,8 @@ private:
QNetworkReply *m_reply; QNetworkReply *m_reply;
QNetworkAccessManager *m_manager; QNetworkAccessManager *m_manager;
uint m_start_time;
float roundNumber(const float &input); float roundNumber(const float &input);
}; };

File diff suppressed because one or more lines are too long