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

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  * Copyright (c) 2008 Soren Harward <stharward@gmail.com>                               *
0004  * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org>                                        *
0005  * Copyright (c) 2010 Nanno Langstraat <langstr@gmail.com>                              *
0006  *                                                                                      *
0007  * This program is free software; you can redistribute it and/or modify it under        *
0008  * the terms of the GNU General Public License as published by the Free Software        *
0009  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0010  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0011  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0012  * version 3 of the license.                                                            *
0013  *                                                                                      *
0014  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0015  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0016  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0017  *                                                                                      *
0018  * You should have received a copy of the GNU General Public License along with         *
0019  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0020  ****************************************************************************************/
0021 
0022 #define DEBUG_PREFIX "Playlist::RandomAlbumNavigator"
0023 
0024 // WORKAROUND for QTBUG-25960. Required for Qt versions < 4.8.5 in combination with libc++.
0025 #define QT_NO_STL 1
0026     #include <qiterator.h>
0027 #undef QT_NO_STL
0028 
0029 #include "RandomAlbumNavigator.h"
0030 
0031 #include "core/support/Debug.h"
0032 #include "core/meta/Meta.h"
0033 
0034 #include <algorithm> // For std::random_shuffle
0035 
0036 
0037 Playlist::RandomAlbumNavigator::RandomAlbumNavigator()
0038 {
0039     loadFromSourceModel();
0040 }
0041 
0042 
0043 void
0044 Playlist::RandomAlbumNavigator::planOne()
0045 {
0046     DEBUG_BLOCK
0047 
0048     // Try to find next item in same album
0049     if ( m_plannedItems.isEmpty() )
0050     {
0051         ItemList itemsInAlbum = m_itemsPerAlbum.value( currentAlbum() );    // May be default-constructed empty list.
0052 
0053         int currentRow = itemsInAlbum.indexOf( currentItem() );    // -1 if currentItem() == 0.
0054         if ( currentRow != -1 )
0055         {
0056             int nextRow = currentRow + 1;
0057             if ( nextRow < itemsInAlbum.size() )
0058                 m_plannedItems.append( itemsInAlbum.at( nextRow ) );
0059         }
0060     }
0061 
0062     // Try to find first item in next album
0063     if ( m_plannedItems.isEmpty() )
0064     {
0065         if ( m_plannedAlbums.isEmpty() )    // Handle end of planned album list
0066             notifyAlbumsInserted( m_itemsPerAlbum.uniqueKeys() );
0067 
0068         if ( !m_plannedAlbums.isEmpty() )
0069         {
0070             AlbumId newAlbum = m_plannedAlbums.takeFirst();
0071             quint64 newCurrentItem = m_itemsPerAlbum.value( newAlbum ).first();
0072             m_plannedItems.append( newCurrentItem );
0073         }
0074     }
0075 }
0076 
0077 void
0078 Playlist::RandomAlbumNavigator::notifyAlbumsInserted( const QList<AlbumId> &insertedAlbums )
0079 {
0080     DEBUG_BLOCK
0081 
0082     m_plannedAlbums.append( insertedAlbums );
0083     std::random_shuffle( m_plannedAlbums.begin(), m_plannedAlbums.end() );
0084     if ( !m_plannedAlbums.isEmpty() )
0085         if ( m_plannedAlbums.first() == currentAlbum() )
0086             m_plannedAlbums.append( m_plannedAlbums.takeFirst() );    // Try to avoid playing same album twice.
0087 }