From b9e772f3e3bcc2faaa90216607b4b821a88e84d8 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 5 May 2012 16:43:47 +0200 Subject: [PATCH] * Added similar tracks and playback history methods to Artist. --- src/libtomahawk/Artist.cpp | 111 ++++++++++++++++++++++++++++++++++--- src/libtomahawk/Artist.h | 16 ++++++ 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/src/libtomahawk/Artist.cpp b/src/libtomahawk/Artist.cpp index 3bd9bfec0..b0d79c1e8 100644 --- a/src/libtomahawk/Artist.cpp +++ b/src/libtomahawk/Artist.cpp @@ -23,8 +23,8 @@ #include "Collection.h" #include "database/Database.h" #include "database/DatabaseImpl.h" -#include "Query.h" #include "database/DatabaseCommand_AllAlbums.h" +#include "database/DatabaseCommand_TrackStats.h" #include "utils/Logger.h" @@ -83,6 +83,7 @@ Artist::Artist( unsigned int id, const QString& name ) , m_name( name ) , m_infoLoaded( false ) , m_infoLoading( false ) + , m_simArtistsLoaded( false ) , m_infoJobs( 0 ) #ifndef ENABLE_HEADLESS , m_cover( 0 ) @@ -138,12 +139,12 @@ Artist::albums( ModelMode mode, const Tomahawk::collection_ptr& collection ) con requestData.type = Tomahawk::InfoSystem::InfoArtistReleases; connect( Tomahawk::InfoSystem::InfoSystem::instance(), - SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), - SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), Qt::UniqueConnection ); + SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), + SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), Qt::UniqueConnection ); connect( Tomahawk::InfoSystem::InfoSystem::instance(), - SIGNAL( finished( QString ) ), - SLOT( infoSystemFinished( QString ) ), Qt::UniqueConnection ); + SIGNAL( finished( QString ) ), + SLOT( infoSystemFinished( QString ) ), Qt::UniqueConnection ); m_infoJobs++; Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData ); @@ -164,6 +165,87 @@ Artist::albums( ModelMode mode, const Tomahawk::collection_ptr& collection ) con } +QList +Artist::similarArtists() const +{ + if ( !m_simArtistsLoaded ) + { + Tomahawk::InfoSystem::InfoStringHash artistInfo; + artistInfo["artist"] = name(); + + Tomahawk::InfoSystem::InfoRequestData requestData; + requestData.caller = m_uuid; + requestData.customData = QVariantMap(); + + requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( artistInfo ); + requestData.type = Tomahawk::InfoSystem::InfoArtistSimilars; + requestData.requestId = TomahawkUtils::infosystemRequestId(); + + connect( Tomahawk::InfoSystem::InfoSystem::instance(), + SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), + SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), Qt::UniqueConnection ); + + connect( Tomahawk::InfoSystem::InfoSystem::instance(), + SIGNAL( finished( QString ) ), + SLOT( infoSystemFinished( QString ) ), Qt::UniqueConnection ); + + m_infoJobs++; + Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData ); + } + + return m_similarArtists; +} + + +void +Artist::loadStats() +{ + artist_ptr a = m_ownRef.toStrongRef(); + + DatabaseCommand_TrackStats* cmd = new DatabaseCommand_TrackStats( a ); + Database::instance()->enqueue( QSharedPointer(cmd) ); +} + + +QList< Tomahawk::PlaybackLog > +Artist::playbackHistory( const Tomahawk::source_ptr& source ) const +{ + QList< Tomahawk::PlaybackLog > history; + + foreach ( const PlaybackLog& log, m_playbackHistory ) + { + if ( source.isNull() || log.source == source ) + { + history << log; + } + } + + return history; +} + + +void +Artist::setPlaybackHistory( const QList< Tomahawk::PlaybackLog >& playbackData ) +{ + m_playbackHistory = playbackData; + emit statsLoaded(); +} + + +unsigned int +Artist::playbackCount( const source_ptr& source ) +{ + unsigned int count = 0; + foreach ( const PlaybackLog& log, m_playbackHistory ) + { + if ( source.isNull() || log.source == source ) + count++; + } + + return count; +} + + void Artist::onAlbumsFound( const QList< album_ptr >& albums, const QVariant& data ) { @@ -183,11 +265,11 @@ Artist::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVari if ( requestData.caller != m_uuid ) return; + QVariantMap returnedData = output.value< QVariantMap >(); switch ( requestData.type ) { case Tomahawk::InfoSystem::InfoArtistReleases: { - QVariantMap returnedData = output.value< QVariantMap >(); QStringList albumNames = returnedData[ "albums" ].toStringList(); Tomahawk::InfoSystem::InfoStringHash inputInfo; inputInfo = requestData.input.value< InfoSystem::InfoStringHash >(); @@ -212,7 +294,6 @@ Artist::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVari { if ( !output.isNull() && output.isValid() ) { - QVariantMap returnedData = output.value< QVariantMap >(); const QByteArray ba = returnedData["imgbytes"].toByteArray(); if ( ba.length() ) { @@ -224,7 +305,21 @@ Artist::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVari break; } - + + case InfoSystem::InfoArtistSimilars: + { + const QStringList artists = returnedData["artists"].toStringList(); + foreach ( const QString& artist, artists ) + { + m_similarArtists << Artist::get( artist ); + } + + m_simArtistsLoaded = true; + emit similarArtistsLoaded(); + + break; + } + default: Q_ASSERT( false ); } diff --git a/src/libtomahawk/Artist.h b/src/libtomahawk/Artist.h index 8f9459422..eee1da918 100644 --- a/src/libtomahawk/Artist.h +++ b/src/libtomahawk/Artist.h @@ -30,6 +30,7 @@ #include "Typedefs.h" #include "DllMacro.h" +#include "Query.h" #include "infosystem/InfoSystem.h" namespace Tomahawk @@ -53,6 +54,13 @@ public: bool infoLoaded() const { return m_infoLoaded; } QList albums( ModelMode mode = Mixed, const Tomahawk::collection_ptr& collection = Tomahawk::collection_ptr() ) const; + QList similarArtists() const; + + void loadStats(); + QList< Tomahawk::PlaybackLog > playbackHistory( const Tomahawk::source_ptr& source = Tomahawk::source_ptr() ) const; + void setPlaybackHistory( const QList< Tomahawk::PlaybackLog >& playbackData ); + unsigned int playbackCount( const Tomahawk::source_ptr& source = Tomahawk::source_ptr() ); + #ifndef ENABLE_HEADLESS QPixmap cover( const QSize& size, bool forceLoad = true ) const; #endif @@ -68,6 +76,8 @@ signals: void updated(); void coverChanged(); + void similarArtistsLoaded(); + void statsLoaded(); private slots: void onTracksAdded( const QList& tracks ); @@ -86,11 +96,17 @@ private: bool m_infoLoaded; mutable bool m_infoLoading; QHash m_albumsLoaded; + bool m_simArtistsLoaded; + mutable QString m_uuid; mutable int m_infoJobs; QList m_databaseAlbums; QList m_officialAlbums; + QList m_similarArtists; + + bool m_playbackHistoryLoaded; + QList< PlaybackLog > m_playbackHistory; mutable QByteArray m_coverBuffer; #ifndef ENABLE_HEADLESS