Skip to content

Commit

Permalink
Load XSPF title as playlist name
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaski committed Jan 4, 2025
1 parent e2a928f commit bf98633
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/collection/collectionwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ void CollectionWatcher::UpdateCueAssociatedSongs(const QString &file,
qLog(Error) << "Could not open CUE file" << matching_cue << "for reading:" << cue_file.errorString();
return;
}
const SongList songs = cue_parser_->Load(&cue_file, matching_cue, path, false);
const SongList songs = cue_parser_->Load(&cue_file, matching_cue, path, false).songs;
cue_file.close();

// Update every song that's in the CUE and collection
Expand Down Expand Up @@ -955,7 +955,7 @@ SongList CollectionWatcher::ScanNewFile(const QString &file, const QString &path
// Also, watch out for incorrect media files.
// Playlist parser for CUEs considers every entry in sheet valid, and we don't want invalid media getting into collection!
QString file_nfd = file.normalized(QString::NormalizationForm_D);
SongList cue_songs = cue_parser_->Load(&cue_file, matching_cue, path, false);
SongList cue_songs = cue_parser_->Load(&cue_file, matching_cue, path, false).songs;
cue_file.close();
songs.reserve(cue_songs.count());
for (Song &cue_song : cue_songs) {
Expand Down
10 changes: 7 additions & 3 deletions src/core/songloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ SongLoader::Result SongLoader::LoadLocalAsync(const QString &filename) {
// It's a CUE - create virtual tracks
QFile cue(matching_cue);
if (cue.open(QIODevice::ReadOnly)) {
const SongList songs = cue_parser_->Load(&cue, matching_cue, QDir(filename.section(u'/', 0, -2)));
const SongList songs = cue_parser_->Load(&cue, matching_cue, QDir(filename.section(u'/', 0, -2))).songs;
cue.close();
for (const Song &song : songs) {
if (song.is_valid()) songs_ << song;
Expand Down Expand Up @@ -348,7 +348,9 @@ void SongLoader::LoadPlaylist(ParserBase *parser, const QString &filename) {

QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
songs_ = parser->Load(&file, filename, QFileInfo(filename).path());
const ParserBase::LoadResult result = parser->Load(&file, filename, QFileInfo(filename).path());
songs_ = result.songs;
playlist_name_ = result.playlist_name;
file.close();
}
else {
Expand Down Expand Up @@ -424,7 +426,9 @@ void SongLoader::StopTypefind() {
// Parse the playlist
QBuffer buf(&buffer_);
if (buf.open(QIODevice::ReadOnly)) {
songs_ = parser_->Load(&buf);
const ParserBase::LoadResult result = parser_->Load(&buf);
songs_ = result.songs;
playlist_name_ = result.playlist_name;
buf.close();
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/songloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class SongLoader : public QObject {

const QUrl &url() const { return url_; }
const SongList &songs() const { return songs_; }
const QString &playlist_name() const { return playlist_name_; }

int timeout() const { return timeout_; }
void set_timeout(int msec) { timeout_ = msec; }
Expand Down Expand Up @@ -141,6 +142,7 @@ class SongLoader : public QObject {

QUrl url_;
SongList songs_;
QString playlist_name_;

const SharedPtr<UrlHandlers> url_handlers_;
const SharedPtr<CollectionBackendInterface> collection_backend_;
Expand Down
7 changes: 6 additions & 1 deletion src/playlist/playlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,11 @@ void Playlist::InsertSongs(const SongList &songs, const int pos, const bool play
InsertSongItems<SongPlaylistItem>(songs, pos, play_now, enqueue, enqueue_next);
}

void Playlist::InsertSongsOrCollectionItems(const SongList &songs, const int pos, const bool play_now, const bool enqueue, const bool enqueue_next) {
void Playlist::InsertSongsOrCollectionItems(const SongList &songs, const QString &playlist_name, const int pos, const bool play_now, const bool enqueue, const bool enqueue_next) {

if (!playlist_name.isEmpty()) {
Q_EMIT Rename(id_, playlist_name);
}

PlaylistItemPtrList items;
for (const Song &song : songs) {
Expand All @@ -1201,6 +1205,7 @@ void Playlist::InsertSongsOrCollectionItems(const SongList &songs, const int pos
}
}
}

InsertItems(items, pos, play_now, enqueue, enqueue_next);

}
Expand Down
4 changes: 3 additions & 1 deletion src/playlist/playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Playlist : public QAbstractListModel {
void InsertItems(const PlaylistItemPtrList &itemsIn, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertCollectionItems(const SongList &songs, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertSongs(const SongList &songs, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertSongsOrCollectionItems(const SongList &songs, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertSongsOrCollectionItems(const SongList &songs, const QString &playlist_name = QString(), const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertSmartPlaylist(PlaylistGeneratorPtr gen, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertStreamingItems(StreamingServicePtr service, const SongList &songs, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
void InsertRadioItems(const SongList &songs, const int pos = -1, const bool play_now = false, const bool enqueue = false, const bool enqueue_next = false);
Expand Down Expand Up @@ -327,6 +327,8 @@ class Playlist : public QAbstractListModel {
// Signals that the queue has changed, meaning that the remaining queued items should update their position.
void QueueChanged();

void Rename(const int id, const QString &name);

private:
void SetCurrentIsPaused(const bool paused);
int NextVirtualIndex(int i, const bool ignore_repeat_track) const;
Expand Down
10 changes: 5 additions & 5 deletions src/playlist/playlistbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,24 +298,24 @@ PlaylistItemPtr PlaylistBackend::RestoreCueData(PlaylistItemPtr item, SharedPtr<
return item;
}

SongList song_list;
SongList songs;
{
QMutexLocker locker(&state->mutex_);

if (!state->cached_cues_.contains(cue_path)) {
QFile cue_file(cue_path);
if (!cue_file.open(QIODevice::ReadOnly)) return item;

song_list = cue_parser.Load(&cue_file, cue_path, QDir(cue_path.section(u'/', 0, -2)));
songs = cue_parser.Load(&cue_file, cue_path, QDir(cue_path.section(u'/', 0, -2))).songs;
cue_file.close();
state->cached_cues_[cue_path] = song_list;
state->cached_cues_[cue_path] = songs;
}
else {
song_list = state->cached_cues_[cue_path];
songs = state->cached_cues_[cue_path];
}
}

for (const Song &from_list : std::as_const(song_list)) {
for (const Song &from_list : std::as_const(songs)) {
if (from_list.url().toEncoded() == song.url().toEncoded() && from_list.beginning_nanosec() == song.beginning_nanosec()) {
// We found a matching section; replace the input item with a new one containing CUE metadata
return make_shared<SongPlaylistItem>(from_list);
Expand Down
3 changes: 2 additions & 1 deletion src/playlist/playlistmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Playlist *PlaylistManager::AddPlaylist(const int id, const QString &name, const
QObject::connect(ret, &Playlist::EditingFinished, this, &PlaylistManager::EditingFinished);
QObject::connect(ret, &Playlist::Error, this, &PlaylistManager::Error);
QObject::connect(ret, &Playlist::PlayRequested, this, &PlaylistManager::PlayRequested);
QObject::connect(ret, &Playlist::Rename, this, &PlaylistManager::Rename);
QObject::connect(playlist_container_->view(), &PlaylistView::ColumnAlignmentChanged, ret, &Playlist::SetColumnAlignment);
QObject::connect(&*current_albumcover_loader_, &CurrentAlbumCoverLoader::AlbumCoverLoaded, ret, &Playlist::AlbumCoverLoaded);

Expand Down Expand Up @@ -208,7 +209,7 @@ void PlaylistManager::Load(const QString &filename) {

QFileInfo fileinfo(filename);

int id = playlist_backend_->CreatePlaylist(fileinfo.completeBaseName(), QString());
const int id = playlist_backend_->CreatePlaylist(fileinfo.completeBaseName(), QString());

if (id == -1) {
Q_EMIT Error(tr("Couldn't create playlist"));
Expand Down
22 changes: 12 additions & 10 deletions src/playlist/songloaderinserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ SongLoaderInserter::SongLoaderInserter(const SharedPtr<TaskManager> task_manager

SongLoaderInserter::~SongLoaderInserter() { qDeleteAll(pending_); }

void SongLoaderInserter::Load(Playlist *destination, int row, bool play_now, bool enqueue, bool enqueue_next, const QList<QUrl> &urls) {
void SongLoaderInserter::Load(Playlist *destination, const int row, const bool play_now, const bool enqueue, const bool enqueue_next, const QList<QUrl> &urls) {

destination_ = destination;
row_ = row;
Expand All @@ -69,15 +69,16 @@ void SongLoaderInserter::Load(Playlist *destination, int row, bool play_now, boo
for (const QUrl &url : urls) {
SongLoader *loader = new SongLoader(url_handlers_, collection_backend_, tagreader_client_, this);

SongLoader::Result ret = loader->Load(url);
const SongLoader::Result result = loader->Load(url);

if (ret == SongLoader::Result::BlockingLoadRequired) {
if (result == SongLoader::Result::BlockingLoadRequired) {
pending_.append(loader);
continue;
}

if (ret == SongLoader::Result::Success) {
if (result == SongLoader::Result::Success) {
songs_ << loader->songs();
playlist_name_ = loader->playlist_name();
}
else {
const QStringList errors = loader->errors();
Expand All @@ -101,7 +102,7 @@ void SongLoaderInserter::Load(Playlist *destination, int row, bool play_now, boo
// First, we add tracks (without metadata) into the playlist
// In the meantime, MusicBrainz will be queried to get songs' metadata.
// AudioCDTagsLoaded will be called next, and playlist's items will be updated.
void SongLoaderInserter::LoadAudioCD(Playlist *destination, int row, bool play_now, bool enqueue, bool enqueue_next) {
void SongLoaderInserter::LoadAudioCD(Playlist *destination, const int row, const bool play_now, const bool enqueue, const bool enqueue_next) {

destination_ = destination;
row_ = row;
Expand All @@ -113,8 +114,8 @@ void SongLoaderInserter::LoadAudioCD(Playlist *destination, int row, bool play_n
QObject::connect(loader, &SongLoader::AudioCDTracksLoadFinished, this, [this, loader]() { AudioCDTracksLoadFinished(loader); });
QObject::connect(loader, &SongLoader::LoadAudioCDFinished, this, &SongLoaderInserter::AudioCDTagsLoaded);
qLog(Info) << "Loading audio CD...";
SongLoader::Result ret = loader->LoadAudioCD();
if (ret == SongLoader::Result::Error) {
const SongLoader::Result result = loader->LoadAudioCD();
if (result == SongLoader::Result::Error) {
if (loader->errors().isEmpty())
Q_EMIT Error(tr("Error while loading audio CD."));
else {
Expand Down Expand Up @@ -166,7 +167,7 @@ void SongLoaderInserter::InsertSongs() {

// Insert songs (that haven't been completely loaded) to allow user to see and play them while not loaded completely
if (destination_) {
destination_->InsertSongsOrCollectionItems(songs_, row_, play_now_, enqueue_, enqueue_next_);
destination_->InsertSongsOrCollectionItems(songs_, playlist_name_, row_, play_now_, enqueue_, enqueue_next_);
}

}
Expand All @@ -180,10 +181,10 @@ void SongLoaderInserter::AsyncLoad() {
bool first_loaded = false;
for (int i = 0; i < pending_.count(); ++i) {
SongLoader *loader = pending_.value(i);
SongLoader::Result res = loader->LoadFilenamesBlocking();
const SongLoader::Result result = loader->LoadFilenamesBlocking();
task_manager_->SetTaskProgress(async_load_id, ++async_progress);

if (res == SongLoader::Result::Error) {
if (result == SongLoader::Result::Error) {
const QStringList errors = loader->errors();
for (const QString &error : errors) {
Q_EMIT Error(error);
Expand All @@ -199,6 +200,7 @@ void SongLoaderInserter::AsyncLoad() {
}

songs_ << loader->songs();
playlist_name_ = loader->playlist_name();

}
task_manager_->SetTaskFinished(async_load_id);
Expand Down
5 changes: 3 additions & 2 deletions src/playlist/songloaderinserter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SongLoaderInserter : public QObject {

~SongLoaderInserter() override;

void Load(Playlist *destination, int row, bool play_now, bool enqueue, bool enqueue_next, const QList<QUrl> &urls);
void LoadAudioCD(Playlist *destination, int row, bool play_now, bool enqueue, bool enqueue_next);
void Load(Playlist *destination, const int row, const bool play_now, const bool enqueue, const bool enqueue_next, const QList<QUrl> &urls);
void LoadAudioCD(Playlist *destination, const int row, const bool play_now, const bool enqueue, const bool enqueue_next);

Q_SIGNALS:
void Error(const QString &message);
Expand Down Expand Up @@ -82,6 +82,7 @@ class SongLoaderInserter : public QObject {
bool enqueue_next_;

SongList songs_;
QString playlist_name_;

QList<SongLoader*> pending_;
};
Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/asxiniparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool AsxIniParser::TryMagic(const QByteArray &data) const {
return data.toLower().contains("[reference]");
}

SongList AsxIniParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {
ParserBase::LoadResult AsxIniParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {

Q_UNUSED(playlist_path);

Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/asxiniparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AsxIniParser : public ParserBase {

bool TryMagic(const QByteArray &data) const override;

SongList Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
LoadResult Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
void Save(const QString &playlist_name, const SongList &songs, QIODevice *device, const QDir &dir = QDir(), const PlaylistSettings::PathType path_type = PlaylistSettings::PathType::Automatic) const override;
};

Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/asxparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CollectionBackendInterface;
ASXParser::ASXParser(const SharedPtr<TagReaderClient> tagreader_client, const SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent)
: XMLParser(tagreader_client, collection_backend, parent) {}

SongList ASXParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {
ParserBase::LoadResult ASXParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {

Q_UNUSED(playlist_path);

Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/asxparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ASXParser : public XMLParser {

bool TryMagic(const QByteArray &data) const override;

SongList Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
LoadResult Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
void Save(const QString &playlist_name, const SongList &songs, QIODevice *device, const QDir &dir = QDir(), const PlaylistSettings::PathType path_type = PlaylistSettings::PathType::Automatic) const override;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/cueparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ constexpr char kDisc[] = "discnumber";
CueParser::CueParser(const SharedPtr<TagReaderClient> tagreader_client, const SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent)
: ParserBase(tagreader_client, collection_backend, parent) {}

SongList CueParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {
ParserBase::LoadResult CueParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {

SongList ret;

Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/cueparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CueParser : public ParserBase {

bool TryMagic(const QByteArray &data) const override;

SongList Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
LoadResult Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
void Save(const QString &playlist_name, const SongList &songs, QIODevice *device, const QDir &dir = QDir(), const PlaylistSettings::PathType path_type = PlaylistSettings::PathType::Automatic) const override;

static QString FindCueFilename(const QString &filename);
Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/m3uparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CollectionBackendInterface;
M3UParser::M3UParser(const SharedPtr<TagReaderClient> tagreader_client, const SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent)
: ParserBase(tagreader_client, collection_backend, parent) {}

SongList M3UParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {
ParserBase::LoadResult M3UParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {

Q_UNUSED(playlist_path);

Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/m3uparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class M3UParser : public ParserBase {

bool TryMagic(const QByteArray &data) const override;

SongList Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
LoadResult Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const override;
void Save(const QString &playlist_name, const SongList &songs, QIODevice *device, const QDir &dir = QDir(), const PlaylistSettings::PathType path_type = PlaylistSettings::PathType::Automatic) const override;

private:
Expand Down
9 changes: 8 additions & 1 deletion src/playlistparsers/parserbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class ParserBase : public QObject {
public:
explicit ParserBase(const SharedPtr<TagReaderClient> tagreader_client, const SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent = nullptr);

class LoadResult {
public:
LoadResult(const SongList &_songs = SongList(), const QString &_playlist_name = QString()) : songs(_songs), playlist_name(_playlist_name) {}
SongList songs;
QString playlist_name;
};

virtual QString name() const = 0;
virtual QStringList file_extensions() const = 0;
virtual bool load_supported() const = 0;
Expand All @@ -59,7 +66,7 @@ class ParserBase : public QObject {
// This method might not return all the songs found in the playlist.
// Any playlist parser may decide to leave out some entries if it finds them incomplete or invalid.
// This means that the final resulting SongList should be considered valid (at least from the parser's point of view).
virtual SongList Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const = 0;
virtual LoadResult Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const = 0;
virtual void Save(const QString &playlist_name, const SongList &songs, QIODevice *device, const QDir &dir = QDir(), const PlaylistSettings::PathType path_type = PlaylistSettings::PathType::Automatic) const = 0;

Q_SIGNALS:
Expand Down
4 changes: 2 additions & 2 deletions src/playlistparsers/playlistparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ SongList PlaylistParser::LoadFromFile(const QString &filename) const {
return SongList();
}

const SongList songs = parser->Load(&file, filename, fileinfo.absolutePath(), true);
const SongList songs = parser->Load(&file, filename, fileinfo.absolutePath(), true).songs;
file.close();

return songs;
Expand All @@ -210,7 +210,7 @@ SongList PlaylistParser::LoadFromDevice(QIODevice *device, const QString &path_h
return SongList();
}

return parser->Load(device, path_hint, dir_hint);
return parser->Load(device, path_hint, dir_hint).songs;

}

Expand Down
2 changes: 1 addition & 1 deletion src/playlistparsers/plsparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CollectionBackendInterface;
PLSParser::PLSParser(const SharedPtr<TagReaderClient> tagreader_client, const SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent)
: ParserBase(tagreader_client, collection_backend, parent) {}

SongList PLSParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {
ParserBase::LoadResult PLSParser::Load(QIODevice *device, const QString &playlist_path, const QDir &dir, const bool collection_lookup) const {

Q_UNUSED(playlist_path);

Expand Down
Loading

0 comments on commit bf98633

Please sign in to comment.