diff --git a/src/libtomahawk/AlbumPlaylistInterface.cpp b/src/libtomahawk/AlbumPlaylistInterface.cpp index 35b24cdbd..cb30086de 100644 --- a/src/libtomahawk/AlbumPlaylistInterface.cpp +++ b/src/libtomahawk/AlbumPlaylistInterface.cpp @@ -23,7 +23,10 @@ #include "database/Database.h" #include "database/DatabaseImpl.h" #include "database/DatabaseCommand_AllTracks.h" +#include "Pipeline.h" #include "Query.h" +#include "Source.h" +#include "SourceList.h" #include "utils/Logger.h" @@ -31,10 +34,14 @@ using namespace Tomahawk; AlbumPlaylistInterface::AlbumPlaylistInterface() {} -AlbumPlaylistInterface::AlbumPlaylistInterface( Tomahawk::Album *album ) +AlbumPlaylistInterface::AlbumPlaylistInterface( Tomahawk::Album* album, Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection ) : Tomahawk::PlaylistInterface() , m_currentItem( 0 ) , m_currentTrack( 0 ) + , m_infoSystemLoaded( false ) + , m_databaseLoaded( false ) + , m_mode( mode ) + , m_collection( collection ) , m_album( QWeakPointer< Tomahawk::Album >( album ) ) { } @@ -88,15 +95,35 @@ AlbumPlaylistInterface::tracks() { if ( m_queries.isEmpty() && m_album ) { - DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks(); - cmd->setAlbum( m_album ); - cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); - //this takes discnumber into account as well + if ( ( m_mode == Mixed || m_mode == InfoSystemMode ) && !m_infoSystemLoaded ) + { + Tomahawk::InfoSystem::InfoStringHash artistInfo; + artistInfo["artist"] = m_album.data()->artist()->name(); + artistInfo["album"] = m_album.data()->name(); - connect( cmd, SIGNAL( tracks( QList, QVariant ) ), - m_album.data(), SLOT( onTracksAdded( QList ) ) ); + Tomahawk::InfoSystem::InfoRequestData requestData; + requestData.caller = uuid(); + requestData.input = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( artistInfo ); + requestData.type = Tomahawk::InfoSystem::InfoAlbumSongs; + requestData.timeoutMillis = 0; + requestData.allSources = true; + Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData ); - Database::instance()->enqueue( QSharedPointer( cmd ) ); + connect( Tomahawk::InfoSystem::InfoSystem::instance(), + SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), + SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) ); + } + else if ( m_mode == DatabaseMode && !m_databaseLoaded ) + { + DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); + cmd->setAlbum( m_album ); + cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + + connect( cmd, SIGNAL( tracks( QList, QVariant ) ), + SLOT( onTracksLoaded( QList ) ) ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); + } } return m_queries; @@ -104,7 +131,121 @@ AlbumPlaylistInterface::tracks() void -AlbumPlaylistInterface::addQueries( const QList< query_ptr >& tracks ) +AlbumPlaylistInterface::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output ) { - m_queries << tracks; + switch ( requestData.type ) + { + case Tomahawk::InfoSystem::InfoAlbumSongs: + { + QVariantMap returnedData = output.value< QVariantMap >(); + if ( !returnedData.isEmpty() ) + { + Tomahawk::InfoSystem::InfoStringHash inputInfo; + inputInfo = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); + + QStringList tracks = returnedData[ "tracks" ].toStringList(); + QList ql; + + //TODO: Figure out how to do this with a multi-disk album without breaking the + // current behaviour. I just know too little about InfoSystem to deal with + // it right now, I've only taken the liberty of adding Query::setDiscNumber + // which should make this easier. --Teo 11/2011 + unsigned int trackNo = 1; + + foreach ( const QString& trackName, tracks ) + { + query_ptr query = Query::get( inputInfo[ "artist" ], trackName, inputInfo[ "album" ] ); + query->setAlbumPos( trackNo++ ); + ql << query; + tDebug() << Q_FUNC_INFO << query->toString(); + } + Pipeline::instance()->resolve( ql ); + + m_queries << ql; + } + + break; + } + + default: + { + Q_ASSERT( false ); + break; + } + } + + m_infoSystemLoaded = true; + disconnect( Tomahawk::InfoSystem::InfoSystem::instance(), SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ), + this, SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) ); + + if ( m_queries.isEmpty() && m_mode == Mixed ) + { + DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); + cmd->setAlbum( m_album ); + //this takes discnumber into account as well + cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + + connect( cmd, SIGNAL( tracks( QList, QVariant ) ), + SLOT( onTracksLoaded( QList ) ) ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); + } + else + { + emit tracksLoaded( m_mode, m_collection ); + } +} + + +void +AlbumPlaylistInterface::onTracksLoaded( const QList< query_ptr >& tracks ) +{ + m_databaseLoaded = true; + + if ( m_collection.isNull() ) + m_queries << filterTracks( tracks ); + else + m_queries << tracks; + + emit tracksLoaded( m_mode, m_collection ); +} + + +QList +AlbumPlaylistInterface::filterTracks( const QList& queries ) +{ + QList result; + + for ( int i = 0; i < queries.count(); i++ ) + { + bool picked = true; + const query_ptr q1 = queries.at( i ); + + for ( int j = 0; j < result.count(); j++ ) + { + if ( !picked ) + break; + + const query_ptr& q2 = result.at( j ); + + if ( q1->track() == q2->track() ) + { + picked = false; + } + } + + if ( picked ) + { + query_ptr q = Query::get( q1->artist(), q1->track(), q1->album(), uuid(), true ); + q->setAlbumPos( q1->results().first()->albumpos() ); + q->setDiscNumber( q1->discnumber() ); + result << q; + } + } + + foreach ( const query_ptr& q, result ) + { + tDebug() << q->albumpos() << q->track(); + } + return result; } diff --git a/src/libtomahawk/AlbumPlaylistInterface.h b/src/libtomahawk/AlbumPlaylistInterface.h index 89c1e347b..f5eaeff4f 100644 --- a/src/libtomahawk/AlbumPlaylistInterface.h +++ b/src/libtomahawk/AlbumPlaylistInterface.h @@ -26,6 +26,7 @@ #include "Album.h" #include "Typedefs.h" #include "PlaylistInterface.h" +#include "infosystem/InfoSystem.h" #include "DllMacro.h" namespace Tomahawk @@ -36,7 +37,7 @@ class DLLEXPORT AlbumPlaylistInterface : public Tomahawk::PlaylistInterface Q_OBJECT public: - AlbumPlaylistInterface( Tomahawk::Album *album ); + AlbumPlaylistInterface( Tomahawk::Album* album, Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection ); virtual ~AlbumPlaylistInterface(); QList tracks(); @@ -57,8 +58,6 @@ public: virtual void setFilter( const QString& /*pattern*/ ) {} - virtual void addQueries( const QList& tracks ); - signals: void repeatModeChanged( Tomahawk::PlaylistInterface::RepeatMode mode ); void shuffleModeChanged( bool enabled ); @@ -68,12 +67,26 @@ signals: void nextTrackReady(); + void tracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection ); + +private slots: + void onTracksLoaded( const QList< Tomahawk::query_ptr >& tracks ); + void infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output ); + private: AlbumPlaylistInterface(); + + QList filterTracks( const QList& queries ); QList< Tomahawk::query_ptr > m_queries; result_ptr m_currentItem; unsigned int m_currentTrack; + + bool m_infoSystemLoaded; + bool m_databaseLoaded; + + Tomahawk::ModelMode m_mode; + Tomahawk::collection_ptr m_collection; QWeakPointer< Tomahawk::Album > m_album; };