diff --git a/src/libtomahawk/PlaylistInterface.cpp b/src/libtomahawk/PlaylistInterface.cpp index 0e7af24c7..d458e34b5 100644 --- a/src/libtomahawk/PlaylistInterface.cpp +++ b/src/libtomahawk/PlaylistInterface.cpp @@ -30,6 +30,9 @@ PlaylistInterface::PlaylistInterface () : QObject() , m_latchMode( PlaylistModes::StayOnSong ) , m_finished( false ) + , m_prevAvail( false ) + , m_nextAvail( false ) + , m_currentIndex( -1 ) { m_id = uuid(); } @@ -55,9 +58,9 @@ PlaylistInterface::nextResult() const Tomahawk::result_ptr -PlaylistInterface::siblingResult( int itemsAway ) const +PlaylistInterface::siblingResult( int itemsAway, qint64 rootIndex ) const { - qint64 idx = siblingIndex( itemsAway ); + qint64 idx = siblingIndex( itemsAway, rootIndex ); qint64 safetyCheck = -1; // If safetyCheck equals idx, this means the interface keeps returning the same item and we won't discover anything new - abort @@ -168,3 +171,55 @@ PlaylistInterface::hasPreviousResult() const { return ( siblingResult( -1 ) ); } + + +void +PlaylistInterface::onItemsChanged() +{ + if ( QThread::currentThread() != thread() ) + { + QMetaObject::invokeMethod( this, "onItemsChanged", Qt::QueuedConnection ); + return; + } + + Tomahawk::result_ptr prevResult = siblingResult( -1, m_currentIndex ); + Tomahawk::result_ptr nextResult = siblingResult( 1, m_currentIndex ); + + if ( prevResult ) + { + bool avail = prevResult->toQuery()->playable(); + if ( avail != m_prevAvail ) + { + m_prevAvail = avail; + emit previousTrackAvailable(); + } + } + else if ( m_prevAvail ) + { + m_prevAvail = false; + emit previousTrackAvailable(); + } + + if ( nextResult ) + { + bool avail = nextResult->toQuery()->playable(); + if ( avail != m_nextAvail ) + { + m_nextAvail = avail; + emit nextTrackAvailable(); + } + } + else if ( m_nextAvail ) + { + m_nextAvail = false; + emit nextTrackAvailable(); + } +} + + +void +PlaylistInterface::setCurrentIndex( qint64 index ) +{ + m_currentIndex = index; + onItemsChanged(); +} diff --git a/src/libtomahawk/PlaylistInterface.h b/src/libtomahawk/PlaylistInterface.h index 8c557f8c3..d4dbcfa9a 100644 --- a/src/libtomahawk/PlaylistInterface.h +++ b/src/libtomahawk/PlaylistInterface.h @@ -46,7 +46,7 @@ public: virtual int trackCount() const = 0; virtual Tomahawk::result_ptr currentItem() const = 0; - virtual void setCurrentIndex( qint64 index ) = 0; + virtual void setCurrentIndex( qint64 index ); virtual bool hasNextResult() const; virtual bool hasPreviousResult() const; @@ -54,7 +54,7 @@ public: virtual Tomahawk::result_ptr previousResult() const; virtual qint64 siblingIndex( int itemsAway, qint64 rootIndex = -1 ) const = 0; - virtual Tomahawk::result_ptr siblingResult( int itemsAway ) const; + virtual Tomahawk::result_ptr siblingResult( int itemsAway, qint64 rootIndex = -1 ) const; virtual Tomahawk::result_ptr resultAt( qint64 index ) const = 0; virtual Tomahawk::query_ptr queryAt( qint64 index ) const = 0; @@ -100,11 +100,17 @@ signals: void previousTrackAvailable(); void nextTrackAvailable(); +protected slots: + virtual void onItemsChanged(); + 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; private: Q_DISABLE_COPY( PlaylistInterface )