diff --git a/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp b/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp index b50b22a16..25adfe281 100644 --- a/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp +++ b/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "album.h" #include "typedefs.h" @@ -49,7 +50,7 @@ LastFmPlugin::LastFmPlugin() : InfoPlugin() , 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; /* @@ -150,13 +151,10 @@ LastFmPlugin::getInfo( uint requestId, Tomahawk::InfoSystem::InfoRequestData req fetchTopTracks( requestId, requestData ); break; - case InfoChartArtists: - fetchChartArtists( requestId, requestData ); + case InfoChart: + fetchChart( requestId, requestData ); break; - case InfoChartTracks: - fetchChartTracks( requestId, requestData ); - break; case InfoChartCapabilities: fetchChartCapabilities( requestId, requestData ); break; @@ -312,7 +310,7 @@ LastFmPlugin::fetchTopTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestD } void -LastFmPlugin::fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ) +LastFmPlugin::fetchChart( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ) { 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 >(); Tomahawk::InfoSystem::InfoCriteriaHash criteria; - if ( hash.contains( "country" ) ) - { - 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 >() ) + if ( !hash.contains( "chart_id" ) ) { dataError( requestId, requestData ); return; - } - InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >(); - Tomahawk::InfoSystem::InfoCriteriaHash criteria; - if ( hash.contains( "country" ) ) - { - criteria["country"] = hash["country"]; + } else { + criteria["chart_id"] = hash["chart_id"]; } - emit getCachedInfo( requestId, criteria, 2419200000, requestData ); + emit getCachedInfo( requestId, criteria, 0, requestData ); } - void 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 >(); Tomahawk::InfoSystem::InfoCriteriaHash criteria; - emit getCachedInfo( requestId, criteria, 2419200000, requestData ); + emit getCachedInfo( requestId, criteria, 0, requestData ); } void @@ -420,57 +401,38 @@ LastFmPlugin::notInCacheSlot( uint requestId, QHash criteria, switch ( requestData.type ) { - case InfoChartArtists: + case InfoChart: { - tDebug() << "LastfmPlugin: InfoChartArtists notin cache, fetching"; + tDebug() << "LastfmPlugin: InfoChart not in cache, fetching"; QMap args; - if( criteria.contains( "country" ) ) { - args["method"] = "geo.getTopArtists"; - args["country"] = criteria["country"]; - } else { - args["method"] = "chart.getTopArtists"; - } + args["method"] = criteria["chart_id"]; 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( chartTopArtistsReturned() ) ); - return; - } - - case InfoChartTracks: - { - tDebug() << "LastfmPlugin: InfoChartTracks not in cache, fetching"; - QMap 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() ) ); + connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) ); return; } case InfoChartCapabilities: { - tDebug() << "LastfmPlugin: InfoChartCapabilities not in cache, fetching"; - QString json("{'Last.fm': {'Tracks': ['Top Tracks','Hyped Tracks','Most Loved Tracks']," - "'Artists': ['Top Artists', 'Hyped Artists']}}"); - json.replace("'", "\""); - QJson::Parser parser; - bool ok = false; - QVariantMap result = parser.parse (json.toUtf8(), &ok).toMap(); - if(!ok) { - tDebug() << "Lastfm Plugin: parsing json failed"; - return; - } + QList track_charts; + track_charts.append(Chart("chart.getTopTracks", "Top Tracks", "tracks")); + track_charts.append(Chart("chart.getLovedTracks", "Loved Tracks", "tracks")); + track_charts.append(Chart("chart.getHypedTracks", "Hyped Tracks", "tracks")); + + QList artist_charts; + artist_charts.append(Chart("chart.getTopArtists", "Top Artists", "artists")); + artist_charts.append(Chart("chart.getHypedArtists", "Hyped Artists", "artists")); + + QVariantMap charts; + charts.insert("Tracks", QVariant::fromValue >(track_charts)); + charts.insert("Artists", QVariant::fromValue >(artist_charts)); + + QVariantMap result; + result.insert("Last.fm", QVariant::fromValue(charts)); + emit info( requestId, requestData, @@ -571,57 +533,38 @@ LastFmPlugin::similarArtistsReturned() } void -LastFmPlugin::chartTopArtistsReturned() +LastFmPlugin::chartReturned() { - tDebug() << "LastfmPlugin: InfoChartArtists data returned!"; + tDebug() << "LastfmPlugin: InfoChart data returned!"; QNetworkReply* reply = qobject_cast( sender() ); - QList list = lastfm::Artist::list( reply ); - QStringList al; - - tDebug() << "\tgot " << list.size() << " artists"; - - foreach ( const lastfm::Artist& a, list ) - al << a.toString(); QVariantMap returnedData; - returnedData["artists"] = al; - Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(); + if( replyIsTracks( reply) ) { + QList tracks = parseTrackList( reply ); + QList 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( - reply->property( "requestId" ).toUInt(), - requestData, - returnedData - ); - - 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, 2419200000, requestData.type, returnedData ); -} - -void -LastFmPlugin::chartTopTracksReturned() -{ - tDebug() << "LastfmPlugin: InfoChartTracks data returned!"; - QNetworkReply* reply = qobject_cast( sender() ); - QList tracks = parseTrackList( reply ); - - QList top_tracks; - - foreach( const lastfm::Track &t, tracks ) { - ArtistTrackPair pair; - pair.artist = t.artist().toString(); - pair.track = t.title(); - top_tracks << pair; + } else if( replyIsArtists( reply ) ) { + QList list = lastfm::Artist::list( reply ); + QStringList al; + tDebug() << "LastFmPlugin:"<< "\tgot " << list.size() << " artists"; + foreach ( const lastfm::Artist& a, list ) + al << a.toString(); + returnedData["artists"] = al; + returnedData["type"] = "artists"; + } else { + tDebug() << "LastfmPlugin:: got non tracks and non artists"; } - 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 >(); emit info( @@ -629,12 +572,7 @@ LastFmPlugin::chartTopTracksReturned() requestData, returnedData ); - - 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 ); + // TODO update cache } void @@ -885,3 +823,32 @@ LastFmPlugin::parseTrackList( QNetworkReply * reply ) 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; + } +} diff --git a/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.h b/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.h index 11c4fffc8..6095ffb90 100644 --- a/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.h +++ b/src/libtomahawk/infosystem/infoplugins/generic/lastfmplugin.h @@ -52,8 +52,7 @@ public slots: void artistImagesReturned(); void similarArtistsReturned(); void topTracksReturned(); - void chartTopArtistsReturned(); - void chartTopTracksReturned(); + void chartReturned(); void namChangedSlot( QNetworkAccessManager *nam ); @@ -68,8 +67,7 @@ private: void fetchArtistImages( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchSimilarArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchTopTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); - void fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); - void fetchChartTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); + void fetchChart( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChartCapabilities( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); void createScrobbler(); @@ -80,6 +78,8 @@ private: void dataError( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData ); QList parseTrackList( QNetworkReply * reply ); + bool replyIsTracks( QNetworkReply *reply ); + bool replyIsArtists( QNetworkReply *reply ); lastfm::MutableTrack m_track; lastfm::Audioscrobbler* m_scrobbler; diff --git a/src/libtomahawk/infosystem/infosystem.h b/src/libtomahawk/infosystem/infosystem.h index e4428f6c7..4de22ea6d 100644 --- a/src/libtomahawk/infosystem/infosystem.h +++ b/src/libtomahawk/infosystem/infosystem.h @@ -102,9 +102,7 @@ enum InfoType { // as items are saved in cache, mark them here to not change the /** * Documentation for InfoChartArtists */ - InfoChartArtists = 51, - InfoChartAlbums = 52, /*!< Documentation for InfoChartAlbums */ - InfoChartTracks = 53, + InfoChart = 51, InfoMiscTopHotttness = 60, InfoMiscTopTerms = 61, @@ -137,6 +135,18 @@ struct ArtistTrackPair { 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, uint > InfoTimeoutMap; 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( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > ); Q_DECLARE_METATYPE( Tomahawk::InfoSystem::ArtistTrackPair ); +Q_DECLARE_METATYPE( Tomahawk::InfoSystem::Chart ); Q_DECLARE_METATYPE( QList ); +Q_DECLARE_METATYPE( QList ); #endif // TOMAHAWK_INFOSYSTEM_H diff --git a/src/libtomahawk/widgets/whatshotwidget.cpp b/src/libtomahawk/widgets/whatshotwidget.cpp index 5f530a83b..aed53cc2f 100644 --- a/src/libtomahawk/widgets/whatshotwidget.cpp +++ b/src/libtomahawk/widgets/whatshotwidget.cpp @@ -145,7 +145,7 @@ WhatsHotWidget::fetchData() requestData.type = Tomahawk::InfoSystem::InfoChartTracks; 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)); break; } - case InfoSystem::InfoChartArtists: + case InfoSystem::InfoChart: { - const QStringList artists = returnedData["artists"].toStringList(); - tDebug() << "WhatsHot: got artists! " << artists.size(); - tDebug() << artists; - foreach ( const QString& artist, artists ) - { - m_artistsModel->addArtists( Artist::get( artist ) ); - } - break; - } - case InfoSystem::InfoChartTracks: - { - const QList tracks = returnedData["tracks"].value >(); - tDebug() << "WhatsHot: got tracks! " << tracks.size(); - foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks ) - { - query_ptr query = Query::get( track.artist, track.track, QString(), uuid() ); - m_tracksModel->append( query ); + if( !returnedData.contains("type") ) + break; + const QString type = returnedData["type"].toString(); + if( !returnedData.contains(type) ) + break; + tDebug() << "WhatsHot: got chart! " << type; + if( type == "artists" ) { + const QStringList artists = returnedData["artists"].toStringList(); + tDebug() << "WhatsHot: got artists! " << artists.size(); + foreach ( const QString& artist, artists ) + m_artistsModel->addArtists( Artist::get( artist ) ); + } else if( type == "tracks" ) { + const QList tracks = returnedData["tracks"].value >(); + tDebug() << "WhatsHot: got tracks! " << tracks.size(); + foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks ) { + query_ptr query = Query::get( track.artist, track.track, QString(), uuid() ); + m_tracksModel->append( query ); + } + } else { + tDebug() << "WhatsHot: got unknown chart type" << type; } break; } @@ -245,9 +248,16 @@ QStandardItem* WhatsHotWidget::parseNode(QStandardItem* parentItem, const QString &label, const QVariant &data) { tDebug() << "WhatsHot:: parsing " << label; - QStandardItem *sourceItem = new QStandardItem(label); - if( data.canConvert() ) { + QStandardItem *sourceItem = new QStandardItem(label); + if( data.canConvert >() ) { + QList charts = data.value >(); + foreach( Tomahawk::InfoSystem::Chart chart, charts) { + QStandardItem *childItem= new QStandardItem(chart.label); + childItem->setData(chart.id); + sourceItem->appendRow(childItem); + } + } else if( data.canConvert() ) { QVariantMap dataMap = data.toMap(); foreach(const QString childLabel,dataMap.keys()) { QStandardItem *childItem = parseNode( sourceItem, childLabel, dataMap[childLabel] ); diff --git a/src/tomahawkapp.cpp b/src/tomahawkapp.cpp index 6f622261e..8dce6460a 100644 --- a/src/tomahawkapp.cpp +++ b/src/tomahawkapp.cpp @@ -391,6 +391,8 @@ TomahawkApp::registerMetaTypes() qRegisterMetaType< DirLister::Mode >("DirLister::Mode"); qRegisterMetaType< Tomahawk::InfoSystem::ArtistTrackPair >("Tomahawk::InfoSystem::ArtistTrackPair"); qRegisterMetaType< QList >("QList"); + qRegisterMetaType< Tomahawk::InfoSystem::Chart>("Tomahawk::InfoSystem::Chart"); + qRegisterMetaType< QList >("QList"); qRegisterMetaType< QPersistentModelIndex >( "QPersistentModelIndex" ); }