File indexing completed on 2024-05-05 04:48:29

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Caleb Jones <danielcjones@gmail.com>                       *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
0007  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0008  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0009  * version 3 of the license.                                                            *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef AMAROK_BIASEDPLAYLIST_H
0020 #define AMAROK_BIASEDPLAYLIST_H
0021 
0022 #include "Bias.h"
0023 #include "DynamicPlaylist.h"
0024 #include "core/meta/forward_declarations.h"
0025 
0026 #include "amarok_export.h" // we are exporting it for the tests
0027 
0028 #include <ThreadWeaver/Queue>
0029 #include <ThreadWeaver/Job>
0030 
0031 #include <QObject>
0032 #include <QPointer>
0033 
0034 class QXmlStreamWriter;
0035 class QXmlStreamReader;
0036 
0037 namespace Dynamic
0038 {
0039     class BiasSolver;
0040 
0041     /** A concrete implementation of the DynamicPlaylist that uses a bias structure to determine new tracks.
0042     */
0043     class AMAROK_EXPORT BiasedPlaylist : public DynamicPlaylist
0044     {
0045         Q_OBJECT
0046 
0047         public:
0048             /** Creates a new random playlist */
0049             explicit BiasedPlaylist( QObject *parent = nullptr );
0050 
0051             /** Creates a new playlist from an xml stream */
0052             explicit BiasedPlaylist( QXmlStreamReader *reader, QObject *parent = nullptr );
0053 
0054             ~BiasedPlaylist() override;
0055 
0056             void toXml( QXmlStreamWriter *writer ) const override;
0057 
0058             void requestTracks(int) override;
0059 
0060             BiasPtr bias() const;
0061 
0062         public Q_SLOTS:
0063             void requestAbort() override;
0064 
0065         private Q_SLOTS:
0066             void solverFinished();
0067             void biasChanged();
0068             void biasReplaced( const Dynamic::BiasPtr &oldBias, const Dynamic::BiasPtr &newBias );
0069 
0070         private:
0071             /** Starts the BiasSolver (if not already running) and requests a couple of new tracks. */
0072             void startSolver( int numRequested );
0073 
0074             /** Returns all the tracks that will come before the newly generated ones. */
0075             Meta::TrackList getContext();
0076 
0077             /** The bias this playlist uses */
0078             BiasPtr m_bias;
0079 
0080             /** A currently running BiasSolver */
0081             BiasSolver* m_solver;
0082 
0083             static const int BUFFER_SIZE;
0084     };
0085 }
0086 
0087 // Q_DECLARE_METATYPE( Dynamic::BiasedPlaylistPtr )
0088 
0089 #endif