File indexing completed on 2024-05-19 04:49:50

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Jones <danielcjones@gmail.com>                             *
0003  * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org>                                        *
0004  * Copyright (c) 2010 Ralf Engels <ralf-engels@gmx.de>                                  *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0009  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0010  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0011  * version 3 of the license.                                                            *
0012  *                                                                                      *
0013  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0016  *                                                                                      *
0017  * You should have received a copy of the GNU General Public License along with         *
0018  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0019  ****************************************************************************************/
0020 
0021 #include "DynamicTrackNavigator.h"
0022 
0023 #include "amarokconfig.h"
0024 #include "core/meta/Meta.h"
0025 #include "core/support/Debug.h"
0026 #include "dynamic/DynamicPlaylist.h"
0027 #include "dynamic/DynamicModel.h"
0028 #include "playlist/PlaylistModel.h"
0029 #include "playlist/PlaylistModelStack.h"
0030 #include "playlist/PlaylistController.h"
0031 
0032 Playlist::DynamicTrackNavigator::DynamicTrackNavigator()
0033     : m_playlist( nullptr )
0034 {
0035     connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::activeTrackChanged,
0036              this, &DynamicTrackNavigator::trackChanged );
0037     connect( m_model->qaim(), &QAbstractItemModel::modelReset,
0038              this, &DynamicTrackNavigator::repopulate );
0039 
0040     connect( Dynamic::DynamicModel::instance(), &Dynamic::DynamicModel::activeChanged,
0041              this, &DynamicTrackNavigator::activePlaylistChanged );
0042     activePlaylistChanged();
0043 }
0044 
0045 Playlist::DynamicTrackNavigator::~DynamicTrackNavigator()
0046 {
0047     if( !m_playlist )
0048         m_playlist->requestAbort();
0049 }
0050 
0051 void
0052 Playlist::DynamicTrackNavigator::receiveTracks( const Meta::TrackList &tracks )
0053 {
0054     The::playlistController()->insertOptioned( tracks );
0055 }
0056 
0057 void
0058 Playlist::DynamicTrackNavigator::appendUpcoming()
0059 {
0060     // a little bit stupid. the playlist jumps to the newly inserted tracks
0061 
0062     int updateRow = m_model->activeRow() + 1;
0063     int rowCount = m_model->qaim()->rowCount();
0064     int upcomingCountLag = AmarokConfig::upcomingTracks() - ( rowCount - updateRow );
0065 
0066     if( upcomingCountLag > 0 && m_playlist )
0067         m_playlist->requestTracks( upcomingCountLag );
0068 }
0069 
0070 void
0071 Playlist::DynamicTrackNavigator::removePlayed()
0072 {
0073     int activeRow = m_model->activeRow();
0074     if( activeRow > AmarokConfig::previousTracks() )
0075         The::playlistController()->removeRows( 0, activeRow - AmarokConfig::previousTracks() );
0076 }
0077 
0078 void
0079 Playlist::DynamicTrackNavigator::activePlaylistChanged()
0080 {
0081     DEBUG_BLOCK
0082 
0083     Dynamic::DynamicPlaylist *newPlaylist =
0084         Dynamic::DynamicModel::instance()->activePlaylist();
0085 
0086     if( newPlaylist == m_playlist )
0087         return;
0088 
0089     if( m_playlist )
0090     {
0091         disconnect( m_playlist, &Dynamic::DynamicPlaylist::tracksReady,
0092                     this, &DynamicTrackNavigator::receiveTracks );
0093         m_playlist->requestAbort();
0094     }
0095 
0096     m_playlist = newPlaylist;
0097     if( !m_playlist )
0098     {
0099         warning() << "No dynamic playlist current loaded! Creating dynamic track navigator with null playlist!";
0100     }
0101     else
0102     {
0103         connect( m_playlist, &Dynamic::DynamicPlaylist::tracksReady,
0104                  this, &DynamicTrackNavigator::receiveTracks );
0105     }
0106 }
0107 
0108 void
0109 Playlist::DynamicTrackNavigator::trackChanged()
0110 {
0111     appendUpcoming();
0112     removePlayed();
0113 }
0114 
0115 void
0116 Playlist::DynamicTrackNavigator::repopulate()
0117 {
0118     // remove all future tracks
0119     int activeRow = m_model->activeRow();
0120     int rowCount = m_model->qaim()->rowCount();
0121     if( activeRow < rowCount )
0122         The::playlistController()->removeRows( activeRow + 1, rowCount - activeRow - 1);
0123 
0124     appendUpcoming();
0125 }