Debian Codegraph @

src/playlist/navigators/RandomTrackNavigator.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2008 Seb Ruiz <[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) 2013 Daniel Schmitz <[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 #define DEBUG_PREFIX "Playlist::RandomTrackNavigator"
25
26 #include "RandomTrackNavigator.h"
27
28 #include "core/meta/Meta.h"
29 #include "core/support/Debug.h"
30
31 #include <QDateTime>
32 #include <QRandomGenerator> // For 'QRandomGenerator::global()->generate()'
33
34 #include <cmath> // For 'round()'
35
36 Playlist::RandomTrackNavigator::RandomTrackNavigator()
37 {
38 loadFromSourceModel();
39 }
40
41 void
42 Playlist::RandomTrackNavigator::planOne()
43 {
44 DEBUG_BLOCK
45
46 if ( m_plannedItems.isEmpty() )
47 {
48 if ( !allItemsList().isEmpty() )
49 {
50 quint64 chosenItem;
51
52 int avoidRecentlyPlayedSize = AVOID_RECENTLY_PLAYED_MAX; // Start with being very picky.
53
54 // Don't over-constrain ourself:
55 // - Keep enough headroom to be unpredictable.
56 // - Make sure that 'chooseRandomItem()' doesn't need to find a needle in a haystack.
57 avoidRecentlyPlayedSize = qMin( avoidRecentlyPlayedSize, allItemsList().size() / 2 );
58
59 QSet<quint64> avoidSet = getRecentHistory( avoidRecentlyPlayedSize );
60 QSet<quint64> brokenSet;
61 do
62 {
63 chosenItem = chooseRandomItem( avoidSet );
64 brokenSet.insert( chosenItem );
65 if( avoidSet.size() > 0 )
66 avoidSet.erase( avoidSet.begin() ); // One by one, accept recently played tracks
67 } while ( !m_model->trackForId( chosenItem )->isPlayable() &&
68 ( avoidSet.size() + brokenSet.size() ) < allItemsList().size() );
69 if( m_model->trackForId( chosenItem )->isPlayable() ) // Don't feed broken tracks
70 m_plannedItems.append( chosenItem );
71 }
72 }
73 }
74
75 QSet<quint64>
76 Playlist::RandomTrackNavigator::getRecentHistory( int size )
77 {
78 QList<quint64> allHistory = historyItems();
79 QSet<quint64> recentHistory;
80
81 if ( size > 0 ) { // If '== 0', we even need to consider playing the same item again.
82 recentHistory.insert( currentItem() ); // Might be '0'
83 size--;
84 }
85
86 for ( int i = allHistory.size() - 1; ( i >= 0 ) && ( i >= allHistory.size() - size ); i-- )
87 recentHistory.insert( allHistory.at( i ) );
88
89 return recentHistory;
90 }
91
92 quint64
93 Playlist::RandomTrackNavigator::chooseRandomItem( const QSet<quint64> &avoidSet )
94 {
95 quint64 chosenItem;
96
97 do
98 {
99 int randomPosition = QRandomGenerator::global()->generate() % allItemsList().size();
100 chosenItem = allItemsList().at( randomPosition );
101 } while ( avoidSet.contains( chosenItem ) );
102
103 return chosenItem;
104 }