3030#include < QSettings>
3131#include < QTimer>
3232#include < QNetworkReply>
33- #include < QNetworkDiskCache >
33+ #include < QThreadStorage >
3434
3535#ifndef QT_NO_OPENSSL
3636#include < QSslConfiguration>
3737#endif
3838
39+ #include " qgsnetworkdiskcache.h"
3940#include " qgsauthmanager.h"
4041
42+ QgsNetworkAccessManager *QgsNetworkAccessManager::smMainNAM = 0 ;
43+
4144// / @cond PRIVATE
4245class QgsNetworkProxyFactory : public QNetworkProxyFactory
4346{
@@ -99,13 +102,22 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
99102//
100103QgsNetworkAccessManager* QgsNetworkAccessManager::instance ()
101104{
102- static QgsNetworkAccessManager* sInstance ( new QgsNetworkAccessManager ( QApplication::instance () ) );
103- return sInstance ;
105+ static QThreadStorage<QgsNetworkAccessManager> sInstances ;
106+ QgsNetworkAccessManager *nam = &sInstances .localData ();
107+
108+ if ( nam->thread () == qApp->thread () )
109+ smMainNAM = nam;
110+
111+ if ( !nam->mInitialized )
112+ nam->setupDefaultProxyAndCache ();
113+
114+ return nam;
104115}
105116
106117QgsNetworkAccessManager::QgsNetworkAccessManager ( QObject *parent )
107118 : QNetworkAccessManager( parent )
108119 , mUseSystemProxy( false )
120+ , mInitialized( false )
109121{
110122 setProxyFactory ( new QgsNetworkProxyFactory () );
111123}
@@ -221,6 +233,8 @@ void QgsNetworkAccessManager::abortRequest()
221233 QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent () );
222234 Q_ASSERT ( reply );
223235
236+ QgsDebugMsg ( QString ( " Abort [reply:%1]" ).arg (( qint64 ) reply, 0 , 16 ) );
237+
224238 QgsMessageLog::logMessage ( tr ( " Network request %1 timed out" ).arg ( reply->url ().toString () ), tr ( " Network" ) );
225239
226240 if ( reply->isRunning () )
@@ -270,36 +284,36 @@ QNetworkRequest::CacheLoadControl QgsNetworkAccessManager::cacheLoadControlFromN
270284
271285void QgsNetworkAccessManager::setupDefaultProxyAndCache ()
272286{
273- QNetworkProxy proxy;
274- QStringList excludes;
275-
276- QSettings settings;
277-
287+ mInitialized = true ;
278288 mUseSystemProxy = false ;
279289
280- if ( this != instance () )
281- {
282- Qt::ConnectionType connectionType = thread () == instance ()->thread () ? Qt::AutoConnection : Qt::BlockingQueuedConnection;
290+ Q_ASSERT ( smMainNAM );
283291
292+ if ( smMainNAM != this )
293+ {
284294 connect ( this , SIGNAL ( authenticationRequired ( QNetworkReply *, QAuthenticator * ) ),
285- instance () , SIGNAL ( authenticationRequired ( QNetworkReply *, QAuthenticator * ) ),
286- connectionType );
295+ smMainNAM , SIGNAL ( authenticationRequired ( QNetworkReply *, QAuthenticator * ) ),
296+ Qt::BlockingQueuedConnection );
287297
288298 connect ( this , SIGNAL ( proxyAuthenticationRequired ( const QNetworkProxy &, QAuthenticator * ) ),
289- instance () , SIGNAL ( proxyAuthenticationRequired ( const QNetworkProxy &, QAuthenticator * ) ),
290- connectionType );
299+ smMainNAM , SIGNAL ( proxyAuthenticationRequired ( const QNetworkProxy &, QAuthenticator * ) ),
300+ Qt::BlockingQueuedConnection );
291301
292302 connect ( this , SIGNAL ( requestTimedOut ( QNetworkReply* ) ),
293- instance () , SIGNAL ( requestTimedOut ( QNetworkReply* ) ) );
303+ smMainNAM , SIGNAL ( requestTimedOut ( QNetworkReply* ) ) );
294304
295305#ifndef QT_NO_OPENSSL
296306 connect ( this , SIGNAL ( sslErrors ( QNetworkReply *, const QList<QSslError> & ) ),
297- instance () , SIGNAL ( sslErrors ( QNetworkReply *, const QList<QSslError> & ) ),
298- connectionType );
307+ smMainNAM , SIGNAL ( sslErrors ( QNetworkReply *, const QList<QSslError> & ) ),
308+ Qt::BlockingQueuedConnection );
299309#endif
300310 }
301311
302312 // check if proxy is enabled
313+ QSettings settings;
314+ QNetworkProxy proxy;
315+ QStringList excludes;
316+
303317 bool proxyEnabled = settings.value ( " proxy/proxyEnabled" , false ).toBool ();
304318 if ( proxyEnabled )
305319 {
@@ -354,9 +368,9 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
354368
355369 setFallbackProxyAndExcludes ( proxy, excludes );
356370
357- QNetworkDiskCache *newcache = qobject_cast<QNetworkDiskCache *>( cache () );
371+ QgsNetworkDiskCache *newcache = qobject_cast<QgsNetworkDiskCache *>( cache () );
358372 if ( !newcache )
359- newcache = new QNetworkDiskCache ( this );
373+ newcache = new QgsNetworkDiskCache ( this );
360374
361375 QString cacheDirectory = settings.value ( " cache/directory" , QgsApplication::qgisSettingsDirPath () + " cache" ).toString ();
362376 qint64 cacheSize = settings.value ( " cache/size" , 50 * 1024 * 1024 ).toULongLong ();
@@ -370,21 +384,3 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
370384 if ( cache () != newcache )
371385 setCache ( newcache );
372386}
373-
374- void QgsNetworkAccessManager::sendGet ( const QNetworkRequest & request )
375- {
376- QgsDebugMsg ( " Entered" );
377- QNetworkReply * reply = get ( request );
378- emit requestSent ( reply, QObject::sender () );
379- }
380-
381- void QgsNetworkAccessManager::deleteReply ( QNetworkReply * reply )
382- {
383- QgsDebugMsg ( " Entered" );
384- if ( !reply )
385- {
386- return ;
387- }
388- reply->abort ();
389- reply->deleteLater ();
390- }
0 commit comments