From fad7246400e229d6e5ebf86c5830a8ff657d70f1 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Mon, 24 Oct 2016 23:56:36 +0200 Subject: [PATCH] dbhub: Show download progress and allow canceling downloads --- src/RemoteDatabase.cpp | 46 ++++++++++++++++++++++++++++++++++++++---- src/RemoteDatabase.h | 4 ++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/RemoteDatabase.cpp b/src/RemoteDatabase.cpp index 1478c50b..87d61a1c 100644 --- a/src/RemoteDatabase.cpp +++ b/src/RemoteDatabase.cpp @@ -9,7 +9,8 @@ #include "FileDialog.h" RemoteDatabase::RemoteDatabase() : - m_manager(new QNetworkAccessManager) + m_manager(new QNetworkAccessManager), + m_currentReply(nullptr) { // TODO Set up SSL configuration here @@ -42,10 +43,16 @@ void RemoteDatabase::fetchDatabase(const QString& url) // TODO Set SSL configuration here - // Fetch database - m_manager->get(request); + // Fetch database and save pending reply. Note that we're only supporting one active download here at the moment. + m_currentReply = m_manager->get(request); - // TODO Monitor and show download progress. Also add option to cancel running downloads + // Initialise the progress dialog for this request + m_progress.setWindowModality(Qt::ApplicationModal); + m_progress.setCancelButtonText(tr("Cancel")); + m_progress.setLabelText(tr("Downloading remote database from\n%1.").arg(url)); + m_progress.show(); + qApp->processEvents(); + connect(m_currentReply, &QNetworkReply::downloadProgress, this, &RemoteDatabase::updateProgress); } void RemoteDatabase::gotEncrypted(QNetworkReply* /*reply*/) @@ -79,6 +86,8 @@ void RemoteDatabase::gotReply(QNetworkReply* reply) } // Delete reply later, i.e. after returning from this slot function + m_currentReply = nullptr; + m_progress.hide(); reply->deleteLater(); } @@ -89,5 +98,34 @@ void RemoteDatabase::gotError(QNetworkReply* reply, const QList& erro QMessageBox::warning(0, qApp->applicationName(), message); // Delete reply later, i.e. after returning from this slot function + m_progress.hide(); reply->deleteLater(); } + +void RemoteDatabase::updateProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + // Update progress dialog + if(bytesTotal == -1) + { + // We don't know anything about the current progress, but it's still downloading + m_progress.setMinimum(0); + m_progress.setMaximum(0); + m_progress.setValue(0); + } else if(bytesReceived == bytesTotal) { + // The download has finished + m_progress.hide(); + } else { + // It's still downloading and we know the current progress + m_progress.setMinimum(0); + m_progress.setMaximum(bytesTotal); + m_progress.setValue(bytesReceived); + } + + // Check if the Cancel button has been pressed + qApp->processEvents(); + if(m_currentReply && m_progress.wasCanceled()) + { + m_currentReply->abort(); + m_progress.hide(); + } +} diff --git a/src/RemoteDatabase.h b/src/RemoteDatabase.h index dbbdfa4a..954ef5c1 100644 --- a/src/RemoteDatabase.h +++ b/src/RemoteDatabase.h @@ -2,6 +2,7 @@ #define REMOTEDATABASE_H #include +#include class QNetworkAccessManager; class QString; @@ -25,8 +26,11 @@ private: void gotEncrypted(QNetworkReply* reply); void gotReply(QNetworkReply* reply); void gotError(QNetworkReply* reply, const QList& errors); + void updateProgress(qint64 bytesReceived, qint64 bytesTotal); QNetworkAccessManager* m_manager; + QProgressDialog m_progress; + QNetworkReply* m_currentReply; }; #endif