Commit 2e5ca1cb authored by Max Kellermann's avatar Max Kellermann

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

parent 680fb51c
......@@ -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
......
......@@ -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.
......
......@@ -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;
}
......
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