Skip to content
Permalink
Browse files

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 a491e90a041899033076d649f66e4fb8942b9adc
@@ -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

@@ -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.
@@ -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 )
@@ -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,
@@ -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.
*/
@@ -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;

0 comments on commit a491e90

Please sign in to comment.
You can’t perform that action at this time.