1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 08:19:42 +01:00

Revamp how the plugins interact with the infosystemworker

This commit is contained in:
Jeff Mitchell 2011-05-03 18:00:14 -04:00
parent de28596438
commit 7f26f34fdd
10 changed files with 133 additions and 86 deletions

View File

@ -25,16 +25,11 @@ using namespace Echonest;
// for internal neatness
EchoNestPlugin::EchoNestPlugin(InfoSystemWorker *parent)
: InfoPlugin(parent)
, m_infoSystemWorker( parent )
EchoNestPlugin::EchoNestPlugin()
: InfoPlugin()
{
qDebug() << Q_FUNC_INFO;
QSet< InfoType > supportedTypes;
supportedTypes << Tomahawk::InfoSystem::InfoArtistBiography << Tomahawk::InfoSystem::InfoArtistFamiliarity << Tomahawk::InfoSystem::InfoArtistHotttness << Tomahawk::InfoSystem::InfoArtistTerms << Tomahawk::InfoSystem::InfoMiscTopTerms;
parent->registerInfoTypes( this, supportedTypes, QSet< InfoType>() );
connect( parent, SIGNAL( namChanged() ), SLOT( namChangedSlot() ) );
m_supportedGetTypes << Tomahawk::InfoSystem::InfoArtistBiography << Tomahawk::InfoSystem::InfoArtistFamiliarity << Tomahawk::InfoSystem::InfoArtistHotttness << Tomahawk::InfoSystem::InfoArtistTerms << Tomahawk::InfoSystem::InfoMiscTopTerms;
}
EchoNestPlugin::~EchoNestPlugin()
@ -43,10 +38,14 @@ EchoNestPlugin::~EchoNestPlugin()
}
void
EchoNestPlugin::namChangedSlot()
EchoNestPlugin::namChangedSlot( QNetworkAccessManager *nam )
{
qDebug() << Q_FUNC_INFO;
Echonest::Config::instance()->setNetworkAccessManager( m_infoSystemWorker->nam() );
if( !nam )
return;
m_nam = QWeakPointer< QNetworkAccessManager >( nam );
Echonest::Config::instance()->setNetworkAccessManager( nam );
}
void

View File

@ -40,7 +40,7 @@ class EchoNestPlugin : public InfoPlugin
Q_OBJECT
public:
EchoNestPlugin( InfoSystemWorker *parent );
EchoNestPlugin();
virtual ~EchoNestPlugin();
protected slots:
@ -63,7 +63,7 @@ protected slots:
}
public slots:
void namChangedSlot();
void namChangedSlot( QNetworkAccessManager *nam );
private:
void getSongProfile( const QString &caller, const QVariant &input, const InfoCustomData &customData, const QString &item = QString() );
@ -87,7 +87,7 @@ private slots:
private:
QHash< QNetworkReply*, InfoCustomData > m_replyMap;
QHash< QNetworkReply*, QString > m_callerMap;
InfoSystemWorker* m_infoSystemWorker;
QWeakPointer< QNetworkAccessManager > m_nam;
};
}

View File

