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