[Companion][downloadDialog] Canceling the download dialog during download now terminates the active network request and removes the file.

This commit is contained in:
Max Paperno
2018-07-16 18:37:00 -04:00
parent 71fd6dfa47
commit 989abd017e
2 changed files with 34 additions and 20 deletions
+30 -19
View File
@@ -28,7 +28,9 @@
downloadDialog::downloadDialog(QWidget *parent, QString src, QString tgt): downloadDialog::downloadDialog(QWidget *parent, QString src, QString tgt):
QDialog(parent), QDialog(parent),
ui(new Ui::downloadDialog), ui(new Ui::downloadDialog),
file(NULL) reply(nullptr),
file(nullptr),
aborted(false)
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowIcon(CompanionIcon("fwpreferences.png")); setWindowIcon(CompanionIcon("fwpreferences.png"));
@@ -62,29 +64,38 @@ downloadDialog::~downloadDialog()
delete file; delete file;
} }
void downloadDialog::reject()
{
if (reply && reply->isRunning()) {
aborted = true;
reply->abort(); // this will call QNetworkReply::finished()
return;
}
QDialog::reject();
}
void downloadDialog::httpFinished() void downloadDialog::httpFinished()
{ {
file->flush(); file->flush();
file->close(); file->close();
bool ok = true; const bool ok = !(reply->error() || aborted);
if (reply->error()) { if (!ok) {
file->remove(); file->remove();
QMessageBox::information(this, CPN_STR_APP_NAME, if (!aborted)
tr("Download failed: %1.") QMessageBox::information(this, CPN_STR_APP_NAME, tr("Download failed: %1.").arg(reply->errorString()));
.arg(reply->errorString())); }
ok = false;
}
reply->deleteLater(); reply->deleteLater();
reply = 0; reply = nullptr;
delete file; file->deleteLater();
file = NULL; file = nullptr;
if (ok) if (ok)
accept(); accept();
else else
reject(); reject();
} }
void downloadDialog::httpReadyRead() void downloadDialog::httpReadyRead()
+4 -1
View File
@@ -39,6 +39,9 @@ public:
explicit downloadDialog(QWidget *parent = 0, QString src = "", QString tgt = ""); explicit downloadDialog(QWidget *parent = 0, QString src = "", QString tgt = "");
~downloadDialog(); ~downloadDialog();
public slots:
virtual void reject() override;
private slots: private slots:
void fileError(); void fileError();
void httpFinished(); void httpFinished();
@@ -54,7 +57,7 @@ private:
QNetworkAccessManager qnam; QNetworkAccessManager qnam;
QNetworkReply *reply; QNetworkReply *reply;
QFile *file; QFile *file;
bool aborted;
}; };
#endif // _DOWNLOADDIALOG_H_ #endif // _DOWNLOADDIALOG_H_