2012-08-07 21:55:11 +02:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 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 "DatabaseCommands.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.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"
|
2018-09-02 07:45:45 +02:00
|
|
|
#include "protocol/RangeArg.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"
|
2017-02-08 08:57:22 +01:00
|
|
|
#include "tag/ParseName.hxx"
|
2014-04-24 09:43:08 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2018-07-21 07:20:59 +02:00
|
|
|
#include "util/Exception.hxx"
|
2015-08-14 19:48:30 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2018-07-25 11:29:14 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/Filter.hxx"
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2016-10-29 10:37:18 +02:00
|
|
|
#include <memory>
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <vector>
|
2016-10-29 10:37:18 +02:00
|
|
|
|
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);
|
2017-02-25 09:58:58 +01:00
|
|
|
db_selection_print(r, client.GetPartition(),
|
2016-10-29 10:21:57 +02:00
|
|
|
selection, false, true);
|
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);
|
2017-02-25 09:58:58 +01:00
|
|
|
db_selection_print(r, client.GetPartition(),
|
2016-10-29 10:21:57 +02:00
|
|
|
selection, true, false);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 21:36:47 +01:00
|
|
|
static TagType
|
|
|
|
ParseSortTag(const char *s)
|
|
|
|
{
|
|
|
|
if (StringIsEqualIgnoreCase(s, "Last-Modified"))
|
|
|
|
return TagType(SORT_TAG_LAST_MODIFIED);
|
|
|
|
|
|
|
|
TagType tag = tag_name_parse_i(s);
|
|
|
|
if (tag == TAG_NUM_OF_ITEM_TYPES)
|
|
|
|
throw ProtocolError(ACK_ERROR_ARG, "Unknown sort tag");
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:58:22 +01:00
|
|
|
/**
|
|
|
|
* Convert all remaining arguments to a #DatabaseSelection.
|
|
|
|
*
|
|
|
|
* @param filter a buffer to be used for DatabaseSelection::filter
|
|
|
|
*/
|
|
|
|
static DatabaseSelection
|
|
|
|
ParseDatabaseSelection(Request args, bool fold_case, SongFilter &filter)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2018-09-02 07:53:32 +02:00
|
|
|
RangeArg window = RangeArg::All();
|
2015-08-14 19:48:30 +02:00
|
|
|
if (args.size >= 2 && StringIsEqual(args[args.size - 2], "window")) {
|
2015-12-18 09:50:48 +01:00
|
|
|
window = args.ParseRange(args.size - 1);
|
2014-11-18 14:31:27 +01:00
|
|
|
|
|
|
|
args.pop_back();
|
|
|
|
args.pop_back();
|
2018-09-02 07:53:32 +02:00
|
|
|
}
|
2014-11-18 14:31:27 +01:00
|
|
|
|
2017-02-08 09:22:15 +01:00
|
|
|
TagType sort = TAG_NUM_OF_ITEM_TYPES;
|
2017-12-18 21:50:14 +01:00
|
|
|
bool descending = false;
|
2017-02-08 09:22:15 +01:00
|
|
|
if (args.size >= 2 && StringIsEqual(args[args.size - 2], "sort")) {
|
2017-12-18 21:50:14 +01:00
|
|
|
const char *s = args.back();
|
|
|
|
if (*s == '-') {
|
|
|
|
descending = true;
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:36:47 +01:00
|
|
|
sort = ParseSortTag(s);
|
2017-02-08 09:22:15 +01:00
|
|
|
|
|
|
|
args.pop_back();
|
|
|
|
args.pop_back();
|
|
|
|
}
|
|
|
|
|
2018-07-21 07:20:59 +02:00
|
|
|
try {
|
|
|
|
filter.Parse(args, fold_case);
|
|
|
|
} catch (...) {
|
2019-03-25 18:58:22 +01:00
|
|
|
throw ProtocolError(ACK_ERROR_ARG,
|
|
|
|
GetFullMessage(std::current_exception()).c_str());
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
2018-08-02 19:42:53 +02:00
|
|
|
filter.Optimize();
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2018-09-02 10:42:47 +02:00
|
|
|
DatabaseSelection selection("", true, &filter);
|
|
|
|
selection.window = window;
|
|
|
|
selection.sort = sort;
|
|
|
|
selection.descending = descending;
|
2019-03-25 18:58:22 +01:00
|
|
|
return selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CommandResult
|
|
|
|
handle_match(Client &client, Request args, Response &r, bool fold_case)
|
|
|
|
{
|
|
|
|
SongFilter filter;
|
|
|
|
const auto selection = ParseDatabaseSelection(args, fold_case, filter);
|
2012-08-07 23:22:37 +02:00
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
db_selection_print(r, client.GetPartition(),
|
2018-09-02 10:42:47 +02:00
|
|
|
selection, true, false);
|
2016-10-29 10:21:57 +02:00
|
|
|
return CommandResult::OK;
|
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
|
2019-03-25 18:30:56 +01:00
|
|
|
handle_match_add(Client &client, Request args, bool fold_case)
|
2012-08-07 21:55:11 +02:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
SongFilter filter;
|
2019-03-25 18:30:56 +01:00
|
|
|
const auto selection = ParseDatabaseSelection(args, fold_case, filter);
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
auto &partition = client.GetPartition();
|
|
|
|
AddFromDatabase(partition, selection);
|
2016-10-29 10:21:57 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2019-03-25 18:30:56 +01:00
|
|
|
handle_findadd(Client &client, Request args, Response &)
|
2012-08-08 00:44:58 +02:00
|
|
|
{
|
2019-03-25 18:30:56 +01:00
|
|
|
return handle_match_add(client, args, false);
|
2012-08-08 00:44:58 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2019-03-25 18:30:56 +01:00
|
|
|
handle_searchadd(Client &client, Request args, Response &)
|
2012-08-08 00:44:58 +02:00
|
|
|
{
|
2019-03-25 18:30:56 +01:00
|
|
|
return handle_match_add(client, args, true);
|
2012-08-08 00:44:58 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2019-03-25 18:30:56 +01:00
|
|
|
handle_searchaddpl(Client &client, Request args, Response &)
|
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;
|
2019-03-25 18:30:56 +01:00
|
|
|
const auto selection = ParseDatabaseSelection(args, true, filter);
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2016-10-26 18:47:19 +02:00
|
|
|
const Database &db = client.GetDatabaseOrThrow();
|
2014-02-01 01:11:50 +01:00
|
|
|
|
2017-02-08 09:47:43 +01:00
|
|
|
search_add_to_playlist(db, client.GetStorage(),
|
2019-03-25 18:48:31 +01:00
|
|
|
playlist, selection);
|
2016-10-29 10:21:57 +02:00
|
|
|
return CommandResult::OK;
|
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;
|
2018-07-21 07:20:59 +02:00
|
|
|
if (!args.empty()) {
|
|
|
|
try {
|
|
|
|
filter.Parse(args, false);
|
|
|
|
} catch (...) {
|
|
|
|
r.Error(ACK_ERROR_ARG,
|
|
|
|
GetFullMessage(std::current_exception()).c_str());
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
2018-08-02 19:42:53 +02:00
|
|
|
|
|
|
|
filter.Optimize();
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
PrintSongCount(r, client.GetPartition(), "", &filter, group);
|
2016-10-29 10:21:57 +02:00
|
|
|
return CommandResult::OK;
|
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
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
db_selection_print(r, client.GetPartition(),
|
2016-10-29 10:21:57 +02:00
|
|
|
DatabaseSelection(uri, true),
|
|
|
|
false, false);
|
|
|
|
return CommandResult::OK;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 11:27:12 +02:00
|
|
|
static CommandResult
|
|
|
|
handle_list_file(Client &client, Request args, Response &r)
|
|
|
|
{
|
|
|
|
std::unique_ptr<SongFilter> filter;
|
|
|
|
|
|
|
|
if (!args.empty()) {
|
2020-02-01 04:52:08 +01:00
|
|
|
filter = std::make_unique<SongFilter>();
|
2018-07-25 11:27:12 +02:00
|
|
|
try {
|
|
|
|
filter->Parse(args, false);
|
|
|
|
} catch (...) {
|
|
|
|
r.Error(ACK_ERROR_ARG,
|
|
|
|
GetFullMessage(std::current_exception()).c_str());
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
2018-08-02 19:42:53 +02:00
|
|
|
filter->Optimize();
|
2018-07-25 11:27:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PrintSongUris(r, client.GetPartition(), filter.get());
|
|
|
|
return CommandResult::OK;
|
|
|
|
}
|
|
|
|
|
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();
|
2018-07-25 11:29:14 +02:00
|
|
|
if (StringEqualsCaseASCII(tag_name, "file") ||
|
|
|
|
StringEqualsCaseASCII(tag_name, "filename"))
|
2018-07-25 11:27:12 +02:00
|
|
|
return handle_list_file(client, args, r);
|
|
|
|
|
2018-07-25 11:29:14 +02:00
|
|
|
const auto tagType = tag_name_parse_i(tag_name);
|
|
|
|
if (tagType == TAG_NUM_OF_ITEM_TYPES) {
|
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
|
|
|
}
|
|
|
|
|
2016-10-29 10:37:18 +02:00
|
|
|
std::unique_ptr<SongFilter> filter;
|
2018-11-19 11:17:25 +01:00
|
|
|
std::vector<TagType> tag_types;
|
2014-04-24 11:38:27 +02:00
|
|
|
|
2019-03-14 19:50:09 +01:00
|
|
|
if (args.size == 1 &&
|
|
|
|
/* parantheses are the syntax for filter expressions: no
|
|
|
|
compatibility mode */
|
|
|
|
args.front()[0] != '(') {
|
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
|
|
|
}
|
|
|
|
|
2020-02-01 04:52:08 +01:00
|
|
|
filter = std::make_unique<SongFilter>(TAG_ARTIST,
|
|
|
|
args.shift());
|
2014-04-24 11:38:27 +02:00
|
|
|
}
|
2014-04-24 09:43:08 +02:00
|
|
|
|
2018-11-19 11:17:25 +01:00
|
|
|
while (args.size >= 2 &&
|
|
|
|
StringIsEqual(args[args.size - 2], "group")) {
|
2014-04-24 10:20:24 +02:00
|
|
|
const char *s = args[args.size - 1];
|
2018-11-19 11:17:25 +01:00
|
|
|
const auto group = tag_name_parse_i(s);
|
2018-10-22 11:35:22 +02:00
|
|
|
if (group == 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;
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:17:25 +01:00
|
|
|
if (group == tagType ||
|
|
|
|
std::find(tag_types.begin(), tag_types.end(),
|
|
|
|
group) != tag_types.end()) {
|
|
|
|
r.Error(ACK_ERROR_ARG, "Conflicting group");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag_types.emplace_back(group);
|
|
|
|
|
2014-04-24 10:20:24 +02:00
|
|
|
args.pop_back();
|
|
|
|
args.pop_back();
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:17:25 +01:00
|
|
|
tag_types.emplace_back(tagType);
|
|
|
|
|
2017-11-10 19:24:33 +01:00
|
|
|
if (!args.empty()) {
|
2020-02-01 04:52:08 +01:00
|
|
|
filter = std::make_unique<SongFilter>();
|
2018-07-21 07:20:59 +02:00
|
|
|
try {
|
|
|
|
filter->Parse(args, false);
|
|
|
|
} catch (...) {
|
|
|
|
r.Error(ACK_ERROR_ARG,
|
|
|
|
GetFullMessage(std::current_exception()).c_str());
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|
2018-08-02 19:42:53 +02:00
|
|
|
filter->Optimize();
|
2014-04-24 11:38:27 +02:00
|
|
|
}
|
2012-08-07 21:55:11 +02:00
|
|
|
|
2018-07-25 11:27:12 +02:00
|
|
|
PrintUniqueTags(r, client.GetPartition(),
|
2018-11-19 11:17:25 +01:00
|
|
|
{&tag_types.front(), tag_types.size()},
|
|
|
|
filter.get());
|
2016-10-29 10:21:57 +02:00
|
|
|
return CommandResult::OK;
|
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_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
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
db_selection_print(r, client.GetPartition(),
|
2016-10-29 10:21:57 +02:00
|
|
|
DatabaseSelection(uri, true),
|
|
|
|
true, false);
|
|
|
|
return CommandResult::OK;
|
2012-08-07 21:55:11 +02:00
|
|
|
}
|