1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-05 05:37:29 +02:00

Vastly simplify mpris plugin by getting rid of unnecessary info system call

This commit is contained in:
Jeff Mitchell
2012-04-09 12:03:12 -04:00
parent b0e8fd33ce
commit b6cd43d25f
3 changed files with 33 additions and 120 deletions

View File

@@ -23,6 +23,7 @@
#include <QtCore/QUrl> #include <QtCore/QUrl>
#include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkReply>
#include <QTemporaryFile>
#include "playlistinterface.h" #include "playlistinterface.h"
#include "sourceplaylistinterface.h" #include "sourceplaylistinterface.h"
@@ -353,6 +354,25 @@ AudioEngine::onNowPlayingInfoReady( const Tomahawk::InfoSystem::InfoType type )
QImage cover; QImage cover;
cover = m_currentTrack->album()->cover( QSize( 0, 0 ) ).toImage(); cover = m_currentTrack->album()->cover( QSize( 0, 0 ) ).toImage();
playInfo["cover"] = cover; playInfo["cover"] = cover;
QTemporaryFile coverTempFile( QDir::toNativeSeparators( QDir::tempPath() + "/" + m_currentTrack->artist()->name() + "_" + m_currentTrack->album()->name() + "_tomahawk_cover.png" ) );
if ( !coverTempFile.open() )
{
tDebug() << "WARNING: could not write temporary file for cover art!";
}
// Finally, save the image to the new temp file
if ( cover.save( &coverTempFile, "PNG" ) )
{
tDebug( LOGVERBOSE ) << "Saving cover image to:" << QFileInfo( coverTempFile ).absoluteFilePath();
coverTempFile.close();
playInfo["coveruri"] = QFileInfo( coverTempFile ).absoluteFilePath();
}
else
{
tDebug() << Q_FUNC_INFO << "failed to save cover image!";
coverTempFile.close();
}
#endif #endif
} }

View File

