Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsNetworkAccessManager signal for reply download progress
This signal is propagated to the main thread QgsNetworkAccessManager
instance, so it is necessary only to connect to the main thread's signal
in order to receive notifications about requests created in any thread.

Also includes the original requestId to allow linked download progress
to original request
  • Loading branch information
nyalldawson committed Jan 25, 2019
1 parent df1d47b commit a491e90
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/core/auto_generated/qgsnetworkaccessmanager.sip.in
Expand Up @@ -220,6 +220,22 @@ created in any thread.

.. seealso:: :py:func:`finished`

.. versionadded:: 3.6
%End

void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
%Docstring
Emitted when a network reply receives a progress report.

The ``requestId`` argument reflects the unique ID identifying the original request which the progress report relates to.

The ``bytesReceived`` parameter indicates the number of bytes received, while ``bytesTotal`` indicates the total number
of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, ``bytesTotal`` will be -1.

This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary
only to connect to the main thread's signal in order to receive notifications about requests
created in any thread.

.. versionadded:: 3.6
%End

Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsnetworkaccessmanager.cpp
Expand Up @@ -224,6 +224,8 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
emit requestCreated( reply );
Q_NOWARN_DEPRECATED_POP

connect( reply, &QNetworkReply::downloadProgress, this, &QgsNetworkAccessManager::onReplyDownloadProgress );

// The timer will call abortRequest slot to abort the connection if needed.
// The timer is stopped by the finished signal and is restarted on downloadProgress and
// uploadProgress.
Expand Down Expand Up @@ -262,6 +264,17 @@ void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
emit finished( QgsNetworkReplyContent( reply ) );
}

void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesRecevied, qint64 bytesTotal )
{
if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
{
bool ok = false;
int requestId = reply->property( "requestId" ).toInt( &ok );
if ( ok )
emit downloadProgress( requestId, bytesRecevied, bytesTotal );
}
}

QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl control )
{
switch ( control )
Expand Down Expand Up @@ -330,6 +343,8 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
connect( this, qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ),
sMainNAM, qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ) );

connect( this, &QgsNetworkAccessManager::downloadProgress, sMainNAM, &QgsNetworkAccessManager::downloadProgress );

#ifndef QT_NO_SSL
connect( this, &QNetworkAccessManager::sslErrors,
sMainNAM, &QNetworkAccessManager::sslErrors,
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsnetworkaccessmanager.h
Expand Up @@ -221,6 +221,22 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
*/
void requestTimedOut( QgsNetworkRequestParameters request );

/**
* Emitted when a network reply receives a progress report.
*
* The \a requestId argument reflects the unique ID identifying the original request which the progress report relates to.
*
* The \a bytesReceived parameter indicates the number of bytes received, while \a bytesTotal indicates the total number
* of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, \a bytesTotal will be -1.
*
* This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary
* only to connect to the main thread's signal in order to receive notifications about requests
* created in any thread.
*
* \since QGIS 3.6
*/
void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );

/**
* \deprecated Use the thread-safe requestAboutToBeCreated( QgsNetworkRequestParameters ) signal instead.
*/
Expand All @@ -233,6 +249,8 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager

void onReplyFinished( QNetworkReply *reply );

void onReplyDownloadProgress( qint64 bytesRecevied, qint64 bytesTotal );

protected:
QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;

Expand Down

0 comments on commit a491e90

Please sign in to comment.