Skip to content

Commit f1040ef

Browse files
committedFeb 1, 2019
Nicer API for network timeouts
1 parent dcb6104 commit f1040ef

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed
 

‎python/core/auto_generated/qgsnetworkaccessmanager.sip.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@ for the constructor of this class.
234234
bool useSystemProxy() const;
235235
%Docstring
236236
Returns whether the system proxy should be used.
237+
%End
238+
239+
static int timeout();
240+
%Docstring
241+
Returns the network timeout length, in milliseconds.
242+
243+
.. seealso:: :py:func:`setTimeout`
244+
245+
.. versionadded:: 3.6
246+
%End
247+
248+
static void setTimeout( int time );
249+
%Docstring
250+
Sets the maximum timeout ``time`` for network requests, in milliseconds.
251+
252+
.. seealso:: :py:func:`timeout`
253+
254+
.. versionadded:: 3.6
237255
%End
238256

239257
signals:

‎src/app/qgsoptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
321321
connect( mBtnMoveHelpDown, &QAbstractButton::clicked, this, &QgsOptions::moveHelpPathDown );
322322

323323
//Network timeout
324-
mNetworkTimeoutSpinBox->setValue( mSettings->value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt() );
324+
mNetworkTimeoutSpinBox->setValue( QgsNetworkAccessManager::timeout() );
325325
leUserAgent->setText( mSettings->value( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), "Mozilla/5.0" ).toString() );
326326

327327
// WMS capabilities expiry time
@@ -1357,7 +1357,7 @@ void QgsOptions::saveOptions()
13571357
mSettings->setValue( QStringLiteral( "help/helpSearchPath" ), helpPaths );
13581358

13591359
//Network timeout
1360-
mSettings->setValue( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), mNetworkTimeoutSpinBox->value() );
1360+
QgsNetworkAccessManager::setTimeout( mNetworkTimeoutSpinBox->value() );
13611361
mSettings->setValue( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), leUserAgent->text() );
13621362

13631363
// WMS capabiltiies expiry time

‎src/core/qgsnetworkaccessmanager.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
255255
timer->setObjectName( QStringLiteral( "timeoutTimer" ) );
256256
connect( timer, &QTimer::timeout, this, &QgsNetworkAccessManager::abortRequest );
257257
timer->setSingleShot( true );
258-
const int timeout = s.value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt();
259-
timer->start( timeout );
258+
timer->start( timeout() );
260259

261260
connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
262261
connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
@@ -384,7 +383,7 @@ void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
384383
Q_ASSERT( !timer->isActive() );
385384
QgsDebugMsg( QStringLiteral( "Restarting network reply timeout" ) );
386385
timer->setSingleShot( true );
387-
timer->start( QgsSettings().value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt() );
386+
timer->start( timeout() );
388387
}
389388
}
390389

@@ -609,6 +608,16 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
609608
setCache( newcache );
610609
}
611610

611+
int QgsNetworkAccessManager::timeout()
612+
{
613+
return QgsSettings().value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt();
614+
}
615+
616+
void QgsNetworkAccessManager::setTimeout( const int time )
617+
{
618+
QgsSettings().setValue( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), time );
619+
}
620+
612621

613622
//
614623
// QgsNetworkRequestParameters

‎src/core/qgsnetworkaccessmanager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,22 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
398398
*/
399399
bool useSystemProxy() const { return mUseSystemProxy; }
400400

401+
/**
402+
* Returns the network timeout length, in milliseconds.
403+
*
404+
* \see setTimeout()
405+
* \since QGIS 3.6
406+
*/
407+
static int timeout();
408+
409+
/**
410+
* Sets the maximum timeout \a time for network requests, in milliseconds.
411+
*
412+
* \see timeout()
413+
* \since QGIS 3.6
414+
*/
415+
static void setTimeout( int time );
416+
401417
signals:
402418

403419
/**

‎tests/src/core/testqgsnetworkaccessmanager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ void TestQgsNetworkAccessManager::fetchTimeout()
634634
if ( QgsTest::isTravis() )
635635
QSKIP( "This test is disabled on Travis CI environment" );
636636

637+
QgsNetworkAccessManager::setTimeout( 2000 );
638+
QCOMPARE( QgsNetworkAccessManager::timeout(), 2000 );
639+
QgsNetworkAccessManager::setTimeout( 1000 );
640+
637641
QObject context;
638642
//test fetching from a blank url
639643
bool gotRequestAboutToBeCreatedSignal = false;

0 commit comments

Comments
 (0)
Please sign in to comment.