From 376c983d214781fdf027524b887e36560db022b3 Mon Sep 17 00:00:00 2001 From: Xinyu Hou Date: Thu, 13 Nov 2014 12:09:11 +0000 Subject: [PATCH] Added cancel download --- src/gui/src/DataDownloader.cpp | 12 ++++++++++-- src/gui/src/DataDownloader.h | 2 ++ src/gui/src/MainWindow.cpp | 16 ++++++++++------ src/gui/src/MainWindow.h | 2 ++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/gui/src/DataDownloader.cpp b/src/gui/src/DataDownloader.cpp index 739c4e9e..ced73f88 100644 --- a/src/gui/src/DataDownloader.cpp +++ b/src/gui/src/DataDownloader.cpp @@ -24,7 +24,7 @@ DataDownloader::DataDownloader(QUrl url, QObject* parent) : SLOT(fileDownloaded(QNetworkReply*))); QNetworkRequest request(url); - m_WebCtrl.get(request); + m_pReply = m_WebCtrl.get(request); } DataDownloader::~DataDownloader() @@ -36,10 +36,18 @@ void DataDownloader::fileDownloaded(QNetworkReply* reply) { m_DownloadedData = reply->readAll(); reply->deleteLater(); - emit downloaded(); + + if (!m_DownloadedData.isEmpty()) { + emit downloaded(); + } } QByteArray DataDownloader::downloadedData() const { return m_DownloadedData; } + +void DataDownloader::cancelDownload() +{ + m_pReply->abort(); +} diff --git a/src/gui/src/DataDownloader.h b/src/gui/src/DataDownloader.h index ca27e56d..4839c57d 100644 --- a/src/gui/src/DataDownloader.h +++ b/src/gui/src/DataDownloader.h @@ -32,6 +32,7 @@ public: virtual ~DataDownloader(); QByteArray downloadedData() const; + void cancelDownload(); signals: void downloaded(); @@ -43,6 +44,7 @@ private: QNetworkAccessManager m_WebCtrl; QByteArray m_DownloadedData; + QNetworkReply* m_pReply; }; #endif // DATADOWNLOADER_H diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 715d3fe4..ff370c42 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -82,7 +82,8 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : m_pMenuHelp(NULL), m_pZeroconfService(NULL), m_pDataDownloader(NULL), - m_DownloadMessageBox(NULL) + m_DownloadMessageBox(NULL), + m_pCancelButton(NULL) { setupUi(this); @@ -988,18 +989,21 @@ void MainWindow::downloadBonjour() } m_pDataDownloader = new DataDownloader(url, this); + connect(m_pDataDownloader, SIGNAL(downloaded()), SLOT(installBonjour())); if (m_DownloadMessageBox == NULL) { m_DownloadMessageBox = new QMessageBox(); - m_DownloadMessageBox->setModal(false); - m_DownloadMessageBox->setStandardButtons(0); m_DownloadMessageBox->setText("Installing Bonjour"); - m_DownloadMessageBox->resize(100, 10); + m_DownloadMessageBox->setStandardButtons(0); + m_pCancelButton = m_DownloadMessageBox->addButton( + tr("Cancel"), QMessageBox::RejectRole); } - m_DownloadMessageBox->show(); + m_DownloadMessageBox->exec(); - connect(m_pDataDownloader, SIGNAL(downloaded()), SLOT(installBonjour())); + if (m_DownloadMessageBox->clickedButton() == m_pCancelButton) { + m_pDataDownloader->cancelDownload(); + } #endif } diff --git a/src/gui/src/MainWindow.h b/src/gui/src/MainWindow.h index a0669741..3b4b3828 100644 --- a/src/gui/src/MainWindow.h +++ b/src/gui/src/MainWindow.h @@ -46,6 +46,7 @@ class QCheckBox; class QRadioButton; class QTemporaryFile; class QMessageBox; +class QAbstractButton; class LogDialog; class QSynergyApplication; @@ -173,6 +174,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase ZeroconfService* m_pZeroconfService; DataDownloader* m_pDataDownloader; QMessageBox* m_DownloadMessageBox; + QAbstractButton* m_pCancelButton; private slots: void on_m_pAutoConnectCheckBox_toggled(bool checked);