Debian Codegraph @

src/playlist/navigators/FavoredRandomTrackNavigator.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2009 Edward Toroshchin <[email protected]> *
3 * Copyright (c) 2010 Nanno Langstraat <[email protected]> *
4 * *
5 * This program is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU General Public License as published by the Free Software *
7 * Foundation; either version 2 of the License, or (at your option) version 3 or *
8 * any later version accepted by the membership of KDE e.V. (or its successor approved *
9 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of *
10 * version 3 of the license. *
11 * *
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License along with *
17 * this program. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************************/
19
20 #include "FavoredRandomTrackNavigator.h"
21
22 #include "playlist/PlaylistModelStack.h"
23
24 #include "amarokconfig.h"
25 #include "core/meta/Meta.h"
26 #include "core/meta/Statistics.h"
27 #include "core/support/Debug.h"
28
29 #include <QRandomGenerator>
30
31
32 Playlist::FavoredRandomTrackNavigator::FavoredRandomTrackNavigator()
33 {
34 loadFromSourceModel();
35 }
36
37 void
38 Playlist::FavoredRandomTrackNavigator::planOne()
39 {
40 DEBUG_BLOCK
41
42 if ( m_plannedItems.isEmpty() && !allItemsList().isEmpty() )
43 {
44 int avoidRecentlyPlayedSize = AVOID_RECENTLY_PLAYED_MAX; // Start with being very picky.
45
46 // Don't over-constrain ourself:
47 // - Keep enough headroom to be unpredictable.
48 // - Make sure that 'chooseRandomItem()' doesn't need to find a needle in a haystack.
49 avoidRecentlyPlayedSize = qMin( avoidRecentlyPlayedSize, allItemsList().size() / 2 );
50
51 QSet<quint64> avoidSet = getRecentHistory( avoidRecentlyPlayedSize );
52 QList<qreal> weights;
53
54 quint64 chosenItem = std::numeric_limits<quint64>::max();
55 do
56 {
57 weights = rowWeights( avoidSet );
58
59 // Choose a weighed random row.
60 if( !weights.isEmpty() )
61 {
62 qreal totalWeight = 0.0;
63 for ( qreal weight : weights )
64 totalWeight += weight;
65
66 qreal randomCumulWeight = ( QRandomGenerator::global()->generate() /
67 (double)std::numeric_limits<quint32>::max() ) * totalWeight;
68
69 int row = 0;
70 qreal rowCumulWeight = weights[ row ];
71 while ( randomCumulWeight > rowCumulWeight + 0.0000000001 )
72 rowCumulWeight += weights[ ++row ];
73
74 chosenItem = m_model->idAt( row );
75 avoidSet.insert( chosenItem );
76 } // Avoid deadlock by verifying we are giving playable tracks to nextItemChooseDonorList()
77 } while ( !weights.isEmpty() && !m_model->trackForId( chosenItem )->isPlayable()
78 && avoidSet.size() < allItemsList().size() );
79 if( chosenItem != std::numeric_limits<quint64>::max() && m_model->trackForId( chosenItem )->isPlayable() )
80 m_plannedItems.append( chosenItem );
81 }
82 }
83
84 QList<qreal>
85 Playlist::FavoredRandomTrackNavigator::rowWeights(const QSet<quint64> &avoidSet )
86 {
87 QList<qreal> weights;
88
89 int favorType = AmarokConfig::favorTracks();
90 int rowCount = m_model->qaim()->rowCount();
91
92 for( int row = 0; row < rowCount; row++ )
93 {
94 qreal weight = 0.0;
95 Meta::StatisticsPtr statistics = m_model->trackAt( row )->statistics();
96
97 if ( !avoidSet.contains( m_model->idAt( row ) ) )
98 {
99 switch( favorType )
100 {
101 case AmarokConfig::EnumFavorTracks::HigherScores:
102 {
103 int score = statistics->score();
104 weight = score ? score : 50.0; // "Unknown" weight: in the middle, 50%
105 break;
106 }
107
108 case AmarokConfig::EnumFavorTracks::HigherRatings:
109 {
110 int rating = statistics->rating();
111 weight = rating ? rating : 5.0;
112 break;
113 }
114
115 case AmarokConfig::EnumFavorTracks::LessRecentlyPlayed:
116 {
117 QDateTime lastPlayed = statistics->lastPlayed();
118 if ( lastPlayed.isValid() )
119 {
120 weight = lastPlayed.secsTo( QDateTime::currentDateTime() );
121 if ( weight < 0 ) // If 'lastPlayed()' is nonsense, or the system clock has been set back:
122 weight = 1 * 60 * 60; // "Nonsense" weight: 1 hour.
123 }
124 else
125 weight = 365 * 24 * 60 * 60; // "Never" weight: 1 year.
126 break;
127 }
128 }
129 }
130
131 weights.append( weight );
132 }
133
134 return weights;
135 }
136
137 QSet<quint64>
138 Playlist::FavoredRandomTrackNavigator::getRecentHistory( int size )
139 {
140 QList<quint64> allHistory = historyItems();
141 QSet<quint64> recentHistory;
142
143 if ( size > 0 ) { // If '== 0', we even need to consider playing the same item again.
144 recentHistory.insert( currentItem() ); // Might be '0'
145 size--;
146 }
147
148 for ( int i = allHistory.size() - 1; ( i >= 0 ) && ( i >= allHistory.size() - size ); i-- )
149 recentHistory.insert( allHistory.at( i ) );
150
151 return recentHistory;
152 }