Debian Codegraph @

src/playlist/navigators/DynamicTrackNavigator.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2008 Daniel Jones <[email protected]> *
3 * Copyright (c) 2009 Téo Mrnjavac <[email protected]> *
4 * Copyright (c) 2010 Ralf Engels <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License as published by the Free Software *
8 * Foundation; either version 2 of the License, or (at your option) version 3 or *
9 * any later version accepted by the membership of KDE e.V. (or its successor approved *
10 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of *
11 * version 3 of the license. *
12 * *
13 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
15 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License along with *
18 * this program. If not, see <http://www.gnu.org/licenses/>. *
19 ****************************************************************************************/
20
21 #include "DynamicTrackNavigator.h"
22
23 #include "amarokconfig.h"
24 #include "core/meta/Meta.h"
25 #include "core/support/Debug.h"
26 #include "dynamic/DynamicPlaylist.h"
27 #include "dynamic/DynamicModel.h"
28 #include "playlist/PlaylistModel.h"
29 #include "playlist/PlaylistModelStack.h"
30 #include "playlist/PlaylistController.h"
31
32 Playlist::DynamicTrackNavigator::DynamicTrackNavigator()
33 : m_playlist( nullptr )
34 {
35 connect( Playlist::ModelStack::instance()->bottom(), &Playlist::Model::activeTrackChanged,
36 this, &DynamicTrackNavigator::trackChanged );
37 connect( m_model->qaim(), &QAbstractItemModel::modelReset,
38 this, &DynamicTrackNavigator::repopulate );
39
40 connect( Dynamic::DynamicModel::instance(), &Dynamic::DynamicModel::activeChanged,
41 this, &DynamicTrackNavigator::activePlaylistChanged );
42 activePlaylistChanged();
43 }
44
45 Playlist::DynamicTrackNavigator::~DynamicTrackNavigator()
46 {
47 if( m_playlist )
48 m_playlist->requestAbort();
49 }
50
51 void
52 Playlist::DynamicTrackNavigator::receiveTracks( const Meta::TrackList &tracks )
53 {
54 The::playlistController()->insertOptioned( tracks );
55 }
56
57 void
58 Playlist::DynamicTrackNavigator::appendUpcoming()
59 {
60 // a little bit stupid. the playlist jumps to the newly inserted tracks
61
62 int updateRow = m_model->activeRow() + 1;
63 int rowCount = m_model->qaim()->rowCount();
64 int upcomingCountLag = AmarokConfig::upcomingTracks() - ( rowCount - updateRow );
65
66 if( upcomingCountLag > 0 && m_playlist )
67 m_playlist->requestTracks( upcomingCountLag );
68 }
69
70 void
71 Playlist::DynamicTrackNavigator::removePlayed()
72 {
73 int activeRow = m_model->activeRow();
74 if( activeRow > AmarokConfig::previousTracks() )
75 The::playlistController()->removeRows( 0, activeRow - AmarokConfig::previousTracks() );
76 }
77
78 void
79 Playlist::DynamicTrackNavigator::activePlaylistChanged()
80 {
81 DEBUG_BLOCK
82
83 Dynamic::DynamicPlaylist *newPlaylist =
84 Dynamic::DynamicModel::instance()->activePlaylist();
85
86 if( newPlaylist == m_playlist )
87 return;
88
89 if( m_playlist )
90 {
91 disconnect( m_playlist, &Dynamic::DynamicPlaylist::tracksReady,
92 this, &DynamicTrackNavigator::receiveTracks );
93 m_playlist->requestAbort();
94 }
95
96 m_playlist = newPlaylist;
97 if( !m_playlist )
98 {
99 warning() << "No dynamic playlist current loaded! Creating dynamic track navigator with null playlist!";
100 }
101 else
102 {
103 connect( m_playlist, &Dynamic::DynamicPlaylist::tracksReady,
104 this, &DynamicTrackNavigator::receiveTracks );
105 }
106 }
107
108 void
109 Playlist::DynamicTrackNavigator::trackChanged()
110 {
111 appendUpcoming();
112 removePlayed();
113 }
114
115 void
116 Playlist::DynamicTrackNavigator::repopulate()
117 {
118 // remove all future tracks
119 int activeRow = m_model->activeRow();
120 int rowCount = m_model->qaim()->rowCount();
121 if( activeRow < rowCount )
122 The::playlistController()->removeRows( activeRow + 1, rowCount - activeRow - 1);
123
124 appendUpcoming();
125 }