1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  * Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
6  *
7  * Strawberry is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Strawberry is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef PLAYLISTBACKEND_H
23 #define PLAYLISTBACKEND_H
24 
25 #include "config.h"
26 
27 #include <memory>
28 
29 #include <QObject>
30 #include <QMutex>
31 #include <QHash>
32 #include <QList>
33 #include <QSet>
34 #include <QString>
35 #include <QSqlQuery>
36 
37 #include "core/song.h"
38 #include "core/sqlquery.h"
39 #include "collection/sqlrow.h"
40 #include "playlistitem.h"
41 #include "smartplaylists/playlistgenerator.h"
42 
43 class QThread;
44 class Application;
45 class Database;
46 
47 class PlaylistBackend : public QObject {
48   Q_OBJECT
49 
50  public:
51   Q_INVOKABLE explicit PlaylistBackend(Application *app, QObject *parent = nullptr);
52 
53   struct Playlist {
PlaylistPlaylist54     Playlist() : id(-1), favorite(false), last_played(0) {}
55 
56     int id;
57     QString name;
58     QString ui_path;
59     bool favorite;
60     int last_played;
61     QString special_type;
62     PlaylistGenerator::Type dynamic_type;
63     QString dynamic_backend;
64     QByteArray dynamic_data;
65   };
66   typedef QList<Playlist> PlaylistList;
67 
68   static const int kSongTableJoins;
69 
70   void Close();
71   void ExitAsync();
72 
73   PlaylistList GetAllPlaylists();
74   PlaylistList GetAllOpenPlaylists();
75   PlaylistList GetAllFavoritePlaylists();
76   PlaylistBackend::Playlist GetPlaylist(const int id);
77 
78   QList<PlaylistItemPtr> GetPlaylistItems(const int playlist);
79   SongList GetPlaylistSongs(const int playlist);
80 
81   void SetPlaylistOrder(const QList<int> &ids);
82   void SetPlaylistUiPath(const int id, const QString &path);
83 
84   int CreatePlaylist(const QString &name, const QString &special_type);
85   void SavePlaylistAsync(const int playlist, const PlaylistItemList &items, const int last_played, PlaylistGeneratorPtr dynamic);
86   void RenamePlaylist(const int id, const QString &new_name);
87   void FavoritePlaylist(const int id, bool is_favorite);
88   void RemovePlaylist(const int id);
89 
app()90   Application *app() const { return app_; }
91 
92  public slots:
93   void Exit();
94   void SavePlaylist(const int playlist, const PlaylistItemList &items, const int last_played, PlaylistGeneratorPtr dynamic);
95 
96  signals:
97   void ExitFinished();
98 
99  private:
100   struct NewSongFromQueryState {
101     QHash<QString, SongList> cached_cues_;
102     QMutex mutex_;
103   };
104 
105   Song NewSongFromQuery(const SqlRow &row, std::shared_ptr<NewSongFromQueryState> state);
106   PlaylistItemPtr NewPlaylistItemFromQuery(const SqlRow &row, std::shared_ptr<NewSongFromQueryState> state);
107   PlaylistItemPtr RestoreCueData(PlaylistItemPtr item, std::shared_ptr<NewSongFromQueryState> state);
108 
109   enum GetPlaylistsFlags {
110     GetPlaylists_OpenInUi = 1,
111     GetPlaylists_Favorite = 2,
112     GetPlaylists_All = GetPlaylists_OpenInUi | GetPlaylists_Favorite
113   };
114   PlaylistList GetPlaylists(const GetPlaylistsFlags flags);
115 
116   Application *app_;
117   Database *db_;
118   QThread *original_thread_;
119 };
120 
121 #endif  // PLAYLISTBACKEND_H
122