1
0
mirror of https://github.com/strawberrymusicplayer/strawberry synced 2025-02-06 04:14:13 +01:00

Add error handling to playlist parsers

This commit is contained in:
Jonas Kvinge 2024-06-24 20:20:49 +02:00
parent 0038cf8c4e
commit 6446942e73
6 changed files with 39 additions and 13 deletions

View File

@ -100,6 +100,8 @@ void PlaylistManager::Init(SharedPtr<CollectionBackend> collection_backend, Shar
QObject::connect(&*collection_backend_, &CollectionBackend::SongsStatisticsChanged, this, &PlaylistManager::UpdateCollectionSongs);
QObject::connect(&*collection_backend_, &CollectionBackend::SongsRatingChanged, this, &PlaylistManager::UpdateCollectionSongs);
QObject::connect(parser_, &PlaylistParser::Error, this, &PlaylistManager::Error);
for (const PlaylistBackend::Playlist &p : playlist_backend->GetAllOpenPlaylists()) {
++playlists_loading_;
Playlist *ret = AddPlaylist(p.id, p.name, p.special_type, p.ui_path, p.favorite);

View File

@ -394,6 +394,8 @@ void CueParser::Save(const SongList &songs, QIODevice *device, const QDir &dir,
Q_UNUSED(dir);
Q_UNUSED(path_type);
emit Error(tr("Saving CUE files is not supported."));
// TODO
}

View File

@ -59,6 +59,7 @@ void ParserBase::LoadSong(const QString &filename_or_url, const qint64 beginning
}
else {
qLog(Error) << "Don't know how to handle" << url;
emit Error(tr("Don't know how to handle %1").arg(filename_or_url));
return;
}
}

View File

@ -61,6 +61,9 @@ class ParserBase : public QObject {
virtual SongList Load(QIODevice *device, const QString &playlist_path = QLatin1String(""), const QDir &dir = QDir(), const bool collection_lookup = true) const = 0;
virtual void Save(const SongList &songs, QIODevice *device, const QDir &dir = QDir(), const PlaylistSettingsPage::PathType path_type = PlaylistSettingsPage::PathType::Automatic) const = 0;
signals:
void Error(const QString &error) const;
protected:
// Loads a song. If filename_or_url is a URL (with a scheme other than "file") then it is set on the song and the song marked as a stream.
// Also sets the song's metadata by searching in the Collection, or loading from the file as a fallback.

View File

@ -44,16 +44,26 @@
const int PlaylistParser::kMagicSize = 512;
PlaylistParser::PlaylistParser(SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent) : QObject(parent) {
PlaylistParser::PlaylistParser(SharedPtr<CollectionBackendInterface> collection_backend, QObject *parent) : QObject(parent), default_parser_(nullptr) {
default_parser_ = new XSPFParser(collection_backend, this);
parsers_ << default_parser_;
parsers_ << new M3UParser(collection_backend, this);
parsers_ << new PLSParser(collection_backend, this);
parsers_ << new ASXParser(collection_backend, this);
parsers_ << new AsxIniParser(collection_backend, this);
parsers_ << new CueParser(collection_backend, this);
parsers_ << new WplParser(collection_backend, this);
AddParser(new XSPFParser(collection_backend, this));
AddParser(new M3UParser(collection_backend, this));
AddParser(new PLSParser(collection_backend, this));
AddParser(new ASXParser(collection_backend, this));
AddParser(new AsxIniParser(collection_backend, this));
AddParser(new CueParser(collection_backend, this));
AddParser(new WplParser(collection_backend, this));
}
void PlaylistParser::AddParser(ParserBase *parser) {
if (!default_parser_) {
default_parser_ = parser;
}
parsers_ << parser;
QObject::connect(parser, &ParserBase::Error, this, &PlaylistParser::Error);
}
@ -171,7 +181,8 @@ SongList PlaylistParser::LoadFromFile(const QString &filename) const {
// Find a parser that supports this file extension
ParserBase *parser = ParserForExtension(Type::Load, fileinfo.suffix());
if (!parser) {
qLog(Warning) << "Unknown filetype:" << filename;
qLog(Error) << "Unknown filetype:" << filename;
emit Error(tr("Unknown filetype: %1").arg(filename));
return SongList();
}
@ -204,14 +215,16 @@ void PlaylistParser::Save(const SongList &songs, const QString &filename, const
QDir dir(fileinfo.path());
if (!dir.exists()) {
qLog(Warning) << "Directory does not exist" << dir.path();
qLog(Error) << "Directory" << dir.path() << "does not exist";
emit Error(tr("Directory %1 does not exist.").arg(dir.path()));
return;
}
// Find a parser that supports this file extension
ParserBase *parser = ParserForExtension(Type::Save, fileinfo.suffix());
if (!parser) {
qLog(Warning) << "Unknown filetype" << filename;
qLog(Error) << "Unknown filetype" << filename;
emit Error(tr("Unknown filetype: %1").arg(filename));
return;
}
@ -225,7 +238,8 @@ void PlaylistParser::Save(const SongList &songs, const QString &filename, const
// Open the file
QFile file(fileinfo.absoluteFilePath());
if (!file.open(QIODevice::WriteOnly)) {
qLog(Warning) << "Failed to open" << filename << "for writing.";
qLog(Error) << "Failed to open" << filename << "for writing.";
emit Error(tr("Failed to open %1 for writing.").arg(filename));
return;
}

View File

@ -67,7 +67,11 @@ class PlaylistParser : public QObject {
SongList LoadFromDevice(QIODevice *device, const QString &path_hint = QString(), const QDir &dir_hint = QDir()) const;
void Save(const SongList &songs, const QString &filename, const PlaylistSettingsPage::PathType) const;
signals:
void Error(const QString &error) const;
private:
void AddParser(ParserBase *parser);
bool ParserIsSupported(const Type type, ParserBase *parser) const;
static QString FilterForParser(const ParserBase *parser, QStringList *all_extensions = nullptr);