Skip to content

Commit

Permalink
Experimental commit for synchroneous auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 14, 2018
1 parent 666a0e9 commit 8fc5b10
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/providers/wfs/qgswfsrequest.cpp
Expand Up @@ -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 );
Expand All @@ -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
Expand All @@ -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 )
{
Expand All @@ -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();
}

Expand Down
28 changes: 28 additions & 0 deletions src/providers/wfs/qgswfsrequest.h
Expand Up @@ -19,6 +19,7 @@
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QAuthenticator>

#include "qgswfsdatasourceuri.h"

Expand Down Expand Up @@ -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.