Skip to content

Commit

Permalink
Rework QgsNetworkAccessManager::requestAboutToBeCreated() to be thread
Browse files Browse the repository at this point in the history
safe, and encapsulate more useful request information
  • Loading branch information
nyalldawson committed Jan 22, 2019
1 parent 0e0146d commit 8a920e0
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
57 changes: 57 additions & 0 deletions python/core/auto_generated/qgsnetworkaccessmanager.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,51 @@



class QgsNetworkRequestParameters
{
%Docstring
Encapsulates parameters and properties of a network request.

.. versionadded:: 3.6
%End

%TypeHeaderCode
#include "qgsnetworkaccessmanager.h"
%End
public:

QgsNetworkRequestParameters();
%Docstring
Default constructor.
%End

QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
const QNetworkRequest &request );
%Docstring
Constructor for QgsNetworkRequestParameters, with the specified network
``operation`` and original ``request``.
%End

QNetworkAccessManager::Operation operation() const;
%Docstring
Returns the request operation, e.g. GET or POST.
%End

QNetworkRequest request() const;
%Docstring
Returns the network request.

This is the original network request sent to :py:class:`QgsNetworkAccessManager`, but with QGIS specific
configuration options such as proxy handling and SSL exceptions applied.
%End

QString originatingThreadId() const;
%Docstring
Returns a string identifying the thread which the request originated from.
%End

};

class QgsNetworkAccessManager : QNetworkAccessManager
{
%Docstring
Expand Down Expand Up @@ -111,6 +156,18 @@ Returns whether the system proxy should be used

signals:
void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * );

void requestAboutToBeCreated( QgsNetworkRequestParameters request );
%Docstring
Emitted when a network request is about to be created.

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

void requestCreated( QNetworkReply * );
void requestTimedOut( QNetworkReply * );

Expand Down
1 change: 1 addition & 0 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void QgsApplication::init( QString profileFolder )
qRegisterMetaType<QgsStyle::StyleEntity>( "QgsStyle::StyleEntity" );
qRegisterMetaType<QgsCoordinateReferenceSystem>( "QgsCoordinateReferenceSystem" );
qRegisterMetaType<QgsAuthManager::MessageLevel>( "QgsAuthManager::MessageLevel" );
qRegisterMetaType<QgsNetworkRequestParameters>( "QgsNetworkRequestParameters" );

( void ) resolvePkgPath();

Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsnetworkaccessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
}
#endif

emit requestAboutToBeCreated( QgsNetworkRequestParameters( op, req ) );
emit requestAboutToBeCreated( op, req, outgoingData );
QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );

Expand Down Expand Up @@ -301,6 +302,9 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
connect( this, &QgsNetworkAccessManager::requestTimedOut,
sMainNAM, &QgsNetworkAccessManager::requestTimedOut );

connect( this, qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ),
sMainNAM, qgis::overload< QgsNetworkRequestParameters >::of( &QgsNetworkAccessManager::requestAboutToBeCreated ) );

#ifndef QT_NO_SSL
connect( this, &QNetworkAccessManager::sslErrors,
sMainNAM, &QNetworkAccessManager::sslErrors,
Expand Down Expand Up @@ -394,3 +398,13 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
setCache( newcache );
}

//
// QgsNetworkRequestParameters
//

QgsNetworkRequestParameters::QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation, const QNetworkRequest &request )
: mOperation( operation )
, mRequest( request )
, mOriginatingThreadId( QStringLiteral( "0x%2" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) ) )
{
}
60 changes: 60 additions & 0 deletions src/core/qgsnetworkaccessmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,54 @@

#include "qgis_core.h"

/**
* \class QgsNetworkRequestParameters
* \ingroup core
* Encapsulates parameters and properties of a network request.
* \since QGIS 3.6
*/
class CORE_EXPORT QgsNetworkRequestParameters
{
public:

/**
* Default constructor.
*/
QgsNetworkRequestParameters() = default;

/**
* Constructor for QgsNetworkRequestParameters, with the specified network
* \a operation and original \a request.
*/
QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
const QNetworkRequest &request );

/**
* Returns the request operation, e.g. GET or POST.
*/
QNetworkAccessManager::Operation operation() const { return mOperation; }

/**
* Returns the network request.
*
* This is the original network request sent to QgsNetworkAccessManager, but with QGIS specific
* configuration options such as proxy handling and SSL exceptions applied.
*/
QNetworkRequest request() const { return mRequest; }

/**
* Returns a string identifying the thread which the request originated from.
*/
QString originatingThreadId() const { return mOriginatingThreadId; }

private:

QNetworkAccessManager::Operation mOperation;
QNetworkRequest mRequest;
QString mOriginatingThreadId;

};

/**
* \class QgsNetworkAccessManager
* \brief network access manager for QGIS
Expand Down Expand Up @@ -110,6 +158,18 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager

signals:
void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * );

/**
* Emitted when a network request is about to be created.
*
* 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 requestAboutToBeCreated( QgsNetworkRequestParameters request );

void requestCreated( QNetworkReply * );
void requestTimedOut( QNetworkReply * );

Expand Down

0 comments on commit 8a920e0

Please sign in to comment.