Skip to content
Permalink
Browse files

Experimental commit for synchroneous auth support

  • Loading branch information
m-kuhn committed May 24, 2018
1 parent 666a0e9 commit 8fc5b10122e9450584d3bfa1fb2603b2129f3bc7
Showing with 46 additions and 4 deletions.
  1. +18 −4 src/providers/wfs/qgswfsrequest.cpp
  2. +28 −0 src/providers/wfs/qgswfsrequest.h
@@ -128,7 +128,10 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
}

std::function<bool()> downloaderFunction = [ this, request, synchronous ]()
QWaitCondition waitCondition;

QMutex mutex;
std::function<bool()> downloaderFunction = [ this, request, synchronous, &waitCondition ]()
{
bool success = true;
mReply = QgsNetworkAccessManager::instance()->get( request );
@@ -139,6 +142,7 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
mErrorCode = QgsWfsRequest::NetworkError;
mErrorMessage = errorMessageFailedAuth();
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
waitCondition.wakeAll();
success = false;
}
else
@@ -148,6 +152,7 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
// * or the owner thread of mReply is currently not doing anything because it's blocked in future.waitForFinished() (if it is the main thread)
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished, Qt::DirectConnection );
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress, Qt::DirectConnection );
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::authenticationRequired, this, [&waitCondition]() { waitCondition.wakeAll(); } );

if ( synchronous )
{
@@ -156,21 +161,30 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
loop.exec();
}
}
waitCondition.wakeAll();
return success;
};

bool success;

if ( synchronous && QThread::currentThread() == QApplication::instance()->thread() )
{
QFuture<bool> future = QtConcurrent::run( downloaderFunction );
future.waitForFinished();
success = future.result();
std::unique_ptr<DownloaderThread> downloaderThread = qgis::make_unique<DownloaderThread>( downloaderFunction );
downloaderThread->start();
while ( !downloaderThread->isFinished() )
{
waitCondition.wait( &mutex );
if ( !downloaderThread->isFinished() )
QgsApplication::instance()->processEvents();
}

success = downloaderThread->success();
}
else
{
success = downloaderFunction();
}
mutex.unlock();
return success && mErrorMessage.isEmpty();
}

@@ -19,6 +19,7 @@
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QAuthenticator>

#include "qgswfsdatasourceuri.h"

@@ -117,4 +118,31 @@ class QgsWfsRequest : public QObject

};


class DownloaderThread : public QThread
{
Q_OBJECT

public:
DownloaderThread( std::function<bool()> function, QObject *parent = nullptr )
: QThread( parent )
, mFunction( function )
{
}

void run() override
{
mSuccess = mFunction();
}

bool success() const
{
return mSuccess;
}

private:
std::function<bool()> mFunction;
bool mSuccess = false;
};

#endif // QGSWFSREQUEST_H

0 comments on commit 8fc5b10

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