Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 /* $Id$ */
22 
24 #include <qgslogger.h>
25 
26 #include <QUrl>
27 
28 #if QT_VERSION >= 0x40500
29 class QgsNetworkProxyFactory : public QNetworkProxyFactory
30 {
31  public:
32  QgsNetworkProxyFactory() {}
33  virtual ~QgsNetworkProxyFactory() {}
34 
35  virtual QList<QNetworkProxy> queryProxy( const QNetworkProxyQuery & query = QNetworkProxyQuery() )
36  {
38 
39  // iterate proxies factories and take first non empty list
40  foreach( QNetworkProxyFactory *f, nam->proxyFactories() )
41  {
42  QList<QNetworkProxy> proxies = f->queryProxy( query );
43  if ( proxies.size() > 0 )
44  return proxies;
45  }
46 
47  // no proxies from the proxy factor list check for excludes
48  if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
49  return QList<QNetworkProxy>() << nam->fallbackProxy();
50 
51  QString url = query.url().toString();
52 
53  foreach( QString exclude, nam->excludeList() )
54  {
55  if ( url.startsWith( exclude ) )
56  {
57  QgsDebugMsg( QString( "using default proxy for %1 [exclude %2]" ).arg( url ).arg( exclude ) );
58  return QList<QNetworkProxy>() << QNetworkProxy();
59  }
60  }
61 
62  QgsDebugMsg( QString( "using user proxy for %1" ).arg( url ) );
63  return QList<QNetworkProxy>() << nam->fallbackProxy();
64  }
65 };
66 #endif
67 
69 
71 {
72  if ( smNAM )
73  return smNAM;
74 
76 
77  return smNAM;
78 }
79 
81  : QNetworkAccessManager( parent )
82 {
83 #if QT_VERSION >= 0x40500
84  setProxyFactory( new QgsNetworkProxyFactory() );
85 #endif
86 }
87 
89 {
90 }
91 
92 #if QT_VERSION >= 0x40500
93 void QgsNetworkAccessManager::insertProxyFactory( QNetworkProxyFactory *factory )
94 {
95  mProxyFactories.insert( 0, factory );
96 }
97 
98 void QgsNetworkAccessManager::removeProxyFactory( QNetworkProxyFactory *factory )
99 {
100  mProxyFactories.removeAll( factory );
101 }
102 
103 const QList<QNetworkProxyFactory *> QgsNetworkAccessManager::proxyFactories() const
104 {
105  return mProxyFactories;
106 }
107 #endif
108 
109 const QStringList &QgsNetworkAccessManager::excludeList() const
110 {
111  return mExcludedURLs;
112 }
113 
114 const QNetworkProxy &QgsNetworkAccessManager::fallbackProxy() const
115 {
116  return mFallbackProxy;
117 }
118 
119 void QgsNetworkAccessManager::setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes )
120 {
121  mFallbackProxy = proxy;
122  mExcludedURLs = excludes;
123 }
124 
125 QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData )
126 {
127  emit requestAboutToBeCreated( op, req, outgoingData );
128  QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
129  emit requestCreated( reply );
130  return reply;
131 }