client/Response: new Client wrapper class for writing responses
This commit is contained in:
@@ -28,11 +28,11 @@
|
||||
#include "db/Selection.hxx"
|
||||
#include "CommandError.hxx"
|
||||
#include "client/Client.hxx"
|
||||
#include "client/Response.hxx"
|
||||
#include "tag/Tag.hxx"
|
||||
#include "util/ConstBuffer.hxx"
|
||||
#include "util/Error.hxx"
|
||||
#include "SongFilter.hxx"
|
||||
#include "protocol/Result.hxx"
|
||||
#include "BulkEdit.hxx"
|
||||
|
||||
#include <string.h>
|
||||
@@ -40,11 +40,13 @@
|
||||
CommandResult
|
||||
handle_listfiles_db(Client &client, const char *uri)
|
||||
{
|
||||
Response r(client);
|
||||
const DatabaseSelection selection(uri, false);
|
||||
|
||||
Error error;
|
||||
if (!db_selection_print(client, selection, false, true, error))
|
||||
return print_error(client, error);
|
||||
if (!db_selection_print(r, client.partition,
|
||||
selection, false, true, error))
|
||||
return print_error(r, error);
|
||||
|
||||
return CommandResult::OK;
|
||||
}
|
||||
@@ -52,14 +54,17 @@ handle_listfiles_db(Client &client, const char *uri)
|
||||
CommandResult
|
||||
handle_lsinfo2(Client &client, Request args)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
/* default is root directory */
|
||||
const auto uri = args.GetOptional(0, "");
|
||||
|
||||
const DatabaseSelection selection(uri, false);
|
||||
|
||||
Error error;
|
||||
if (!db_selection_print(client, selection, true, false, error))
|
||||
return print_error(client, error);
|
||||
if (!db_selection_print(r, client.partition,
|
||||
selection, true, false, error))
|
||||
return print_error(r, error);
|
||||
|
||||
return CommandResult::OK;
|
||||
}
|
||||
@@ -67,9 +72,11 @@ handle_lsinfo2(Client &client, Request args)
|
||||
static CommandResult
|
||||
handle_match(Client &client, Request args, bool fold_case)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
RangeArg window;
|
||||
if (args.size >= 2 && strcmp(args[args.size - 2], "window") == 0) {
|
||||
if (!args.Parse(args.size - 1, window, client))
|
||||
if (!args.Parse(args.size - 1, window, r))
|
||||
return CommandResult::ERROR;
|
||||
|
||||
args.pop_back();
|
||||
@@ -79,17 +86,18 @@ handle_match(Client &client, Request args, bool fold_case)
|
||||
|
||||
SongFilter filter;
|
||||
if (!filter.Parse(args, fold_case)) {
|
||||
command_error(client, ACK_ERROR_ARG, "incorrect arguments");
|
||||
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
const DatabaseSelection selection("", true, &filter);
|
||||
|
||||
Error error;
|
||||
return db_selection_print(client, selection, true, false,
|
||||
return db_selection_print(r, client.partition,
|
||||
selection, true, false,
|
||||
window.start, window.end, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
}
|
||||
|
||||
CommandResult
|
||||
@@ -107,9 +115,11 @@ handle_search(Client &client, Request args)
|
||||
static CommandResult
|
||||
handle_match_add(Client &client, Request args, bool fold_case)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
SongFilter filter;
|
||||
if (!filter.Parse(args, fold_case)) {
|
||||
command_error(client, ACK_ERROR_ARG, "incorrect arguments");
|
||||
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
@@ -119,7 +129,7 @@ handle_match_add(Client &client, Request args, bool fold_case)
|
||||
Error error;
|
||||
return AddFromDatabase(client.partition, selection, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
}
|
||||
|
||||
CommandResult
|
||||
@@ -137,34 +147,38 @@ handle_searchadd(Client &client, Request args)
|
||||
CommandResult
|
||||
handle_searchaddpl(Client &client, Request args)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
const char *playlist = args.shift();
|
||||
|
||||
SongFilter filter;
|
||||
if (!filter.Parse(args, true)) {
|
||||
command_error(client, ACK_ERROR_ARG, "incorrect arguments");
|
||||
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
Error error;
|
||||
const Database *db = client.GetDatabase(error);
|
||||
if (db == nullptr)
|
||||
return print_error(client, error);
|
||||
return print_error(r, error);
|
||||
|
||||
return search_add_to_playlist(*db, *client.GetStorage(),
|
||||
"", playlist, &filter, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
}
|
||||
|
||||
CommandResult
|
||||
handle_count(Client &client, Request args)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
TagType group = TAG_NUM_OF_ITEM_TYPES;
|
||||
if (args.size >= 2 && strcmp(args[args.size - 2], "group") == 0) {
|
||||
const char *s = args[args.size - 1];
|
||||
group = tag_name_parse_i(s);
|
||||
if (group == TAG_NUM_OF_ITEM_TYPES) {
|
||||
command_error(client, ACK_ERROR_ARG,
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", s);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
@@ -175,38 +189,43 @@ handle_count(Client &client, Request args)
|
||||
|
||||
SongFilter filter;
|
||||
if (!args.IsEmpty() && !filter.Parse(args, false)) {
|
||||
command_error(client, ACK_ERROR_ARG, "incorrect arguments");
|
||||
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
Error error;
|
||||
return PrintSongCount(client, "", &filter, group, error)
|
||||
return PrintSongCount(r, client.partition, "", &filter, group, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
}
|
||||
|
||||
CommandResult
|
||||
handle_listall(Client &client, Request args)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
/* default is root directory */
|
||||
const auto uri = args.GetOptional(0, "");
|
||||
|
||||
Error error;
|
||||
return db_selection_print(client, DatabaseSelection(uri, true),
|
||||
return db_selection_print(r, client.partition,
|
||||
DatabaseSelection(uri, true),
|
||||
false, false, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
}
|
||||
|
||||
CommandResult
|
||||
handle_list(Client &client, Request args)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
const char *tag_name = args.shift();
|
||||
unsigned tagType = locate_parse_type(tag_name);
|
||||
|
||||
if (tagType >= TAG_NUM_OF_ITEM_TYPES &&
|
||||
tagType != LOCATE_TAG_FILE_TYPE) {
|
||||
command_error(client, ACK_ERROR_ARG,
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", tag_name);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
@@ -217,7 +236,7 @@ handle_list(Client &client, Request args)
|
||||
if (args.size == 1) {
|
||||
/* for compatibility with < 0.12.0 */
|
||||
if (tagType != TAG_ALBUM) {
|
||||
command_error(client, ACK_ERROR_ARG,
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"should be \"%s\" for 3 arguments",
|
||||
tag_item_names[TAG_ALBUM]);
|
||||
return CommandResult::ERROR;
|
||||
@@ -231,7 +250,7 @@ handle_list(Client &client, Request args)
|
||||
const char *s = args[args.size - 1];
|
||||
TagType gt = tag_name_parse_i(s);
|
||||
if (gt == TAG_NUM_OF_ITEM_TYPES) {
|
||||
command_error(client, ACK_ERROR_ARG,
|
||||
r.FormatError(ACK_ERROR_ARG,
|
||||
"Unknown tag type: %s", s);
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
@@ -246,8 +265,7 @@ handle_list(Client &client, Request args)
|
||||
filter = new SongFilter();
|
||||
if (!filter->Parse(args, false)) {
|
||||
delete filter;
|
||||
command_error(client, ACK_ERROR_ARG,
|
||||
"not able to parse args");
|
||||
r.Error(ACK_ERROR_ARG, "not able to parse args");
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
}
|
||||
@@ -255,15 +273,16 @@ handle_list(Client &client, Request args)
|
||||
if (tagType < TAG_NUM_OF_ITEM_TYPES &&
|
||||
group_mask & (1u << tagType)) {
|
||||
delete filter;
|
||||
command_error(client, ACK_ERROR_ARG, "Conflicting group");
|
||||
r.Error(ACK_ERROR_ARG, "Conflicting group");
|
||||
return CommandResult::ERROR;
|
||||
}
|
||||
|
||||
Error error;
|
||||
CommandResult ret =
|
||||
PrintUniqueTags(client, tagType, group_mask, filter, error)
|
||||
PrintUniqueTags(r, client.partition,
|
||||
tagType, group_mask, filter, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
|
||||
delete filter;
|
||||
|
||||
@@ -273,12 +292,15 @@ handle_list(Client &client, Request args)
|
||||
CommandResult
|
||||
handle_listallinfo(Client &client, Request args)
|
||||
{
|
||||
Response r(client);
|
||||
|
||||
/* default is root directory */
|
||||
const auto uri = args.GetOptional(0, "");
|
||||
|
||||
Error error;
|
||||
return db_selection_print(client, DatabaseSelection(uri, true),
|
||||
return db_selection_print(r, client.partition,
|
||||
DatabaseSelection(uri, true),
|
||||
true, false, error)
|
||||
? CommandResult::OK
|
||||
: print_error(client, error);
|
||||
: print_error(r, error);
|
||||
}
|
||||
|
Reference in New Issue
Block a user