1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 15:59:42 +01: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 <QCryptographicHash>
#include <QNetworkConfiguration>
#include <QDomElement>
#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<QString, QString> criteria,
switch ( requestData.type )
{
case InfoChartArtists:
case InfoChart:
{
tDebug() << "LastfmPlugin: InfoChartArtists notin cache, fetching";
tDebug() << "LastfmPlugin: InfoChart not in cache, fetching";
QMap<QString, QString> 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<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() ) );
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<Chart> 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<Chart> 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<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(
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<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;
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(
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<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;
} else if( replyIsArtists( reply ) ) {
QList<lastfm::Artist> 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;
}
}

View File

@ -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<lastfm::Track> parseTrackList( QNetworkReply * reply );
bool replyIsTracks( QNetworkReply *reply );
bool replyIsArtists( QNetworkReply *reply );
lastfm::MutableTrack m_track;
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
*/
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<Tomahawk::InfoSystem::ArtistTrackPair> );
Q_DECLARE_METATYPE( QList<Tomahawk::InfoSystem::Chart> );
#endif // TOMAHAWK_INFOSYSTEM_H

View File

@ -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<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >();
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<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >();
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<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();
foreach(const QString childLabel,dataMap.keys()) {
QStandardItem *childItem = parseNode( sourceItem, childLabel, dataMap[childLabel] );

View File

@ -391,6 +391,8 @@ TomahawkApp::registerMetaTypes()
qRegisterMetaType< DirLister::Mode >("DirLister::Mode");
qRegisterMetaType< Tomahawk::InfoSystem::ArtistTrackPair >("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" );
}