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

Change how chart lists and charts are passed through the info system.

This commit is contained in:
Casey Link
2011-09-04 20:11:53 -05:00
parent 33d8c0314c
commit 0a7e49f6d5
5 changed files with 136 additions and 145 deletions

View File

@@ -22,6 +22,7 @@
#include <QSettings> #include <QSettings>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QNetworkConfiguration> #include <QNetworkConfiguration>
#include <QDomElement>
#include "album.h" #include "album.h"
#include "typedefs.h" #include "typedefs.h"
@@ -49,7 +50,7 @@ LastFmPlugin::LastFmPlugin()
: InfoPlugin() : InfoPlugin()
, m_scrobbler( 0 ) , m_scrobbler( 0 )
{ {
m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoArtistSimilars << InfoArtistSongs << InfoChartArtists << InfoChartTracks << InfoChartCapabilities; m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoArtistSimilars << InfoArtistSongs << InfoChart << InfoChartCapabilities;
m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying << InfoLove << InfoUnLove; m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying << InfoLove << InfoUnLove;
/* /*
@@ -150,13 +151,10 @@ LastFmPlugin::getInfo( uint requestId, Tomahawk::InfoSystem::InfoRequestData req
fetchTopTracks( requestId, requestData ); fetchTopTracks( requestId, requestData );
break; break;
case InfoChartArtists: case InfoChart:
fetchChartArtists( requestId, requestData ); fetchChart( requestId, requestData );
break; break;
case InfoChartTracks:
fetchChartTracks( requestId, requestData );
break;
case InfoChartCapabilities: case InfoChartCapabilities:
fetchChartCapabilities( requestId, requestData ); fetchChartCapabilities( requestId, requestData );
break; break;
@@ -312,7 +310,7 @@ LastFmPlugin::fetchTopTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestD
} }
void void
LastFmPlugin::fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ) LastFmPlugin::fetchChart( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData )
{ {
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoCriteriaHash >() ) if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoCriteriaHash >() )
{ {
@@ -321,34 +319,17 @@ LastFmPlugin::fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoReque
} }
InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >(); InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >();
Tomahawk::InfoSystem::InfoCriteriaHash criteria; Tomahawk::InfoSystem::InfoCriteriaHash criteria;
if ( hash.contains( "country" ) ) if ( !hash.contains( "chart_id" ) )
{
criteria["country"] = hash["country"];
}
emit getCachedInfo( requestId, criteria, 2419200000, requestData );
}
void
LastFmPlugin::fetchChartTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData )
{
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoCriteriaHash >() )
{ {
dataError( requestId, requestData ); dataError( requestId, requestData );
return; return;
} } else {
InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >(); criteria["chart_id"] = hash["chart_id"];
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
if ( hash.contains( "country" ) )
{
criteria["country"] = hash["country"];
} }
emit getCachedInfo( requestId, criteria, 2419200000, requestData ); emit getCachedInfo( requestId, criteria, 0, requestData );
} }
void void
LastFmPlugin::fetchChartCapabilities( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ) LastFmPlugin::fetchChartCapabilities( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData )
{ {
@@ -360,7 +341,7 @@ LastFmPlugin::fetchChartCapabilities( uint requestId, Tomahawk::InfoSystem::Info
InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >(); InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >();
Tomahawk::InfoSystem::InfoCriteriaHash criteria; Tomahawk::InfoSystem::InfoCriteriaHash criteria;
emit getCachedInfo( requestId, criteria, 2419200000, requestData ); emit getCachedInfo( requestId, criteria, 0, requestData );
} }
void void
@@ -420,57 +401,38 @@ LastFmPlugin::notInCacheSlot( uint requestId, QHash<QString, QString> criteria,
switch ( requestData.type ) switch ( requestData.type )
{ {
case InfoChartArtists: case InfoChart:
{ {
tDebug() << "LastfmPlugin: InfoChartArtists notin cache, fetching"; tDebug() << "LastfmPlugin: InfoChart not in cache, fetching";
QMap<QString, QString> args; QMap<QString, QString> args;
if( criteria.contains( "country" ) ) { args["method"] = criteria["chart_id"];
args["method"] = "geo.getTopArtists";
args["country"] = criteria["country"];
} else {
args["method"] = "chart.getTopArtists";
}
args["limit"] = "100"; args["limit"] = "100";
QNetworkReply* reply = lastfm::ws::get(args); QNetworkReply* reply = lastfm::ws::get(args);
reply->setProperty( "requestId", requestId ); reply->setProperty( "requestId", requestId );
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
connect( reply, SIGNAL( finished() ), SLOT( chartTopArtistsReturned() ) ); connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) );
return;
}
case InfoChartTracks:
{
tDebug() << "LastfmPlugin: InfoChartTracks not in cache, fetching";
QMap<QString, QString> args;
if( criteria.contains( "country" ) ) {
args["method"] = "geo.getTopTracks";
args["country"] = criteria["country"];
} else {
args["method"] = "chart.getTopTracks";
}
args["limit"] = "100";
QNetworkReply* reply = lastfm::ws::get(args);
reply->setProperty( "requestId", requestId );
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
connect( reply, SIGNAL( finished() ), SLOT( chartTopTracksReturned() ) );
return; return;
} }
case InfoChartCapabilities: case InfoChartCapabilities:
{ {
tDebug() << "LastfmPlugin: InfoChartCapabilities not in cache, fetching"; QList<Chart> track_charts;
QString json("{'Last.fm': {'Tracks': ['Top Tracks','Hyped Tracks','Most Loved Tracks']," track_charts.append(Chart("chart.getTopTracks", "Top Tracks", "tracks"));
"'Artists': ['Top Artists', 'Hyped Artists']}}"); track_charts.append(Chart("chart.getLovedTracks", "Loved Tracks", "tracks"));
json.replace("'", "\""); track_charts.append(Chart("chart.getHypedTracks", "Hyped Tracks", "tracks"));
QJson::Parser parser;
bool ok = false; QList<Chart> artist_charts;
QVariantMap result = parser.parse (json.toUtf8(), &ok).toMap(); artist_charts.append(Chart("chart.getTopArtists", "Top Artists", "artists"));
if(!ok) { artist_charts.append(Chart("chart.getHypedArtists", "Hyped Artists", "artists"));
tDebug() << "Lastfm Plugin: parsing json failed";
return; QVariantMap charts;
} charts.insert("Tracks", QVariant::fromValue<QList<Chart> >(track_charts));
charts.insert("Artists", QVariant::fromValue<QList<Chart> >(artist_charts));
QVariantMap result;
result.insert("Last.fm", QVariant::fromValue<QVariantMap>(charts));
emit info( emit info(
requestId, requestId,
requestData, requestData,
@@ -571,57 +533,38 @@ LastFmPlugin::similarArtistsReturned()
} }
void void
LastFmPlugin::chartTopArtistsReturned() LastFmPlugin::chartReturned()
{ {
tDebug() << "LastfmPlugin: InfoChartArtists data returned!"; tDebug() << "LastfmPlugin: InfoChart data returned!";
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() ); QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
QList<lastfm::Artist> list = lastfm::Artist::list( reply );
QStringList al;
tDebug() << "\tgot " << list.size() << " artists";
foreach ( const lastfm::Artist& a, list )
al << a.toString();
QVariantMap returnedData; QVariantMap returnedData;
returnedData["artists"] = al;
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(); if( replyIsTracks( reply) ) {
QList<lastfm::Track> tracks = parseTrackList( reply );
QList<ArtistTrackPair> top_tracks;
foreach( const lastfm::Track &t, tracks ) {
ArtistTrackPair pair;
pair.artist = t.artist().toString();
pair.track = t.title();
top_tracks << pair;
}
tDebug() << "LastFmPlugin:" << "\tgot " << top_tracks.size() << " tracks";
returnedData["tracks"] = QVariant::fromValue( top_tracks );
returnedData["type"] = "tracks";
emit info( } else if( replyIsArtists( reply ) ) {
reply->property( "requestId" ).toUInt(), QList<lastfm::Artist> list = lastfm::Artist::list( reply );
requestData, QStringList al;
returnedData tDebug() << "LastFmPlugin:"<< "\tgot " << list.size() << " artists";
); foreach ( const lastfm::Artist& a, list )
al << a.toString();
Tomahawk::InfoSystem::InfoCriteriaHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash>(); returnedData["artists"] = al;
Tomahawk::InfoSystem::InfoCriteriaHash criteria; returnedData["type"] = "artists";
if( origData.contains("country") ) } else {
criteria["country"] = origData["country"]; tDebug() << "LastfmPlugin:: got non tracks and non artists";
emit updateCache( criteria, 2419200000, requestData.type, returnedData );
}
void
LastFmPlugin::chartTopTracksReturned()
{
tDebug() << "LastfmPlugin: InfoChartTracks data returned!";
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
QList<lastfm::Track> tracks = parseTrackList( reply );
QList<ArtistTrackPair> top_tracks;
foreach( const lastfm::Track &t, tracks ) {
ArtistTrackPair pair;
pair.artist = t.artist().toString();
pair.track = t.title();
top_tracks << pair;
} }
tDebug() << "\tgot " << top_tracks.size() << " tracks";
QVariantMap returnedData;
returnedData["tracks"] = QVariant::fromValue( top_tracks );
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(); Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
emit info( emit info(
@@ -629,12 +572,7 @@ LastFmPlugin::chartTopTracksReturned()
requestData, requestData,
returnedData returnedData
); );
// TODO update cache
Tomahawk::InfoSystem::InfoCriteriaHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash>();
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
if( origData.contains("country") )
criteria["country"] = origData["country"];
emit updateCache( criteria, 0, requestData.type, returnedData );
} }
void void
@@ -885,3 +823,32 @@ LastFmPlugin::parseTrackList( QNetworkReply * reply )
return tracks; return tracks;
} }
bool
LastFmPlugin::replyIsTracks( QNetworkReply *reply )
{
try {
lastfm::XmlQuery lfm = lastfm::ws::parse(reply);
QDomElement e(lfm["tracks"]);
return !e.isNull();
}
catch (lastfm::ws::ParseError& e)
{
qWarning() << e.what();
return false;
}
}
bool
LastFmPlugin::replyIsArtists( QNetworkReply *reply )
{
try {
lastfm::XmlQuery lfm = lastfm::ws::parse(reply);
QDomElement e(lfm["artists"]);
return !e.isNull();
}
catch (lastfm::ws::ParseError& e)
{
qWarning() << e.what();
return false;
}
}

View File

@@ -52,8 +52,7 @@ public slots:
void artistImagesReturned(); void artistImagesReturned();
void similarArtistsReturned(); void similarArtistsReturned();
void topTracksReturned(); void topTracksReturned();
void chartTopArtistsReturned(); void chartReturned();
void chartTopTracksReturned();
void namChangedSlot( QNetworkAccessManager *nam ); void namChangedSlot( QNetworkAccessManager *nam );
@@ -68,8 +67,7 @@ private:
void fetchArtistImages( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchArtistImages( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchSimilarArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchSimilarArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchTopTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchTopTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChart( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChartTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChartCapabilities( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChartCapabilities( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void createScrobbler(); void createScrobbler();
@@ -80,6 +78,8 @@ private:
void dataError( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void dataError( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
QList<lastfm::Track> parseTrackList( QNetworkReply * reply ); QList<lastfm::Track> parseTrackList( QNetworkReply * reply );
bool replyIsTracks( QNetworkReply *reply );
bool replyIsArtists( QNetworkReply *reply );
lastfm::MutableTrack m_track; lastfm::MutableTrack m_track;
lastfm::Audioscrobbler* m_scrobbler; lastfm::Audioscrobbler* m_scrobbler;

View File

@@ -102,9 +102,7 @@ enum InfoType { // as items are saved in cache, mark them here to not change the
/** /**
* Documentation for InfoChartArtists * Documentation for InfoChartArtists
*/ */
InfoChartArtists = 51, InfoChart = 51,
InfoChartAlbums = 52, /*!< Documentation for InfoChartAlbums */
InfoChartTracks = 53,
InfoMiscTopHotttness = 60, InfoMiscTopHotttness = 60,
InfoMiscTopTerms = 61, InfoMiscTopTerms = 61,
@@ -137,6 +135,18 @@ struct ArtistTrackPair {
QString track; QString track;
}; };
struct Chart {
Chart(){}
Chart(const QString _id, const QString _label, const QString _type) {
id = _id;
label = _label;
type = _type;
}
QString id;
QString label;
QString type;
};
typedef QMap< InfoType, QVariant > InfoTypeMap; typedef QMap< InfoType, QVariant > InfoTypeMap;
typedef QMap< InfoType, uint > InfoTimeoutMap; typedef QMap< InfoType, uint > InfoTimeoutMap;
typedef QMap< QString, QMap< QString, QString > > InfoGenericMap; typedef QMap< QString, QMap< QString, QString > > InfoGenericMap;
@@ -274,6 +284,8 @@ Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoGenericMap );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCriteriaHash ); Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCriteriaHash );
Q_DECLARE_METATYPE( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > ); Q_DECLARE_METATYPE( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::ArtistTrackPair ); Q_DECLARE_METATYPE( Tomahawk::InfoSystem::ArtistTrackPair );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::Chart );
Q_DECLARE_METATYPE( QList<Tomahawk::InfoSystem::ArtistTrackPair> ); Q_DECLARE_METATYPE( QList<Tomahawk::InfoSystem::ArtistTrackPair> );
Q_DECLARE_METATYPE( QList<Tomahawk::InfoSystem::Chart> );
#endif // TOMAHAWK_INFOSYSTEM_H #endif // TOMAHAWK_INFOSYSTEM_H

