[libcalamares] Build synchronous get w/ async-get

This commit is contained in:
Adriaan de Groot 2019-09-02 13:43:10 +02:00
parent f8356a6dcc
commit badbdf59ee

View File

@ -103,7 +103,7 @@ Manager::setCheckHasInternetUrl( const QUrl& url )
* *
* The extra options for the request are taken from @p options, * The extra options for the request are taken from @p options,
* including the timeout setting. A timeout will cause the reply * including the timeout setting. A timeout will cause the reply
* to abort. * to abort. The reply is **not** scheduled for deletion.
* *
* On failure, returns nullptr (e.g. bad URL, timeout). * On failure, returns nullptr (e.g. bad URL, timeout).
*/ */
@ -144,37 +144,29 @@ asynchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl
static QPair< RequestStatus, QNetworkReply* > static QPair< RequestStatus, QNetworkReply* >
synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl& url, const RequestOptions& options ) synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl& url, const RequestOptions& options )
{ {
QNetworkRequest request = QNetworkRequest( url ); auto* reply = asynchronousRun( nam, url, options );
QNetworkReply* reply = nam->get( request ); if ( !reply )
QEventLoop loop;
QTimer timer;
// Bail out early if the request is bad
if ( reply->error() )
{ {
reply->deleteLater();
return qMakePair( RequestStatus( RequestStatus::Failed ), nullptr ); return qMakePair( RequestStatus( RequestStatus::Failed ), nullptr );
} }
options.applyToRequest( &request ); QEventLoop loop;
if ( options.hasTimeout() )
{
timer.setSingleShot( true );
QObject::connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
timer.start( options.timeout() );
}
QObject::connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit ); QObject::connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit );
loop.exec(); loop.exec();
if ( options.hasTimeout() && !timer.isActive() )
{
reply->deleteLater(); reply->deleteLater();
if ( reply->isRunning() )
{
return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr ); return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr );
} }
else if ( reply->error() != QNetworkReply::NoError )
reply->deleteLater(); {
return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr );
}
else
{
return qMakePair( RequestStatus( RequestStatus::Ok ), reply ); return qMakePair( RequestStatus( RequestStatus::Ok ), reply );
} }
}
RequestStatus RequestStatus
Manager::synchronousPing( const QUrl& url, const RequestOptions& options ) Manager::synchronousPing( const QUrl& url, const RequestOptions& options )