diff --git a/cockatrice/src/dlg_update.cpp b/cockatrice/src/dlg_update.cpp index e2db8161..d6262f55 100644 --- a/cockatrice/src/dlg_update.cpp +++ b/cockatrice/src/dlg_update.cpp @@ -3,7 +3,7 @@ #include #include #include -#include + #include #include #include @@ -24,17 +24,21 @@ DlgUpdate::DlgUpdate(QWidget *parent) : QDialog(parent) { descriptionLabel = new QLabel(tr("Current release channel:") + " " + tr(settingsCache->getUpdateReleaseChannel()->getName().toUtf8()), this); progress = new QProgressBar(this); - QDialogButtonBox *buttonBox = new QDialogButtonBox(this); + buttonBox = new QDialogButtonBox(this); + buttonBox->setFixedWidth(350); + ok = new QPushButton("Close", this); manualDownload = new QPushButton(tr("Reinstall"), this); - enableUpdateButton(false); //Unless we know there's an update available, you can't install + stopDownload = new QPushButton(tr("Cancel Download"), this); gotoDownload = new QPushButton(tr("Open Download Page"), this); - buttonBox->addButton(manualDownload, QDialogButtonBox::ActionRole); - buttonBox->addButton(gotoDownload, QDialogButtonBox::ActionRole); + + addStopDownloadAndRemoveOthers(false); // Add all buttons to box + enableUpdateButton(false); //Unless we know there's an update available, you can't install buttonBox->addButton(ok, QDialogButtonBox::AcceptRole); connect(gotoDownload, SIGNAL(clicked()), this, SLOT(gotoDownloadPage())); connect(manualDownload, SIGNAL(clicked()), this, SLOT(downloadUpdate())); + connect(stopDownload, SIGNAL(clicked()), this, SLOT(cancelDownload())); connect(ok, SIGNAL(clicked()), this, SLOT(closeDialog())); QVBoxLayout *parentLayout = new QVBoxLayout(this); @@ -56,22 +60,17 @@ DlgUpdate::DlgUpdate(QWidget *parent) : QDialog(parent) { //Initialize the checker and downloader class uDownloader = new UpdateDownloader(this); connect(uDownloader, SIGNAL(downloadSuccessful(QUrl)), this, SLOT(downloadSuccessful(QUrl))); - connect(uDownloader, SIGNAL(progressMade(qint64, qint64)), - this, SLOT(downloadProgressMade(qint64, qint64))); - connect(uDownloader, SIGNAL(error(QString)), - this, SLOT(downloadError(QString))); + connect(uDownloader, SIGNAL(progressMade(qint64, qint64)), this, SLOT(downloadProgressMade(qint64, qint64))); + connect(uDownloader, SIGNAL(error(QString)), this, SLOT(downloadError(QString))); ReleaseChannel * channel = settingsCache->getUpdateReleaseChannel(); - connect(channel, SIGNAL(finishedCheck(bool, bool, Release * )), - this, SLOT(finishedUpdateCheck(bool, bool, Release * ))); - connect(channel, SIGNAL(error(QString)), - this, SLOT(updateCheckError(QString))); + connect(channel, SIGNAL(finishedCheck(bool, bool, Release *)), this, SLOT(finishedUpdateCheck(bool, bool, Release *))); + connect(channel, SIGNAL(error(QString)), this, SLOT(updateCheckError(QString))); //Check for updates beginUpdateCheck(); } - void DlgUpdate::closeDialog() { accept(); } @@ -83,11 +82,17 @@ void DlgUpdate::gotoDownloadPage() { void DlgUpdate::downloadUpdate() { setLabel(tr("Downloading update...")); - enableOkButton(false); - enableUpdateButton(false); + addStopDownloadAndRemoveOthers(true); // Will remove all other buttons uDownloader->beginDownload(updateUrl); } +void DlgUpdate::cancelDownload() { + emit uDownloader->stopDownload(); + setLabel("Download Canceled"); + addStopDownloadAndRemoveOthers(false); + downloadProgressMade(0, 1); +} + void DlgUpdate::beginUpdateCheck() { progress->setMinimum(0); progress->setMaximum(0); @@ -142,6 +147,19 @@ void DlgUpdate::enableUpdateButton(bool enable) { manualDownload->setEnabled(enable); } +void DlgUpdate::addStopDownloadAndRemoveOthers(bool enable) { + if (enable) { + buttonBox->addButton(stopDownload, QDialogButtonBox::ActionRole); + buttonBox->removeButton(manualDownload); + buttonBox->removeButton(gotoDownload); + } + else { + buttonBox->removeButton(stopDownload); + buttonBox->addButton(manualDownload, QDialogButtonBox::ActionRole); + buttonBox->addButton(gotoDownload, QDialogButtonBox::ActionRole); + } +} + void DlgUpdate::enableOkButton(bool enable) { ok->setEnabled(enable); } diff --git a/cockatrice/src/dlg_update.h b/cockatrice/src/dlg_update.h index 67f1426d..ddf24752 100644 --- a/cockatrice/src/dlg_update.h +++ b/cockatrice/src/dlg_update.h @@ -3,6 +3,7 @@ #include #include +#include #include "update_downloader.h" class Release; @@ -16,6 +17,7 @@ private slots: void finishedUpdateCheck(bool needToUpdate, bool isCompatible, Release *release); void gotoDownloadPage(); void downloadUpdate(); + void cancelDownload(); void updateCheckError(QString errorString); void downloadSuccessful(QUrl filepath); void downloadProgressMade(qint64 bytesRead, qint64 totalBytes); @@ -25,13 +27,15 @@ private: QUrl updateUrl; void enableUpdateButton(bool enable); void enableOkButton(bool enable); + void addStopDownloadAndRemoveOthers(bool enable); void beginUpdateCheck(); void setLabel(QString text); QLabel *statusLabel, *descriptionLabel; QProgressBar *progress; - QPushButton *manualDownload, *gotoDownload, *ok; + QPushButton *manualDownload, *gotoDownload, *ok, *stopDownload; QPushButton *cancel; UpdateDownloader *uDownloader; + QDialogButtonBox *buttonBox; }; #endif diff --git a/cockatrice/src/update_downloader.cpp b/cockatrice/src/update_downloader.cpp index 362514b0..263c12e1 100644 --- a/cockatrice/src/update_downloader.cpp +++ b/cockatrice/src/update_downloader.cpp @@ -1,4 +1,5 @@ #include +#include #include "update_downloader.h" @@ -14,10 +15,13 @@ void UpdateDownloader::beginDownload(QUrl downloadUrl) { response = netMan->get(QNetworkRequest(downloadUrl)); connect(response, SIGNAL(finished()), this, SLOT(fileFinished())); connect(response, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); - connect(response, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError))); + connect(this, SIGNAL(stopDownload()), response, SLOT(abort())); } void UpdateDownloader::downloadError(QNetworkReply::NetworkError) { + if (response == nullptr) + return; + emit error(response->errorString().toUtf8()); } diff --git a/cockatrice/src/update_downloader.h b/cockatrice/src/update_downloader.h index e216f926..f72787bb 100644 --- a/cockatrice/src/update_downloader.h +++ b/cockatrice/src/update_downloader.h @@ -19,6 +19,7 @@ signals: void downloadSuccessful(QUrl filepath); void progressMade(qint64 bytesRead, qint64 totalBytes); void error(QString errorString); + void stopDownload(); private: QUrl originalUrl; QNetworkAccessManager *netMan;