PageRenderTime 109ms CodeModel.GetById 60ms app.highlight 13ms RepoModel.GetById 27ms app.codeStats 7ms

/thirdparty/liblastfm2/src/ws/NetworkAccessManager.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 159 lines | 107 code | 22 blank | 30 comment | 12 complexity | 921e5e7975f39b7f19e32ffe6822ff8b MD5 | raw file
  1/*
  2   Copyright 2009 Last.fm Ltd. 
  3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4
  5   This file is part of liblastfm.
  6
  7   liblastfm is free software: you can redistribute it and/or modify
  8   it under the terms of the GNU General Public License as published by
  9   the Free Software Foundation, either version 3 of the License, or
 10   (at your option) any later version.
 11
 12   liblastfm is distributed in the hope that it will be useful,
 13   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License
 18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 19*/
 20#include "NetworkAccessManager.h"
 21#include "InternetConnectionMonitor.h"
 22#include <lastfm/ws.h>
 23#include <lastfm/misc.h>
 24#include <QCoreApplication>
 25#include <QNetworkRequest>
 26#ifdef WIN32
 27#include "win/IeSettings.h"
 28#include "win/Pac.h"
 29#endif
 30#ifdef __APPLE__
 31#include "mac/ProxyDict.h"
 32#endif
 33
 34
 35static struct NetworkAccessManagerInit
 36{
 37    // We do this upfront because then our Firehose QTcpSocket will have a proxy 
 38    // set by default. As well as any plain QNetworkAcessManager stuff, and the
 39    // scrobbler
 40    // In theory we should do this every request in case the configuration 
 41    // changes but that is fairly unlikely use case, init? Maybe we should 
 42    // anyway..
 43
 44    NetworkAccessManagerInit()
 45    {
 46    #ifdef WIN32
 47        IeSettings s;
 48        // if it's autodetect, we determine the proxy everytime in proxy()
 49        // we don't really want to do a PAC lookup here, as it times out
 50        // at two seconds, so that hangs startup
 51        if (!s.fAutoDetect && s.lpszProxy)
 52        {
 53            QUrl url( QString::fromUtf16((const unsigned short*)s.lpszProxy) );
 54            QNetworkProxy proxy( QNetworkProxy::HttpProxy );
 55            proxy.setHostName( url.host() );
 56            proxy.setPort( url.port() );
 57            QNetworkProxy::setApplicationProxy( proxy );
 58        }
 59    #endif
 60    #ifdef __APPLE__
 61        ProxyDict dict;
 62        if (dict.isProxyEnabled())
 63        {
 64            QNetworkProxy proxy( QNetworkProxy::HttpProxy );
 65            proxy.setHostName( dict.host );
 66            proxy.setPort( dict.port );
 67
 68            QNetworkProxy::setApplicationProxy( proxy );
 69        }
 70    #endif
 71    }
 72} init;    
 73
 74
 75namespace lastfm 
 76{
 77    LASTFM_DLLEXPORT QByteArray UserAgent;
 78}
 79
 80
 81lastfm::NetworkAccessManager::NetworkAccessManager( QObject* parent )
 82               : QNetworkAccessManager( parent )
 83            #ifdef WIN32
 84               , m_pac( 0 )
 85               , m_monitor( 0 )
 86            #endif
 87{
 88    // can't be done in above init, as applicationName() won't be set
 89    if (lastfm::UserAgent.isEmpty())
 90    {
 91        QByteArray name = QCoreApplication::applicationName().toUtf8();
 92        QByteArray version = QCoreApplication::applicationVersion().toUtf8();
 93        if (version.size()) version.prepend( ' ' );
 94        lastfm::UserAgent = name + version + " (" + lastfm::platform() + ")";
 95    }
 96}
 97
 98
 99lastfm::NetworkAccessManager::~NetworkAccessManager()
100{
101#ifdef WIN32
102    delete m_pac;
103#endif
104}
105
106
107QNetworkProxy
108lastfm::NetworkAccessManager::proxy( const QNetworkRequest& request )
109{   
110    Q_UNUSED( request );
111    
112#ifdef WIN32
113    IeSettings s;
114    if (s.fAutoDetect) 
115    {
116        if (!m_pac) {
117            m_pac = new Pac;
118            if ( !m_monitor )
119            {
120                m_monitor = new InternetConnectionMonitor( this );
121                connect( m_monitor, SIGNAL( connectivityChanged( bool ) ), SLOT( onConnectivityChanged( bool ) ) );
122            }
123        }
124        return m_pac->resolve( request, s.lpszAutoConfigUrl );
125    } 
126#endif
127    
128    return QNetworkProxy::applicationProxy();
129}
130
131
132QNetworkReply*
133lastfm::NetworkAccessManager::createRequest( Operation op, const QNetworkRequest& request_, QIODevice* outgoingData )
134{
135    QNetworkRequest request = request_;
136
137    request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
138    request.setRawHeader( "User-Agent", lastfm::UserAgent );
139    
140#ifdef WIN32
141    // PAC proxies can vary by domain, so we have to check everytime :(
142    QNetworkProxy proxy = this->proxy( request );
143    if (proxy.type() != QNetworkProxy::NoProxy)
144        QNetworkAccessManager::setProxy( proxy );
145#endif
146
147    return QNetworkAccessManager::createRequest( op, request, outgoingData );
148}
149
150
151void
152lastfm::NetworkAccessManager::onConnectivityChanged( bool up )
153{
154    Q_UNUSED( up );
155    
156#ifdef WIN32
157    if (up && m_pac) m_pac->resetFailedState();
158#endif
159}