From 989abd017ea8715a8d8f1a9d554a18750bfe0902 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Mon, 16 Jul 2018 18:37:00 -0400 Subject: [PATCH] [Companion][downloadDialog] Canceling the download dialog during download now terminates the active network request and removes the file. --- companion/src/downloaddialog.cpp | 49 +++++++++++++++++++------------- companion/src/downloaddialog.h | 5 +++- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/companion/src/downloaddialog.cpp b/companion/src/downloaddialog.cpp index e422c3a85..7b2f9ffbd 100644 --- a/companion/src/downloaddialog.cpp +++ b/companion/src/downloaddialog.cpp @@ -28,7 +28,9 @@ downloadDialog::downloadDialog(QWidget *parent, QString src, QString tgt): QDialog(parent), ui(new Ui::downloadDialog), - file(NULL) + reply(nullptr), + file(nullptr), + aborted(false) { ui->setupUi(this); setWindowIcon(CompanionIcon("fwpreferences.png")); @@ -62,29 +64,38 @@ downloadDialog::~downloadDialog() delete file; } +void downloadDialog::reject() +{ + if (reply && reply->isRunning()) { + aborted = true; + reply->abort(); // this will call QNetworkReply::finished() + return; + } + + QDialog::reject(); +} + void downloadDialog::httpFinished() { - file->flush(); - file->close(); + file->flush(); + file->close(); - bool ok = true; - if (reply->error()) { - file->remove(); - QMessageBox::information(this, CPN_STR_APP_NAME, - tr("Download failed: %1.") - .arg(reply->errorString())); - ok = false; - } + const bool ok = !(reply->error() || aborted); + if (!ok) { + file->remove(); + if (!aborted) + QMessageBox::information(this, CPN_STR_APP_NAME, tr("Download failed: %1.").arg(reply->errorString())); + } - reply->deleteLater(); - reply = 0; - delete file; - file = NULL; + reply->deleteLater(); + reply = nullptr; + file->deleteLater(); + file = nullptr; - if (ok) - accept(); - else - reject(); + if (ok) + accept(); + else + reject(); } void downloadDialog::httpReadyRead() diff --git a/companion/src/downloaddialog.h b/companion/src/downloaddialog.h index 624c9b18f..90b94b52a 100644 --- a/companion/src/downloaddialog.h +++ b/companion/src/downloaddialog.h @@ -39,6 +39,9 @@ public: explicit downloadDialog(QWidget *parent = 0, QString src = "", QString tgt = ""); ~downloadDialog(); + public slots: + virtual void reject() override; + private slots: void fileError(); void httpFinished(); @@ -54,7 +57,7 @@ private: QNetworkAccessManager qnam; QNetworkReply *reply; QFile *file; - + bool aborted; }; #endif // _DOWNLOADDIALOG_H_