Commit bf35b0ca authored by Ming Rui Zhang's avatar Ming Rui Zhang Committed by Andreas Traczyk

downloadmanager: add cancel ability

- change download speed display to download progress
- add cancel ability to make user enable to abort update or
  download

Change-Id: Ibb725897a1cf0c636186275541f25322819d5517
parent 5bf134aa
...@@ -53,8 +53,6 @@ void DownloadManager::downloadFile(const QUrl& fileUrl, ...@@ -53,8 +53,6 @@ void DownloadManager::downloadFile(const QUrl& fileUrl,
return; return;
} }
downloadTime_.start();
QNetworkRequest request(fileUrl); QNetworkRequest request(fileUrl);
currentDownload_ = manager_.get(request); currentDownload_ = manager_.get(request);
...@@ -67,6 +65,10 @@ void DownloadManager::downloadFile(const QUrl& fileUrl, ...@@ -67,6 +65,10 @@ void DownloadManager::downloadFile(const QUrl& fileUrl,
#if QT_CONFIG(ssl) #if QT_CONFIG(ssl)
connect(currentDownload_, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(slotSslErrors(QList<QSslError>))); connect(currentDownload_, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(slotSslErrors(QList<QSslError>)));
#endif #endif
connect(&progressBar_, &UpdateDownloadDialog::isCanceled,
[this] {
cancelDownload();
});
if (withUI_) { if (withUI_) {
progressBar_.exec(); progressBar_.exec();
...@@ -77,6 +79,11 @@ void DownloadManager::slotDownloadFinished() ...@@ -77,6 +79,11 @@ void DownloadManager::slotDownloadFinished()
{ {
statusCode_ = currentDownload_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); statusCode_ = currentDownload_->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (httpRequestAborted_) {
// request aborted
statusCode_ = 0;
}
currentDownload_->deleteLater(); currentDownload_->deleteLater();
currentDownload_ = nullptr; currentDownload_ = nullptr;
...@@ -88,7 +95,7 @@ void DownloadManager::slotDownloadFinished() ...@@ -88,7 +95,7 @@ void DownloadManager::slotDownloadFinished()
progressBar_.setMaximum(0); progressBar_.setMaximum(0);
progressBar_.setValue(0); progressBar_.setValue(0);
progressBar_.update("0"); progressBar_.update("0");
progressBar_.close(); progressBar_.done(0);
} }
if (doneCb_) if (doneCb_)
...@@ -109,26 +116,7 @@ void DownloadManager::slotDownloadProgress(qint64 bytesReceived, qint64 bytesTot ...@@ -109,26 +116,7 @@ void DownloadManager::slotDownloadProgress(qint64 bytesReceived, qint64 bytesTot
progressBar_.setMaximum(bytesTotal); progressBar_.setMaximum(bytesTotal);
progressBar_.setValue(bytesReceived); progressBar_.setValue(bytesReceived);
int presentTime = downloadTime_.elapsed(); progressBar_.update(Utils::humanFileSize(bytesReceived) + " / " + Utils::humanFileSize(bytesTotal));
// calculate the download speed
double speed = (bytesReceived - previousDownloadBytes_) * 1000.0 / (presentTime - previousTime_);
if (speed < 0)
speed = 0;
previousTime_ = presentTime;
previousDownloadBytes_ = bytesReceived;
QString unit;
if (speed < 1024) {
unit = "B/s";
} else if (speed < 1024 * 1024) {
speed /= 1024;
unit = "kB/s";
} else {
speed /= 1024 * 1024;
unit = "MB/s";
}
progressBar_.update(QString::number(speed) + " " + unit);
} }
void DownloadManager::slotHttpReadyRead() void DownloadManager::slotHttpReadyRead()
...@@ -156,3 +144,10 @@ int DownloadManager::getDownloadStatus() ...@@ -156,3 +144,10 @@ int DownloadManager::getDownloadStatus()
{ {
return statusCode_; return statusCode_;
} }
void DownloadManager::cancelDownload()
{
httpRequestAborted_ = true;
if(currentDownload_)
currentDownload_->abort();
}
...@@ -40,6 +40,7 @@ public: ...@@ -40,6 +40,7 @@ public:
bool withUI, bool withUI,
std::function<void(int)> doneCb = {}); std::function<void(int)> doneCb = {});
int getDownloadStatus(); int getDownloadStatus();
void cancelDownload();
public slots: public slots:
void slotSslErrors(const QList<QSslError>& sslErrors); void slotSslErrors(const QList<QSslError>& sslErrors);
...@@ -52,13 +53,11 @@ private: ...@@ -52,13 +53,11 @@ private:
QNetworkAccessManager manager_; QNetworkAccessManager manager_;
QNetworkReply* currentDownload_; QNetworkReply* currentDownload_;
updateDownloadDialog progressBar_; UpdateDownloadDialog progressBar_;
std::unique_ptr<QFile> file_; std::unique_ptr<QFile> file_;
QTime downloadTime_;
int previousTime_ = 0;
qint64 previousDownloadBytes_ = 0;
int statusCode_; int statusCode_;
bool withUI_; bool withUI_;
bool httpRequestAborted_ { false };
std::function<void(int)> doneCb_; std::function<void(int)> doneCb_;
......
...@@ -216,6 +216,25 @@ QPushButton#sipInputPanelButton:checked { ...@@ -216,6 +216,25 @@ QPushButton#sipInputPanelButton:checked {
border: solid 1px; border: solid 1px;
} }
QPushButton#updateCancelButton {
padding: 4px;
background: #369;
color: white;
font-size: 12px;
border: 0;
border-radius: 3px;
outline: 0px;
width: 50px;
}
QPushButton#updateCancelButton:hover {
background: #47a;
}
QPushButton#updateCancelButton:pressed {
background: #58b;
}
QMessageBox QPushButton { QMessageBox QPushButton {
padding: 4px; padding: 4px;
background: #369; background: #369;
...@@ -272,7 +291,7 @@ QScrollBar::handle:vertical{ ...@@ -272,7 +291,7 @@ QScrollBar::handle:vertical{
} }
QWidget#messagingPage, QWidget#contactRequestPage, QWidget#messagingPage, QWidget#contactRequestPage,
QDialog#DeleteAccountDialog, QDialog#updateDownloadDialog, QDialog#DeleteAccountDialog, QDialog#UpdateDownloadDialog,
QDialog#UpdateConfirmDialog, QDialog#AccountMigrationDialog { QDialog#UpdateConfirmDialog, QDialog#AccountMigrationDialog {
background: rgb(255, 255, 255); background: rgb(255, 255, 255);
} }
......
...@@ -19,34 +19,55 @@ ...@@ -19,34 +19,55 @@
#include "updatedownloaddialog.h" #include "updatedownloaddialog.h"
#include "ui_updatedownloaddialog.h" #include "ui_updatedownloaddialog.h"
updateDownloadDialog::updateDownloadDialog(QWidget* parent) #include <QCloseEvent>
#include <QMessageBox>
UpdateDownloadDialog::UpdateDownloadDialog(QWidget* parent)
: QDialog(parent) : QDialog(parent)
, ui(new Ui::updateDownloadDialog) , ui(new Ui::UpdateDownloadDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->progressBar->setValue(0); ui->progressBar->setValue(0);
ui->progressBar->setVisible(true); ui->progressBar->setVisible(true);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
connect(ui->updateCancelButton, &QAbstractButton::clicked,
[this] {
emit isCanceled();
});
} }
void updateDownloadDialog::setValue(double var) void UpdateDownloadDialog::setValue(double var)
{ {
value_ = var; value_ = var;
} }
void updateDownloadDialog::setMaximum(double var) void UpdateDownloadDialog::setMaximum(double var)
{ {
maximum_ = var; maximum_ = var;
} }
updateDownloadDialog::~updateDownloadDialog() UpdateDownloadDialog::~UpdateDownloadDialog()
{ {
delete ui; delete ui;
} }
void updateDownloadDialog::update(QString displayText) void UpdateDownloadDialog::update(QString displayText)
{ {
int percent = static_cast<int>((1 - ((maximum_ - value_) / maximum_)) * 100); int percent = static_cast<int>((1 - ((maximum_ - value_) / maximum_)) * 100);
ui->Statusedit->setText("Speed: " + displayText); ui->Statusedit->setText("Progress: " + displayText);
ui->progressBar->setValue(percent); ui->progressBar->setValue(percent);
} }
void UpdateDownloadDialog::closeEvent(QCloseEvent *event)
{
QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Update",
tr("Quit Update?\n"),
QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
if (resBtn == QMessageBox::Yes) {
emit isCanceled();
} else {
event->ignore();
}
}
...@@ -21,21 +21,27 @@ ...@@ -21,21 +21,27 @@
#include <QDialog> #include <QDialog>
namespace Ui { namespace Ui {
class updateDownloadDialog; class UpdateDownloadDialog;
} }
class updateDownloadDialog : public QDialog { class UpdateDownloadDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
updateDownloadDialog(QWidget* parent = nullptr); UpdateDownloadDialog(QWidget* parent = nullptr);
~updateDownloadDialog(); ~UpdateDownloadDialog();
void setValue(double var); void setValue(double var);
void setMaximum(double var); void setMaximum(double var);
void update(QString displayText); void update(QString displayText);
signals:
void isCanceled();
private: private:
Ui::updateDownloadDialog* ui; Ui::UpdateDownloadDialog* ui;
double value_ = 0; double value_ = 0;
double maximum_ = -1; double maximum_ = -1;
protected:
void closeEvent(QCloseEvent *event);
}; };
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>updateDownloadDialog</class> <class>UpdateDownloadDialog</class>
<widget class="QDialog" name="updateDownloadDialog"> <widget class="QDialog" name="UpdateDownloadDialog">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>309</width> <width>300</width>
<height>121</height> <height>113</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Update Download</string> <string>Update</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>7</number>
</property>
<property name="leftMargin">
<number>11</number>
</property>
<property name="topMargin">
<number>11</number>
</property>
<property name="rightMargin">
<number>11</number>
</property>
<property name="bottomMargin">
<number>11</number>
</property>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="text">
<string>Update Download Status</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
...@@ -66,7 +65,7 @@ ...@@ -66,7 +65,7 @@
</font> </font>
</property> </property>
<property name="text"> <property name="text">
<string>Speed: </string> <string>Download Progress: </string>
</property> </property>
</widget> </widget>
</item> </item>
...@@ -85,6 +84,61 @@ ...@@ -85,6 +84,61 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::MinimumExpanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>7</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="updateCancelButton">
<property name="minimumSize">
<size>
<width>90</width>
<height>30</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>15</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>
......
...@@ -326,7 +326,7 @@ Utils::checkForUpdates(bool withUI, QWidget* parent) ...@@ -326,7 +326,7 @@ Utils::checkForUpdates(bool withUI, QWidget* parent)
Utils::cleanUpdateFiles(); Utils::cleanUpdateFiles();
QString downloadpath = WinGetEnv("TEMP"); QString downloadpath = WinGetEnv("TEMP");
DownloadManager::instance().downloadFile( DownloadManager::instance().downloadFile(
QUrl::fromEncoded("https://dl.jami.net/windows/version"), QUrl::fromEncoded("https://dl.jami.net/windows/jami-x64.msi"),
downloadpath, downloadpath,
withUI, withUI,
[parent, withUI, downloadpath](int status) { [parent, withUI, downloadpath](int status) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment