mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-23 22:25:11 +02:00
Fetch cover art using LastFm via the InfoSystem plugin
This commit is contained in:
@@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#define LASTFM_DEFAULT_COVER "http://cdn.last.fm/flatness/catalogue/noimage"
|
#define LASTFM_DEFAULT_COVER "http://cdn.last.fm/flatness/catalogue/noimage"
|
||||||
|
|
||||||
|
static QString s_infoIdentifier = QString("AUDIOCONTROLS");
|
||||||
|
|
||||||
AudioControls::AudioControls( QWidget* parent )
|
AudioControls::AudioControls( QWidget* parent )
|
||||||
: QWidget( parent )
|
: QWidget( parent )
|
||||||
@@ -166,6 +167,12 @@ AudioControls::AudioControls( QWidget* parent )
|
|||||||
m_defaultCover = QPixmap( RESPATH "images/no-album-art-placeholder.png" )
|
m_defaultCover = QPixmap( RESPATH "images/no-album-art-placeholder.png" )
|
||||||
.scaled( ui->coverImage->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
|
.scaled( ui->coverImage->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
|
||||||
|
|
||||||
|
connect( TomahawkApp::instance()->infoSystem(),
|
||||||
|
SIGNAL( info( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomDataHash ) ),
|
||||||
|
SLOT( infoSystemInfo( QString, Tomahawk::InfoSystem::InfoType, QVariant, QVariant, Tomahawk::InfoSystem::InfoCustomDataHash ) ) );
|
||||||
|
|
||||||
|
connect( TomahawkApp::instance()->infoSystem(), SIGNAL( finished( QString ) ), SLOT( infoSystemFinished( QString ) ) );
|
||||||
|
|
||||||
onPlaybackStopped(); // initial state
|
onPlaybackStopped(); // initial state
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,12 +249,59 @@ AudioControls::onPlaybackStarted( const Tomahawk::result_ptr& result )
|
|||||||
|
|
||||||
onPlaybackLoading( result );
|
onPlaybackLoading( result );
|
||||||
|
|
||||||
QString imgurl = "http://ws.audioscrobbler.com/2.0/?method=album.imageredirect&artist=%1&album=%2&size=medium&api_key=7a90f6672a04b809ee309af169f34b8b";
|
QString artistName = result->artist()->name();
|
||||||
QNetworkRequest req( imgurl.arg( result->artist()->name() ).arg( result->album()->name() ) );
|
QString albumName = result->album()->name();
|
||||||
QNetworkReply* reply = TomahawkUtils::nam()->get( req );
|
|
||||||
connect( reply, SIGNAL( finished() ), SLOT( onCoverArtDownloaded() ) );
|
Tomahawk::InfoSystem::InfoCustomDataHash trackInfo;
|
||||||
|
|
||||||
|
trackInfo["artist"] = QVariant::fromValue< QString >( result->artist()->name() );
|
||||||
|
trackInfo["album"] = QVariant::fromValue< QString >( result->album()->name() );
|
||||||
|
TomahawkApp::instance()->infoSystem()->getInfo(
|
||||||
|
s_infoIdentifier, Tomahawk::InfoSystem::InfoAlbumCoverArt,
|
||||||
|
QVariant::fromValue< Tomahawk::InfoSystem::InfoCustomDataHash >( trackInfo ), Tomahawk::InfoSystem::InfoCustomDataHash() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
AudioControls::infoSystemInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData )
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO;
|
||||||
|
if ( caller != s_infoIdentifier || type != Tomahawk::InfoSystem::InfoAlbumCoverArt )
|
||||||
|
{
|
||||||
|
qDebug() << "info of wrong type or not with our identifier";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( m_currentTrack.isNull() )
|
||||||
|
{
|
||||||
|
qDebug() << "Current track is null when trying to apply fetched cover art";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !output.canConvert< Tomahawk::InfoSystem::InfoCustomDataHash >() )
|
||||||
|
{
|
||||||
|
qDebug() << "Cannot convert fetched art from a QByteArray";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tomahawk::InfoSystem::InfoCustomDataHash returnedData = output.value< Tomahawk::InfoSystem::InfoCustomDataHash >();
|
||||||
|
const QByteArray ba = returnedData["imgbytes"].toByteArray();
|
||||||
|
if ( ba.length() )
|
||||||
|
{
|
||||||
|
QPixmap pm;
|
||||||
|
pm.loadFromData( ba );
|
||||||
|
|
||||||
|
if ( pm.isNull() || returnedData["url"].toString().startsWith( LASTFM_DEFAULT_COVER ) )
|
||||||
|
ui->coverImage->setPixmap( m_defaultCover );
|
||||||
|
else
|
||||||
|
ui->coverImage->setPixmap( pm.scaled( ui->coverImage->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
AudioControls::infoSystemFinished( QString target )
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
AudioControls::onPlaybackLoading( const Tomahawk::result_ptr& result )
|
AudioControls::onPlaybackLoading( const Tomahawk::result_ptr& result )
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include "result.h"
|
#include "result.h"
|
||||||
#include "playlistinterface.h"
|
#include "playlistinterface.h"
|
||||||
|
#include "tomahawk/infosystem.h"
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui
|
||||||
{
|
{
|
||||||
@@ -44,6 +45,8 @@ signals:
|
|||||||
public slots:
|
public slots:
|
||||||
void onRepeatModeChanged( PlaylistInterface::RepeatMode mode );
|
void onRepeatModeChanged( PlaylistInterface::RepeatMode mode );
|
||||||
void onShuffleModeChanged( bool enabled );
|
void onShuffleModeChanged( bool enabled );
|
||||||
|
void infoSystemInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomDataHash customData );
|
||||||
|
void infoSystemFinished( QString target );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void changeEvent( QEvent* e );
|
void changeEvent( QEvent* e );
|
||||||
|
@@ -47,7 +47,7 @@ LastFmPlugin::LastFmPlugin( QObject* parent )
|
|||||||
, m_authJob( 0 )
|
, m_authJob( 0 )
|
||||||
{
|
{
|
||||||
QSet< InfoType > supportedTypes;
|
QSet< InfoType > supportedTypes;
|
||||||
supportedTypes << Tomahawk::InfoSystem::InfoMiscSubmitScrobble << Tomahawk::InfoSystem::InfoMiscSubmitNowPlaying;
|
supportedTypes << InfoMiscSubmitScrobble << InfoMiscSubmitNowPlaying << InfoAlbumCoverArt;
|
||||||
qobject_cast< InfoSystem* >(parent)->registerInfoTypes(this, supportedTypes);
|
qobject_cast< InfoSystem* >(parent)->registerInfoTypes(this, supportedTypes);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -91,6 +91,14 @@ LastFmPlugin::~LastFmPlugin()
|
|||||||
delete m_scrobbler;
|
delete m_scrobbler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
LastFmPlugin::dataError( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash &customData )
|
||||||
|
{
|
||||||
|
emit info( caller, type, data, QVariant(), customData );
|
||||||
|
emit finished( caller, type );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
LastFmPlugin::getInfo( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash customData )
|
LastFmPlugin::getInfo( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash customData )
|
||||||
{
|
{
|
||||||
@@ -99,6 +107,8 @@ LastFmPlugin::getInfo( const QString &caller, const InfoType type, const QVarian
|
|||||||
nowPlaying( caller, type, data, customData );
|
nowPlaying( caller, type, data, customData );
|
||||||
else if ( type == InfoMiscSubmitScrobble )
|
else if ( type == InfoMiscSubmitScrobble )
|
||||||
scrobble( caller, type, data, customData );
|
scrobble( caller, type, data, customData );
|
||||||
|
else if ( type == InfoAlbumCoverArt )
|
||||||
|
fetchCoverArt( caller, type, data, customData );
|
||||||
else
|
else
|
||||||
dataError( caller, type, data, customData );
|
dataError( caller, type, data, customData );
|
||||||
}
|
}
|
||||||
@@ -132,14 +142,6 @@ LastFmPlugin::nowPlaying( const QString &caller, const InfoType type, const QVar
|
|||||||
emit finished( caller, type );
|
emit finished( caller, type );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
LastFmPlugin::dataError( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash &customData )
|
|
||||||
{
|
|
||||||
emit info( caller, type, data, QVariant(), customData );
|
|
||||||
emit finished( caller, type );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
LastFmPlugin::scrobble( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash &customData )
|
LastFmPlugin::scrobble( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash &customData )
|
||||||
{
|
{
|
||||||
@@ -159,6 +161,71 @@ LastFmPlugin::scrobble( const QString &caller, const InfoType type, const QVaria
|
|||||||
emit finished( caller, type );
|
emit finished( caller, type );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
LastFmPlugin::fetchCoverArt( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash &customData )
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO;
|
||||||
|
if ( !data.canConvert< Tomahawk::InfoSystem::InfoCustomDataHash >() )
|
||||||
|
{
|
||||||
|
dataError( caller, type, data, customData );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
InfoCustomDataHash hash = data.value< Tomahawk::InfoSystem::InfoCustomDataHash >();
|
||||||
|
if ( !hash.contains( "artist" ) || !hash.contains( "album" ) )
|
||||||
|
{
|
||||||
|
dataError( caller, type, data, customData );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString artistName = hash["artist"].toString();
|
||||||
|
QString albumName = hash["album"].toString();
|
||||||
|
|
||||||
|
QString imgurl = "http://ws.audioscrobbler.com/2.0/?method=album.imageredirect&artist=%1&album=%2&size=medium&api_key=7a90f6672a04b809ee309af169f34b8b";
|
||||||
|
QNetworkRequest req( imgurl.arg( artistName ).arg( albumName ) );
|
||||||
|
QNetworkReply* reply = TomahawkUtils::nam()->get( req );
|
||||||
|
reply->setProperty("customData", QVariant::fromValue<Tomahawk::InfoSystem::InfoCustomDataHash>(customData));
|
||||||
|
reply->setProperty("origData", data);
|
||||||
|
reply->setProperty("caller", caller);
|
||||||
|
reply->setProperty("type", (uint)(type) );
|
||||||
|
|
||||||
|
connect( reply, SIGNAL( finished() ), SLOT( coverArtReturned() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
LastFmPlugin::coverArtReturned()
|
||||||
|
{
|
||||||
|
qDebug() << Q_FUNC_INFO;
|
||||||
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
|
||||||
|
QUrl redir = reply->attribute( QNetworkRequest::RedirectionTargetAttribute ).toUrl();
|
||||||
|
if ( redir.isEmpty() )
|
||||||
|
{
|
||||||
|
const QByteArray ba = reply->readAll();
|
||||||
|
Tomahawk::InfoSystem::InfoCustomDataHash returnedData;
|
||||||
|
returnedData["imgbytes"] = ba;
|
||||||
|
returnedData["url"] = reply->url().toString();
|
||||||
|
emit info(
|
||||||
|
reply->property( "caller" ).toString(),
|
||||||
|
(Tomahawk::InfoSystem::InfoType)(reply->property( "type" ).toUInt()),
|
||||||
|
reply->property( "origData" ),
|
||||||
|
returnedData,
|
||||||
|
reply->property( "customData" ).value< Tomahawk::InfoSystem::InfoCustomDataHash >()
|
||||||
|
);
|
||||||
|
emit finished( reply->property( "caller" ).toString(), (Tomahawk::InfoSystem::InfoType)(reply->property( "type" ).toUInt()) );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Follow HTTP redirect
|
||||||
|
QNetworkRequest req( redir );
|
||||||
|
QNetworkReply* newReply = TomahawkUtils::nam()->get( req );
|
||||||
|
newReply->setProperty( "origData", reply->property( "origData" ) );
|
||||||
|
newReply->setProperty( "customData", reply->property( "customData" ) );
|
||||||
|
newReply->setProperty( "caller", reply->property( "caller" ) );
|
||||||
|
newReply->setProperty( "type", reply->property( "type" ) );
|
||||||
|
connect( newReply, SIGNAL( finished() ), SLOT( coverArtReturned() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
LastFmPlugin::settingsChanged()
|
LastFmPlugin::settingsChanged()
|
||||||
|
@@ -48,8 +48,10 @@ public:
|
|||||||
public slots:
|
public slots:
|
||||||
void settingsChanged();
|
void settingsChanged();
|
||||||
void onAuthenticated();
|
void onAuthenticated();
|
||||||
|
void coverArtReturned();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void fetchCoverArt( const QString &caller, const InfoType type, const QVariant& data, Tomahawk::InfoSystem::InfoCustomDataHash &customData );
|
||||||
void scrobble( const QString &caller, const InfoType type, const QVariant& data, InfoCustomDataHash &customData );
|
void scrobble( const QString &caller, const InfoType type, const QVariant& data, InfoCustomDataHash &customData );
|
||||||
void createScrobbler();
|
void createScrobbler();
|
||||||
void nowPlaying( const QString &caller, const InfoType type, const QVariant& data, InfoCustomDataHash &customData );
|
void nowPlaying( const QString &caller, const InfoType type, const QVariant& data, InfoCustomDataHash &customData );
|
||||||
|
Reference in New Issue
Block a user