command/database: add "position" parameter to "findadd" and "searchadd"

Closes https://github.com/MusicPlayerDaemon/MPD/issues/888
This commit is contained in:
Max Kellermann 2021-10-14 14:59:00 +02:00
parent 680fb51c37
commit 2e5ca1cbd2
3 changed files with 39 additions and 3 deletions

1
NEWS
View File

@ -5,6 +5,7 @@ ver 0.23 (not yet released)
- support "listfiles" with arbitrary storage plugins
- support relative positions in "addid"
- fix relative positions in "move" and "moveid"
- add "position" parameter to "findadd" and "searchadd"
* database
- proxy: require MPD 0.20 or later
- proxy: require libmpdclient 2.11 or later

View File

@ -1191,15 +1191,18 @@ The music database
.. _command_search:
:command:`search {FILTER} [sort {TYPE}] [window {START:END}]`
:command:`search {FILTER} [sort {TYPE}] [window {START:END}] [position POS]`
Search the database for songs matching
``FILTER`` (see :ref:`Filters <filter_syntax>`). Parameters
have the same meaning as for :ref:`find <command_find>`,
except that search is not case sensitive.
The ``position`` parameter specifies where the songs will be
inserted.
.. _command_searchadd:
:command:`searchadd {FILTER} [sort {TYPE}] [window {START:END}]`
:command:`searchadd {FILTER} [sort {TYPE}] [window {START:END}] [position POS]`
Search the database for songs matching
``FILTER`` (see :ref:`Filters <filter_syntax>`) and add them to
the queue.

View File

@ -19,6 +19,7 @@
#include "DatabaseCommands.hxx"
#include "Request.hxx"
#include "Partition.hxx"
#include "db/DatabaseQueue.hxx"
#include "db/DatabasePlaylist.hxx"
#include "db/DatabasePrint.hxx"
@ -70,6 +71,21 @@ ParseSortTag(const char *s)
return tag;
}
static unsigned
ParseQueuePosition(Request &args, unsigned queue_length)
{
if (args.size >= 2 && StringIsEqual(args[args.size - 2], "position")) {
unsigned position = args.ParseUnsigned(args.size - 1,
queue_length);
args.pop_back();
args.pop_back();
return position;
}
/* append to the end of the queue by default */
return queue_length;
}
/**
* Convert all remaining arguments to a #DatabaseSelection.
*
@ -142,11 +158,27 @@ handle_search(Client &client, Request args, Response &r)
static CommandResult
handle_match_add(Client &client, Request args, bool fold_case)
{
auto &partition = client.GetPartition();
const auto queue_length = partition.playlist.queue.GetLength();
const unsigned position = ParseQueuePosition(args, queue_length);
SongFilter filter;
const auto selection = ParseDatabaseSelection(args, fold_case, filter);
auto &partition = client.GetPartition();
AddFromDatabase(partition, selection);
if (position < queue_length) {
const auto new_queue_length =
partition.playlist.queue.GetLength();
const RangeArg range{queue_length, new_queue_length};
try {
partition.MoveRange(range, position);
} catch (...) {
/* ignore - shall we handle it? */
}
}
return CommandResult::OK;
}