diff --git a/src/libtomahawk/resolvers/ScriptInfoPlugin.cpp b/src/libtomahawk/resolvers/ScriptInfoPlugin.cpp index 9634faaaa..aa08bf067 100644 --- a/src/libtomahawk/resolvers/ScriptInfoPlugin.cpp +++ b/src/libtomahawk/resolvers/ScriptInfoPlugin.cpp @@ -25,6 +25,7 @@ #include "../utils/Logger.h" #include "../utils/Json.h" +#include "../utils/NetworkAccessManager.h" using namespace Tomahawk; @@ -145,22 +146,79 @@ ScriptInfoPlugin::onNotInCacheRequestDone( const QVariantMap& result ) Q_D( ScriptInfoPlugin ); ScriptJob* job = qobject_cast< ScriptJob* >( sender() ); + sender()->deleteLater(); // retrieve requestData from cache and delete it Tomahawk::InfoSystem::InfoRequestData requestData = d->requestDataCache[ job->id().toInt() ]; d->requestDataCache.remove( job->id().toInt() ); - emit info( requestData, result[ "data" ].toMap() ); - // retrieve criteria from cache and delete it Tomahawk::InfoSystem::InfoStringHash criteria = d->criteriaCache[ job->id().toInt() ]; d->criteriaCache.remove( job->id().toInt() ); - emit updateCache( criteria, result[ "maxAge" ].toLongLong(), requestData.type, result[ "data" ].toMap() ); - sender()->deleteLater(); + QVariantMap resultData = result[ "data" ].toMap(); + switch ( requestData.type ) + { + case InfoSystem::InfoAlbumCoverArt: + { + QNetworkRequest req( resultData[ "url" ].toUrl() ); + QNetworkReply* reply = Tomahawk::Utils::nam()->get( req ); + reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); + reply->setProperty( "criteria", convertInfoStringHashToQVariantMap( criteria ) ); + reply->setProperty( "maxAge", result[ "maxAge" ] ); + + connect( reply, SIGNAL( finished() ), SLOT( onCoverArtReturned() ) ); + } + break; + + default: + { + emit info( requestData, result[ "data" ].toMap() ); + emit updateCache( criteria, result[ "maxAge" ].toLongLong(), requestData.type, resultData ); + break; + } + }; } +void +ScriptInfoPlugin::onCoverArtReturned() +{ + QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() ); + reply->deleteLater(); + + QUrl redir = reply->attribute( QNetworkRequest::RedirectionTargetAttribute ).toUrl(); + if ( redir.isEmpty() ) + { + Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(); + Tomahawk::InfoSystem::InfoStringHash criteria = convertQVariantMapToInfoStringHash( reply->property( "criteria" ).toMap() ); + + QByteArray ba = reply->readAll(); + if ( ba.isNull() || ba.isEmpty() ) + { + tLog() << Q_FUNC_INFO << "Null byte array for cover of" << criteria["artist"] << criteria["album"]; + emit info( requestData, QVariant() ); + return; + } + + QVariantMap returnedData; + returnedData["imgbytes"] = ba; + returnedData["url"] = reply->url().toString(); + + emit info( requestData, returnedData ); + emit updateCache( criteria, reply->property( "maxAge" ).toLongLong(), requestData.type, returnedData ); + } + else + { + // Follow HTTP redirect + QNetworkRequest req( redir ); + QNetworkReply* newReply = Tomahawk::Utils::nam()->get( req ); + newReply->setProperty( "requestData", reply->property( "requestData" ) ); + newReply->setProperty( "criteria", reply->property( "criteria" ) ); + newReply->setProperty( "maxAge", reply->property( "maxAge" ) ); + connect( newReply, SIGNAL( finished() ), SLOT( onCoverArtReturned() ) ); + } +} QSet< Tomahawk::InfoSystem::InfoType > ScriptInfoPlugin::parseSupportedTypes( const QVariant& variant ) diff --git a/src/libtomahawk/resolvers/ScriptInfoPlugin.h b/src/libtomahawk/resolvers/ScriptInfoPlugin.h index fff6051b4..97df1bcf1 100644 --- a/src/libtomahawk/resolvers/ScriptInfoPlugin.h +++ b/src/libtomahawk/resolvers/ScriptInfoPlugin.h @@ -53,6 +53,7 @@ protected slots: void onGetInfoRequestDone( const QVariantMap& result ); void onNotInCacheRequestDone( const QVariantMap& result ); + void onCoverArtReturned(); // boilerplate: to be removed with Qt5 (can be put into ScriptPlugin) void onScriptObjectDeleted();