Commit 1c772ef6 authored by Max Kellermann's avatar Max Kellermann

Playlist: use the Error library to return errors

parent 809b89b5
...@@ -55,10 +55,10 @@ struct Partition final : private PlayerListener, private MixerListener { ...@@ -55,10 +55,10 @@ struct Partition final : private PlayerListener, private MixerListener {
playlist.Clear(pc); playlist.Clear(pc);
} }
PlaylistResult AppendURI(const SongLoader &loader, unsigned AppendURI(const SongLoader &loader,
const char *uri_utf8, const char *uri_utf8,
unsigned *added_id=nullptr) { Error &error) {
return playlist.AppendURI(pc, loader, uri_utf8, added_id); return playlist.AppendURI(pc, loader, uri_utf8, error);
} }
PlaylistResult DeletePosition(unsigned position) { PlaylistResult DeletePosition(unsigned position) {
......
...@@ -114,13 +114,16 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc, ...@@ -114,13 +114,16 @@ playlist_load_spl(struct playlist &playlist, PlayerControl &pc,
end_index = contents.size(); end_index = contents.size();
const SongLoader loader(nullptr, nullptr); const SongLoader loader(nullptr, nullptr);
Error error2;
for (unsigned i = start_index; i < end_index; ++i) { for (unsigned i = start_index; i < end_index; ++i) {
const auto &uri_utf8 = contents[i]; const auto &uri_utf8 = contents[i];
if ((playlist.AppendURI(pc, loader, uri_utf8.c_str())) != PlaylistResult::SUCCESS) unsigned id = playlist.AppendURI(pc, loader, uri_utf8.c_str(),
FormatError(playlist_domain, error2);
"can't add file \"%s\"", uri_utf8.c_str()); if (id == 0)
FormatError(error2, "can't add file \"%s\"",
uri_utf8.c_str());
} }
return true; return true;
......
...@@ -66,16 +66,14 @@ handle_load(Client &client, int argc, char *argv[]) ...@@ -66,16 +66,14 @@ handle_load(Client &client, int argc, char *argv[])
} else if (!check_range(client, &start_index, &end_index, argv[2])) } else if (!check_range(client, &start_index, &end_index, argv[2]))
return CommandResult::ERROR; return CommandResult::ERROR;
Error error;
const SongLoader loader(client); const SongLoader loader(client);
const PlaylistResult result = if (!playlist_open_into_queue(argv[1],
playlist_open_into_queue(argv[1],
start_index, end_index, start_index, end_index,
client.playlist, client.playlist,
client.player_control, loader); client.player_control, loader, error))
if (result != PlaylistResult::NO_SUCH_LIST) return print_error(client, error);
return print_playlist_result(client, result);
Error error;
if (playlist_load_spl(client.playlist, client.player_control, if (playlist_load_spl(client.playlist, client.player_control,
argv[1], start_index, end_index, argv[1], start_index, end_index,
error)) error))
......
...@@ -64,8 +64,12 @@ handle_add(Client &client, gcc_unused int argc, char *argv[]) ...@@ -64,8 +64,12 @@ handle_add(Client &client, gcc_unused int argc, char *argv[])
if (uri_has_scheme(uri) || PathTraitsUTF8::IsAbsolute(uri)) { if (uri_has_scheme(uri) || PathTraitsUTF8::IsAbsolute(uri)) {
const SongLoader loader(client); const SongLoader loader(client);
auto result = client.partition.AppendURI(loader, uri); Error error;
return print_playlist_result(client, result); unsigned id = client.partition.AppendURI(loader, uri, error);
if (id == 0)
return print_error(client, error);
return CommandResult::OK;
} }
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
...@@ -88,18 +92,16 @@ handle_addid(Client &client, int argc, char *argv[]) ...@@ -88,18 +92,16 @@ handle_addid(Client &client, int argc, char *argv[])
return CommandResult::ERROR; return CommandResult::ERROR;
const SongLoader loader(client); const SongLoader loader(client);
Error error;
unsigned added_id; unsigned added_id = client.partition.AppendURI(loader, uri, error);
auto result = client.partition.AppendURI(loader, uri, &added_id); if (added_id == 0)
return print_error(client, error);
if (result != PlaylistResult::SUCCESS)
return print_playlist_result(client, result);
if (argc == 3) { if (argc == 3) {
unsigned to; unsigned to;
if (!check_unsigned(client, &to, argv[2])) if (!check_unsigned(client, &to, argv[2]))
return CommandResult::ERROR; return CommandResult::ERROR;
result = client.partition.MoveId(added_id, to); PlaylistResult result = client.partition.MoveId(added_id, to);
if (result != PlaylistResult::SUCCESS) { if (result != PlaylistResult::SUCCESS) {
CommandResult ret = CommandResult ret =
print_playlist_result(client, result); print_playlist_result(client, result);
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include "Interface.hxx" #include "Interface.hxx"
#include "Partition.hxx" #include "Partition.hxx"
#include "Instance.hxx" #include "Instance.hxx"
#include "util/Error.hxx"
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include <functional> #include <functional>
...@@ -32,17 +31,12 @@ static bool ...@@ -32,17 +31,12 @@ static bool
AddToQueue(Partition &partition, const LightSong &song, Error &error) AddToQueue(Partition &partition, const LightSong &song, Error &error)
{ {
const Storage &storage = *partition.instance.storage; const Storage &storage = *partition.instance.storage;
PlaylistResult result = unsigned id =
partition.playlist.AppendSong(partition.pc, partition.playlist.AppendSong(partition.pc,
DatabaseDetachSong(storage, DatabaseDetachSong(storage,
song), song),
nullptr); error);
if (result != PlaylistResult::SUCCESS) { return id != 0;
error.Set(playlist_domain, int(result), "Playlist error");
return false;
}
return true;
} }
bool bool
......
...@@ -27,16 +27,18 @@ ...@@ -27,16 +27,18 @@
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "thread/Cond.hxx" #include "thread/Cond.hxx"
#include "fs/Traits.hxx" #include "fs/Traits.hxx"
#include "util/Error.hxx"
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
#include "SongLoader.hxx" #include "SongLoader.hxx"
#endif #endif
PlaylistResult bool
playlist_load_into_queue(const char *uri, SongEnumerator &e, playlist_load_into_queue(const char *uri, SongEnumerator &e,
unsigned start_index, unsigned end_index, unsigned start_index, unsigned end_index,
playlist &dest, PlayerControl &pc, playlist &dest, PlayerControl &pc,
const SongLoader &loader) const SongLoader &loader,
Error &error)
{ {
const std::string base_uri = uri != nullptr const std::string base_uri = uri != nullptr
? PathTraitsUTF8::GetParent(uri) ? PathTraitsUTF8::GetParent(uri)
...@@ -58,20 +60,21 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e, ...@@ -58,20 +60,21 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e,
continue; continue;
} }
PlaylistResult result = dest.AppendSong(pc, std::move(*song)); unsigned id = dest.AppendSong(pc, std::move(*song), error);
delete song; delete song;
if (result != PlaylistResult::SUCCESS) if (id == 0)
return result; return false;
} }
return PlaylistResult::SUCCESS; return true;
} }
PlaylistResult bool
playlist_open_into_queue(const char *uri, playlist_open_into_queue(const char *uri,
unsigned start_index, unsigned end_index, unsigned start_index, unsigned end_index,
playlist &dest, PlayerControl &pc, playlist &dest, PlayerControl &pc,
const SongLoader &loader) const SongLoader &loader,
Error &error)
{ {
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
...@@ -81,13 +84,16 @@ playlist_open_into_queue(const char *uri, ...@@ -81,13 +84,16 @@ playlist_open_into_queue(const char *uri,
loader.GetStorage(), loader.GetStorage(),
#endif #endif
mutex, cond); mutex, cond);
if (playlist == nullptr) if (playlist == nullptr) {
return PlaylistResult::NO_SUCH_LIST; error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_LIST),
"No such playlist");
return false;
}
PlaylistResult result = bool result =
playlist_load_into_queue(uri, *playlist, playlist_load_into_queue(uri, *playlist,
start_index, end_index, start_index, end_index,
dest, pc, loader); dest, pc, loader, error);
delete playlist; delete playlist;
return result; return result;
} }
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "PlaylistError.hxx" #include "PlaylistError.hxx"
class Error;
class SongLoader; class SongLoader;
class SongEnumerator; class SongEnumerator;
struct playlist; struct playlist;
...@@ -40,21 +41,22 @@ struct PlayerControl; ...@@ -40,21 +41,22 @@ struct PlayerControl;
* @param start_index the index of the first song * @param start_index the index of the first song
* @param end_index the index of the last song (excluding) * @param end_index the index of the last song (excluding)
*/ */
PlaylistResult bool
playlist_load_into_queue(const char *uri, SongEnumerator &e, playlist_load_into_queue(const char *uri, SongEnumerator &e,
unsigned start_index, unsigned end_index, unsigned start_index, unsigned end_index,
playlist &dest, PlayerControl &pc, playlist &dest, PlayerControl &pc,
const SongLoader &loader); const SongLoader &loader,
Error &error);
/** /**
* Opens a playlist with a playlist plugin and append to the specified * Opens a playlist with a playlist plugin and append to the specified
* play queue. * play queue.
*/ */
PlaylistResult bool
playlist_open_into_queue(const char *uri, playlist_open_into_queue(const char *uri,
unsigned start_index, unsigned end_index, unsigned start_index, unsigned end_index,
playlist &dest, PlayerControl &pc, playlist &dest, PlayerControl &pc,
const SongLoader &loader); const SongLoader &loader, Error &error);
#endif #endif
...@@ -146,14 +146,20 @@ public: ...@@ -146,14 +146,20 @@ public:
void DatabaseModified(const Database &db); void DatabaseModified(const Database &db);
#endif #endif
PlaylistResult AppendSong(PlayerControl &pc, /**
* @return the new song id or 0 on error
*/
unsigned AppendSong(PlayerControl &pc,
DetachedSong &&song, DetachedSong &&song,
unsigned *added_id=nullptr); Error &error);
PlaylistResult AppendURI(PlayerControl &pc, /**
* @return the new song id or 0 on error
*/
unsigned AppendURI(PlayerControl &pc,
const SongLoader &loader, const SongLoader &loader,
const char *uri_utf8, const char *uri_utf8,
unsigned *added_id=nullptr); Error &error);
protected: protected:
void DeleteInternal(PlayerControl &pc, void DeleteInternal(PlayerControl &pc,
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
#include "DetachedSong.hxx" #include "DetachedSong.hxx"
#include "SongLoader.hxx" #include "SongLoader.hxx"
#include "Idle.hxx" #include "Idle.hxx"
#include "Log.hxx"
#include <stdlib.h> #include <stdlib.h>
...@@ -55,14 +54,16 @@ playlist::Clear(PlayerControl &pc) ...@@ -55,14 +54,16 @@ playlist::Clear(PlayerControl &pc)
OnModified(); OnModified();
} }
PlaylistResult unsigned
playlist::AppendSong(PlayerControl &pc, playlist::AppendSong(PlayerControl &pc, DetachedSong &&song, Error &error)
DetachedSong &&song, unsigned *added_id)
{ {
unsigned id; unsigned id;
if (queue.IsFull()) if (queue.IsFull()) {
return PlaylistResult::TOO_LARGE; error.Set(playlist_domain, int(PlaylistResult::TOO_LARGE),
"Playlist is too large");
return 0;
}
const DetachedSong *const queued_song = GetQueuedSong(); const DetachedSong *const queued_song = GetQueuedSong();
...@@ -84,30 +85,19 @@ playlist::AppendSong(PlayerControl &pc, ...@@ -84,30 +85,19 @@ playlist::AppendSong(PlayerControl &pc,
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
OnModified(); OnModified();
if (added_id) return id;
*added_id = id;
return PlaylistResult::SUCCESS;
} }
PlaylistResult unsigned
playlist::AppendURI(PlayerControl &pc, playlist::AppendURI(PlayerControl &pc, const SongLoader &loader,
const SongLoader &loader, const char *uri,
const char *uri, unsigned *added_id) Error &error)
{ {
FormatDebug(playlist_domain, "add to playlist: %s", uri);
Error error;
DetachedSong *song = loader.LoadSong(uri, error); DetachedSong *song = loader.LoadSong(uri, error);
if (song == nullptr) { if (song == nullptr)
// TODO: return the Error return 0;
LogError(error);
return error.IsDomain(playlist_domain)
? PlaylistResult(error.GetCode())
: PlaylistResult::NO_SUCH_SONG;
}
PlaylistResult result = AppendSong(pc, std::move(*song), added_id); unsigned result = AppendSong(pc, std::move(*song), error);
delete song; delete song;
return result; return result;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment