2012-08-07 21:55:11 +02:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
2012-08-07 21:55:11 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "DatabaseCommands.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2014-02-01 01:11:50 +01:00
|
|
|
#include "db/DatabaseGlue.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseQueue.hxx"
|
|
|
|
#include "db/DatabasePlaylist.hxx"
|
|
|
|
#include "db/DatabasePrint.hxx"
|
2014-04-26 22:59:21 +02:00
|
|
|
#include "db/Count.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/Selection.hxx"
|
2012-09-25 12:10:12 +02:00
|
|
|
#include "CommandError.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2014-04-24 09:43:08 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2015-08-14 19:48:30 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2012-08-29 19:12:26 +02:00
|
|
|
#include "SongFilter.hxx"
|
2014-07-11 20:01:53 +02:00
|
|
|
#include "BulkEdit.hxx"
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2014-04-24 10:20:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-02-28 19:02:23 +01:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listfiles_db(Client &client, Response &r, const char *uri)
|
2014-02-28 19:02:23 +01:00
|
|
|
{
|
|
|
|
const DatabaseSelection selection(uri, false);
|
|
|
|
|
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
if (!db_selection_print(r, client.partition,
|
|
|
|
selection, false, true, error))
|
|
|
|
return print_error(r, error);
|
2014-02-28 19:02:23 +01:00
|
|
|
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-10-20 12:07:18 +02:00
|
|
|
handle_lsinfo2(Client &client, const char *uri, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2012-08-07 23:06:41 +02:00
|
|
|
const DatabaseSelection selection(uri, false);
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
if (!db_selection_print(r, client.partition,
|
|
|
|
selection, true, false, error))
|
|
|
|
return print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_match(Client &client, Request args, Response &r, bool fold_case)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2015-08-11 21:35:52 +02:00
|
|
|
RangeArg window;
|
2015-08-14 19:48:30 +02:00
|
|
|
if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
|
2015-08-06 22:10:25 +02:00
|
|
|
if (!args.Parse(args.size - 1, window, r))
|
2014-11-18 14:31:27 +01:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
|
|
|
|
args.pop_back();
|
|
|
|
args.pop_back();
|
2015-08-11 21:35:52 +02:00
|
|
|
} else
|
|
|
|
window.SetAll();
|
2014-11-18 14:31:27 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter filter;
|
2014-04-24 09:43:08 +02:00
|
|
|
if (!filter.Parse(args, fold_case)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 19:29:22 +01:00
|
|
|
const DatabaseSelection selection("", true, &filter);
|
2012-08-07 23:22:37 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
return db_selection_print(r, client.partition,
|
|
|
|
selection, true, false,
|
2015-08-11 21:35:52 +02:00
|
|
|
window.start, window.end, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_find(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2015-08-13 12:48:31 +02:00
|
|
|
return handle_match(client, args, r, false);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_search(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2015-08-13 12:48:31 +02:00
|
|
|
return handle_match(client, args, r, true);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_match_add(Client &client, Request args, Response &r, bool fold_case)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter filter;
|
2014-04-24 09:43:08 +02:00
|
|
|
if (!filter.Parse(args, fold_case)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 20:01:53 +02:00
|
|
|
const ScopeBulkEdit bulk_edit(client.partition);
|
|
|
|
|
2013-01-07 11:33:00 +01:00
|
|
|
const DatabaseSelection selection("", true, &filter);
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-10-19 18:48:38 +02:00
|
|
|
return AddFromDatabase(client.partition, selection, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_findadd(Client &client, Request args, Response &r)
|
2012-08-08 00:44:58 +02:00
|
|
|
{
|
2015-08-13 12:48:31 +02:00
|
|
|
return handle_match_add(client, args, r, false);
|
2012-08-08 00:44:58 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_searchadd(Client &client, Request args, Response &r)
|
2012-08-08 00:44:58 +02:00
|
|
|
{
|
2015-08-13 12:48:31 +02:00
|
|
|
return handle_match_add(client, args, r, true);
|
2012-08-08 00:44:58 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_searchaddpl(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2014-04-24 09:43:08 +02:00
|
|
|
const char *playlist = args.shift();
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter filter;
|
2014-04-24 09:43:08 +02:00
|
|
|
if (!filter.Parse(args, true)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2014-02-01 00:26:34 +01:00
|
|
|
const Database *db = client.GetDatabase(error);
|
2014-02-01 01:11:50 +01:00
|
|
|
if (db == nullptr)
|
2015-08-06 22:10:25 +02:00
|
|
|
return print_error(r, error);
|
2014-02-01 01:11:50 +01:00
|
|
|
|
2014-02-07 00:29:07 +01:00
|
|
|
return search_add_to_playlist(*db, *client.GetStorage(),
|
|
|
|
"", playlist, &filter, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_count(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2014-04-26 23:15:31 +02:00
|
|
|
TagType group = TAG_NUM_OF_ITEM_TYPES;
|
2015-08-14 19:48:30 +02:00
|
|
|
if (args.size >= 2 && StringIsEqual(args[args.size - 2], "group")) {
|
2014-04-26 23:15:31 +02:00
|
|
|
const char *s = args[args.size - 1];
|
|
|
|
group = tag_name_parse_i(s);
|
|
|
|
if (group == TAG_NUM_OF_ITEM_TYPES) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.FormatError(ACK_ERROR_ARG,
|
2014-04-26 23:15:31 +02:00
|
|
|
"Unknown tag type: %s", s);
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.pop_back();
|
|
|
|
args.pop_back();
|
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter filter;
|
2014-04-26 23:15:31 +02:00
|
|
|
if (!args.IsEmpty() && !filter.Parse(args, false)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "incorrect arguments");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
return PrintSongCount(r, client.partition, "", &filter, group, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listall(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
/* default is root directory */
|
2015-08-11 22:18:40 +02:00
|
|
|
const auto uri = args.GetOptional(0, "");
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
return db_selection_print(r, client.partition,
|
|
|
|
DatabaseSelection(uri, true),
|
2014-04-24 18:09:39 +02:00
|
|
|
false, false, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_list(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2014-04-24 11:38:27 +02:00
|
|
|
const char *tag_name = args.shift();
|
|
|
|
unsigned tagType = locate_parse_type(tag_name);
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2014-04-24 08:13:20 +02:00
|
|
|
if (tagType >= TAG_NUM_OF_ITEM_TYPES &&
|
|
|
|
tagType != LOCATE_TAG_FILE_TYPE) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.FormatError(ACK_ERROR_ARG,
|
2014-04-24 11:38:27 +02:00
|
|
|
"Unknown tag type: %s", tag_name);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2014-04-24 11:38:27 +02:00
|
|
|
SongFilter *filter = nullptr;
|
2015-08-24 11:53:30 +02:00
|
|
|
tag_mask_t group_mask = 0;
|
2014-04-24 11:38:27 +02:00
|
|
|
|
|
|
|
if (args.size == 1) {
|
2014-04-24 08:10:56 +02:00
|
|
|
/* for compatibility with < 0.12.0 */
|
2012-08-07 21:55:11 +02:00
|
|
|
if (tagType != TAG_ALBUM) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.FormatError(ACK_ERROR_ARG,
|
2012-08-07 21:55:11 +02:00
|
|
|
"should be \"%s\" for 3 arguments",
|
|
|
|
tag_item_names[TAG_ALBUM]);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2014-04-24 11:38:27 +02:00
|
|
|
filter = new SongFilter((unsigned)TAG_ARTIST, args.shift());
|
|
|
|
}
|
2014-04-24 09:43:08 +02:00
|
|
|
|
2014-04-24 10:20:24 +02:00
|
|
|
while (args.size >= 2 &&
|
2015-08-14 19:48:30 +02:00
|
|
|
StringIsEqual(args[args.size - 2], "group")) {
|
2014-04-24 10:20:24 +02:00
|
|
|
const char *s = args[args.size - 1];
|
|
|
|
TagType gt = tag_name_parse_i(s);
|
|
|
|
if (gt == TAG_NUM_OF_ITEM_TYPES) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.FormatError(ACK_ERROR_ARG,
|
2014-04-24 10:20:24 +02:00
|
|
|
"Unknown tag type: %s", s);
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2015-08-24 11:53:30 +02:00
|
|
|
group_mask |= tag_mask_t(1) << unsigned(gt);
|
2014-04-24 10:20:24 +02:00
|
|
|
|
|
|
|
args.pop_back();
|
|
|
|
args.pop_back();
|
|
|
|
}
|
|
|
|
|
2014-04-24 11:38:27 +02:00
|
|
|
if (!args.IsEmpty()) {
|
2012-08-29 19:27:03 +02:00
|
|
|
filter = new SongFilter();
|
2014-04-24 09:43:08 +02:00
|
|
|
if (!filter->Parse(args, false)) {
|
2012-08-29 19:27:03 +02:00
|
|
|
delete filter;
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "not able to parse args");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
2014-04-24 11:38:27 +02:00
|
|
|
}
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2014-04-25 11:28:17 +02:00
|
|
|
if (tagType < TAG_NUM_OF_ITEM_TYPES &&
|
2015-08-24 11:53:30 +02:00
|
|
|
group_mask & (tag_mask_t(1) << tagType)) {
|
2014-04-25 11:28:17 +02:00
|
|
|
delete filter;
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Conflicting group");
|
2014-04-25 11:28:17 +02:00
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult ret =
|
2015-08-06 22:10:25 +02:00
|
|
|
PrintUniqueTags(r, client.partition,
|
|
|
|
tagType, group_mask, filter, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
delete filter;
|
2012-08-07 21:55:11 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_listallinfo(Client &client, Request args, Response &r)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
/* default is root directory */
|
2015-08-11 22:18:40 +02:00
|
|
|
const auto uri = args.GetOptional(0, "");
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2015-08-06 22:10:25 +02:00
|
|
|
return db_selection_print(r, client.partition,
|
|
|
|
DatabaseSelection(uri, true),
|
2014-04-24 18:09:39 +02:00
|
|
|
true, false, error)
|
2013-10-20 13:10:54 +02:00
|
|
|
? CommandResult::OK
|
2015-08-06 22:10:25 +02:00
|
|
|
: print_error(r, error);
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|