[libcalamares] Build synchronous get w/ async-get

main
Adriaan de Groot 5 years ago
parent f8356a6dcc
commit badbdf59ee

@ -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,36 +144,28 @@ 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 );
}
options.applyToRequest( &request );
if ( options.hasTimeout() )
{ {
timer.setSingleShot( true ); return qMakePair( RequestStatus( RequestStatus::Failed ), nullptr );
QObject::connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
timer.start( options.timeout() );
} }
QEventLoop loop;
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();
if ( reply->isRunning() )
{ {
reply->deleteLater();
return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr ); return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr );
} }
else if ( reply->error() != QNetworkReply::NoError )
reply->deleteLater(); {
return qMakePair( RequestStatus( RequestStatus::Ok ), reply ); return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr );
}
else
{
return qMakePair( RequestStatus( RequestStatus::Ok ), reply );
}
} }
RequestStatus RequestStatus

Loading…
Cancel
Save