1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  *
6  * Strawberry is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Strawberry is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef PLAYLISTGENERATOR_H
22 #define PLAYLISTGENERATOR_H
23 
24 #include "config.h"
25 
26 #include <memory>
27 
28 #include <QObject>
29 #include <QByteArray>
30 #include <QString>
31 
32 #include "playlist/playlistitem.h"
33 
34 class CollectionBackend;
35 
36 class PlaylistGenerator : public QObject, public std::enable_shared_from_this<PlaylistGenerator> {
37   Q_OBJECT
38 
39  public:
40   explicit PlaylistGenerator(QObject *parent = nullptr);
41 
42   static const int kDefaultLimit;
43   static const int kDefaultDynamicHistory;
44   static const int kDefaultDynamicFuture;
45 
46   enum Type {
47     Type_None = 0,
48     Type_Query = 1
49   };
50 
51   // Creates a new PlaylistGenerator of the given type
52   static std::shared_ptr<PlaylistGenerator> Create(const Type type = Type_Query);
53 
54   // Should be called before Load on a new PlaylistGenerator
set_collection(CollectionBackend * backend)55   void set_collection(CollectionBackend *backend) { backend_ = backend; }
set_name(const QString & name)56   void set_name(const QString &name) { name_ = name; }
collection()57   CollectionBackend *collection() const { return backend_; }
name()58   QString name() const { return name_; }
59 
60   // Name of the subclass
61   virtual Type type() const = 0;
62 
63   // Serializes the PlaylistGenerator's settings
64   // Called on UI-thread.
65   virtual void Load(const QByteArray &data) = 0;
66   // Called on UI-thread.
67   virtual QByteArray Save() const = 0;
68 
69   // Creates and returns a playlist
70   // Called from non-UI thread.
71   virtual PlaylistItemList Generate() = 0;
72 
73   // If the generator can be used as a dynamic playlist then GenerateMore should return the next tracks in the sequence.
74   // The subclass should remember the last GetDynamicHistory() + GetDynamicFuture() tracks,
75   // and ensure that the tracks returned from this method are not in that set.
is_dynamic()76   virtual bool is_dynamic() const { return false; }
set_dynamic(const bool dynamic)77   virtual void set_dynamic(const bool dynamic) { Q_UNUSED(dynamic); }
78   // Called from non-UI thread.
GenerateMore(int count)79   virtual PlaylistItemList GenerateMore(int count) {
80     Q_UNUSED(count);
81     return PlaylistItemList();
82   }
83 
GetDynamicHistory()84   virtual int GetDynamicHistory() { return kDefaultDynamicHistory; }
GetDynamicFuture()85   virtual int GetDynamicFuture() { return kDefaultDynamicFuture; }
86 
87  signals:
88   void Error(QString message);
89 
90  protected:
91   CollectionBackend *backend_;
92 
93  private:
94   QString name_;
95 
96 };
97 
98 #include "playlistgenerator_fwd.h"
99 
100 #endif  // PLAYLISTGENERATOR_H
101