[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):
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()
+4 -1
View File
@@ -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_