QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsnetworkaccessmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnetworkaccessmanager.cpp
3  This class implements a QNetworkManager with the ability to chain in
4  own proxy factories.
5 
6  -------------------
7  begin : 2010-05-08
8  copyright : (C) 2010 by Juergen E. Fischer
9  email : jef at norbit dot de
10 
11 ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 
23 #include <qgsmessagelog.h>
24 #include <qgslogger.h>
25 
26 #include <QUrl>
27 #include <QSettings>
28 #include <QTimer>
29 #include <QNetworkReply>
30 
31 #if QT_VERSION >= 0x40500
32 class QgsNetworkProxyFactory : public QNetworkProxyFactory
33 {
34  public:
35  QgsNetworkProxyFactory() {}
36  virtual ~QgsNetworkProxyFactory() {}
37 
38  virtual QList<QNetworkProxy> queryProxy( const QNetworkProxyQuery & query = QNetworkProxyQuery() )
39  {
41 
42  // iterate proxies factories and take first non empty list
43  foreach ( QNetworkProxyFactory *f, nam->proxyFactories() )
44  {
45  QList<QNetworkProxy> systemproxies = f->systemProxyForQuery( query );
46  if ( systemproxies.size() > 0 )
47  return systemproxies;
48 
49  QList<QNetworkProxy> proxies = f->queryProxy( query );
50  if ( proxies.size() > 0 )
51  return proxies;
52  }
53 
54  // no proxies from the proxy factor list check for excludes
55  if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
56  return QList<QNetworkProxy>() << nam->fallbackProxy();
57 
58  QString url = query.url().toString();
59 
60  foreach ( QString exclude, nam->excludeList() )
61  {
62  if ( url.startsWith( exclude ) )
63  {
64  QgsDebugMsg( QString( "using default proxy for %1 [exclude %2]" ).arg( url ).arg( exclude ) );
65  return QList<QNetworkProxy>() << QNetworkProxy();
66  }
67  }
68 
69  QgsDebugMsg( QString( "using user proxy for %1" ).arg( url ) );
70  return QList<QNetworkProxy>() << nam->fallbackProxy();
71  }
72 };
73 #endif
74 
76 
78 {
79  if ( smNAM )
80  return smNAM;
81 
83 
84  return smNAM;
85 }
86 
88  : QNetworkAccessManager( parent )
89 {
90 #if QT_VERSION >= 0x40500
91  setProxyFactory( new QgsNetworkProxyFactory() );
92 #endif
93 }
94 
96 {
97 }
98 
99 #if QT_VERSION >= 0x40500
100 void QgsNetworkAccessManager::insertProxyFactory( QNetworkProxyFactory *factory )
101 {
102  mProxyFactories.insert( 0, factory );
103 }
104 
105 void QgsNetworkAccessManager::removeProxyFactory( QNetworkProxyFactory *factory )
106 {
107  mProxyFactories.removeAll( factory );
108 }
109 
110 const QList<QNetworkProxyFactory *> QgsNetworkAccessManager::proxyFactories() const
111 {
112  return mProxyFactories;
113 }
114 #endif
115 
116 const QStringList &QgsNetworkAccessManager::excludeList() const
117 {
118  return mExcludedURLs;
119 }
120 
121 const QNetworkProxy &QgsNetworkAccessManager::fallbackProxy() const
122 {
123  return mFallbackProxy;
124 }
125 
126 void QgsNetworkAccessManager::setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes )
127 {
128  mFallbackProxy = proxy;
129  mExcludedURLs = excludes;
130 }
131 
132 QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData )
133 {
134  emit requestAboutToBeCreated( op, req, outgoingData );
135  QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
136  emit requestCreated( reply );
137 
138  // abort request, when network timeout happens
139  QTimer *timer = new QTimer( reply );
140  connect( timer, SIGNAL( timeout() ), this, SLOT( abortRequest() ) );
141  QSettings s;
142  timer->setSingleShot( true );
143  timer->start( s.value( "/qgis/networkAndProxy/networkTimeout", "20000" ).toInt() );
144 
145  return reply;
146 }
147 
149 {
150  QTimer *timer = qobject_cast<QTimer *>( sender() );
151  Q_ASSERT( timer );
152 
153  QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
154  Q_ASSERT( reply );
155 
156  QgsMessageLog::logMessage( tr( "Network request %1 timed out" ).arg( reply->url().toString() ), tr( "Network" ) );
157 
158  reply->abort();
159 }
160 
161 QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl theControl )
162 {
163  switch ( theControl )
164  {
165  case QNetworkRequest::AlwaysNetwork:
166  return "AlwaysNetwork";
167  break;
168  case QNetworkRequest::PreferNetwork:
169  return "PreferNetwork";
170  break;
171  case QNetworkRequest::PreferCache:
172  return "PreferCache";
173  break;
174  case QNetworkRequest::AlwaysCache:
175  return "AlwaysCache";
176  break;
177  default:
178  break;
179  }
180  return "PreferNetwork";
181 }
182 
183 QNetworkRequest::CacheLoadControl QgsNetworkAccessManager::cacheLoadControlFromName( const QString &theName )
184 {
185  if ( theName == "AlwaysNetwork" )
186  {
187  return QNetworkRequest::AlwaysNetwork;
188  }
189  else if ( theName == "PreferNetwork" )
190  {
191  return QNetworkRequest::PreferNetwork;
192  }
193  else if ( theName == "PreferCache" )
194  {
195  return QNetworkRequest::PreferCache;
196  }
197  else if ( theName == "AlwaysCache" )
198  {
199  return QNetworkRequest::AlwaysCache;
200  }
201  return QNetworkRequest::PreferNetwork;
202 }