View File

@@ -145,7 +145,7 @@ WhatsHotWidget::fetchData()
requestData.type = Tomahawk::InfoSystem::InfoChartTracks; requestData.type = Tomahawk::InfoSystem::InfoChartTracks;
Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData ); Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
*/ */
tDebug() << "WhatsHot: requested InfoChartArtists+Tracks"; tDebug() << "WhatsHot: requested InfoChartCapabilities";
} }
@@ -190,25 +190,28 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
ui->breadCrumbLeft->currentChangedTriggered(m_crumbModelLeft->index(0,0).child(0,0)); ui->breadCrumbLeft->currentChangedTriggered(m_crumbModelLeft->index(0,0).child(0,0));
break; break;
} }
case InfoSystem::InfoChartArtists: case InfoSystem::InfoChart:
{ {
const QStringList artists = returnedData["artists"].toStringList(); if( !returnedData.contains("type") )
tDebug() << "WhatsHot: got artists! " << artists.size(); break;
tDebug() << artists; const QString type = returnedData["type"].toString();
foreach ( const QString& artist, artists ) if( !returnedData.contains(type) )
{ break;
m_artistsModel->addArtists( Artist::get( artist ) ); tDebug() << "WhatsHot: got chart! " << type;
} if( type == "artists" ) {
break; const QStringList artists = returnedData["artists"].toStringList();
} tDebug() << "WhatsHot: got artists! " << artists.size();
case InfoSystem::InfoChartTracks: foreach ( const QString& artist, artists )
{ m_artistsModel->addArtists( Artist::get( artist ) );
const QList<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >(); } else if( type == "tracks" ) {
tDebug() << "WhatsHot: got tracks! " << tracks.size(); const QList<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >();
foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks ) tDebug() << "WhatsHot: got tracks! " << tracks.size();
{ foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks ) {
query_ptr query = Query::get( track.artist, track.track, QString(), uuid() ); query_ptr query = Query::get( track.artist, track.track, QString(), uuid() );
m_tracksModel->append( query ); m_tracksModel->append( query );
}
} else {
tDebug() << "WhatsHot: got unknown chart type" << type;
} }
break; break;
} }
@@ -245,9 +248,16 @@ QStandardItem*
WhatsHotWidget::parseNode(QStandardItem* parentItem, const QString &label, const QVariant &data) WhatsHotWidget::parseNode(QStandardItem* parentItem, const QString &label, const QVariant &data)
{ {
tDebug() << "WhatsHot:: parsing " << label; tDebug() << "WhatsHot:: parsing " << label;
QStandardItem *sourceItem = new QStandardItem(label);
if( data.canConvert<QVariantMap>() ) { QStandardItem *sourceItem = new QStandardItem(label);
if( data.canConvert<QList<Tomahawk::InfoSystem::Chart> >() ) {
QList<Tomahawk::InfoSystem::Chart> charts = data.value<QList<Tomahawk::InfoSystem::Chart> >();
foreach( Tomahawk::InfoSystem::Chart chart, charts) {
QStandardItem *childItem= new QStandardItem(chart.label);
childItem->setData(chart.id);
sourceItem->appendRow(childItem);
}
} else if( data.canConvert<QVariantMap>() ) {
QVariantMap dataMap = data.toMap(); QVariantMap dataMap = data.toMap();
foreach(const QString childLabel,dataMap.keys()) { foreach(const QString childLabel,dataMap.keys()) {
QStandardItem *childItem = parseNode( sourceItem, childLabel, dataMap[childLabel] ); QStandardItem *childItem = parseNode( sourceItem, childLabel, dataMap[childLabel] );

View File

@@ -391,6 +391,8 @@ TomahawkApp::registerMetaTypes()
qRegisterMetaType< DirLister::Mode >("DirLister::Mode"); qRegisterMetaType< DirLister::Mode >("DirLister::Mode");
qRegisterMetaType< Tomahawk::InfoSystem::ArtistTrackPair >("Tomahawk::InfoSystem::ArtistTrackPair"); qRegisterMetaType< Tomahawk::InfoSystem::ArtistTrackPair >("Tomahawk::InfoSystem::ArtistTrackPair");
qRegisterMetaType< QList<Tomahawk::InfoSystem::ArtistTrackPair> >("QList<Tomahawk::InfoSystem::ArtistTrackPair>"); qRegisterMetaType< QList<Tomahawk::InfoSystem::ArtistTrackPair> >("QList<Tomahawk::InfoSystem::ArtistTrackPair>");
qRegisterMetaType< Tomahawk::InfoSystem::Chart>("Tomahawk::InfoSystem::Chart");
qRegisterMetaType< QList<Tomahawk::InfoSystem::Chart> >("QList<Tomahawk::InfoSystem::Chart>");
qRegisterMetaType< QPersistentModelIndex >( "QPersistentModelIndex" ); qRegisterMetaType< QPersistentModelIndex >( "QPersistentModelIndex" );
} }