From ab5b6f83fda2ec6ead77278b5427d127947a4465 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 14 Feb 2022 14:08:26 +0100 Subject: [PATCH] queue/Print: support sorting by priority --- doc/protocol.rst | 3 ++- src/command/QueueCommands.cxx | 3 +++ src/queue/Print.cxx | 9 +++++++++ src/song/Filter.hxx | 5 +++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/protocol.rst b/doc/protocol.rst index 009a0a4da..525027a33 100644 --- a/doc/protocol.rst +++ b/doc/protocol.rst @@ -787,7 +787,8 @@ Whenever possible, ids should be used. "ArtistSort", "AlbumSort" or "AlbumArtistSort" instead. These will automatically fall back to the former if "\*Sort" doesn't exist. "AlbumArtist" falls back to just "Artist". The type - "Last-Modified" can sort by file modification time. + "Last-Modified" can sort by file modification time, and "prio" + sorts by queue priority. ``window`` can be used to query only a portion of the real response. The parameter is two zero-based queue positions; a diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 8fc9a1dd0..bc1a06811 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -293,6 +293,9 @@ ParseSortTag(const char *s) if (StringIsEqualIgnoreCase(s, "Last-Modified")) return TagType(SORT_TAG_LAST_MODIFIED); + if (StringIsEqualIgnoreCase(s, "prio")) + return TagType(SORT_TAG_PRIO); + TagType tag = tag_name_parse_i(s); if (tag == TAG_NUM_OF_ITEM_TYPES) throw ProtocolError(ACK_ERROR_ARG, "Unknown sort tag"); diff --git a/src/queue/Print.cxx b/src/queue/Print.cxx index f4ecffb8e..aebb6a71e 100644 --- a/src/queue/Print.cxx +++ b/src/queue/Print.cxx @@ -143,6 +143,15 @@ PrintSortedQueue(Response &r, const Queue &queue, return a.GetLastModified() < b.GetLastModified(); }); + else if (sort == TagType(SORT_TAG_PRIO)) + std::stable_sort(v.begin(), v.end(), + [&queue, descending](unsigned a_pos, unsigned b_pos){ + if (descending) + std::swap(a_pos, b_pos); + + return queue.GetPriorityAtPosition(a_pos) < + queue.GetPriorityAtPosition(b_pos); + }); else std::stable_sort(v.begin(), v.end(), [&queue, sort, descending](unsigned a_pos, diff --git a/src/song/Filter.hxx b/src/song/Filter.hxx index 68e441cfa..09738d668 100644 --- a/src/song/Filter.hxx +++ b/src/song/Filter.hxx @@ -31,6 +31,11 @@ */ #define SORT_TAG_LAST_MODIFIED (TAG_NUM_OF_ITEM_TYPES + 3) +/** + * Special value for QueueSelection::sort + */ +#define SORT_TAG_PRIO (TAG_NUM_OF_ITEM_TYPES + 4) + template struct ConstBuffer; enum TagType : uint8_t; struct LightSong;