From cf7ce9aa0e0549fe786760f947bd561a8f5c06d7 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 12 Nov 2014 09:24:12 +0100 Subject: [PATCH] PlaylistInterface emits a signal when the first playable track has been resolved. --- src/libtomahawk/AlbumPlaylistInterface.cpp | 21 ++------- src/libtomahawk/AlbumPlaylistInterface.h | 2 - src/libtomahawk/ArtistPlaylistInterface.cpp | 17 +------ src/libtomahawk/ArtistPlaylistInterface.h | 2 - src/libtomahawk/PlaylistInterface.cpp | 51 ++++++++++++++++++++- src/libtomahawk/PlaylistInterface.h | 12 +++-- 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/libtomahawk/AlbumPlaylistInterface.cpp b/src/libtomahawk/AlbumPlaylistInterface.cpp index 82907f462..cfe4fbdea 100644 --- a/src/libtomahawk/AlbumPlaylistInterface.cpp +++ b/src/libtomahawk/AlbumPlaylistInterface.cpp @@ -141,7 +141,7 @@ AlbumPlaylistInterface::tracks() const const_cast< int& >( m_lastQueryTimestamp ) = QDateTime::currentMSecsSinceEpoch(); } - else if ( m_mode == DatabaseMode && !m_databaseLoaded && !m_finished ) + else if ( m_mode == DatabaseMode && !m_databaseLoaded && !isFinished() ) { if ( m_collection.isNull() ) //we do a dbcmd directly, for the SuperCollection I guess? { @@ -205,7 +205,6 @@ AlbumPlaylistInterface::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData re Pipeline::instance()->resolve( ql ); m_queries << ql; - checkQueries(); } break; @@ -238,7 +237,7 @@ AlbumPlaylistInterface::infoSystemFinished( const QString& infoId ) this, SLOT( infoSystemFinished( QString ) ) ); // Add !m_finished check to not endlessly reload on an empty album. - if ( m_queries.isEmpty() && m_mode == Mixed && !m_finished ) + if ( m_queries.isEmpty() && m_mode == Mixed && !isFinished() ) { if ( m_collection.isNull() ) //we do a dbcmd directly, for the SuperCollection I guess? { @@ -262,7 +261,7 @@ AlbumPlaylistInterface::infoSystemFinished( const QString& infoId ) } else { - m_finished = true; + finishLoading(); emit tracksLoaded( m_mode, m_collection ); } } @@ -279,9 +278,7 @@ AlbumPlaylistInterface::onTracksLoaded( const QList< query_ptr >& tracks ) else m_queries << tracks; - checkQueries(); - - m_finished = true; + finishLoading(); emit tracksLoaded( m_mode, m_collection ); } @@ -339,13 +336,3 @@ AlbumPlaylistInterface::resultAt( qint64 index ) const return Tomahawk::result_ptr(); } - - -void -AlbumPlaylistInterface::checkQueries() -{ - foreach ( const Tomahawk::query_ptr& query, m_queries ) - { - connect( query.data(), SIGNAL( playableStateChanged( bool ) ), SLOT( onItemsChanged() ), Qt::UniqueConnection ); - } -} diff --git a/src/libtomahawk/AlbumPlaylistInterface.h b/src/libtomahawk/AlbumPlaylistInterface.h index 0bb320d5a..88f5af97b 100644 --- a/src/libtomahawk/AlbumPlaylistInterface.h +++ b/src/libtomahawk/AlbumPlaylistInterface.h @@ -70,8 +70,6 @@ private slots: void infoSystemFinished( const QString& infoId ); private: - void checkQueries(); - QList< Tomahawk::query_ptr > m_queries; mutable result_ptr m_currentItem; diff --git a/src/libtomahawk/ArtistPlaylistInterface.cpp b/src/libtomahawk/ArtistPlaylistInterface.cpp index 51aa73c37..5aa044cdd 100644 --- a/src/libtomahawk/ArtistPlaylistInterface.cpp +++ b/src/libtomahawk/ArtistPlaylistInterface.cpp @@ -162,7 +162,6 @@ ArtistPlaylistInterface::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData r } m_queries << ql; - checkQueries(); } break; @@ -207,7 +206,7 @@ ArtistPlaylistInterface::infoSystemFinished( const QString &infoId ) } else { - m_finished = true; + finishLoading(); emit tracksLoaded( m_mode, m_collection ); } } @@ -224,9 +223,7 @@ ArtistPlaylistInterface::onTracksLoaded( const QList< query_ptr >& tracks ) else m_queries << tracks; - checkQueries(); - - m_finished = true; + finishLoading(); emit tracksLoaded( m_mode, m_collection ); } @@ -284,13 +281,3 @@ ArtistPlaylistInterface::resultAt( qint64 index ) const return Tomahawk::result_ptr(); } - - -void -ArtistPlaylistInterface::checkQueries() -{ - foreach ( const Tomahawk::query_ptr& query, m_queries ) - { - connect( query.data(), SIGNAL( playableStateChanged( bool ) ), SLOT( onItemsChanged() ), Qt::UniqueConnection ); - } -} diff --git a/src/libtomahawk/ArtistPlaylistInterface.h b/src/libtomahawk/ArtistPlaylistInterface.h index 5c2e8d89c..850a0470a 100644 --- a/src/libtomahawk/ArtistPlaylistInterface.h +++ b/src/libtomahawk/ArtistPlaylistInterface.h @@ -69,8 +69,6 @@ private slots: private: Q_DISABLE_COPY( ArtistPlaylistInterface ) - void checkQueries(); - QList< Tomahawk::query_ptr > m_queries; mutable result_ptr m_currentItem; diff --git a/src/libtomahawk/PlaylistInterface.cpp b/src/libtomahawk/PlaylistInterface.cpp index 7b04bf2ec..0177ca4c3 100644 --- a/src/libtomahawk/PlaylistInterface.cpp +++ b/src/libtomahawk/PlaylistInterface.cpp @@ -30,10 +30,11 @@ using namespace Tomahawk; PlaylistInterface::PlaylistInterface() : QObject() , m_latchMode( PlaylistModes::StayOnSong ) - , m_finished( false ) , m_prevAvail( false ) , m_nextAvail( false ) , m_currentIndex( -1 ) + , m_finished( false ) + , m_foundFirstTrack( false ) { m_id = uuid(); } @@ -246,3 +247,51 @@ PlaylistInterface::setCurrentIndex( qint64 index ) emit currentIndexChanged(); onItemsChanged(); } + + +void +PlaylistInterface::finishLoading() +{ + foreach ( const Tomahawk::query_ptr& query, tracks() ) + { + connect( query.data(), SIGNAL( playableStateChanged( bool ) ), SLOT( onItemsChanged() ), Qt::UniqueConnection ); + connect( query.data(), SIGNAL( resolvingFinished( bool ) ), SLOT( onQueryResolved() ), Qt::UniqueConnection ); + } + + m_finished = true; + emit finishedLoading(); +} + + +void +PlaylistInterface::onQueryResolved() +{ + if ( m_foundFirstTrack ) + return; + + // We're looking for the first playable track, but want to make sure the + // second track doesn't start playing before the first really finished resolving + foreach ( const Tomahawk::query_ptr& query, tracks() ) + { + if ( !query->resolvingFinished() ) + { + // Wait for this track to be resolved + return; + } + + if ( query->playable() ) + { + // We have a playable track, all previous tracks are resolved but not playable + break; + } + } + + // We have either found the first playable track or none of the tracks are playable + m_foundFirstTrack = true; + emit foundFirstPlayableTrack(); + + foreach ( const Tomahawk::query_ptr& query, tracks() ) + { + disconnect( query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( onQueryResolved() ) ); + } +} diff --git a/src/libtomahawk/PlaylistInterface.h b/src/libtomahawk/PlaylistInterface.h index bbd4f94d7..e279c2e77 100644 --- a/src/libtomahawk/PlaylistInterface.h +++ b/src/libtomahawk/PlaylistInterface.h @@ -40,7 +40,8 @@ public: const QString id() const { return m_id; } virtual QList< Tomahawk::query_ptr > tracks() const = 0; - virtual bool isFinished() const { return m_finished; } + bool isFinished() const { return m_finished; } + bool hasFirstPlayableTrack() const { return m_foundFirstTrack; } virtual int trackCount() const = 0; @@ -56,7 +57,7 @@ public: virtual qint64 siblingResultIndex( int itemsAway, qint64 rootIndex = -1 ) const; virtual Tomahawk::result_ptr siblingResult( int itemsAway, qint64 rootIndex = -1 ) const; virtual Tomahawk::result_ptr setSiblingResult( int itemsAway, qint64 rootIndex = -1 ); - + virtual Tomahawk::result_ptr resultAt( qint64 index ) const = 0; virtual Tomahawk::query_ptr queryAt( qint64 index ) const = 0; virtual qint64 indexOfResult( const Tomahawk::result_ptr& result ) const = 0; @@ -102,15 +103,18 @@ signals: void nextTrackAvailable( bool available ); void currentIndexChanged(); + void finishedLoading(); + void foundFirstPlayableTrack(); protected slots: virtual void onItemsChanged(); + void finishLoading(); + void onQueryResolved(); protected: virtual QList filterTracks( const QList& queries ); PlaylistModes::LatchMode m_latchMode; - bool m_finished; mutable bool m_prevAvail; mutable bool m_nextAvail; mutable qint64 m_currentIndex; @@ -121,6 +125,8 @@ private: private: QString m_id; QString m_filter; + bool m_finished; + bool m_foundFirstTrack; }; }