From 1c772ef69947127e01e7171b007a2295d51e7ae7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 27 Feb 2014 17:27:23 +0100 Subject: Playlist: use the Error library to return errors --- src/Partition.hxx | 8 ++++---- src/PlaylistSave.cxx | 9 ++++++--- src/command/PlaylistCommands.cxx | 14 ++++++-------- src/command/QueueCommands.cxx | 20 +++++++++++--------- src/db/DatabaseQueue.cxx | 12 +++--------- src/playlist/PlaylistQueue.cxx | 30 ++++++++++++++++++------------ src/playlist/PlaylistQueue.hxx | 10 ++++++---- src/queue/Playlist.hxx | 22 ++++++++++++++-------- src/queue/PlaylistEdit.cxx | 40 +++++++++++++++------------------------- 9 files changed, 83 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/Partition.hxx b/src/Partition.hxx index 7f7def4af..4341a9ed3 100644 --- a/src/Partition.hxx +++ b/src/Partition.hxx @@ -55,10 +55,10 @@ struct Partition final : private PlayerListener, private MixerListener { playlist.Clear(pc); } - PlaylistResult AppendURI(const SongLoader &loader, - const char *uri_utf8, - unsigned *added_id=nullptr) { - return playlist.AppendURI(pc, loader, uri_utf8, added_id); + unsigned AppendURI(const SongLoader &loader, + const char *uri_utf8, + Error &error) { + return playlist.AppendURI(pc, loader, uri_utf8, error); } PlaylistResult DeletePosition(unsigned position) { diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 875fb1eeb..78a8fdb91 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -114,13 +114,16 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc, end_index = contents.size(); const SongLoader loader(nullptr, nullptr); + Error error2; for (unsigned i = start_index; i < end_index; ++i) { const auto &uri_utf8 = contents[i]; - if ((playlist.AppendURI(pc, loader, uri_utf8.c_str())) != PlaylistResult::SUCCESS) - FormatError(playlist_domain, - "can't add file \"%s\"", uri_utf8.c_str()); + unsigned id = playlist.AppendURI(pc, loader, uri_utf8.c_str(), + error2); + if (id == 0) + FormatError(error2, "can't add file \"%s\"", + uri_utf8.c_str()); } return true; diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index 35dc0ceb3..bc426db4e 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -66,16 +66,14 @@ handle_load(Client &client, int argc, char *argv[]) } else if (!check_range(client, &start_index, &end_index, argv[2])) return CommandResult::ERROR; + Error error; const SongLoader loader(client); - const PlaylistResult result = - playlist_open_into_queue(argv[1], - start_index, end_index, - client.playlist, - client.player_control, loader); - if (result != PlaylistResult::NO_SUCH_LIST) - return print_playlist_result(client, result); + if (!playlist_open_into_queue(argv[1], + start_index, end_index, + client.playlist, + client.player_control, loader, error)) + return print_error(client, error); - Error error; if (playlist_load_spl(client.playlist, client.player_control, argv[1], start_index, end_index, error)) diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 105a33ebb..81e5098bb 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -64,8 +64,12 @@ handle_add(Client &client, gcc_unused int argc, char *argv[]) if (uri_has_scheme(uri) || PathTraitsUTF8::IsAbsolute(uri)) { const SongLoader loader(client); - auto result = client.partition.AppendURI(loader, uri); - return print_playlist_result(client, result); + Error error; + unsigned id = client.partition.AppendURI(loader, uri, error); + if (id == 0) + return print_error(client, error); + + return CommandResult::OK; } #ifdef ENABLE_DATABASE @@ -88,18 +92,16 @@ handle_addid(Client &client, int argc, char *argv[]) return CommandResult::ERROR; const SongLoader loader(client); - - unsigned added_id; - auto result = client.partition.AppendURI(loader, uri, &added_id); - - if (result != PlaylistResult::SUCCESS) - return print_playlist_result(client, result); + Error error; + unsigned added_id = client.partition.AppendURI(loader, uri, error); + if (added_id == 0) + return print_error(client, error); if (argc == 3) { unsigned to; if (!check_unsigned(client, &to, argv[2])) return CommandResult::ERROR; - result = client.partition.MoveId(added_id, to); + PlaylistResult result = client.partition.MoveId(added_id, to); if (result != PlaylistResult::SUCCESS) { CommandResult ret = print_playlist_result(client, result); diff --git a/src/db/DatabaseQueue.cxx b/src/db/DatabaseQueue.cxx index be012647a..490678188 100644 --- a/src/db/DatabaseQueue.cxx +++ b/src/db/DatabaseQueue.cxx @@ -23,7 +23,6 @@ #include "Interface.hxx" #include "Partition.hxx" #include "Instance.hxx" -#include "util/Error.hxx" #include "DetachedSong.hxx" #include @@ -32,17 +31,12 @@ static bool AddToQueue(Partition &partition, const LightSong &song, Error &error) { const Storage &storage = *partition.instance.storage; - PlaylistResult result = + unsigned id = partition.playlist.AppendSong(partition.pc, DatabaseDetachSong(storage, song), - nullptr); - if (result != PlaylistResult::SUCCESS) { - error.Set(playlist_domain, int(result), "Playlist error"); - return false; - } - - return true; + error); + return id != 0; } bool diff --git a/src/playlist/PlaylistQueue.cxx b/src/playlist/PlaylistQueue.cxx index 3c7194ad7..b10a26172 100644 --- a/src/playlist/PlaylistQueue.cxx +++ b/src/playlist/PlaylistQueue.cxx @@ -27,16 +27,18 @@ #include "thread/Mutex.hxx" #include "thread/Cond.hxx" #include "fs/Traits.hxx" +#include "util/Error.hxx" #ifdef ENABLE_DATABASE #include "SongLoader.hxx" #endif -PlaylistResult +bool playlist_load_into_queue(const char *uri, SongEnumerator &e, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - const SongLoader &loader) + const SongLoader &loader, + Error &error) { const std::string base_uri = uri != nullptr ? PathTraitsUTF8::GetParent(uri) @@ -58,20 +60,21 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e, continue; } - PlaylistResult result = dest.AppendSong(pc, std::move(*song)); + unsigned id = dest.AppendSong(pc, std::move(*song), error); delete song; - if (result != PlaylistResult::SUCCESS) - return result; + if (id == 0) + return false; } - return PlaylistResult::SUCCESS; + return true; } -PlaylistResult +bool playlist_open_into_queue(const char *uri, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - const SongLoader &loader) + const SongLoader &loader, + Error &error) { Mutex mutex; Cond cond; @@ -81,13 +84,16 @@ playlist_open_into_queue(const char *uri, loader.GetStorage(), #endif mutex, cond); - if (playlist == nullptr) - return PlaylistResult::NO_SUCH_LIST; + if (playlist == nullptr) { + error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_LIST), + "No such playlist"); + return false; + } - PlaylistResult result = + bool result = playlist_load_into_queue(uri, *playlist, start_index, end_index, - dest, pc, loader); + dest, pc, loader, error); delete playlist; return result; } diff --git a/src/playlist/PlaylistQueue.hxx b/src/playlist/PlaylistQueue.hxx index 48e42c70e..28eb86fcc 100644 --- a/src/playlist/PlaylistQueue.hxx +++ b/src/playlist/PlaylistQueue.hxx @@ -26,6 +26,7 @@ #include "PlaylistError.hxx" +class Error; class SongLoader; class SongEnumerator; struct playlist; @@ -40,21 +41,22 @@ struct PlayerControl; * @param start_index the index of the first song * @param end_index the index of the last song (excluding) */ -PlaylistResult +bool playlist_load_into_queue(const char *uri, SongEnumerator &e, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - const SongLoader &loader); + const SongLoader &loader, + Error &error); /** * Opens a playlist with a playlist plugin and append to the specified * play queue. */ -PlaylistResult +bool playlist_open_into_queue(const char *uri, unsigned start_index, unsigned end_index, playlist &dest, PlayerControl &pc, - const SongLoader &loader); + const SongLoader &loader, Error &error); #endif diff --git a/src/queue/Playlist.hxx b/src/queue/Playlist.hxx index 09980155e..4a4c7f30c 100644 --- a/src/queue/Playlist.hxx +++ b/src/queue/Playlist.hxx @@ -146,14 +146,20 @@ public: void DatabaseModified(const Database &db); #endif - PlaylistResult AppendSong(PlayerControl &pc, - DetachedSong &&song, - unsigned *added_id=nullptr); - - PlaylistResult AppendURI(PlayerControl &pc, - const SongLoader &loader, - const char *uri_utf8, - unsigned *added_id=nullptr); + /** + * @return the new song id or 0 on error + */ + unsigned AppendSong(PlayerControl &pc, + DetachedSong &&song, + Error &error); + + /** + * @return the new song id or 0 on error + */ + unsigned AppendURI(PlayerControl &pc, + const SongLoader &loader, + const char *uri_utf8, + Error &error); protected: void DeleteInternal(PlayerControl &pc, diff --git a/src/queue/PlaylistEdit.cxx b/src/queue/PlaylistEdit.cxx index 8d2c76e6e..ef409a260 100644 --- a/src/queue/PlaylistEdit.cxx +++ b/src/queue/PlaylistEdit.cxx @@ -32,7 +32,6 @@ #include "DetachedSong.hxx" #include "SongLoader.hxx" #include "Idle.hxx" -#include "Log.hxx" #include @@ -55,14 +54,16 @@ playlist::Clear(PlayerControl &pc) OnModified(); } -PlaylistResult -playlist::AppendSong(PlayerControl &pc, - DetachedSong &&song, unsigned *added_id) +unsigned +playlist::AppendSong(PlayerControl &pc, DetachedSong &&song, Error &error) { unsigned id; - if (queue.IsFull()) - return PlaylistResult::TOO_LARGE; + if (queue.IsFull()) { + error.Set(playlist_domain, int(PlaylistResult::TOO_LARGE), + "Playlist is too large"); + return 0; + } const DetachedSong *const queued_song = GetQueuedSong(); @@ -84,30 +85,19 @@ playlist::AppendSong(PlayerControl &pc, UpdateQueuedSong(pc, queued_song); OnModified(); - if (added_id) - *added_id = id; - - return PlaylistResult::SUCCESS; + return id; } -PlaylistResult -playlist::AppendURI(PlayerControl &pc, - const SongLoader &loader, - const char *uri, unsigned *added_id) +unsigned +playlist::AppendURI(PlayerControl &pc, const SongLoader &loader, + const char *uri, + Error &error) { - FormatDebug(playlist_domain, "add to playlist: %s", uri); - - Error error; DetachedSong *song = loader.LoadSong(uri, error); - if (song == nullptr) { - // TODO: return the Error - LogError(error); - return error.IsDomain(playlist_domain) - ? PlaylistResult(error.GetCode()) - : PlaylistResult::NO_SUCH_SONG; - } + if (song == nullptr) + return 0; - PlaylistResult result = AppendSong(pc, std::move(*song), added_id); + unsigned result = AppendSong(pc, std::move(*song), error); delete song; return result; -- cgit v1.2.3