Skip to content

Commit

Permalink
No need for extra variables
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 14, 2018
1 parent 402f3ff commit 6dd3529
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/providers/wfs/qgswfsrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,10 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
}

QWaitCondition mainThreadWaitCondition;
QMutex mainThreadMutex;
QWaitCondition waitCondition;
QMutex waitConditionMutex;

QWaitCondition downloaderThreadWaitCondition;
QMutex downloaderThreadMutex;

std::function<bool()> downloaderFunction = [ this, request, synchronous, &mainThreadMutex, &mainThreadWaitCondition, &downloaderThreadMutex, &downloaderThreadWaitCondition ]()
std::function<bool()> downloaderFunction = [ this, request, synchronous, &waitConditionMutex, &waitCondition ]()
{
if ( QThread::currentThread() != QgsApplication::instance()->thread() )
QgsNetworkAccessManager::instance( Qt::DirectConnection );
Expand All @@ -148,7 +145,7 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
mErrorCode = QgsWfsRequest::NetworkError;
mErrorMessage = errorMessageFailedAuth();
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
mainThreadWaitCondition.wakeAll();
waitCondition.wakeAll();
success = false;
}
else
Expand All @@ -161,23 +158,23 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres

if ( synchronous )
{
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::authenticationRequired, this, [&mainThreadMutex, &mainThreadWaitCondition, &downloaderThreadMutex, &downloaderThreadWaitCondition]()
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::authenticationRequired, this, [&waitConditionMutex, &waitCondition]()
{
mainThreadMutex.lock();
mainThreadWaitCondition.wakeAll();
mainThreadMutex.unlock();
waitConditionMutex.lock();
waitCondition.wakeAll();
waitConditionMutex.unlock();

downloaderThreadMutex.lock();
downloaderThreadWaitCondition.wait( &downloaderThreadMutex );
downloaderThreadMutex.unlock();
waitConditionMutex.lock();
waitCondition.wait( &waitConditionMutex );
waitConditionMutex.unlock();
}, Qt::DirectConnection
);
QEventLoop loop;
connect( this, &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit, Qt::DirectConnection );
loop.exec();
}
}
mainThreadWaitCondition.wakeAll();
waitCondition.wakeAll();
return success;
};

Expand All @@ -187,17 +184,23 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
{
std::unique_ptr<DownloaderThread> downloaderThread = qgis::make_unique<DownloaderThread>( downloaderFunction );
downloaderThread->start();

while ( !downloaderThread->isFinished() )
{
mainThreadMutex.lock();
mainThreadWaitCondition.wait( &mainThreadMutex );
mainThreadMutex.unlock();
waitConditionMutex.lock();
waitCondition.wait( &waitConditionMutex );
waitConditionMutex.unlock();

// If the downloader thread wakes us (the main thread) up and is not yet finished
// he needs the authentication to run.
// The processEvents() call gives the auth manager the chance to show a dialog and
// once done with that, we can wake the downloaderThread again and continue the download.
if ( !downloaderThread->isFinished() )
{
QgsApplication::instance()->processEvents();
downloaderThreadMutex.lock();
downloaderThreadWaitCondition.wakeAll();
downloaderThreadMutex.unlock();
waitConditionMutex.lock();
waitCondition.wakeAll();
waitConditionMutex.unlock();
}
}

Expand Down

0 comments on commit 6dd3529

Please sign in to comment.