File indexing completed on 2025-01-05 04:26:11

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "MultiTrack.h"
0018 
0019 #include "core/meta/Statistics.h"
0020 #include "core-impl/capabilities/multisource/MultiSourceCapabilityImpl.h"
0021 
0022 using namespace Meta;
0023 
0024 MultiTrack::MultiTrack( Playlists::PlaylistPtr playlist )
0025     : QObject()
0026     , Track()
0027     , m_playlist( playlist )
0028 {
0029     Q_ASSERT( playlist );
0030     if( playlist->trackCount() < 0 )
0031     {
0032         PlaylistObserver::subscribeTo( playlist );
0033         playlist->triggerTrackLoad();
0034     }
0035     if( !playlist->tracks().isEmpty() )
0036         setSource( 0 );
0037 }
0038 
0039 MultiTrack::~MultiTrack()
0040 {
0041 }
0042 
0043 QStringList
0044 Meta::MultiTrack::sources() const
0045 {
0046     QStringList trackNames;
0047     foreach ( TrackPtr track, m_playlist->tracks() )
0048     {
0049         trackNames << track->prettyUrl();
0050     }
0051 
0052     return trackNames;
0053 }
0054 
0055 void
0056 MultiTrack::setSource( int source )
0057 {
0058     QWriteLocker locker( &m_lock );
0059     setSourceImpl( source );
0060     locker.unlock();
0061 
0062     notifyObservers();
0063     Q_EMIT urlChanged( playableUrl() );
0064 }
0065 
0066 int
0067 Meta::MultiTrack::current() const
0068 {
0069     QReadLocker locker( &m_lock );
0070     return m_playlist->tracks().indexOf( m_currentTrack );
0071 }
0072 
0073 QUrl
0074 MultiTrack::nextUrl() const
0075 {
0076     int index = current() + 1;
0077     Meta::TrackPtr track = m_playlist->tracks().value( index );
0078     if( track )
0079     {
0080         track->prepareToPlay();
0081         return track->playableUrl();
0082     }
0083     return QUrl();
0084 }
0085 
0086 bool
0087 MultiTrack::hasCapabilityInterface(Capabilities::Capability::Type type) const
0088 {
0089     return type == Capabilities::Capability::MultiSource;
0090 }
0091 
0092 Capabilities::Capability *
0093 MultiTrack::createCapabilityInterface(Capabilities::Capability::Type type)
0094 {
0095     switch( type )
0096     {
0097         case Capabilities::Capability::MultiSource:
0098             return new Capabilities::MultiSourceCapabilityImpl( this );
0099         default:
0100             return nullptr;
0101     }
0102 }
0103 
0104 void
0105 MultiTrack::prepareToPlay()
0106 {
0107     QReadLocker locker( &m_lock );
0108     if( m_currentTrack )
0109         m_currentTrack->prepareToPlay();
0110 }
0111 
0112 Meta::StatisticsPtr
0113 Meta::MultiTrack::statistics()
0114 {
0115     QReadLocker locker( &m_lock );
0116     return m_currentTrack ? m_currentTrack->statistics() : Track::statistics();
0117 }
0118 
0119 void
0120 Meta::MultiTrack::metadataChanged(const TrackPtr &track )
0121 {
0122     Q_UNUSED( track )
0123     // forward changes from active tracks
0124     notifyObservers();
0125 }
0126 
0127 void
0128 MultiTrack::trackAdded(const Playlists::PlaylistPtr &, const TrackPtr &, int )
0129 {
0130     PlaylistObserver::unsubscribeFrom( m_playlist );
0131 
0132     QWriteLocker locker( &m_lock );
0133     if( !m_currentTrack )
0134     {
0135         setSourceImpl( 0 );
0136         locker.unlock();
0137 
0138         notifyObservers();
0139         Q_EMIT urlChanged( playableUrl() );
0140     }
0141 }
0142 
0143 void
0144 MultiTrack::setSourceImpl( int source )
0145 {
0146     if( source < 0 || source >= m_playlist->tracks().count() )
0147         return;
0148 
0149     if( m_currentTrack )
0150         Observer::unsubscribeFrom( m_currentTrack );
0151 
0152     m_currentTrack = m_playlist->tracks().at( source );
0153     Observer::subscribeTo( m_currentTrack );
0154 }