From 05b6d7507b0302938716050740594c49542d1654 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Mon, 15 Jul 2013 17:54:25 +0200 Subject: [PATCH] Move nam() out of TomahawkUtils.h --- .../hatchet/account/HatchetAccount.cpp | 5 +- src/accounts/xmpp/sip/XmppSip.cpp | 3 +- .../generic/charts/ChartsPlugin.cpp | 7 +- .../generic/discogs/DiscogsPlugin.cpp | 5 +- .../generic/echonest/EchonestPlugin.cpp | 6 +- src/infoplugins/generic/hypem/HypemPlugin.cpp | 3 +- .../generic/musicbrainz/MusicBrainzPlugin.cpp | 7 +- .../generic/musixmatch/MusixMatchPlugin.cpp | 5 +- .../generic/newreleases/NewReleasesPlugin.cpp | 7 +- src/infoplugins/generic/rovi/RoviPlugin.cpp | 3 +- .../generic/spotify/SpotifyPlugin.cpp | 5 +- src/libtomahawk/AtticaManager.cpp | 9 +- src/libtomahawk/CMakeLists.txt | 2 + src/libtomahawk/GlobalActionManager.cpp | 5 +- .../accounts/lastfm/LastFmConfig.cpp | 7 +- .../accounts/lastfm/LastFmInfoPlugin.cpp | 15 +- .../accounts/spotify/SpotifyInfoPlugin.cpp | 5 +- src/libtomahawk/network/Servent.cpp | 5 +- src/libtomahawk/resolvers/JSResolver.cpp | 3 +- .../resolvers/JSResolverHelper.cpp | 3 +- src/libtomahawk/resolvers/ScriptResolver.cpp | 7 +- src/libtomahawk/utils/GroovesharkParser.cpp | 5 +- src/libtomahawk/utils/ItunesParser.cpp | 3 +- src/libtomahawk/utils/JspfLoader.cpp | 5 +- .../utils/NetworkAccessManager.cpp | 278 ++++++++++++++++++ src/libtomahawk/utils/NetworkAccessManager.h | 38 +++ src/libtomahawk/utils/NetworkProxyFactory.cpp | 24 ++ src/libtomahawk/utils/NetworkProxyFactory.h | 67 +++++ src/libtomahawk/utils/NetworkReply.cpp | 7 +- src/libtomahawk/utils/RdioParser.cpp | 3 +- src/libtomahawk/utils/ResultUrlChecker.cpp | 3 +- src/libtomahawk/utils/ShortenedLinkParser.cpp | 3 +- src/libtomahawk/utils/SpotifyParser.cpp | 5 +- src/libtomahawk/utils/TomahawkUtils.cpp | 237 --------------- src/libtomahawk/utils/TomahawkUtils.h | 31 -- src/libtomahawk/utils/XspfLoader.cpp | 5 +- src/tomahawk/SettingsDialog.cpp | 4 +- src/tomahawk/TomahawkApp.cpp | 7 +- src/tomahawk/TomahawkWindow.cpp | 4 +- 39 files changed, 511 insertions(+), 335 deletions(-) create mode 100644 src/libtomahawk/utils/NetworkAccessManager.cpp create mode 100644 src/libtomahawk/utils/NetworkAccessManager.h create mode 100644 src/libtomahawk/utils/NetworkProxyFactory.cpp create mode 100644 src/libtomahawk/utils/NetworkProxyFactory.h diff --git a/src/accounts/hatchet/account/HatchetAccount.cpp b/src/accounts/hatchet/account/HatchetAccount.cpp index 7cf840409..35bca4d06 100644 --- a/src/accounts/hatchet/account/HatchetAccount.cpp +++ b/src/accounts/hatchet/account/HatchetAccount.cpp @@ -24,6 +24,7 @@ #include "utils/Logger.h" #include "sip/HatchetSip.h" #include "utils/TomahawkUtils.h" +#include "utils/NetworkAccessManager.cpp" #include #include @@ -243,7 +244,7 @@ HatchetAccount::loginWithPassword( const QString& username, const QString& passw QNetworkRequest req( QUrl( c_loginServer + "/auth/credentials") ); req.setHeader( QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8" ); - QNetworkReply* reply = TomahawkUtils::nam()->post( req, msgJson ); + QNetworkReply* reply = Tomahawk::Utils::nam()->post( req, msgJson ); NewClosure( reply, SIGNAL( finished() ), this, SLOT( onPasswordLoginFinished( QNetworkReply*, const QString& ) ), reply, username ); } @@ -264,7 +265,7 @@ HatchetAccount::fetchAccessTokens( const QString& type ) tLog() << "Fetching access tokens"; QNetworkRequest req( QUrl( c_accessTokenServer + "/tokens/" + type + "?authtoken=" + authToken() ) ); - QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( req ); connect( reply, SIGNAL( finished() ), this, SLOT( onFetchAccessTokensFinished() ) ); } diff --git a/src/accounts/xmpp/sip/XmppSip.cpp b/src/accounts/xmpp/sip/XmppSip.cpp index 530f65ed6..3ec9d6e7e 100644 --- a/src/accounts/xmpp/sip/XmppSip.cpp +++ b/src/accounts/xmpp/sip/XmppSip.cpp @@ -32,6 +32,7 @@ #include "TomahawkSettings.h" #include "utils/TomahawkUtilsGui.h" #include "sip/PeerInfo.h" +#include "utils/NetworkProxyFactory.h" #include "config.h" #include "TomahawkVersion.h" @@ -604,7 +605,7 @@ XmppSipPlugin::configurationChanged() void XmppSipPlugin::setupClientHelper() { - m_client->setProxyFactory( TomahawkUtils::proxyFactory( true ) ); + m_client->setProxyFactory( Tomahawk::Utils::proxyFactory( true ) ); Jreen::JID jid = Jreen::JID( m_currentUsername ); m_client->setJID( jid ); m_client->setPassword( m_currentPassword ); diff --git a/src/infoplugins/generic/charts/ChartsPlugin.cpp b/src/infoplugins/generic/charts/ChartsPlugin.cpp index e1e2d3b51..8fd9734ca 100644 --- a/src/infoplugins/generic/charts/ChartsPlugin.cpp +++ b/src/infoplugins/generic/charts/ChartsPlugin.cpp @@ -29,6 +29,7 @@ #include "utils/TomahawkUtils.h" #include "utils/Logger.h" #include "utils/TomahawkCache.h" +#include "utils/NetworkAccessManager.h" #include "Source.h" #include @@ -280,7 +281,7 @@ ChartsPlugin::fetchChartSourcesList( bool fetchOnlySourceList ) TomahawkUtils::urlAddQueryItem( url, "version", TomahawkUtils::appFriendlyVersion() ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "only_source_list", fetchOnlySourceList ); connect( reply, SIGNAL( finished() ), SLOT( chartSourcesList() ) ); @@ -376,7 +377,7 @@ ChartsPlugin::fetchSource(const QString& source) QUrl url = QUrl( QString( CHART_URL "charts/%1" ).arg( source ) ); TomahawkUtils::urlAddQueryItem( url, "version", TomahawkUtils::appFriendlyVersion() ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "chart_source", source ); tDebug() << Q_FUNC_INFO << "fetching:" << url; @@ -422,7 +423,7 @@ ChartsPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData, con tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "fetching: " << url; - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) ); diff --git a/src/infoplugins/generic/discogs/DiscogsPlugin.cpp b/src/infoplugins/generic/discogs/DiscogsPlugin.cpp index acf066b87..e25b4d9d3 100644 --- a/src/infoplugins/generic/discogs/DiscogsPlugin.cpp +++ b/src/infoplugins/generic/discogs/DiscogsPlugin.cpp @@ -22,6 +22,7 @@ #include "utils/TomahawkUtils.h" #include "utils/Logger.h" #include "utils/Closure.h" +#include "utils/NetworkAccessManager.h" #include @@ -97,7 +98,7 @@ DiscogsPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requestD QNetworkRequest req( url ); req.setRawHeader( "User-Agent", "TomahawkPlayer/1.0 +http://tomahawk-player.org" ); - QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( req ); NewClosure( reply, SIGNAL( finished() ), this, SLOT( albumSearchSlot( Tomahawk::InfoSystem::InfoRequestData, QNetworkReply* ) ), requestData, reply ); break; @@ -136,7 +137,7 @@ DiscogsPlugin::albumSearchSlot( const InfoRequestData &requestData, QNetworkRepl QNetworkRequest req( url ); req.setRawHeader( "User-Agent", "TomahawkPlayer/1.0 +http://tomahawk-player.org" ); - QNetworkReply* reply2 = TomahawkUtils::nam()->get( req ); + QNetworkReply* reply2 = Tomahawk::Utils::nam()->get( req ); NewClosure( reply2, SIGNAL( finished() ), this, SLOT( albumInfoSlot( Tomahawk::InfoSystem::InfoRequestData, QNetworkReply* ) ), requestData, reply2 ); } diff --git a/src/infoplugins/generic/echonest/EchonestPlugin.cpp b/src/infoplugins/generic/echonest/EchonestPlugin.cpp index e2bbb22f8..c0135a0b5 100644 --- a/src/infoplugins/generic/echonest/EchonestPlugin.cpp +++ b/src/infoplugins/generic/echonest/EchonestPlugin.cpp @@ -18,10 +18,12 @@ */ #include "EchonestPlugin.h" -#include #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" + +#include #include @@ -50,7 +52,7 @@ EchonestPlugin::~EchonestPlugin() void EchonestPlugin::init() { - Echonest::Config::instance()->setNetworkAccessManager( TomahawkUtils::nam() ); + Echonest::Config::instance()->setNetworkAccessManager( Tomahawk::Utils::nam() ); } diff --git a/src/infoplugins/generic/hypem/HypemPlugin.cpp b/src/infoplugins/generic/hypem/HypemPlugin.cpp index 4210c0717..67adeac61 100644 --- a/src/infoplugins/generic/hypem/HypemPlugin.cpp +++ b/src/infoplugins/generic/hypem/HypemPlugin.cpp @@ -31,6 +31,7 @@ #include "utils/TomahawkUtils.h" #include "infosystem/InfoSystemWorker.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include "Source.h" #define HYPEM_URL "http://hypem.com/playlist/" @@ -204,7 +205,7 @@ HypemPlugin::notInCacheSlot( QHash criteria, Tomahawk::InfoSys QUrl url = QUrl( QString( HYPEM_URL "%1/%2" ).arg( criteria["chart_id"].toLower() ).arg(HYPEM_END_URL) ); qDebug() << Q_FUNC_INFO << "Getting chart url" << url; - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) ); return; diff --git a/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp b/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp index d378658b0..92b7b0ab2 100644 --- a/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp +++ b/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp @@ -21,6 +21,7 @@ #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include #include @@ -110,7 +111,7 @@ MusicBrainzPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ TomahawkUtils::urlAddQueryItem( url, "limit", "100" ); tDebug() << Q_FUNC_INFO << url.toString(); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect( reply, SIGNAL( finished() ), SLOT( gotReleaseGroupsSlot() ) ); @@ -130,7 +131,7 @@ MusicBrainzPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ TomahawkUtils::urlAddQueryItem( url, "limit", "100" ); tDebug() << Q_FUNC_INFO << url.toString(); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect( reply, SIGNAL( finished() ), SLOT( gotReleasesSlot() ) ); @@ -236,7 +237,7 @@ MusicBrainzPlugin::gotReleasesSlot() TomahawkUtils::urlAddQueryItem( url, "inc", "recordings" ); tDebug() << Q_FUNC_INFO << url.toString(); - QNetworkReply* newReply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); newReply->setProperty( "requestData", oldReply->property( "requestData" ) ); connect( newReply, SIGNAL( finished() ), SLOT( gotRecordingsSlot() ) ); diff --git a/src/infoplugins/generic/musixmatch/MusixMatchPlugin.cpp b/src/infoplugins/generic/musixmatch/MusixMatchPlugin.cpp index 550ea20cc..be8f8ef1c 100644 --- a/src/infoplugins/generic/musixmatch/MusixMatchPlugin.cpp +++ b/src/infoplugins/generic/musixmatch/MusixMatchPlugin.cpp @@ -21,6 +21,7 @@ #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include #include @@ -68,7 +69,7 @@ MusixMatchPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) TomahawkUtils::urlAddQueryItem( url, "q_artist", artist ); TomahawkUtils::urlAddQueryItem( url, "q_track", track ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect( reply, SIGNAL( finished() ), SLOT( trackSearchSlot() ) ); @@ -128,7 +129,7 @@ MusixMatchPlugin::trackSearchSlot() TomahawkUtils::urlAddQueryItem( url, "apikey", m_apiKey ); TomahawkUtils::urlAddQueryItem( url, "track_id", track_id ); - QNetworkReply* newReply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); newReply->setProperty( "requestData", oldReply->property( "requestData" ) ); connect( newReply, SIGNAL( finished() ), SLOT( trackLyricsSlot() ) ); } diff --git a/src/infoplugins/generic/newreleases/NewReleasesPlugin.cpp b/src/infoplugins/generic/newreleases/NewReleasesPlugin.cpp index 856eb980d..b640ec529 100644 --- a/src/infoplugins/generic/newreleases/NewReleasesPlugin.cpp +++ b/src/infoplugins/generic/newreleases/NewReleasesPlugin.cpp @@ -30,6 +30,7 @@ #include "utils/Logger.h" #include "utils/TomahawkCache.h" #include "Source.h" +#include "utils/NetworkAccessManager.h" #include #include @@ -268,7 +269,7 @@ NewReleasesPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ TomahawkUtils::urlAddQueryItem( url, "version", TomahawkUtils::appFriendlyVersion() ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest ( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest ( url ) ); reply->setProperty( "only_source_list", true ); tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "fetching:" << url; @@ -401,7 +402,7 @@ NewReleasesPlugin::fetchAllNRSources() TomahawkUtils::urlAddQueryItem( url, "version", TomahawkUtils::appFriendlyVersion() ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "nr_source", source[ "nr_source" ] ); tDebug() << Q_FUNC_INFO << "fetching:" << url; @@ -423,7 +424,7 @@ NewReleasesPlugin::fetchNR( InfoRequestData requestData, const QString& source, tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "fetching: " << url; - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect ( reply, SIGNAL( finished() ), SLOT( nrReturned() ) ); diff --git a/src/infoplugins/generic/rovi/RoviPlugin.cpp b/src/infoplugins/generic/rovi/RoviPlugin.cpp index 803236b54..550271a31 100644 --- a/src/infoplugins/generic/rovi/RoviPlugin.cpp +++ b/src/infoplugins/generic/rovi/RoviPlugin.cpp @@ -20,6 +20,7 @@ #include "RoviPlugin.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include @@ -183,7 +184,7 @@ RoviPlugin::makeRequest( QUrl url ) TomahawkUtils::urlAddQueryItem( url, "sig", generateSig() ); qDebug() << "Rovi request url:" << url.toString(); - return TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + return Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); } diff --git a/src/infoplugins/generic/spotify/SpotifyPlugin.cpp b/src/infoplugins/generic/spotify/SpotifyPlugin.cpp index 2d594262b..593a27115 100644 --- a/src/infoplugins/generic/spotify/SpotifyPlugin.cpp +++ b/src/infoplugins/generic/spotify/SpotifyPlugin.cpp @@ -31,6 +31,7 @@ #include "TomahawkSettings.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include "CountryUtils.h" #include "Source.h" @@ -147,7 +148,7 @@ SpotifyPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, To QUrl url = QUrl( QString( SPOTIFY_API_URL "toplist/%1/" ).arg( criteria["chart_id"] ) ); qDebug() << Q_FUNC_INFO << "Getting chart url" << url; - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) ); return; @@ -164,7 +165,7 @@ SpotifyPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, To tDebug() << "SpotifyPlugin: InfoChart fetching possible resources"; QUrl url = QUrl( QString( SPOTIFY_API_URL "toplist/charts" ) ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); tDebug() << Q_FUNC_INFO << "fetching:" << url; connect( reply, SIGNAL( finished() ), SLOT( chartTypes() ) ); m_chartsFetchJobs++; diff --git a/src/libtomahawk/AtticaManager.cpp b/src/libtomahawk/AtticaManager.cpp index f72e01ed8..1e2214a3d 100644 --- a/src/libtomahawk/AtticaManager.cpp +++ b/src/libtomahawk/AtticaManager.cpp @@ -29,6 +29,7 @@ #include "accounts/AccountManager.h" #include "utils/BinaryInstallerHelper.h" #include "utils/Closure.h" +#include "utils/NetworkAccessManager.h" #include @@ -68,7 +69,7 @@ AtticaManager::AtticaManager( QObject* parent ) // m_manager.addProviderFile( QUrl( "http://bakery.tomahawk-player.org/resolvers/providers.xml" ) ); const QString url = QString( "%1/resolvers/providers.xml?version=%2" ).arg( hostname() ).arg( TomahawkUtils::appFriendlyVersion() ); - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( url ) ) ); NewClosure( reply, SIGNAL( finished() ), this, SLOT( providerFetched( QNetworkReply* ) ), reply ); connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( providerError( QNetworkReply::NetworkError ) ) ); @@ -103,7 +104,7 @@ AtticaManager::fetchMissingIcons() if ( !m_resolverStates.value( resolver.id() ).pixmap && !resolver.icons().isEmpty() && !resolver.icons().first().url().isEmpty() ) { - QNetworkReply* fetch = TomahawkUtils::nam()->get( QNetworkRequest( resolver.icons().first().url() ) ); + QNetworkReply* fetch = Tomahawk::Utils::nam()->get( QNetworkRequest( resolver.icons().first().url() ) ); fetch->setProperty( "resolverId", resolver.id() ); connect( fetch, SIGNAL( finished() ), this, SLOT( resolverIconFetched() ) ); @@ -550,7 +551,7 @@ void AtticaManager::doInstallResolver( const Content& resolver, bool autoCreate, QUrl url( QString( "%1/resolvers/v1/content/download/%2/1" ).arg( hostname() ).arg( resolver.id() ) ); TomahawkUtils::urlAddQueryItem( url, "tomahawkversion", TomahawkUtils::appFriendlyVersion() ); - QNetworkReply* r = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* r = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); NewClosure( r, SIGNAL( finished() ), this, SLOT( resolverDownloadFinished( QNetworkReply* ) ), r ); r->setProperty( "resolverId", resolver.id() ); r->setProperty( "createAccount", autoCreate ); @@ -611,7 +612,7 @@ AtticaManager::resolverDownloadFinished ( QNetworkReply *j ) tLog() << "Found overridden signature in binary download:" << sig; signature = sig; } - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ); connect( reply, SIGNAL( finished() ), this, SLOT( payloadFetched() ) ); reply->setProperty( "resolverId", j->property( "resolverId" ) ); reply->setProperty( "createAccount", j->property( "createAccount" ) ); diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index fe9417648..4d70fe1b1 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -124,6 +124,8 @@ set( libGuiSources utils/SharedTimeLine.cpp utils/ResultUrlChecker.cpp utils/NetworkReply.cpp + utils/NetworkProxyFactory.cpp + utils/NetworkAccessManager.cpp widgets/AnimatedCounterLabel.cpp widgets/BasicHeader.cpp diff --git a/src/libtomahawk/GlobalActionManager.cpp b/src/libtomahawk/GlobalActionManager.cpp index 5ba15e433..91eee3b18 100644 --- a/src/libtomahawk/GlobalActionManager.cpp +++ b/src/libtomahawk/GlobalActionManager.cpp @@ -38,6 +38,7 @@ #include "utils/TomahawkUtils.h" #include "utils/XspfLoader.h" #include "utils/XspfGenerator.h" +#include "utils/NetworkAccessManager.h" #include "widgets/SearchWidget.h" #include "Album.h" @@ -157,7 +158,7 @@ GlobalActionManager::shortenLink( const QUrl& url, const QVariant& callbackObj ) request.setUrl( url ); qDebug() << "Doing lookup:" << url.toEncoded(); - QNetworkReply *reply = TomahawkUtils::nam()->get( request ); + QNetworkReply *reply = Tomahawk::Utils::nam()->get( request ); if ( callbackObj.isValid() ) reply->setProperty( "callbackobj", callbackObj ); connect( reply, SIGNAL( finished() ), SLOT( shortenLinkRequestFinished() ) ); @@ -245,7 +246,7 @@ GlobalActionManager::getShortLink( const playlist_ptr& pl ) const QUrl url( QString( "%1/p/").arg( hostname() ) ); QNetworkRequest req( url ); req.setHeader( QNetworkRequest::ContentTypeHeader, QString( "multipart/form-data; boundary=%1" ).arg( QString::fromLatin1( boundary ) ) ); - QNetworkReply *reply = TomahawkUtils::nam()->post( req, data ); + QNetworkReply *reply = Tomahawk::Utils::nam()->post( req, data ); connect( reply, SIGNAL( finished() ), SLOT( postShortenFinished() ) ); connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), SLOT( shortenLinkRequestError( QNetworkReply::NetworkError ) ) ); diff --git a/src/libtomahawk/accounts/lastfm/LastFmConfig.cpp b/src/libtomahawk/accounts/lastfm/LastFmConfig.cpp index 9d0fa442b..085ae9b20 100644 --- a/src/libtomahawk/accounts/lastfm/LastFmConfig.cpp +++ b/src/libtomahawk/accounts/lastfm/LastFmConfig.cpp @@ -19,8 +19,6 @@ #include "LastFmConfig.h" #include "ui_LastFmConfig.h" -#include - #include "LastFmAccount.h" #include "database/Database.h" #include "database/DatabaseCommand_LogPlayback.h" @@ -29,12 +27,15 @@ #include "utils/TomahawkUtils.h" #include "utils/Logger.h" #include "utils/Closure.h" +#include "utils/NetworkAccessManager.h" #include #include #include #include +#include + using namespace Tomahawk::Accounts; @@ -101,7 +102,7 @@ LastFmConfig::testLogin() query[ "authToken" ] = authToken; // ensure they have up-to-date settings - lastfm::setNetworkAccessManager( TomahawkUtils::nam() ); + lastfm::setNetworkAccessManager( Tomahawk::Utils::nam() ); QNetworkReply* authJob = lastfm::ws::post( query ); diff --git a/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp b/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp index d61b90c5b..6ca02f615 100644 --- a/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp +++ b/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp @@ -32,6 +32,7 @@ #include "accounts/lastfm/LastFmAccount.h" #include "Source.h" #include "TomahawkSettings.h" +#include "utils/NetworkAccessManager.h" #include #include @@ -64,7 +65,7 @@ LastFmInfoPlugin::init() lastfm::ws::ApiKey = "7194b85b6d1f424fe1668173a78c0c4a"; lastfm::ws::SharedSecret = "ba80f1df6d27ae63e9cb1d33ccf2052f"; lastfm::ws::Username = m_account.data()->username(); - lastfm::setNetworkAccessManager( TomahawkUtils::nam() ); + lastfm::setNetworkAccessManager( Tomahawk::Utils::nam() ); m_pw = m_account.data()->password(); @@ -411,7 +412,7 @@ LastFmInfoPlugin::fetchAlbumInfo( Tomahawk::InfoSystem::InfoRequestData requestD void LastFmInfoPlugin::notInCacheSlot( QHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) { - if ( !TomahawkUtils::nam() ) + if ( !Tomahawk::Utils::nam() ) { tLog() << "Have a null QNAM, uh oh"; emit info( requestData, QVariant() ); @@ -761,7 +762,7 @@ LastFmInfoPlugin::artistInfoReturned() imgurl = artist.imageUrl( lastfm::AbstractType::LargeImage ); QNetworkRequest req( imgurl ); - QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( req ); newReply->setProperty( "requestData", reply->property( "requestData" ) ); connect( newReply, SIGNAL( finished() ), SLOT( coverArtReturned() ) ); } @@ -786,7 +787,7 @@ LastFmInfoPlugin::albumInfoReturned() imgurl = QUrl( lfm["album"]["image size=large"].text() ); QNetworkRequest req( imgurl ); - QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( req ); newReply->setProperty( "requestData", reply->property( "requestData" ) ); connect( newReply, SIGNAL( finished() ), SLOT( coverArtReturned() ) ); } @@ -833,7 +834,7 @@ LastFmInfoPlugin::coverArtReturned() { // Follow HTTP redirect QNetworkRequest req( redir ); - QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( req ); newReply->setProperty( "requestData", reply->property( "requestData" ) ); connect( newReply, SIGNAL( finished() ), SLOT( coverArtReturned() ) ); } @@ -877,7 +878,7 @@ LastFmInfoPlugin::artistImagesReturned() } else { - if ( !TomahawkUtils::nam() ) + if ( !Tomahawk::Utils::nam() ) { tLog() << Q_FUNC_INFO << "Uh oh, nam is null"; emit info( reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant() ); @@ -885,7 +886,7 @@ LastFmInfoPlugin::artistImagesReturned() } // Follow HTTP redirect QNetworkRequest req( redir ); - QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( req ); newReply->setProperty( "requestData", reply->property( "requestData" ) ); connect( newReply, SIGNAL( finished() ), SLOT( artistImagesReturned() ) ); } diff --git a/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp b/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp index aa0fcc154..41f41d5a6 100644 --- a/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp +++ b/src/libtomahawk/accounts/spotify/SpotifyInfoPlugin.cpp @@ -21,6 +21,7 @@ #include "SpotifyAccount.h" #include "utils/Closure.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include @@ -144,7 +145,7 @@ SpotifyInfoPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ QUrl lookupUrl( "http://ws.spotify.com/search/1/album.json" ); TomahawkUtils::urlAddQueryItem( lookupUrl, "q", QString( "%1 %2" ).arg( artist ).arg( album ) ); - QNetworkReply * reply = TomahawkUtils::nam()->get( QNetworkRequest( lookupUrl ) ); + QNetworkReply * reply = Tomahawk::Utils::nam()->get( QNetworkRequest( lookupUrl ) ); NewClosure( reply, SIGNAL( finished() ), this, SLOT( albumIdLookupFinished( QNetworkReply*, Tomahawk::InfoSystem::InfoRequestData ) ), reply, requestData ); } else @@ -234,7 +235,7 @@ SpotifyInfoPlugin::albumIdLookupFinished( QNetworkReply* reply, const InfoReques QUrl lookupUrl( QString( "http://spotikea.tomahawk-player.org/browse/%1" ).arg( id ) ); - QNetworkReply * reply = TomahawkUtils::nam()->get( QNetworkRequest( lookupUrl ) ); + QNetworkReply * reply = Tomahawk::Utils::nam()->get( QNetworkRequest( lookupUrl ) ); NewClosure( reply, SIGNAL( finished() ), this, SLOT( albumContentsLookupFinished( QNetworkReply*, Tomahawk::InfoSystem::InfoRequestData ) ), reply, requestData ); } else diff --git a/src/libtomahawk/network/Servent.cpp b/src/libtomahawk/network/Servent.cpp index 4702ce7c9..5e548c692 100644 --- a/src/libtomahawk/network/Servent.cpp +++ b/src/libtomahawk/network/Servent.cpp @@ -33,6 +33,7 @@ #include "utils/Closure.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include "AclRegistry.h" #include "BufferIoDevice.h" @@ -183,7 +184,7 @@ Servent::startListening( QHostAddress ha, bool upnp, int port, Tomahawk::Network d->externalPort = externalPort; if ( autoDetectExternalIp ) { - QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( "http://toma.hk/?stat=1" ) ) ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( "http://toma.hk/?stat=1" ) ) ); connect( reply, SIGNAL( finished() ), SLOT( ipDetected() ) ); // Not emitting ready here as we are not done. } @@ -1385,7 +1386,7 @@ Servent::httpIODeviceFactory( const Tomahawk::result_ptr& result, boost::function< void ( QSharedPointer< QIODevice >& ) > callback ) { QNetworkRequest req( result->url() ); - QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( req ); //boost::functions cannot accept temporaries as parameters QSharedPointer< QIODevice > sp = QSharedPointer< QIODevice >( reply, &QObject::deleteLater ); diff --git a/src/libtomahawk/resolvers/JSResolver.cpp b/src/libtomahawk/resolvers/JSResolver.cpp index ed25ec529..92a5174b1 100644 --- a/src/libtomahawk/resolvers/JSResolver.cpp +++ b/src/libtomahawk/resolvers/JSResolver.cpp @@ -40,6 +40,7 @@ #include "SourceList.h" #include "TomahawkSettings.h" #include "TomahawkVersion.h" +#include "utils/NetworkAccessManager.h" #include #include @@ -873,7 +874,7 @@ JSResolver::loadCollections() { QNetworkRequest req( iconUrl ); tDebug() << "Creating a QNetworkReply with url:" << req.url().toString(); - QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( req ); reply->setProperty( "collectionName", collection->name() ); connect( reply, SIGNAL( finished() ), diff --git a/src/libtomahawk/resolvers/JSResolverHelper.cpp b/src/libtomahawk/resolvers/JSResolverHelper.cpp index c8907b581..fbab2c9e1 100644 --- a/src/libtomahawk/resolvers/JSResolverHelper.cpp +++ b/src/libtomahawk/resolvers/JSResolverHelper.cpp @@ -27,6 +27,7 @@ #include "resolvers/ScriptEngine.h" #include "network/Servent.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include "config.h" #include "JSResolver_p.h" @@ -446,7 +447,7 @@ JSResolverHelper::returnStreamUrl( const QString& streamUrl, boost::function< vo QUrl url = QUrl::fromEncoded( streamUrl.toUtf8() ); QNetworkRequest req( url ); tDebug() << "Creating a QNetowrkReply with url:" << req.url().toString(); - QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( req ); //boost::functions cannot accept temporaries as parameters sp = QSharedPointer< QIODevice >( reply, &QObject::deleteLater ); diff --git a/src/libtomahawk/resolvers/ScriptResolver.cpp b/src/libtomahawk/resolvers/ScriptResolver.cpp index 7449888ce..36ee11417 100644 --- a/src/libtomahawk/resolvers/ScriptResolver.cpp +++ b/src/libtomahawk/resolvers/ScriptResolver.cpp @@ -30,6 +30,9 @@ #include "accounts/AccountConfigWidget.h" #include "utils/TomahawkUtilsGui.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" +#include "utils/NetworkProxyFactory.h" + #include #include @@ -57,7 +60,7 @@ ScriptResolver::ScriptResolver( const QString& exe ) startProcess(); - if ( !TomahawkUtils::nam() ) + if ( !Tomahawk::Utils::nam() ) return; // set the name to the binary, if we launch properly we'll get the name the resolver reports @@ -136,7 +139,7 @@ ScriptResolver::sendConfig() m_configSent = true; - TomahawkUtils::NetworkProxyFactory* factory = dynamic_cast( TomahawkUtils::nam()->proxyFactory() ); + Tomahawk::Utils::NetworkProxyFactory* factory = dynamic_cast( Tomahawk::Utils::nam()->proxyFactory() ); QNetworkProxy proxy = factory->proxy(); QString proxyType = ( proxy.type() == QNetworkProxy::Socks5Proxy ? "socks5" : "none" ); m.insert( "proxytype", proxyType ); diff --git a/src/libtomahawk/utils/GroovesharkParser.cpp b/src/libtomahawk/utils/GroovesharkParser.cpp index 65a5f19b1..11da43879 100644 --- a/src/libtomahawk/utils/GroovesharkParser.cpp +++ b/src/libtomahawk/utils/GroovesharkParser.cpp @@ -42,6 +42,7 @@ #include "utils/NetworkReply.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" using namespace Tomahawk; @@ -133,7 +134,7 @@ GroovesharkParser::lookupGroovesharkPlaylist( const QString& linkRaw ) QString hash = QCA::arrayToHex( resultArray.toByteArray() ); QUrl url = QUrl( base_url + hash ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->post( QNetworkRequest( url ), data ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->post( QNetworkRequest( url ), data ) ); connect( reply, SIGNAL( finished() ), SLOT( groovesharkLookupFinished() ) ); #ifndef ENABLE_HEADLESS @@ -150,7 +151,7 @@ GroovesharkParser::lookupGroovesharkTrack( const QString& track ) { tLog() << "Parsing Grooveshark Track Page:" << track; - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( track ) ) ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( track ) ) ) ); connect( reply, SIGNAL( finished() ), SLOT( trackPageFetchFinished() ) ); #ifndef ENABLE_HEADLESS diff --git a/src/libtomahawk/utils/ItunesParser.cpp b/src/libtomahawk/utils/ItunesParser.cpp index 9959da5b4..118c807c4 100644 --- a/src/libtomahawk/utils/ItunesParser.cpp +++ b/src/libtomahawk/utils/ItunesParser.cpp @@ -33,6 +33,7 @@ #include "utils/NetworkReply.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" // Forward Declarations breaking QSharedPointer #if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 ) @@ -112,7 +113,7 @@ ItunesParser::lookupItunesUri( const QString& link ) url = QUrl( QString( "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsLookup?id=%1&entity=song" ).arg( ( trackId.isEmpty() ? id : trackId ) ) ); } - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( url ) ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ) ); connect( reply, SIGNAL( finished() ), SLOT( itunesResponseLookupFinished() ) ); #ifndef ENABLE_HEADLESS diff --git a/src/libtomahawk/utils/JspfLoader.cpp b/src/libtomahawk/utils/JspfLoader.cpp index cf4ad1f76..32658c40b 100644 --- a/src/libtomahawk/utils/JspfLoader.cpp +++ b/src/libtomahawk/utils/JspfLoader.cpp @@ -22,6 +22,7 @@ #include "utils/Logger.h" #include "utils/NetworkReply.h" #include "utils/TomahawkUtils.h" +#include "utils/NetworkAccessManager.h" #include "Playlist.h" #include "SourceList.h" @@ -61,8 +62,8 @@ JSPFLoader::load( const QUrl& url ) { QNetworkRequest request( url ); - Q_ASSERT( TomahawkUtils::nam() != 0 ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( request ) ); + Q_ASSERT( Tomahawk::Utils::nam() != 0 ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( request ) ); connect( reply, SIGNAL( finished() ), SLOT( networkLoadFinished() ) ); connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), SLOT( networkError( QNetworkReply::NetworkError ) ) ); diff --git a/src/libtomahawk/utils/NetworkAccessManager.cpp b/src/libtomahawk/utils/NetworkAccessManager.cpp new file mode 100644 index 000000000..e64992e7a --- /dev/null +++ b/src/libtomahawk/utils/NetworkAccessManager.cpp @@ -0,0 +1,278 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Dominik Schmidt + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ +#include "NetworkAccessManager.h" + +#include "NetworkProxyFactory.h" +#include "utils/Logger.h" + +// TODO: get rid of this here! +#include "TomahawkSettings.h" + + +#include +#include +#include +#include +#include + +namespace Tomahawk +{ +namespace Utils +{ + +static QMutex s_noProxyHostsMutex; +static QStringList s_noProxyHosts; + +NetworkProxyFactory::NetworkProxyFactory( const NetworkProxyFactory& other ) +{ + m_proxy = QNetworkProxy( other.m_proxy ); +} + + +QList< QNetworkProxy > +NetworkProxyFactory::queryProxy( const QNetworkProxyQuery& query ) +{ + //tDebug() << Q_FUNC_INFO << "query hostname is" << query.peerHostName() << ", proxy host is" << m_proxy.hostName(); + + QList< QNetworkProxy > proxies; + QString hostname = query.peerHostName(); + s_noProxyHostsMutex.lock(); + if ( !hostname.isEmpty() && s_noProxyHosts.contains( hostname ) ) + proxies << QNetworkProxy::NoProxy << systemProxyForQuery( query ); + else if ( m_proxy.hostName().isEmpty() || TomahawkSettings::instance()->proxyType() == QNetworkProxy::NoProxy ) + proxies << systemProxyForQuery( query ); + else + proxies << m_proxy << systemProxyForQuery( query ); + s_noProxyHostsMutex.unlock(); + return proxies; +} + + +void +NetworkProxyFactory::setNoProxyHosts( const QStringList& hosts ) +{ + QStringList newList; + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "No-proxy hosts:" << hosts; + foreach ( const QString& host, hosts ) + { + QString munge = host.simplified(); + newList << munge; + //TODO: wildcard support + } + + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "New no-proxy hosts:" << newList; + + s_noProxyHostsMutex.lock(); + s_noProxyHosts = newList; + s_noProxyHostsMutex.unlock(); +} + + +void +NetworkProxyFactory::setProxy( const QNetworkProxy& proxy ) +{ + m_proxyChanged = false; + if ( m_proxy != proxy ) + m_proxyChanged = true; + + m_proxy = proxy; + QFlags< QNetworkProxy::Capability > proxyCaps; + proxyCaps |= QNetworkProxy::TunnelingCapability; + proxyCaps |= QNetworkProxy::ListeningCapability; + if ( TomahawkSettings::instance()->proxyDns() ) + proxyCaps |= QNetworkProxy::HostNameLookupCapability; + + m_proxy.setCapabilities( proxyCaps ); + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Proxy using host" << proxy.hostName() << "and port" << proxy.port(); + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "setting proxy to use proxy DNS?" << (TomahawkSettings::instance()->proxyDns() ? "true" : "false"); +} + + +NetworkProxyFactory& +NetworkProxyFactory::operator=( const NetworkProxyFactory& rhs ) +{ + tDebug( LOGVERBOSE ) << Q_FUNC_INFO; + if ( this != &rhs ) + { + m_proxy = QNetworkProxy( rhs.m_proxy ); + } + + return *this; +} + + +bool NetworkProxyFactory::operator==( const NetworkProxyFactory& other ) const +{ + tDebug( LOGVERBOSE ) << Q_FUNC_INFO; + if ( m_proxy != other.m_proxy ) + return false; + + return true; +} + +static QMap< QThread*, QNetworkAccessManager* > s_threadNamHash; +static QMap< QThread*, NetworkProxyFactory* > s_threadProxyFactoryHash; +static QMutex s_namAccessMutex; + +NetworkProxyFactory* +proxyFactory( bool makeClone, bool noMutexLocker ) +{ + // Don't lock if being called from nam() + tDebug( LOGVERBOSE ) << Q_FUNC_INFO; + QMutex otherMutex; + QMutexLocker locker( noMutexLocker ? &otherMutex : &s_namAccessMutex ); + + if ( !makeClone ) + { + if ( s_threadProxyFactoryHash.contains( QThread::currentThread() ) ) + return s_threadProxyFactoryHash[ QThread::currentThread() ]; + } + + // create a new proxy factory for this thread + NetworkProxyFactory *newProxyFactory = new NetworkProxyFactory(); + if ( s_threadProxyFactoryHash.contains( QCoreApplication::instance()->thread() ) ) + { + NetworkProxyFactory *mainProxyFactory = s_threadProxyFactoryHash[ QCoreApplication::instance()->thread() ]; + *newProxyFactory = *mainProxyFactory; + } + + if ( !makeClone ) + s_threadProxyFactoryHash[ QThread::currentThread() ] = newProxyFactory; + + return newProxyFactory; +} + + +void +setProxyFactory( NetworkProxyFactory* factory, bool noMutexLocker ) +{ + tDebug( LOGVERBOSE ) << Q_FUNC_INFO; + Q_ASSERT( factory ); + // Don't lock if being called from setNam() + QMutex otherMutex; + QMutexLocker locker( noMutexLocker ? &otherMutex : &s_namAccessMutex ); + + if ( !s_threadProxyFactoryHash.contains( QCoreApplication::instance()->thread() ) ) + return; + + if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) + { + foreach ( QThread* thread, s_threadProxyFactoryHash.keys() ) + { + if ( thread != QThread::currentThread() ) + { + NetworkProxyFactory *currFactory = s_threadProxyFactoryHash[ thread ]; + *currFactory = *factory; + } + } + QNetworkProxyFactory::setApplicationProxyFactory( factory ); + } + + *s_threadProxyFactoryHash[ QThread::currentThread() ] = *factory; +} + + +QNetworkAccessManager* +nam() +{ + QMutexLocker locker( &s_namAccessMutex ); + if ( s_threadNamHash.contains( QThread::currentThread() ) ) + { + //tDebug() << Q_FUNC_INFO << "Found current thread in nam hash"; + return s_threadNamHash[ QThread::currentThread() ]; + } + + if ( !s_threadNamHash.contains( QCoreApplication::instance()->thread() ) ) + { + if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) + { + setNam( new QNetworkAccessManager(), true ); + return s_threadNamHash[ QThread::currentThread() ]; + } + else + return 0; + } + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Found gui thread in nam hash"; + + // Create a nam for this thread based on the main thread's settings but with its own proxyfactory + QNetworkAccessManager *mainNam = s_threadNamHash[ QCoreApplication::instance()->thread() ]; + QNetworkAccessManager* newNam = new QNetworkAccessManager(); + + newNam->setConfiguration( QNetworkConfiguration( mainNam->configuration() ) ); + newNam->setNetworkAccessible( mainNam->networkAccessible() ); + newNam->setProxyFactory( proxyFactory( false, true ) ); + + s_threadNamHash[ QThread::currentThread() ] = newNam; + + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "created new nam for thread" << QThread::currentThread(); + //QNetworkProxy proxy = dynamic_cast< NetworkProxyFactory* >( newNam->proxyFactory() )->proxy(); + //tDebug() << Q_FUNC_INFO << "reply proxy properties:" << proxy.type() << proxy.hostName() << proxy.port(); + + return newNam; +} + + +void +setNam( QNetworkAccessManager* nam, bool noMutexLocker ) +{ + Q_ASSERT( nam ); + // Don't lock if being called from nam()() + QMutex otherMutex; + QMutexLocker locker( noMutexLocker ? &otherMutex : &s_namAccessMutex ); + if ( !s_threadNamHash.contains( QCoreApplication::instance()->thread() ) && + QThread::currentThread() == QCoreApplication::instance()->thread() ) + { + tDebug( LOGVERBOSE ) << "creating initial gui thread (" << QCoreApplication::instance()->thread() << ") nam"; + // Should only get here on first initialization of the nam + TomahawkSettings *s = TomahawkSettings::instance(); + NetworkProxyFactory* proxyFactory = new NetworkProxyFactory(); + if ( s->proxyType() != QNetworkProxy::NoProxy && !s->proxyHost().isEmpty() ) + { + tDebug( LOGVERBOSE ) << "Setting proxy to saved values"; + QNetworkProxy proxy( s->proxyType(), s->proxyHost(), s->proxyPort(), s->proxyUsername(), s->proxyPassword() ); + proxyFactory->setProxy( proxy ); + //FIXME: Jreen is broke without this + //QNetworkProxy::setApplicationProxy( proxy ); + s_noProxyHostsMutex.lock(); + if ( !s->proxyNoProxyHosts().isEmpty() && s_noProxyHosts.isEmpty() ) + { + s_noProxyHostsMutex.unlock(); + proxyFactory->setNoProxyHosts( s->proxyNoProxyHosts().split( ',', QString::SkipEmptyParts ) ); + } + else + s_noProxyHostsMutex.unlock(); + } + + QNetworkProxyFactory::setApplicationProxyFactory( proxyFactory ); + nam->setProxyFactory( proxyFactory ); + s_threadNamHash[ QThread::currentThread() ] = nam; + s_threadProxyFactoryHash[ QThread::currentThread() ] = proxyFactory; + return; + } + + s_threadNamHash[ QThread::currentThread() ] = nam; + + if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) + setProxyFactory( dynamic_cast< NetworkProxyFactory* >( nam->proxyFactory() ), true ); +} + +} +} diff --git a/src/libtomahawk/utils/NetworkAccessManager.h b/src/libtomahawk/utils/NetworkAccessManager.h new file mode 100644 index 000000000..937001f95 --- /dev/null +++ b/src/libtomahawk/utils/NetworkAccessManager.h @@ -0,0 +1,38 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Dominik Schmidt + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef TOMAHAWK_UTILS_NETWORKACCESSMANAGER_H +#define TOMAHAWK_UTILS_NETWORKACCESSMANAGER_H + +#include "DllMacro.h" + +class QNetworkAccessManager; + +namespace Tomahawk +{ +namespace Utils +{ + DLLEXPORT QNetworkAccessManager* nam(); + DLLEXPORT void setNam( QNetworkAccessManager* nam, bool noMutexLocker = false ); +} +} + +#endif // TOMAHAWK_UTILS_NETWORKACCESSMANAGER_H diff --git a/src/libtomahawk/utils/NetworkProxyFactory.cpp b/src/libtomahawk/utils/NetworkProxyFactory.cpp new file mode 100644 index 000000000..7eb8405f7 --- /dev/null +++ b/src/libtomahawk/utils/NetworkProxyFactory.cpp @@ -0,0 +1,24 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Dominik Schmidt + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "NetworkProxyFactory.h" + +//MAJORHACK: all implementation in NetworkAccessManager.cpp even for NetworkProxyFactory.h because they share private s_* vars :C diff --git a/src/libtomahawk/utils/NetworkProxyFactory.h b/src/libtomahawk/utils/NetworkProxyFactory.h new file mode 100644 index 000000000..36750837a --- /dev/null +++ b/src/libtomahawk/utils/NetworkProxyFactory.h @@ -0,0 +1,67 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Dominik Schmidt + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef TOMAHAWK_UTILS_NETWORKPROXYFACTORY_H +#define TOMAHAWK_UTILS_NETWORKPROXYFACTORY_H + +#include +#include + +#include "DllMacro.h" + +namespace Tomahawk +{ +namespace Utils +{ + class DLLEXPORT NetworkProxyFactory : public QNetworkProxyFactory + { + public: + NetworkProxyFactory() + : m_proxy( QNetworkProxy::NoProxy ) + , m_proxyChanged( false ) + {} + + NetworkProxyFactory( const NetworkProxyFactory &other ); + virtual ~NetworkProxyFactory() {} + + virtual QList< QNetworkProxy > queryProxy( const QNetworkProxyQuery & query = QNetworkProxyQuery() ); + + virtual void setNoProxyHosts( const QStringList &hosts ); + virtual QStringList noProxyHosts() const { return m_noProxyHosts; } + virtual void setProxy( const QNetworkProxy &proxy ); + virtual QNetworkProxy proxy() { return m_proxy; } + + virtual NetworkProxyFactory& operator=( const NetworkProxyFactory &rhs ); + virtual bool operator==( const NetworkProxyFactory &other ) const; + bool changed() const { return m_proxyChanged; } + + private: + QStringList m_noProxyHosts; + QNetworkProxy m_proxy; + bool m_proxyChanged; + }; + + DLLEXPORT void setProxyFactory( NetworkProxyFactory* factory, bool noMutexLocker = false ); + DLLEXPORT NetworkProxyFactory* proxyFactory( bool makeClone = false, bool noMutexLocker = false ); +} +} + +#endif // TOMAHAWK_UTILS_NETWORKPROXYFACTORY_H diff --git a/src/libtomahawk/utils/NetworkReply.cpp b/src/libtomahawk/utils/NetworkReply.cpp index 45f5873e9..fb2f4d4b9 100644 --- a/src/libtomahawk/utils/NetworkReply.cpp +++ b/src/libtomahawk/utils/NetworkReply.cpp @@ -20,6 +20,7 @@ #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include @@ -62,7 +63,7 @@ NetworkReply::load( const QUrl& url ) m_url = url; QNetworkRequest request( url ); - Q_ASSERT( TomahawkUtils::nam() != 0 ); + Q_ASSERT( Tomahawk::Utils::nam() != 0 ); QNetworkAccessManager::Operation op = m_reply->operation(); m_reply->deleteLater(); @@ -70,11 +71,11 @@ NetworkReply::load( const QUrl& url ) switch ( op ) { case QNetworkAccessManager::HeadOperation: - m_reply = TomahawkUtils::nam()->head( request ); + m_reply = Tomahawk::Utils::nam()->head( request ); break; default: - m_reply = TomahawkUtils::nam()->get( request ); + m_reply = Tomahawk::Utils::nam()->get( request ); } connect( m_reply, SIGNAL( finished() ), SLOT( networkLoadFinished() ) ); diff --git a/src/libtomahawk/utils/RdioParser.cpp b/src/libtomahawk/utils/RdioParser.cpp index 004ab0da7..c0fcf3270 100644 --- a/src/libtomahawk/utils/RdioParser.cpp +++ b/src/libtomahawk/utils/RdioParser.cpp @@ -31,6 +31,7 @@ #include "utils/NetworkReply.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include @@ -130,7 +131,7 @@ RdioParser::fetchObjectsFromUrl( const QString& url, DropJob::DropType type ) QNetworkRequest request = generateRequest( "getObjectFromUrl", cleanedUrl, params, &data ); request.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1String( "application/x-www-form-urlencoded" ) ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->post( request, data ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->post( request, data ) ); connect( reply, SIGNAL( finished() ), SLOT( rdioReturned() ) ); #ifndef ENABLE_HEADLESS diff --git a/src/libtomahawk/utils/ResultUrlChecker.cpp b/src/libtomahawk/utils/ResultUrlChecker.cpp index be131ab68..ab6edc3e3 100644 --- a/src/libtomahawk/utils/ResultUrlChecker.cpp +++ b/src/libtomahawk/utils/ResultUrlChecker.cpp @@ -27,6 +27,7 @@ #include "Result.h" #include "Source.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" using namespace Tomahawk; @@ -55,7 +56,7 @@ ResultUrlChecker::check() if ( url.isEmpty() || !url.toString().startsWith( "http" ) ) continue; - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( url ) ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->head( QNetworkRequest( url ) ) ); m_replies.insert( reply, result ); connect( reply, SIGNAL( finished() ), SLOT( headFinished() ) ); } diff --git a/src/libtomahawk/utils/ShortenedLinkParser.cpp b/src/libtomahawk/utils/ShortenedLinkParser.cpp index 73433487c..3ec524bc5 100644 --- a/src/libtomahawk/utils/ShortenedLinkParser.cpp +++ b/src/libtomahawk/utils/ShortenedLinkParser.cpp @@ -32,6 +32,7 @@ #include "utils/NetworkReply.h" #include "utils/TomahawkUtilsGui.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" using namespace Tomahawk; @@ -78,7 +79,7 @@ ShortenedLinkParser::lookupUrl( const QString& url ) if ( cleaned.contains( "/#/s/" ) ) cleaned.replace( "/#", "" ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( cleaned ) ) ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( cleaned ) ) ) ); connect( reply, SIGNAL( finished() ), SLOT( lookupFinished() ) ); m_queries.insert( reply ); diff --git a/src/libtomahawk/utils/SpotifyParser.cpp b/src/libtomahawk/utils/SpotifyParser.cpp index 295f63ebb..e9849472f 100644 --- a/src/libtomahawk/utils/SpotifyParser.cpp +++ b/src/libtomahawk/utils/SpotifyParser.cpp @@ -34,6 +34,7 @@ #include "utils/NetworkReply.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" using namespace Tomahawk; @@ -175,7 +176,7 @@ SpotifyParser::lookupSpotifyBrowse( const QString& link ) url = QUrl( QString( SPOTIFY_PLAYLIST_API_URL "/browse/%1/%2" ).arg( m_browseUri ) .arg ( m_limit ) ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( url ) ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ) ); connect( reply, SIGNAL( finished() ), SLOT( spotifyBrowseFinished() ) ); #ifndef ENABLE_HEADLESS @@ -204,7 +205,7 @@ SpotifyParser::lookupTrack( const QString& link ) QUrl url = QUrl( QString( "http://ws.spotify.com/lookup/1/.json?uri=%1" ).arg( uri ) ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( url ) ) ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( url ) ) ); connect( reply, SIGNAL( finished() ), SLOT( spotifyTrackLookupFinished() ) ); #ifndef ENABLE_HEADLESS diff --git a/src/libtomahawk/utils/TomahawkUtils.cpp b/src/libtomahawk/utils/TomahawkUtils.cpp index d6a6559e6..95d3e5454 100644 --- a/src/libtomahawk/utils/TomahawkUtils.cpp +++ b/src/libtomahawk/utils/TomahawkUtils.cpp @@ -428,243 +428,6 @@ levenshtein( const QString& source, const QString& target ) } -static QMutex s_noProxyHostsMutex; -static QStringList s_noProxyHosts; - -NetworkProxyFactory::NetworkProxyFactory( const NetworkProxyFactory& other ) -{ - m_proxy = QNetworkProxy( other.m_proxy ); -} - - -QList< QNetworkProxy > -NetworkProxyFactory::queryProxy( const QNetworkProxyQuery& query ) -{ - //tDebug() << Q_FUNC_INFO << "query hostname is" << query.peerHostName() << ", proxy host is" << m_proxy.hostName(); - - QList< QNetworkProxy > proxies; - QString hostname = query.peerHostName(); - s_noProxyHostsMutex.lock(); - if ( !hostname.isEmpty() && s_noProxyHosts.contains( hostname ) ) - proxies << QNetworkProxy::NoProxy << systemProxyForQuery( query ); - else if ( m_proxy.hostName().isEmpty() || TomahawkSettings::instance()->proxyType() == QNetworkProxy::NoProxy ) - proxies << systemProxyForQuery( query ); - else - proxies << m_proxy << systemProxyForQuery( query ); - s_noProxyHostsMutex.unlock(); - return proxies; -} - - -void -NetworkProxyFactory::setNoProxyHosts( const QStringList& hosts ) -{ - QStringList newList; - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "No-proxy hosts:" << hosts; - foreach ( const QString& host, hosts ) - { - QString munge = host.simplified(); - newList << munge; - //TODO: wildcard support - } - - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "New no-proxy hosts:" << newList; - - s_noProxyHostsMutex.lock(); - s_noProxyHosts = newList; - s_noProxyHostsMutex.unlock(); -} - - -void -NetworkProxyFactory::setProxy( const QNetworkProxy& proxy ) -{ - m_proxyChanged = false; - if ( m_proxy != proxy ) - m_proxyChanged = true; - - m_proxy = proxy; - QFlags< QNetworkProxy::Capability > proxyCaps; - proxyCaps |= QNetworkProxy::TunnelingCapability; - proxyCaps |= QNetworkProxy::ListeningCapability; - if ( TomahawkSettings::instance()->proxyDns() ) - proxyCaps |= QNetworkProxy::HostNameLookupCapability; - - m_proxy.setCapabilities( proxyCaps ); - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Proxy using host" << proxy.hostName() << "and port" << proxy.port(); - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "setting proxy to use proxy DNS?" << (TomahawkSettings::instance()->proxyDns() ? "true" : "false"); -} - - -NetworkProxyFactory& -NetworkProxyFactory::operator=( const NetworkProxyFactory& rhs ) -{ - tDebug( LOGVERBOSE ) << Q_FUNC_INFO; - if ( this != &rhs ) - { - m_proxy = QNetworkProxy( rhs.m_proxy ); - } - - return *this; -} - - -bool NetworkProxyFactory::operator==( const NetworkProxyFactory& other ) const -{ - tDebug( LOGVERBOSE ) << Q_FUNC_INFO; - if ( m_proxy != other.m_proxy ) - return false; - - return true; -} - -static QMap< QThread*, QNetworkAccessManager* > s_threadNamHash; -static QMap< QThread*, NetworkProxyFactory* > s_threadProxyFactoryHash; -static QMutex s_namAccessMutex; - -NetworkProxyFactory* -proxyFactory( bool makeClone, bool noMutexLocker ) -{ - // Don't lock if being called from nam() - tDebug( LOGVERBOSE ) << Q_FUNC_INFO; - QMutex otherMutex; - QMutexLocker locker( noMutexLocker ? &otherMutex : &s_namAccessMutex ); - - if ( !makeClone ) - { - if ( s_threadProxyFactoryHash.contains( QThread::currentThread() ) ) - return s_threadProxyFactoryHash[ QThread::currentThread() ]; - } - - // create a new proxy factory for this thread - TomahawkUtils::NetworkProxyFactory *newProxyFactory = new TomahawkUtils::NetworkProxyFactory(); - if ( s_threadProxyFactoryHash.contains( QCoreApplication::instance()->thread() ) ) - { - TomahawkUtils::NetworkProxyFactory *mainProxyFactory = s_threadProxyFactoryHash[ QCoreApplication::instance()->thread() ]; - *newProxyFactory = *mainProxyFactory; - } - - if ( !makeClone ) - s_threadProxyFactoryHash[ QThread::currentThread() ] = newProxyFactory; - - return newProxyFactory; -} - - -void -setProxyFactory( NetworkProxyFactory* factory, bool noMutexLocker ) -{ - tDebug( LOGVERBOSE ) << Q_FUNC_INFO; - Q_ASSERT( factory ); - // Don't lock if being called from setNam() - QMutex otherMutex; - QMutexLocker locker( noMutexLocker ? &otherMutex : &s_namAccessMutex ); - - if ( !s_threadProxyFactoryHash.contains( QCoreApplication::instance()->thread() ) ) - return; - - if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) - { - foreach ( QThread* thread, s_threadProxyFactoryHash.keys() ) - { - if ( thread != QThread::currentThread() ) - { - TomahawkUtils::NetworkProxyFactory *currFactory = s_threadProxyFactoryHash[ thread ]; - *currFactory = *factory; - } - } - QNetworkProxyFactory::setApplicationProxyFactory( factory ); - } - - *s_threadProxyFactoryHash[ QThread::currentThread() ] = *factory; -} - - -QNetworkAccessManager* -nam() -{ - QMutexLocker locker( &s_namAccessMutex ); - if ( s_threadNamHash.contains( QThread::currentThread() ) ) - { - //tDebug() << Q_FUNC_INFO << "Found current thread in nam hash"; - return s_threadNamHash[ QThread::currentThread() ]; - } - - if ( !s_threadNamHash.contains( QCoreApplication::instance()->thread() ) ) - { - if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) - { - setNam( new QNetworkAccessManager(), true ); - return s_threadNamHash[ QThread::currentThread() ]; - } - else - return 0; - } - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Found gui thread in nam hash"; - - // Create a nam for this thread based on the main thread's settings but with its own proxyfactory - QNetworkAccessManager *mainNam = s_threadNamHash[ QCoreApplication::instance()->thread() ]; - QNetworkAccessManager* newNam = new QNetworkAccessManager(); - - newNam->setConfiguration( QNetworkConfiguration( mainNam->configuration() ) ); - newNam->setNetworkAccessible( mainNam->networkAccessible() ); - newNam->setProxyFactory( proxyFactory( false, true ) ); - - s_threadNamHash[ QThread::currentThread() ] = newNam; - - tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "created new nam for thread" << QThread::currentThread(); - //QNetworkProxy proxy = dynamic_cast< TomahawkUtils::NetworkProxyFactory* >( newNam->proxyFactory() )->proxy(); - //tDebug() << Q_FUNC_INFO << "reply proxy properties:" << proxy.type() << proxy.hostName() << proxy.port(); - - return newNam; -} - - -void -setNam( QNetworkAccessManager* nam, bool noMutexLocker ) -{ - Q_ASSERT( nam ); - // Don't lock if being called from nam()() - QMutex otherMutex; - QMutexLocker locker( noMutexLocker ? &otherMutex : &s_namAccessMutex ); - if ( !s_threadNamHash.contains( QCoreApplication::instance()->thread() ) && - QThread::currentThread() == QCoreApplication::instance()->thread() ) - { - tDebug( LOGVERBOSE ) << "creating initial gui thread (" << QCoreApplication::instance()->thread() << ") nam"; - // Should only get here on first initialization of the nam - TomahawkSettings *s = TomahawkSettings::instance(); - TomahawkUtils::NetworkProxyFactory* proxyFactory = new TomahawkUtils::NetworkProxyFactory(); - if ( s->proxyType() != QNetworkProxy::NoProxy && !s->proxyHost().isEmpty() ) - { - tDebug( LOGVERBOSE ) << "Setting proxy to saved values"; - QNetworkProxy proxy( s->proxyType(), s->proxyHost(), s->proxyPort(), s->proxyUsername(), s->proxyPassword() ); - proxyFactory->setProxy( proxy ); - //FIXME: Jreen is broke without this - //QNetworkProxy::setApplicationProxy( proxy ); - s_noProxyHostsMutex.lock(); - if ( !s->proxyNoProxyHosts().isEmpty() && s_noProxyHosts.isEmpty() ) - { - s_noProxyHostsMutex.unlock(); - proxyFactory->setNoProxyHosts( s->proxyNoProxyHosts().split( ',', QString::SkipEmptyParts ) ); - } - else - s_noProxyHostsMutex.unlock(); - } - - QNetworkProxyFactory::setApplicationProxyFactory( proxyFactory ); - nam->setProxyFactory( proxyFactory ); - s_threadNamHash[ QThread::currentThread() ] = nam; - s_threadProxyFactoryHash[ QThread::currentThread() ] = proxyFactory; - return; - } - - s_threadNamHash[ QThread::currentThread() ] = nam; - - if ( QThread::currentThread() == QCoreApplication::instance()->thread() ) - setProxyFactory( dynamic_cast< TomahawkUtils::NetworkProxyFactory* >( nam->proxyFactory() ), true ); -} - - bool newerVersion( const QString& oldVersion, const QString& newVersion ) { diff --git a/src/libtomahawk/utils/TomahawkUtils.h b/src/libtomahawk/utils/TomahawkUtils.h index a48187430..b8b4dba68 100644 --- a/src/libtomahawk/utils/TomahawkUtils.h +++ b/src/libtomahawk/utils/TomahawkUtils.h @@ -138,33 +138,6 @@ namespace TomahawkUtils }; - class DLLEXPORT NetworkProxyFactory : public QNetworkProxyFactory - { - public: - NetworkProxyFactory() - : m_proxy( QNetworkProxy::NoProxy ) - , m_proxyChanged( false ) - {} - - NetworkProxyFactory( const NetworkProxyFactory &other ); - virtual ~NetworkProxyFactory() {} - - virtual QList< QNetworkProxy > queryProxy( const QNetworkProxyQuery & query = QNetworkProxyQuery() ); - - virtual void setNoProxyHosts( const QStringList &hosts ); - virtual QStringList noProxyHosts() const { return m_noProxyHosts; } - virtual void setProxy( const QNetworkProxy &proxy ); - virtual QNetworkProxy proxy() { return m_proxy; } - - virtual NetworkProxyFactory& operator=( const NetworkProxyFactory &rhs ); - virtual bool operator==( const NetworkProxyFactory &other ) const; - bool changed() const { return m_proxyChanged; } - private: - QStringList m_noProxyHosts; - QNetworkProxy m_proxy; - bool m_proxyChanged; - }; - DLLEXPORT bool headless(); DLLEXPORT void setHeadless( bool headless ); @@ -185,10 +158,6 @@ namespace TomahawkUtils DLLEXPORT bool newerVersion( const QString& oldVersion, const QString& newVersion ); DLLEXPORT int levenshtein( const QString& source, const QString& target ); - DLLEXPORT NetworkProxyFactory* proxyFactory( bool makeClone = false, bool noMutexLocker = false ); - DLLEXPORT void setProxyFactory( TomahawkUtils::NetworkProxyFactory* factory, bool noMutexLocker = false ); - DLLEXPORT QNetworkAccessManager* nam(); - DLLEXPORT void setNam( QNetworkAccessManager* nam, bool noMutexLocker = false ); DLLEXPORT quint64 infosystemRequestId(); DLLEXPORT QString md5( const QByteArray& data ); diff --git a/src/libtomahawk/utils/XspfLoader.cpp b/src/libtomahawk/utils/XspfLoader.cpp index 0d044373f..be3af176c 100644 --- a/src/libtomahawk/utils/XspfLoader.cpp +++ b/src/libtomahawk/utils/XspfLoader.cpp @@ -30,6 +30,7 @@ #include "utils/NetworkReply.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" +#include "utils/NetworkAccessManager.h" #include "Pipeline.h" #include "Playlist.h" @@ -102,8 +103,8 @@ XSPFLoader::load( const QUrl& url ) m_url = url; QNetworkRequest request( url ); - Q_ASSERT( TomahawkUtils::nam() != 0 ); - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( request ) ); + Q_ASSERT( Tomahawk::Utils::nam() != 0 ); + NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( request ) ); connect( reply, SIGNAL( finished() ), SLOT( networkLoadFinished() ) ); connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), SLOT( networkError( QNetworkReply::NetworkError ) ) ); diff --git a/src/tomahawk/SettingsDialog.cpp b/src/tomahawk/SettingsDialog.cpp index bfc0775ab..75fa66a24 100644 --- a/src/tomahawk/SettingsDialog.cpp +++ b/src/tomahawk/SettingsDialog.cpp @@ -54,6 +54,8 @@ #include "jobview/JobStatusView.h" #include "jobview/JobStatusModel.h" #include "jobview/ErrorStatusMessage.h" +#include "utils/NetworkAccessManager.h" +#include "utils/NetworkProxyFactory.h" #include #include @@ -290,7 +292,7 @@ SettingsDialog::saveSettings() m_collectionWidgetUi->dirTree->cleanup(); - TomahawkUtils::NetworkProxyFactory* proxyFactory = TomahawkUtils::proxyFactory(); + Tomahawk::Utils::NetworkProxyFactory* proxyFactory = Tomahawk::Utils::proxyFactory(); if ( !m_advancedWidgetUi->enableProxyCheckBox->isChecked() ) { tDebug() << Q_FUNC_INFO << "Got NoProxy selected"; diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index d82503b4c..d7ec1895b 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -50,6 +50,7 @@ #include "EchonestCatalogSynchronizer.h" #include "database/DatabaseImpl.h" #include "network/Msg.h" +#include "utils/NetworkAccessManager.h" #include "accounts/lastfm/LastFmAccount.h" #include "accounts/spotify/SpotifyAccount.h" @@ -198,7 +199,7 @@ TomahawkApp::init() tDebug( LOGINFO ) << "Setting NAM."; // Cause the creation of the nam, but don't need to address it directly, so prevent warning - Q_UNUSED( TomahawkUtils::nam() ); + Q_UNUSED( Tomahawk::Utils::nam() ); m_audioEngine = QPointer( new AudioEngine ); @@ -617,7 +618,7 @@ TomahawkApp::onInfoSystemReady() TomahawkSettings* s = TomahawkSettings::instance(); - Echonest::Config::instance()->setNetworkAccessManager( TomahawkUtils::nam() ); + Echonest::Config::instance()->setNetworkAccessManager( Tomahawk::Utils::nam() ); #ifndef ENABLE_HEADLESS EchonestGenerator::setupCatalogs(); @@ -687,7 +688,7 @@ TomahawkApp::onInfoSystemReady() GlobalActionManager::instance(); // check if our spotify playlist api server is up and running, and enable spotify playlist drops if so - QNetworkReply* r = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( SPOTIFY_PLAYLIST_API_URL "/pong" ) ) ); + QNetworkReply* r = Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( SPOTIFY_PLAYLIST_API_URL "/pong" ) ) ); connect( r, SIGNAL( finished() ), this, SLOT( spotifyApiCheckFinished() ) ); #endif diff --git a/src/tomahawk/TomahawkWindow.cpp b/src/tomahawk/TomahawkWindow.cpp index 3711a53b7..8a4f8f7bc 100644 --- a/src/tomahawk/TomahawkWindow.cpp +++ b/src/tomahawk/TomahawkWindow.cpp @@ -443,8 +443,8 @@ TomahawkWindow::setupUpdateCheck() updaterUrl.setUrl( "http://download.tomahawk-player.org/sparklewin" ); qtsparkle::Updater* updater = new qtsparkle::Updater( updaterUrl, this ); - Q_ASSERT( TomahawkUtils::nam() != 0 ); - updater->SetNetworkAccessManager( TomahawkUtils::nam() ); + Q_ASSERT( Tomahawk::Utils::nam() != 0 ); + updater->SetNetworkAccessManager( Tomahawk::Utils::nam() ); updater->SetVersion( TomahawkUtils::appFriendlyVersion() ); connect( ActionCollection::instance()->getAction( "checkForUpdates" ), SIGNAL( triggered() ),