@ -41,18 +41,13 @@ md5( const QByteArray& src )
}
LastFmPlugin::LastFmPlugin( InfoSystemWorker* parent )
: InfoPlugin(parent)
LastFmPlugin::LastFmPlugin()
: InfoPlugin()
, m_scrobbler( 0 )
, m_authJob( 0 )
, m_infoSystemWorker( parent )
{
QSet< InfoType > supportedGetTypes, supportedPushTypes;
supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages;
supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying;
parent->registerInfoTypes( this, supportedGetTypes, supportedPushTypes );
connect( parent, SIGNAL( namChanged() ), SLOT( namChangedSlot() ) );
m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages;
m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying;
/*
Your API Key is 7194b85b6d1f424fe1668173a78c0c4a
@ -96,11 +91,13 @@ LastFmPlugin::~LastFmPlugin()
void
LastFmPlugin::namChangedSlot()
LastFmPlugin::namChangedSlot( QNetworkAccessManager *nam )
{
qDebug() << Q_FUNC_INFO;
qDebug() << Q_FUNC_INFO << " using nam " << m_infoSystemWorker->nam();
lastfm::setNetworkAccessManager( m_infoSystemWorker->nam() );
if( !nam )
return;
m_nam = QWeakPointer< QNetworkAccessManager >( nam );
settingsChanged(); // to get the scrobbler set up
}
@ -138,15 +135,15 @@ void
LastFmPlugin::pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input )
{
qDebug() << Q_FUNC_INFO;
Q_UNUSED( caller )
switch ( type )
{
case InfoSubmitNowPlaying:
nowPlaying( caller, type, input );
nowPlaying( input );
break;
case InfoSubmitScrobble:
scrobble( caller, type, input );
scrobble();
break;
default:
@ -155,7 +152,7 @@ LastFmPlugin::pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoTy
}
void
LastFmPlugin::nowPlaying( const QString &caller, const InfoType type, const QVariant &input )
LastFmPlugin::nowPlaying( const QVariant &input )
{
qDebug() << Q_FUNC_INFO;
if ( !input.canConvert< Tomahawk::InfoSystem::InfoCriteriaHash >() || !m_scrobbler )
@ -186,7 +183,7 @@ LastFmPlugin::nowPlaying( const QString &caller, const InfoType type, const QVar
void
LastFmPlugin::scrobble( const QString &caller, const InfoType type, const QVariant &input )
LastFmPlugin::scrobble()
{
qDebug() << Q_FUNC_INFO;
//Q_ASSERT( QThread::currentThread() == thread() );
@ -252,6 +249,13 @@ LastFmPlugin::notInCacheSlot( const QHash<QString, QString> criteria, const QStr
{
qDebug() << Q_FUNC_INFO;
if ( m_nam.isNull() )
{
qDebug() << "Have a null QNAM, uh oh";
emit info( caller, type, input, QVariant(), customData );
return;
}
switch ( type )
{
case InfoAlbumCoverArt:
@ -261,7 +265,7 @@ LastFmPlugin::notInCacheSlot( const QHash<QString, QString> criteria, const QStr
QString imgurl = "http://ws.audioscrobbler.com/2.0/?method=album.imageredirect&artist=%1&album=%2&autocorrect=1&size=medium&api_key=7a90f6672a04b809ee309af169f34b8b";
QNetworkRequest req( imgurl.arg( artistName ).arg( albumName ) );
QNetworkReply* reply = m_infoSystemWorker->nam()->get( req );
QNetworkReply* reply = m_nam.data()->get( req );
reply->setProperty( "customData", QVariant::fromValue<Tomahawk::InfoSystem::InfoCustomData>( customData ) );
reply->setProperty( "origData", input );
reply->setProperty( "caller", caller );
@ -277,7 +281,7 @@ LastFmPlugin::notInCacheSlot( const QHash<QString, QString> criteria, const QStr
QString imgurl = "http://ws.audioscrobbler.com/2.0/?method=artist.imageredirect&artist=%1&autocorrect=1&size=medium&api_key=7a90f6672a04b809ee309af169f34b8b";
QNetworkRequest req( imgurl.arg( artistName ) );
QNetworkReply* reply = m_infoSystemWorker->nam()->get( req );
QNetworkReply* reply = m_nam.data()->get( req );
reply->setProperty( "customData", QVariant::fromValue<Tomahawk::InfoSystem::InfoCustomData>( customData ) );
reply->setProperty( "origData", input );
reply->setProperty( "caller", caller );
@ -288,7 +292,11 @@ LastFmPlugin::notInCacheSlot( const QHash<QString, QString> criteria, const QStr
}
default:
{
qDebug() << "Couldn't figure out what to do with this type of request after cache miss";
emit info( caller, type, input, QVariant(), customData );
return;
}
}
}
@ -330,9 +338,17 @@ LastFmPlugin::coverArtReturned()
}
else
{
if ( m_nam.isNull() )
{
qDebug() << "Uh oh, nam is null";
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;
}
// Follow HTTP redirect
QNetworkRequest req( redir );
QNetworkReply* newReply = m_infoSystemWorker->nam()->get( req );
QNetworkReply* newReply = m_nam.data()->get( req );
newReply->setProperty( "origData", reply->property( "origData" ) );
newReply->setProperty( "customData", reply->property( "customData" ) );
newReply->setProperty( "caller", reply->property( "caller" ) );
@ -363,26 +379,28 @@ LastFmPlugin::artistImagesReturned()
returnedData["imgbytes"] = ba;
returnedData["url"] = reply->url().toString();
InfoCustomData customData = reply->property( "customData" ).value< Tomahawk::InfoSystem::InfoCustomData >();
InfoType type = (Tomahawk::InfoSystem::InfoType)(reply->property( "type" ).toUInt());
emit info(
reply->property( "caller" ).toString(),
type,
reply->property( "origData" ),
returnedData,
customData
);
InfoCustomData customData = reply->property( "customData" ).value< Tomahawk::InfoSystem::InfoCustomData >();
emit info( reply->property( "caller" ).toString(), type, reply->property( "origData" ), returnedData, customData );
InfoCriteriaHash origData = reply->property( "origData" ).value< Tomahawk::InfoSystem::InfoCriteriaHash >();
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
criteria["artist"] = origData["artist"];
emit updateCache( criteria, 2419200000, type, returnedData );
InfoCriteriaHash origData = reply->property( "origData" ).value< Tomahawk::InfoSystem::InfoCriteriaHash >();
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
criteria["artist"] = origData["artist"];
emit updateCache( criteria, 2419200000, type, returnedData );
}
else
{
if ( m_nam.isNull() )
{
qDebug() << "Uh oh, nam is null";
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;
}
// Follow HTTP redirect
QNetworkRequest req( redir );
QNetworkReply* newReply = m_infoSystemWorker->nam()->get( req );
QNetworkReply* newReply = m_nam.data()->get( req );
newReply->setProperty( "origData", reply->property( "origData" ) );
newReply->setProperty( "customData", reply->property( "customData" ) );
newReply->setProperty( "caller", reply->property( "caller" ) );

View File

@ -41,7 +41,7 @@ class LastFmPlugin : public InfoPlugin
Q_OBJECT
public:
LastFmPlugin( InfoSystemWorker *parent );
LastFmPlugin();
virtual ~LastFmPlugin();
public slots:
@ -51,7 +51,7 @@ public slots:
void coverArtReturned();
void artistImagesReturned();
void namChangedSlot();
void namChangedSlot( QNetworkAccessManager *nam );
protected slots:
virtual void getInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const Tomahawk::InfoSystem::InfoCustomData customData );
@ -64,8 +64,8 @@ private:
void fetchArtistImages( const QString &caller, const Tomahawk::InfoSystem::InfoType type, const QVariant &input, const Tomahawk::InfoSystem::InfoCustomData &customData );
void createScrobbler();
void scrobble( const QString &caller, const Tomahawk::InfoSystem::InfoType type, const QVariant &input );
void nowPlaying( const QString &caller, const Tomahawk::InfoSystem::InfoType type, const QVariant &input );
void nowPlaying( const QVariant &input );
void scrobble();
void dataError( const QString &caller, const Tomahawk::InfoSystem::InfoType type, const QVariant &input, const Tomahawk::InfoSystem::InfoCustomData &customData );
@ -77,7 +77,7 @@ private:
QNetworkReply* m_authJob;
InfoSystemWorker* m_infoSystemWorker;
QWeakPointer< QNetworkAccessManager > m_nam;
};
}

View File

@ -27,15 +27,12 @@ using namespace Tomahawk::InfoSystem;
// for internal neatness
MusixMatchPlugin::MusixMatchPlugin(InfoSystemWorker *parent)
: InfoPlugin(parent)
MusixMatchPlugin::MusixMatchPlugin()
: InfoPlugin()
, m_apiKey("61be4ea5aea7dd942d52b2f1311dd9fe")
, m_infoSystemWorker( parent )
{
qDebug() << Q_FUNC_INFO;
QSet< InfoType > supportedTypes;
supportedTypes << Tomahawk::InfoSystem::InfoTrackLyrics;
parent->registerInfoTypes( this, supportedTypes, QSet< InfoType>() );
m_supportedGetTypes << Tomahawk::InfoSystem::InfoTrackLyrics;
}
MusixMatchPlugin::~MusixMatchPlugin()
@ -43,11 +40,21 @@ MusixMatchPlugin::~MusixMatchPlugin()
qDebug() << Q_FUNC_INFO;
}
void
MusixMatchPlugin::namChangedSlot( QNetworkAccessManager *nam )
{
qDebug() << Q_FUNC_INFO;
if( !nam )
return;
m_nam = QWeakPointer< QNetworkAccessManager >( nam );
}
void
MusixMatchPlugin::getInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const Tomahawk::InfoSystem::InfoCustomData customData )
{
qDebug() << Q_FUNC_INFO;
if( !isValidTrackData(caller, input, customData) || !input.canConvert<Tomahawk::InfoSystem::InfoCustomData>())
if( !isValidTrackData(caller, input, customData) || !input.canConvert<Tomahawk::InfoSystem::InfoCustomData>() || m_nam.isNull() )
return;
Tomahawk::InfoSystem::InfoCustomData hash = input.value<Tomahawk::InfoSystem::InfoCustomData>();
QString artist = hash["artistName"].toString();
@ -63,7 +70,7 @@ MusixMatchPlugin::getInfo( const QString caller, const Tomahawk::InfoSystem::Inf
url.addQueryItem("apikey", m_apiKey);
url.addQueryItem("q_artist", artist);
url.addQueryItem("q_track", track);
QNetworkReply* reply = m_infoSystemWorker->nam()->get(QNetworkRequest(url));
QNetworkReply* reply = m_nam.data()->get(QNetworkRequest(url));
reply->setProperty("customData", QVariant::fromValue<Tomahawk::InfoSystem::InfoCustomData>(customData));
reply->setProperty("origData", input);
reply->setProperty("caller", caller);
@ -102,7 +109,7 @@ MusixMatchPlugin::trackSearchSlot()
{
qDebug() << Q_FUNC_INFO;
QNetworkReply* oldReply = qobject_cast<QNetworkReply*>( sender() );
if (!oldReply)
if ( !oldReply || m_nam.isNull() )
{
emit info(QString(), Tomahawk::InfoSystem::InfoTrackLyrics, QVariant(), QVariant(), Tomahawk::InfoSystem::InfoCustomData());
return;
@ -121,7 +128,7 @@ MusixMatchPlugin::trackSearchSlot()
QUrl url(requestString);
url.addQueryItem("apikey", m_apiKey);
url.addQueryItem("track_id", track_id);
QNetworkReply* newReply = m_infoSystemWorker->nam()->get(QNetworkRequest(url));
QNetworkReply* newReply = m_nam.data()->get(QNetworkRequest(url));
newReply->setProperty("origData", oldReply->property("origData"));
newReply->setProperty("customData", oldReply->property("customData"));
newReply->setProperty("caller", oldReply->property("caller"));

View File

@ -35,13 +35,15 @@ class MusixMatchPlugin : public InfoPlugin
Q_OBJECT
public:
MusixMatchPlugin( InfoSystemWorker *parent );
MusixMatchPlugin();
virtual ~MusixMatchPlugin();
public slots:
void trackSearchSlot();
void trackLyricsSlot();
void namChangedSlot( QNetworkAccessManager *nam );
protected slots:
virtual void getInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const Tomahawk::InfoSystem::InfoCustomData customData );
@ -66,7 +68,7 @@ private:
QString m_apiKey;
InfoSystemWorker* m_infoSystemWorker;
QWeakPointer< QNetworkAccessManager > m_nam;
};
}

View File

@ -32,12 +32,16 @@ namespace Tomahawk
namespace InfoSystem
{
InfoPlugin::InfoPlugin( InfoSystemWorker *parent )
:QObject( parent )
InfoPlugin::InfoPlugin()
: QObject()
{
qDebug() << Q_FUNC_INFO;
}
InfoPlugin::~InfoPlugin()
{
qDebug() << Q_FUNC_INFO;
}
InfoSystem* InfoSystem::s_instance = 0;

View File

@ -31,6 +31,8 @@
#include "dllmacro.h"
class QNetworkAccessManager;
namespace Tomahawk {
namespace InfoSystem {
@ -105,12 +107,12 @@ class DLLEXPORT InfoPlugin : public QObject
Q_OBJECT
public:
InfoPlugin( InfoSystemWorker *parent );
InfoPlugin();
virtual ~InfoPlugin()
{
qDebug() << Q_FUNC_INFO;
}
virtual ~InfoPlugin();
QSet< InfoType > supportedGetTypes() const { return m_supportedGetTypes; }
QSet< InfoType > supportedPushTypes() const { return m_supportedPushTypes; }
signals:
void getCachedInfo( Tomahawk::InfoSystem::InfoCriteriaHash criteria, qint64 newMaxAge, QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input, Tomahawk::InfoSystem::InfoCustomData customData );
@ -123,8 +125,12 @@ protected slots:
virtual void pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant data ) = 0;
virtual void notInCacheSlot( const Tomahawk::InfoSystem::InfoCriteriaHash criteria, const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input, const Tomahawk::InfoSystem::InfoCustomData customData ) = 0;
virtual void namChangedSlot( QNetworkAccessManager *nam ) = 0;
protected:
InfoType m_type;
QSet< InfoType > m_supportedGetTypes;
QSet< InfoType > m_supportedPushTypes;
private:
friend class InfoSystem;

View File

@ -36,7 +36,6 @@ namespace InfoSystem
{
InfoSystemWorker::InfoSystemWorker()
: m_nam( 0 )
{
qDebug() << Q_FUNC_INFO;
}
@ -56,12 +55,15 @@ InfoSystemWorker::~InfoSystemWorker()
void InfoSystemWorker::init()
{
InfoPluginPtr enptr( new EchoNestPlugin( this ) );
InfoPluginPtr enptr( new EchoNestPlugin() );
m_plugins.append( enptr );
InfoPluginPtr mmptr( new MusixMatchPlugin( this ) );
registerInfoTypes( enptr, enptr.data()->supportedGetTypes(), enptr.data()->supportedPushTypes() );
InfoPluginPtr mmptr( new MusixMatchPlugin() );
m_plugins.append( mmptr );
InfoPluginPtr lfmptr( new LastFmPlugin( this ) );
registerInfoTypes( mmptr, mmptr.data()->supportedGetTypes(), mmptr.data()->supportedPushTypes() );
InfoPluginPtr lfmptr( new LastFmPlugin() );
m_plugins.append( lfmptr );
registerInfoTypes( lfmptr, lfmptr.data()->supportedGetTypes(), lfmptr.data()->supportedPushTypes() );
InfoSystemCache *cache = InfoSystem::instance()->getCache();
@ -93,6 +95,12 @@ void InfoSystemWorker::init()
cache,
SLOT( updateCacheSlot( Tomahawk::InfoSystem::InfoCriteriaHash, qint64, Tomahawk::InfoSystem::InfoType, QVariant ) )
);
connect(
this,
SIGNAL( namChanged( QNetworkAccessManger* ) ),
plugin.data(),
SLOT( namChangedSlot( QNetworkAccessManager* ) )
);
}
QMetaObject::invokeMethod( this, "newNam" );
@ -160,7 +168,10 @@ InfoSystemWorker::pushInfo( const QString caller, const InfoType type, const QVa
QNetworkAccessManager*
InfoSystemWorker::nam() const
{
return m_nam;
if ( m_nam.isNull() )
return 0;
return m_nam.data();
}
@ -169,13 +180,12 @@ InfoSystemWorker::newNam()
{
qDebug() << Q_FUNC_INFO << " begin";
Q_ASSERT( TomahawkUtils::nam() != 0 );
QNetworkAccessManager *oldNam = TomahawkUtils::nam();
if ( oldNam && oldNam->thread() == thread() )
{
qDebug() << Q_FUNC_INFO << " returning old nam as it's the same thread as me";
m_nam = oldNam;
emit namChanged();
qDebug() << Q_FUNC_INFO << " using old nam as it's the same thread as me";
m_nam = QWeakPointer< QNetworkAccessManager >( oldNam );
emit namChanged( m_nam.data() );
return;
}
@ -186,20 +196,21 @@ InfoSystemWorker::newNam()
#else
newNam = new QNetworkAccessManager( this );
#endif
if ( m_nam )
delete m_nam;
if ( !m_nam.isNull() )
delete m_nam.data();
if ( !oldNam )
{
m_nam = newNam;
m_nam = QWeakPointer< QNetworkAccessManager >( newNam );
return;
}
newNam->setConfiguration( oldNam->configuration() );
newNam->setNetworkAccessible( oldNam->networkAccessible() );
newNam->setProxy( oldNam->proxy() );
m_nam = newNam;
m_nam = QWeakPointer< QNetworkAccessManager >( newNam );
emit namChanged();
emit namChanged( m_nam.data() );
}

View File

@ -49,7 +49,7 @@ public:
signals:
void info( QString target, Tomahawk::InfoSystem::InfoType, QVariant input, QVariant output, Tomahawk::InfoSystem::InfoCustomData customData );
void namChanged();
void namChanged( QNetworkAccessManager* );
public slots:
void init();
@ -66,7 +66,7 @@ private:
QMap< InfoType, QLinkedList< InfoPluginPtr > > m_infoGetMap;
QMap< InfoType, QLinkedList< InfoPluginPtr > > m_infoPushMap;
QNetworkAccessManager *m_nam;
QWeakPointer< QNetworkAccessManager> m_nam;
};
}