From bb6da0daa91063160528cda84cc2a35762d9f373 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 27 Jun 2011 15:00:25 -0400 Subject: [PATCH] Fix broken proxying. Not sure when it broke but this is a more robust solution considering that both lastfm and echonest manager their own nams. See notes; liblastfm may be problematic on Windows and Mac. --- .../infoplugins/generic/echonestplugin.cpp | 22 +++++++++- .../infoplugins/generic/lastfmplugin.cpp | 43 +++++++++++++++++-- .../infosystem/infosystemcache.cpp | 4 +- src/libtomahawk/playlist/albummodel.cpp | 2 +- src/libtomahawk/playlist/treemodel.cpp | 2 +- src/tomahawkapp.cpp | 23 +++++----- 6 files changed, 77 insertions(+), 19 deletions(-) diff --git a/src/libtomahawk/infosystem/infoplugins/generic/echonestplugin.cpp b/src/libtomahawk/infosystem/infoplugins/generic/echonestplugin.cpp index 979b84a9c..bca856a2b 100644 --- a/src/libtomahawk/infosystem/infoplugins/generic/echonestplugin.cpp +++ b/src/libtomahawk/infosystem/infoplugins/generic/echonestplugin.cpp @@ -20,6 +20,10 @@ #include #include +#include "utils/tomahawkutils.h" + +#include + using namespace Tomahawk::InfoSystem; using namespace Echonest; @@ -43,9 +47,23 @@ EchoNestPlugin::namChangedSlot( QNetworkAccessManager *nam ) qDebug() << Q_FUNC_INFO; if( !nam ) return; + + QNetworkAccessManager* currNam = Echonest::Config::instance()->nam(); + TomahawkUtils::NetworkProxyFactory* oldProxyFactory = dynamic_cast< TomahawkUtils::NetworkProxyFactory* >( nam->proxyFactory() ); + + if ( !oldProxyFactory ) + { + qDebug() << "Could not get old proxyFactory!"; + return; + } - m_nam = QWeakPointer< QNetworkAccessManager >( nam ); - Echonest::Config::instance()->setNetworkAccessManager( nam ); + currNam->setConfiguration( nam->configuration() ); + currNam->setNetworkAccessible( nam->networkAccessible() ); + TomahawkUtils::NetworkProxyFactory* newProxyFactory = new TomahawkUtils::NetworkProxyFactory(); + newProxyFactory->setNoProxyHosts( oldProxyFactory->noProxyHosts() ); + newProxyFactory->setProxy( oldProxyFactory->proxy() ); + currNam->setProxyFactory( newProxyFactory ); + m_nam = QWeakPointer< QNetworkAccessManager >( currNam ); } void diff --git a/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp b/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp index ded62fd51..387becd3b 100644 --- a/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp +++ b/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "album.h" #include "typedefs.h" @@ -93,10 +94,30 @@ void LastFmPlugin::namChangedSlot( QNetworkAccessManager *nam ) { qDebug() << Q_FUNC_INFO; - if( !nam ) + + if ( !nam ) return; + + QNetworkAccessManager* currNam = lastfm::nam(); + TomahawkUtils::NetworkProxyFactory* oldProxyFactory = dynamic_cast< TomahawkUtils::NetworkProxyFactory* >( nam->proxyFactory() ); + + if ( !oldProxyFactory ) + { + qDebug() << "Could not get old proxyFactory!"; + return; + } + + currNam->setConfiguration( nam->configuration() ); + currNam->setNetworkAccessible( nam->networkAccessible() ); + TomahawkUtils::NetworkProxyFactory* newProxyFactory = new TomahawkUtils::NetworkProxyFactory(); + newProxyFactory->setNoProxyHosts( oldProxyFactory->noProxyHosts() ); + newProxyFactory->setProxy( oldProxyFactory->proxy() ); + currNam->setProxyFactory( newProxyFactory ); + //FIXME: on Mac/Win as liblastfm's network access manager also sets its overriding application proxy + //may have to do a QNetworkProxy::setApplicationProxy and clobber our own factory to override it + + m_nam = QWeakPointer< QNetworkAccessManager >( currNam ); - m_nam = QWeakPointer< QNetworkAccessManager >( nam ); settingsChanged(); // to get the scrobbler set up } @@ -172,7 +193,6 @@ LastFmPlugin::nowPlaying( const QVariant &input ) if ( !hash.contains( "title" ) || !hash.contains( "artist" ) || !hash.contains( "album" ) || !hash.contains( "duration" ) ) return; - qDebug() << "LastFmPlugin::nowPlaying valid criteria hash"; m_track = lastfm::MutableTrack(); m_track.stamp(); @@ -350,6 +370,14 @@ LastFmPlugin::coverArtReturned() if ( redir.isEmpty() ) { QByteArray ba = reply->readAll(); + if ( ba.isNull() || !ba.length() ) + { + qDebug() << "Uh oh, null byte array"; + InfoType type = (Tomahawk::InfoSystem::InfoType)(reply->property( "type" ).toUInt()); + InfoCustomData customData = reply->property( "customData" ).value< Tomahawk::InfoSystem::InfoCustomData >(); + emit info( reply->property( "caller" ).toString(), type, reply->property( "origData" ), QVariant(), customData ); + return; + } foreach ( const QUrl& url, m_badUrls ) { if ( reply->url().toString().startsWith( url.toString() ) ) @@ -409,12 +437,19 @@ LastFmPlugin::artistImagesReturned() if ( redir.isEmpty() ) { QByteArray ba = reply->readAll(); + if ( ba.isNull() || !ba.length() ) + { + qDebug() << "Uh oh, null byte array"; + InfoType type = (Tomahawk::InfoSystem::InfoType)(reply->property( "type" ).toUInt()); + InfoCustomData customData = reply->property( "customData" ).value< Tomahawk::InfoSystem::InfoCustomData >(); + emit info( reply->property( "caller" ).toString(), type, reply->property( "origData" ), QVariant(), customData ); + return; + } foreach ( const QUrl& url, m_badUrls ) { if ( reply->url().toString().startsWith( url.toString() ) ) ba = QByteArray(); } - InfoCustomData returnedData; returnedData["imgbytes"] = ba; returnedData["url"] = reply->url().toString(); diff --git a/src/libtomahawk/infosystem/infosystemcache.cpp b/src/libtomahawk/infosystem/infosystemcache.cpp index 64ef7bd77..0623b32db 100644 --- a/src/libtomahawk/infosystem/infosystemcache.cpp +++ b/src/libtomahawk/infosystem/infosystemcache.cpp @@ -159,13 +159,15 @@ InfoSystemCache::getCachedInfoSlot( const Tomahawk::InfoSystem::InfoCriteriaHash if ( !m_dataCache.contains( criteriaHashVal ) ) { QSettings cachedSettings( fileLocationHash[criteriaHashVal], QSettings::IniFormat ); - QVariant output = cachedSettings.value( "data" ); + QVariant output = cachedSettings.value( "data" ); m_dataCache.insert( criteriaHashVal, new QVariant( output ) ); emit info( caller, type, input, output, customData ); } else + { emit info( caller, type, input, QVariant( *(m_dataCache[criteriaHashVal]) ), customData ); + } } diff --git a/src/libtomahawk/playlist/albummodel.cpp b/src/libtomahawk/playlist/albummodel.cpp index 3db313e8d..bf82bd864 100644 --- a/src/libtomahawk/playlist/albummodel.cpp +++ b/src/libtomahawk/playlist/albummodel.cpp @@ -314,7 +314,7 @@ void AlbumModel::infoSystemInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData ) { Q_UNUSED( customData ); - qDebug() << Q_FUNC_INFO; + qDebug() << Q_FUNC_INFO << " with caller " << caller; if ( caller != s_tmInfoIdentifier || ( type != Tomahawk::InfoSystem::InfoAlbumCoverArt && type != Tomahawk::InfoSystem::InfoArtistImages ) ) diff --git a/src/libtomahawk/playlist/treemodel.cpp b/src/libtomahawk/playlist/treemodel.cpp index c57d17b94..aa9995f52 100644 --- a/src/libtomahawk/playlist/treemodel.cpp +++ b/src/libtomahawk/playlist/treemodel.cpp @@ -602,11 +602,11 @@ TreeModel::infoSystemInfo( QString caller, Tomahawk::InfoSystem::InfoType type, Tomahawk::InfoSystem::InfoCriteriaHash pptr = input.value< Tomahawk::InfoSystem::InfoCriteriaHash >(); Tomahawk::InfoSystem::InfoCustomData returnedData = output.value< Tomahawk::InfoSystem::InfoCustomData >(); const QByteArray ba = returnedData["imgbytes"].toByteArray(); + qDebug() << "ba.length = " << ba.length(); if ( ba.length() ) { QPixmap pm; pm.loadFromData( ba ); - bool ok; qlonglong p = pptr["pptr"].toLongLong( &ok ); TreeModelItem* ai = reinterpret_cast(p); diff --git a/src/tomahawkapp.cpp b/src/tomahawkapp.cpp index 10543e925..20a13f3f6 100644 --- a/src/tomahawkapp.cpp +++ b/src/tomahawkapp.cpp @@ -204,6 +204,14 @@ TomahawkApp::init() new TomahawkSettings( this ); TomahawkSettings* s = TomahawkSettings::instance(); + #ifdef LIBLASTFM_FOUND + qDebug() << "Setting NAM."; + TomahawkUtils::setNam( lastfm::nam() ); + #else + qDebug() << "Setting NAM."; + TomahawkUtils::setNam( new QNetworkAccessManager() ); + #endif + TomahawkUtils::NetworkProxyFactory* proxyFactory = new TomahawkUtils::NetworkProxyFactory(); if( s->proxyType() != QNetworkProxy::NoProxy && @@ -212,20 +220,15 @@ TomahawkApp::init() qDebug() << "Setting proxy to saved values"; QNetworkProxy proxy( static_cast( s->proxyType() ), s->proxyHost(), s->proxyPort(), s->proxyUsername(), s->proxyPassword() ); proxyFactory->setProxy( proxy ); + //TODO: On Windows and Mac because liblastfm sets an application level proxy it may override our factory, so may need to explicitly do + //a QNetworkProxy::setApplicationProxy with our own proxy (but then also overriding our own factory :-( ) } if ( !s->proxyNoProxyHosts().isEmpty() ) proxyFactory->setNoProxyHosts( s->proxyNoProxyHosts().split( ',', QString::SkipEmptyParts ) ); - TomahawkUtils::NetworkProxyFactory::setApplicationProxyFactory( proxyFactory ); - -#ifdef LIBLASTFM_FOUND - qDebug() << "Setting NAM."; - TomahawkUtils::setNam( lastfm::nam() ); -#else - qDebug() << "Setting NAM."; - TomahawkUtils::setNam( new QNetworkAccessManager() ); -#endif - + + TomahawkUtils::setProxyFactory( proxyFactory ); + Echonest::Config::instance()->setAPIKey( "JRIHWEP6GPOER2QQ6" ); m_audioEngine = QWeakPointer( new AudioEngine );