Skip to content

Commit

Permalink
Send request from spawned thread
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 14, 2018
1 parent 9b5c5b0 commit c59df79
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/providers/wfs/qgswfsrequest.cpp
Expand Up @@ -128,41 +128,46 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
}

mReply = QgsNetworkAccessManager::instance()->get( request );
mReply->setReadBufferSize( READ_BUFFER_SIZE_HINT );
if ( !mUri.auth().setAuthorizationReply( mReply ) )
std::function<bool()> downloaderFunction = [ this, request, synchronous ]()
{
mErrorCode = QgsWfsRequest::NetworkError;
mErrorMessage = errorMessageFailedAuth();
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
return false;
}
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished );
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress );

if ( !synchronous )
return true;
else
{
QEventLoop loop;
connect( this, &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit );

if ( QThread::currentThread() == QApplication::instance()->thread() )
bool success = true;
mReply = QgsNetworkAccessManager::instance()->get( request );
mReply->setReadBufferSize( READ_BUFFER_SIZE_HINT );
if ( !mUri.auth().setAuthorizationReply( mReply ) )
{
QFuture<void> future = QtConcurrent::run( [ &loop ]()
{
loop.exec();
} );

future.waitForFinished();
mErrorCode = QgsWfsRequest::NetworkError;
mErrorMessage = errorMessageFailedAuth();
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
success = false;
}
else
{
loop.exec();
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished );
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress );

if ( synchronous )
{
QEventLoop loop;
connect( this, &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit );
loop.exec();
}
}
return success;
};

bool success;

return mErrorMessage.isEmpty();
if ( synchronous && QThread::currentThread() == QApplication::instance()->thread() )
{
QFuture<bool> future = QtConcurrent::run( downloaderFunction );
future.waitForFinished();
success = future.result();
}
else
{
success = downloaderFunction();
}
return success && mErrorMessage.isEmpty();
}

bool QgsWfsRequest::sendPOST( const QUrl &url, const QString &contentTypeHeader, const QByteArray &data )
Expand Down

0 comments on commit c59df79

Please sign in to comment.