@@ -41,7 +41,6 @@ static QString s_mpInfoIdentifier = QString( "MPRISPLUGIN" );
MprisPlugin::MprisPlugin() MprisPlugin::MprisPlugin()
: InfoPlugin() : InfoPlugin()
, m_coverTempFile( 0 )
{ {
// init // init
m_playbackStatus = "Stopped"; m_playbackStatus = "Stopped";
@@ -75,22 +74,11 @@ MprisPlugin::MprisPlugin()
// Connect to AudioEngine's seeked signal // Connect to AudioEngine's seeked signal
connect( AudioEngine::instance(), SIGNAL( seeked( qint64 ) ), connect( AudioEngine::instance(), SIGNAL( seeked( qint64 ) ),
SLOT( onSeeked( qint64 ) ) ); SLOT( onSeeked( qint64 ) ) );
// Connect to the InfoSystem (we need to get album covers via getInfo)
connect( Tomahawk::InfoSystem::InfoSystem::instance(),
SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ),
SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) );
connect( Tomahawk::InfoSystem::InfoSystem::instance(),
SIGNAL( finished( QString ) ),
SLOT( infoSystemFinished( QString ) ) );
} }
MprisPlugin::~MprisPlugin() MprisPlugin::~MprisPlugin()
{ {
delete m_coverTempFile;
} }
@@ -273,26 +261,11 @@ MprisPlugin::metadata() const
metadataMap.insert( "xesam:title", track->track() ); metadataMap.insert( "xesam:title", track->track() );
// Only return art if tempfile exists, and if its name contains the same "artist_album_tomahawk_cover.png" // Only return art if tempfile exists, and if its name contains the same "artist_album_tomahawk_cover.png"
if ( m_coverTempFile && m_coverTempFile->exists() && if ( !m_coverTempFile.isEmpty() )
m_coverTempFile->fileName().contains( track->artist()->name() + "_" + track->album()->name() + "_tomahawk_cover.png" ) )
{ {
metadataMap.insert( "mpris:artUrl", QString( QUrl::fromLocalFile( QFileInfo( *m_coverTempFile ).absoluteFilePath() ).toEncoded() ) ); QFile coverFile( m_coverTempFile );
} if ( coverFile.exists() && coverFile.fileName().contains( track->artist()->name() + "_" + track->album()->name() + "_tomahawk_cover.png" ) )
else metadataMap.insert( "mpris:artUrl", QString( QUrl::fromLocalFile( m_coverTempFile ).toEncoded() ) );
{
// Need to fetch the album cover
Tomahawk::InfoSystem::InfoStringHash trackInfo;
trackInfo["artist"] = track->artist()->name();
trackInfo["album"] = track->album()->name();
Tomahawk::InfoSystem::InfoRequestData requestData;
requestData.caller = s_mpInfoIdentifier;
requestData.type = Tomahawk::InfoSystem::InfoAlbumCoverArt;
requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo );
requestData.customData = QVariantMap();
Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
} }
} }
@@ -458,16 +431,6 @@ MprisPlugin::Stop()
// InfoPlugin Methods // InfoPlugin Methods
void
MprisPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
{
Q_UNUSED( requestData );
return;
}
void void
MprisPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) MprisPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData )
{ {
@@ -526,6 +489,10 @@ MprisPlugin::audioStarted( const QVariant& input )
return; return;
m_playbackStatus = "Playing"; m_playbackStatus = "Playing";
if ( map.contains( "coveruri" ) )
m_coverTempFile = map[ "coveruri" ].toString();
notifyPropertyChanged( "org.mpris.MediaPlayer2.Player", "Metadata" ); notifyPropertyChanged( "org.mpris.MediaPlayer2.Player", "Metadata" );
} }
@@ -601,80 +568,6 @@ MprisPlugin::onSeeked( qint64 ms )
} }
void
MprisPlugin::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output )
{
// If the caller for the request was not us, or not the type of info we are seeking, ignore it
if ( requestData.caller != s_mpInfoIdentifier || requestData.type != Tomahawk::InfoSystem::InfoAlbumCoverArt )
{
//notifyPropertyChanged( "org.mpris.MediaPlayer2.Player", "Metadata" );
return;
}
if ( !output.canConvert< QVariantMap >() )
{
//notifyPropertyChanged( "org.mpris.MediaPlayer2.Player", "Metadata" );
tDebug( LOGINFO ) << "Cannot convert fetched art from a QByteArray";
return;
}
// Pull image data into byte array
QVariantMap returnedData = output.value< QVariantMap >();
const QByteArray ba = returnedData["imgbytes"].toByteArray();
if ( ba.length() )
{
// Load from byte array to image
QImage image;
image.loadFromData( ba );
// Pull out request data for album+artist
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
{
qDebug() << "Cannot convert metadata input to album cover retrieval";
return;
}
Tomahawk::InfoSystem::InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>();
// delete the old tempfile and make new one, to avoid caching of filename by mpris clients
if ( m_coverTempFile )
{
delete m_coverTempFile;
m_coverTempFile = 0;
}
if ( image.isNull() )
return;
m_coverTempFile = new QTemporaryFile( QDir::toNativeSeparators( QDir::tempPath() + "/" + hash["artist"] + "_" + hash["album"] + "_tomahawk_cover.png" ) );
if ( !m_coverTempFile->open() )
{
qDebug() << "WARNING: could not write temporary file for cover art!";
}
// Finally, save the image to the new temp file
if ( image.save( m_coverTempFile, "PNG" ) )
{
qDebug() << "Saving cover image to:" << QFileInfo( *m_coverTempFile ).absoluteFilePath();
m_coverTempFile->close();
notifyPropertyChanged( "org.mpris.MediaPlayer2.Player", "Metadata" );
}
else
{
tDebug() << Q_FUNC_INFO << "failed to save cover image!";
m_coverTempFile->close();
}
}
}
void
MprisPlugin::infoSystemFinished( QString target )
{
Q_UNUSED( target );
}
void void
MprisPlugin::notifyPropertyChanged( const QString& interface, const QString& propertyName ) MprisPlugin::notifyPropertyChanged( const QString& interface, const QString& propertyName )
{ {

View File

@@ -140,7 +140,10 @@ public slots:
void Stop(); void Stop();
protected slots: protected slots:
void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ); virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
{
Q_UNUSED( requestData );
}
void pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ); void pushInfo( Tomahawk::InfoSystem::InfoPushData pushData );
private slots: private slots:
@@ -150,9 +153,6 @@ private slots:
void onTrackCountChanged( unsigned int tracks ); void onTrackCountChanged( unsigned int tracks );
void onSeeked( qint64 ms ); void onSeeked( qint64 ms );
void infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output );
void infoSystemFinished( QString target );
signals: signals:
void Seeked( qlonglong Position ); void Seeked( qlonglong Position );
@@ -169,7 +169,7 @@ private:
// DBus // DBus
void notifyPropertyChanged( const QString& interface, const QString& propertyName ); void notifyPropertyChanged( const QString& interface, const QString& propertyName );
QString m_playbackStatus; QString m_playbackStatus;
QTemporaryFile* m_coverTempFile; QString m_coverTempFile;
}; };
}; };