strawberry-audio-player-win.../src/playlist/playlistmanager.h

268 lines
10 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef PLAYLISTMANAGER_H
#define PLAYLISTMANAGER_H
#include "config.h"
2020-02-08 15:03:11 +01:00
#include <QtGlobal>
2018-02-27 18:06:05 +01:00
#include <QObject>
2019-03-22 23:23:50 +01:00
#include <QItemSelectionModel>
#include <QFuture>
#include <QList>
#include <QMap>
#include <QString>
#include <QUrl>
2018-02-27 18:06:05 +01:00
#include "core/shared_ptr.h"
2018-02-27 18:06:05 +01:00
#include "core/song.h"
#include "settings/playlistsettingspage.h"
2018-02-27 18:06:05 +01:00
#include "playlist.h"
#include "smartplaylists/playlistgenerator.h"
2018-02-27 18:06:05 +01:00
2020-02-08 15:03:11 +01:00
class QModelIndex;
2018-02-27 18:06:05 +01:00
class Application;
class CollectionBackend;
class PlaylistBackend;
class PlaylistContainer;
class PlaylistParser;
class PlaylistSequence;
class PlaylistManagerInterface : public QObject {
Q_OBJECT
2020-04-07 16:49:15 +02:00
public:
explicit PlaylistManagerInterface(Application *app, QObject *parent) : QObject(parent) { Q_UNUSED(app); }
2018-02-27 18:06:05 +01:00
virtual int current_id() const = 0;
virtual int active_id() const = 0;
2020-09-10 22:05:12 +02:00
virtual Playlist *playlist(const int id) const = 0;
2018-02-27 18:06:05 +01:00
virtual Playlist *current() const = 0;
virtual Playlist *active() const = 0;
// Returns the collection of playlists managed by this PlaylistManager.
virtual QList<Playlist*> GetAllPlaylists() const = 0;
// Grays out and reloads all deleted songs in all playlists.
virtual void InvalidateDeletedSongs() = 0;
// Removes all deleted songs from all playlists.
virtual void RemoveDeletedSongs() = 0;
2020-08-23 19:17:50 +02:00
virtual QItemSelection selection(const int id) const = 0;
2018-02-27 18:06:05 +01:00
virtual QItemSelection current_selection() const = 0;
virtual QItemSelection active_selection() const = 0;
2020-08-23 19:17:50 +02:00
virtual QString GetPlaylistName(const int index) const = 0;
2018-02-27 18:06:05 +01:00
virtual SharedPtr<CollectionBackend> collection_backend() const = 0;
virtual SharedPtr<PlaylistBackend> playlist_backend() const = 0;
2018-02-27 18:06:05 +01:00
virtual PlaylistSequence *sequence() const = 0;
virtual PlaylistParser *parser() const = 0;
virtual PlaylistContainer *playlist_container() const = 0;
virtual void PlaySmartPlaylist(PlaylistGeneratorPtr generator, const bool as_new, const bool clear) = 0;
2020-04-07 16:49:15 +02:00
public slots:
2021-06-12 20:53:23 +02:00
virtual void New(const QString &name, const SongList &songs = SongList(), const QString &special_type = QString()) = 0;
virtual void Load(const QString &filename) = 0;
virtual void Save(const int id, const QString &filename, const PlaylistSettingsPage::PathType path_type) = 0;
2020-08-23 19:17:50 +02:00
virtual void Rename(const int id, const QString &new_name) = 0;
virtual void Delete(const int id) = 0;
virtual bool Close(const int id) = 0;
virtual void Open(const int id) = 0;
2021-06-12 20:53:23 +02:00
virtual void ChangePlaylistOrder(const QList<int> &ids) = 0;
2018-02-27 18:06:05 +01:00
2020-08-23 19:17:50 +02:00
virtual void SongChangeRequestProcessed(const QUrl &url, const bool valid) = 0;
2018-02-27 18:06:05 +01:00
2020-08-23 19:17:50 +02:00
virtual void SetCurrentPlaylist(const int id) = 0;
virtual void SetActivePlaylist(const int id) = 0;
2018-02-27 18:06:05 +01:00
virtual void SetActiveToCurrent() = 0;
virtual void SelectionChanged(const QItemSelection &selection) = 0;
2018-02-27 18:06:05 +01:00
// Convenience slots that defer to either current() or active()
virtual void ClearCurrent() = 0;
virtual void ShuffleCurrent() = 0;
virtual void RemoveDuplicatesCurrent() = 0;
virtual void RemoveUnavailableCurrent() = 0;
virtual void SetActivePlaying() = 0;
virtual void SetActivePaused() = 0;
virtual void SetActiveStopped() = 0;
// Rate current song using 0.0 - 1.0 scale.
2021-10-30 18:53:14 +02:00
virtual void RateCurrentSong(const float rating) = 0;
// Rate current song using 0 - 5 scale.
2021-10-30 18:53:14 +02:00
virtual void RateCurrentSong2(const int rating) = 0;
2019-03-22 23:23:50 +01:00
signals:
2018-02-27 18:06:05 +01:00
void PlaylistManagerInitialized();
2019-05-02 11:31:31 +02:00
void AllPlaylistsLoaded();
2018-02-27 18:06:05 +01:00
void PlaylistAdded(const int id, const QString &name, const bool favorite);
void PlaylistDeleted(const int id);
void PlaylistClosed(const int id);
void PlaylistRenamed(const int id, const QString &new_name);
void PlaylistFavorited(const int id, const bool favorite);
void CurrentChanged(Playlist *new_playlist, const int scroll_position = 0);
2018-02-27 18:06:05 +01:00
void ActiveChanged(Playlist *new_playlist);
void Error(const QString &message);
void SummaryTextChanged(const QString &summary);
2018-02-27 18:06:05 +01:00
// Forwarded from individual playlists
void CurrentSongChanged(const Song &song);
void SongMetadataChanged(const Song &song);
2018-02-27 18:06:05 +01:00
// Signals that one of manager's playlists has changed (new items, new ordering etc.) - the argument shows which.
2018-02-27 18:06:05 +01:00
void PlaylistChanged(Playlist *playlist);
void EditingFinished(const int playlist_id, const QModelIndex idx);
void PlayRequested(const QModelIndex idx, const Playlist::AutoScroll autoscroll);
2018-02-27 18:06:05 +01:00
};
class PlaylistManager : public PlaylistManagerInterface {
Q_OBJECT
public:
2020-04-07 16:49:15 +02:00
explicit PlaylistManager(Application *app, QObject *parent = nullptr);
2020-06-15 21:55:05 +02:00
~PlaylistManager() override;
2018-02-27 18:06:05 +01:00
2020-06-15 21:55:05 +02:00
int current_id() const override { return current_; }
int active_id() const override { return active_; }
2018-02-27 18:06:05 +01:00
2020-08-23 19:17:50 +02:00
Playlist *playlist(const int id) const override { return playlists_[id].p; }
2020-06-15 21:55:05 +02:00
Playlist *current() const override { return playlist(current_id()); }
Playlist *active() const override { return playlist(active_id()); }
2018-02-27 18:06:05 +01:00
// Returns the collection of playlists managed by this PlaylistManager.
2020-06-15 21:55:05 +02:00
QList<Playlist*> GetAllPlaylists() const override;
2018-02-27 18:06:05 +01:00
// Grays out and reloads all deleted songs in all playlists.
2020-06-15 21:55:05 +02:00
void InvalidateDeletedSongs() override;
2018-02-27 18:06:05 +01:00
// Removes all deleted songs from all playlists.
2020-06-15 21:55:05 +02:00
void RemoveDeletedSongs() override;
2018-02-27 18:06:05 +01:00
// Returns true if the playlist is open
2020-08-23 19:17:50 +02:00
bool IsPlaylistOpen(const int id);
2018-02-27 18:06:05 +01:00
// Returns a pretty automatic name for playlist created from the given list of songs.
2021-01-26 16:48:04 +01:00
static QString GetNameForNewPlaylist(const SongList &songs);
2018-02-27 18:06:05 +01:00
2020-08-23 19:17:50 +02:00
QItemSelection selection(const int id) const override;
2020-06-15 21:55:05 +02:00
QItemSelection current_selection() const override { return selection(current_id()); }
QItemSelection active_selection() const override { return selection(active_id()); }
2018-02-27 18:06:05 +01:00
2020-09-10 22:05:12 +02:00
QString GetPlaylistName(const int index) const override { return playlists_[index].name; }
bool IsPlaylistFavorite(const int index) const { return playlists_[index].p->is_favorite(); }
2018-02-27 18:06:05 +01:00
void Init(SharedPtr<CollectionBackend> collection_backend, SharedPtr<PlaylistBackend> playlist_backend, PlaylistSequence *sequence, PlaylistContainer *playlist_container);
2018-02-27 18:06:05 +01:00
SharedPtr<CollectionBackend> collection_backend() const override { return collection_backend_; }
SharedPtr<PlaylistBackend> playlist_backend() const override { return playlist_backend_; }
2020-06-15 21:55:05 +02:00
PlaylistSequence *sequence() const override { return sequence_; }
PlaylistParser *parser() const override { return parser_; }
PlaylistContainer *playlist_container() const override { return playlist_container_; }
2018-02-27 18:06:05 +01:00
2019-03-22 23:23:50 +01:00
public slots:
2020-06-15 21:55:05 +02:00
void New(const QString &name, const SongList &songs = SongList(), const QString &special_type = QString()) override;
void Load(const QString &filename) override;
void Save(const int id, const QString &filename, const PlaylistSettingsPage::PathType path_type) override;
2018-02-27 18:06:05 +01:00
// Display a file dialog to let user choose a file before saving the file
2020-08-23 19:17:50 +02:00
void SaveWithUI(const int id, const QString &playlist_name);
void Rename(const int id, const QString &new_name) override;
void Favorite(const int id, bool favorite);
void Delete(const int id) override;
bool Close(const int id) override;
void Open(const int id) override;
2021-01-26 16:48:04 +01:00
void ChangePlaylistOrder(const QList<int> &ids) override;
2018-02-27 18:06:05 +01:00
2020-08-23 19:17:50 +02:00
void SetCurrentPlaylist(const int id) override;
void SetActivePlaylist(const int id) override;
2020-06-15 21:55:05 +02:00
void SetActiveToCurrent() override;
2018-02-27 18:06:05 +01:00
2020-06-15 21:55:05 +02:00
void SelectionChanged(const QItemSelection &selection) override;
2018-02-27 18:06:05 +01:00
// Makes a playlist current if it's open already, or opens it and makes it current if it is hidden.
2020-08-23 19:17:50 +02:00
void SetCurrentOrOpen(const int id);
2018-02-27 18:06:05 +01:00
// Convenience slots that defer to either current() or active()
2020-06-15 21:55:05 +02:00
void ClearCurrent() override;
void ShuffleCurrent() override;
void RemoveDuplicatesCurrent() override;
void RemoveUnavailableCurrent() override;
2018-02-27 18:06:05 +01:00
2021-06-12 20:53:23 +02:00
void SongChangeRequestProcessed(const QUrl &url, const bool valid) override;
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
void InsertUrls(const int id, const QList<QUrl> &urls, const int pos = -1, const bool play_now = false, const bool enqueue = false);
void InsertSongs(const int id, const SongList &songs, const int pos = -1, const bool play_now = false, const bool enqueue = false);
2018-02-27 18:06:05 +01:00
// Removes items with given indices from the playlist. This operation is not undoable.
2021-06-12 20:53:23 +02:00
void RemoveItemsWithoutUndo(const int id, const QList<int> &indices);
2018-02-27 18:06:05 +01:00
// Remove the current playing song
2021-06-22 13:45:29 +02:00
void RemoveCurrentSong() const;
2018-02-27 18:06:05 +01:00
void PlaySmartPlaylist(PlaylistGeneratorPtr generator, const bool as_new, const bool clear) override;
// Rate current song using 0.0 - 1.0 scale.
2021-10-30 18:53:14 +02:00
void RateCurrentSong(const float rating) override;
// Rate current song using 0 - 5 scale.
2021-10-30 18:53:14 +02:00
void RateCurrentSong2(const int rating) override;
2022-07-20 01:09:00 +02:00
void SaveAllPlaylists();
2018-02-27 18:06:05 +01:00
private slots:
2020-06-15 21:55:05 +02:00
void SetActivePlaying() override;
void SetActivePaused() override;
void SetActiveStopped() override;
2018-02-27 18:06:05 +01:00
void OneOfPlaylistsChanged();
void UpdateSummaryText();
2021-06-12 20:53:23 +02:00
void SongsDiscovered(const SongList &songs);
void ItemsLoadedForSavePlaylist(const SongList &songs, const QString &filename, const PlaylistSettingsPage::PathType path_type);
2019-05-02 11:31:31 +02:00
void PlaylistLoaded();
2018-02-27 18:06:05 +01:00
private:
2020-08-23 19:17:50 +02:00
Playlist *AddPlaylist(const int id, const QString &name, const QString &special_type, const QString &ui_path, const bool favorite);
2018-02-27 18:06:05 +01:00
2019-03-22 23:23:50 +01:00
private:
2018-02-27 18:06:05 +01:00
struct Data {
2020-08-23 19:17:50 +02:00
explicit Data(Playlist *_p = nullptr, const QString &_name = QString()) : p(_p), name(_name), scroll_position(0) {}
2018-02-27 18:06:05 +01:00
Playlist *p;
QString name;
QItemSelection selection;
int scroll_position;
2018-02-27 18:06:05 +01:00
};
Application *app_;
SharedPtr<PlaylistBackend> playlist_backend_;
SharedPtr<CollectionBackend> collection_backend_;
2018-02-27 18:06:05 +01:00
PlaylistSequence *sequence_;
PlaylistParser *parser_;
PlaylistContainer *playlist_container_;
// key = id
QMap<int, Data> playlists_;
int current_;
int active_;
2019-05-02 11:31:31 +02:00
int playlists_loading_;
2018-02-27 18:06:05 +01:00
};
#endif // PLAYLISTMANAGER_H