Added cancel download

This commit is contained in:
Xinyu Hou 2014-11-13 12:09:11 +00:00
parent 8503833e3d
commit 376c983d21
4 changed files with 24 additions and 8 deletions

View File

@ -24,7 +24,7 @@ DataDownloader::DataDownloader(QUrl url, QObject* parent) :
SLOT(fileDownloaded(QNetworkReply*))); SLOT(fileDownloaded(QNetworkReply*)));
QNetworkRequest request(url); QNetworkRequest request(url);
m_WebCtrl.get(request); m_pReply = m_WebCtrl.get(request);
} }
DataDownloader::~DataDownloader() DataDownloader::~DataDownloader()
@ -36,10 +36,18 @@ void DataDownloader::fileDownloaded(QNetworkReply* reply)
{ {
m_DownloadedData = reply->readAll(); m_DownloadedData = reply->readAll();
reply->deleteLater(); reply->deleteLater();
if (!m_DownloadedData.isEmpty()) {
emit downloaded(); emit downloaded();
} }
}
QByteArray DataDownloader::downloadedData() const QByteArray DataDownloader::downloadedData() const
{ {
return m_DownloadedData; return m_DownloadedData;
} }
void DataDownloader::cancelDownload()
{
m_pReply->abort();
}

View File

@ -32,6 +32,7 @@ public:
virtual ~DataDownloader(); virtual ~DataDownloader();
QByteArray downloadedData() const; QByteArray downloadedData() const;
void cancelDownload();
signals: signals:
void downloaded(); void downloaded();
@ -43,6 +44,7 @@ private:
QNetworkAccessManager m_WebCtrl; QNetworkAccessManager m_WebCtrl;
QByteArray m_DownloadedData; QByteArray m_DownloadedData;
QNetworkReply* m_pReply;
}; };
#endif // DATADOWNLOADER_H #endif // DATADOWNLOADER_H

View File

@ -82,7 +82,8 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
m_pMenuHelp(NULL), m_pMenuHelp(NULL),
m_pZeroconfService(NULL), m_pZeroconfService(NULL),
m_pDataDownloader(NULL), m_pDataDownloader(NULL),
m_DownloadMessageBox(NULL) m_DownloadMessageBox(NULL),
m_pCancelButton(NULL)
{ {
setupUi(this); setupUi(this);
@ -988,18 +989,21 @@ void MainWindow::downloadBonjour()
} }
m_pDataDownloader = new DataDownloader(url, this); m_pDataDownloader = new DataDownloader(url, this);
connect(m_pDataDownloader, SIGNAL(downloaded()), SLOT(installBonjour()));
if (m_DownloadMessageBox == NULL) { if (m_DownloadMessageBox == NULL) {
m_DownloadMessageBox = new QMessageBox(); m_DownloadMessageBox = new QMessageBox();
m_DownloadMessageBox->setModal(false);
m_DownloadMessageBox->setStandardButtons(0);
m_DownloadMessageBox->setText("Installing Bonjour"); 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 #endif
} }

View File

@ -46,6 +46,7 @@ class QCheckBox;
class QRadioButton; class QRadioButton;
class QTemporaryFile; class QTemporaryFile;
class QMessageBox; class QMessageBox;
class QAbstractButton;
class LogDialog; class LogDialog;
class QSynergyApplication; class QSynergyApplication;
@ -173,6 +174,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase
ZeroconfService* m_pZeroconfService; ZeroconfService* m_pZeroconfService;
DataDownloader* m_pDataDownloader; DataDownloader* m_pDataDownloader;
QMessageBox* m_DownloadMessageBox; QMessageBox* m_DownloadMessageBox;
QAbstractButton* m_pCancelButton;
private slots: private slots:
void on_m_pAutoConnectCheckBox_toggled(bool checked); void on_m_pAutoConnectCheckBox_toggled(bool checked);