| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2007 Ian Monroe <[email protected]> * |
| 3 |
* Copyright (c) 2008 Soren Harward <[email protected]> * |
| 4 |
* Copyright (c) 2008 Nikolaj Hald Nielsen <[email protected]> * |
| 5 |
* Copyright (c) 2009 Téo Mrnjavac <[email protected]> * |
| 6 |
* Copyright (c) 2010 Nanno Langstraat <[email protected]> * |
| 7 |
* Copyright (c) 2011 Sandeep Raghuraman <[email protected]> * |
| 8 |
* * |
| 9 |
* This program is free software; you can redistribute it and/or modify it under * |
| 10 |
* the terms of the GNU General Public License as published by the Free Software * |
| 11 |
* Foundation; either version 2 of the License, or (at your option) version 3 or * |
| 12 |
* any later version accepted by the membership of KDE e.V. (or its successor approved * |
| 13 |
* by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of * |
| 14 |
* version 3 of the license. * |
| 15 |
* * |
| 16 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 17 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 18 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 19 |
* * |
| 20 |
* You should have received a copy of the GNU General Public License along with * |
| 21 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 22 |
****************************************************************************************/ |
| 23 |
|
| 24 |
#include "StandardTrackNavigator.h" |
| 25 |
|
| 26 |
#include "amarokconfig.h" |
| 27 |
#include "core/meta/Meta.h" |
| 28 |
#include "core/support/Amarok.h" |
| 29 |
#include "playlist/PlaylistModelStack.h" |
| 30 |
|
| 31 |
Playlist::StandardTrackNavigator::StandardTrackNavigator() |
| 32 |
{ |
| 33 |
m_repeatPlaylist = ( AmarokConfig::trackProgression() == AmarokConfig::EnumTrackProgression::RepeatPlaylist ); |
| 34 |
m_onlyQueue = ( AmarokConfig::trackProgression() == AmarokConfig::EnumTrackProgression::OnlyQueue ); |
| 35 |
} |
| 36 |
|
| 37 |
quint64 |
| 38 |
Playlist::StandardTrackNavigator::chooseLastTrack( bool repeatPlaylist ) |
| 39 |
{ |
| 40 |
Meta::TrackPtr track; |
| 41 |
bool playableTrackFound = false; |
| 42 |
int lastRow; |
| 43 |
|
| 44 |
// reminder: if that function is modified, it's important to research if changes must also |
| 45 |
// be applied to similar code in `Playlist::StandardTrackNavigator::chooseNextTrack()` |
| 46 |
|
| 47 |
// search for a playable track in order from right before the currently active track till the start |
| 48 |
for ( lastRow = m_model->activeRow() - 1 ; lastRow >= 0 ; lastRow-- ) // 'activeRow()' may be -1 |
| 49 |
{ |
| 50 |
track = m_model->trackAt(lastRow); |
| 51 |
if ( track->isPlayable() ) |
| 52 |
{ |
| 53 |
playableTrackFound = true; |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Even though the user is explicitly asking us to go back, still only do wrap-around |
| 59 |
// in mode 'm_repeatPlaylist'. Reason: many users of the standard navigator like to |
| 60 |
// move to the start of the playlist by blindly pressing "Prev" a lot of times. |
| 61 |
// If no playable track was found, the playlist needs to be repeated, it's not empty |
| 62 |
// and there's an active track: search from end of playlist till currently active track. |
| 63 |
if ( !playableTrackFound && repeatPlaylist && m_model->qaim()->rowCount() > 0 && m_model->activeRow() >= 0) |
| 64 |
{ |
| 65 |
for ( lastRow = m_model->qaim()->rowCount() - 1 ; lastRow >= m_model->activeRow() ; lastRow--) |
| 66 |
{ |
| 67 |
track = m_model->trackAt( lastRow ); |
| 68 |
if ( track->isPlayable() ) |
| 69 |
{ |
| 70 |
playableTrackFound = true; |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if ( playableTrackFound && m_model->rowExists( lastRow ) ) |
| 77 |
return m_model->idAt( lastRow ); |
| 78 |
else |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
quint64 |
| 83 |
Playlist::StandardTrackNavigator::requestNextTrack() |
| 84 |
{ |
| 85 |
if( !m_queue.isEmpty() ) { |
| 86 |
quint64 ret = m_queue.takeFirst(); |
| 87 |
Playlist::ModelStack::instance()->bottom()->emitQueueChanged(); |
| 88 |
return ret; |
| 89 |
} |
| 90 |
|
| 91 |
return chooseNextTrack( m_repeatPlaylist ); |
| 92 |
} |
| 93 |
|
| 94 |
quint64 |
| 95 |
Playlist::StandardTrackNavigator::requestUserNextTrack() |
| 96 |
{ |
| 97 |
if( !m_queue.isEmpty() ) { |
| 98 |
quint64 ret = m_queue.takeFirst(); |
| 99 |
Playlist::ModelStack::instance()->bottom()->emitQueueChanged(); |
| 100 |
return ret; |
| 101 |
} |
| 102 |
|
| 103 |
// Don't make wrap-around conditional on 'm_repeatPlaylist': the user is explicitly asking for this. |
| 104 |
return chooseNextTrack( true ); |
| 105 |
} |
| 106 |
|
| 107 |
quint64 |
| 108 |
Playlist::StandardTrackNavigator::requestLastTrack() |
| 109 |
{ |
| 110 |
if( !m_queue.isEmpty() ) { |
| 111 |
quint64 ret = m_queue.takeFirst(); |
| 112 |
Playlist::ModelStack::instance()->bottom()->emitQueueChanged(); |
| 113 |
return ret; |
| 114 |
} |
| 115 |
|
| 116 |
return chooseLastTrack( m_repeatPlaylist ); |
| 117 |
} |
| 118 |
|
| 119 |
quint64 |
| 120 |
Playlist::StandardTrackNavigator::chooseNextTrack( bool repeatPlaylist ) |
| 121 |
{ |
| 122 |
if( !m_queue.isEmpty() ) |
| 123 |
return m_queue.first(); |
| 124 |
|
| 125 |
if( m_onlyQueue ) |
| 126 |
return 0; |
| 127 |
|
| 128 |
Meta::TrackPtr track; |
| 129 |
bool playableTrackFound = false; |
| 130 |
int nextRow; |
| 131 |
|
| 132 |
// reminder: if that function is modified, it's important to research if changes must also |
| 133 |
// be applied to similar code in `Playlist::StandardTrackNavigator::chooseLastTrack()` |
| 134 |
|
| 135 |
//search for a playable track in order from right after the currently active track till the end |
| 136 |
for( nextRow = m_model->activeRow() + 1 ; nextRow < m_model->qaim()->rowCount() ; nextRow++ ) // 'activeRow()' may be -1. |
| 137 |
{ |
| 138 |
track = m_model->trackAt(nextRow); |
| 139 |
if( track->isPlayable() ) |
| 140 |
{ |
| 141 |
playableTrackFound = true; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
//if no playable track was found and the playlist needs to be repeated, search from top of playlist till currently active track |
| 147 |
if( !playableTrackFound && repeatPlaylist ) |
| 148 |
{ |
| 149 |
//nextRow=0; This row is still invalid if 'rowCount() == 0'. |
| 150 |
for( nextRow = 0 ; nextRow <= m_model->activeRow() ; nextRow++) |
| 151 |
{ |
| 152 |
track = m_model->trackAt( nextRow ); |
| 153 |
if( track->isPlayable() ) |
| 154 |
{ |
| 155 |
playableTrackFound = true; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if( playableTrackFound && m_model->rowExists( nextRow ) ) |
| 162 |
return m_model->idAt( nextRow ); |
| 163 |
else |
| 164 |
return 0; |
| 165 |
} |