queue/PlaylistEdit: convert start/end parameters to RangeArg

This commit is contained in:
Max Kellermann
2021-02-15 19:25:36 +01:00
parent 157ddcbab1
commit 471c37be59
7 changed files with 85 additions and 100 deletions

View File

@@ -26,6 +26,7 @@
enum TagType : uint8_t;
struct Tag;
struct RangeArg;
class PlayerControl;
class DetachedSong;
class Database;
@@ -240,7 +241,7 @@ public:
* @param start the position of the first song to delete
* @param end the position after the last song to delete
*/
void DeleteRange(PlayerControl &pc, unsigned start, unsigned end);
void DeleteRange(PlayerControl &pc, RangeArg range);
/**
* Mark the given song as "stale", i.e. as not being available
@@ -250,10 +251,9 @@ public:
*/
void StaleSong(PlayerControl &pc, const char *uri) noexcept;
void Shuffle(PlayerControl &pc, unsigned start, unsigned end) noexcept;
void Shuffle(PlayerControl &pc, RangeArg range);
void MoveRange(PlayerControl &pc, unsigned start,
unsigned end, int to);
void MoveRange(PlayerControl &pc, RangeArg range, int to);
void MoveId(PlayerControl &pc, unsigned id, int to);
@@ -262,7 +262,7 @@ public:
void SwapIds(PlayerControl &pc, unsigned id1, unsigned id2);
void SetPriorityRange(PlayerControl &pc,
unsigned start_position, unsigned end_position,
RangeArg position_range,
uint8_t priority);
void SetPriorityId(PlayerControl &pc,

View File

@@ -27,6 +27,7 @@
#include "Listener.hxx"
#include "PlaylistError.hxx"
#include "player/Control.hxx"
#include "protocol/RangeArg.hxx"
#include "song/DetachedSong.hxx"
#include "SongLoader.hxx"
@@ -168,16 +169,13 @@ playlist::SwapIds(PlayerControl &pc, unsigned id1, unsigned id2)
void
playlist::SetPriorityRange(PlayerControl &pc,
unsigned start, unsigned end,
RangeArg range,
uint8_t priority)
{
if (start >= GetLength())
if (!range.CheckClip(GetLength()))
throw PlaylistError::BadRange();
if (end > GetLength())
end = GetLength();
if (start >= end)
if (range.IsEmpty())
return;
/* remember "current" and "queued" */
@@ -187,7 +185,7 @@ playlist::SetPriorityRange(PlayerControl &pc,
/* apply the priority changes */
queue.SetPriorityRange(start, end, priority, current);
queue.SetPriorityRange(range.start, range.end, priority, current);
/* restore "current" and choose a new "queued" */
@@ -206,7 +204,7 @@ playlist::SetPriorityId(PlayerControl &pc,
if (song_position < 0)
throw PlaylistError::NoSuchSong();
SetPriorityRange(pc, song_position, song_position + 1, priority);
SetPriorityRange(pc, {unsigned(song_position), song_position + 1U}, priority);
}
void
@@ -272,22 +270,19 @@ playlist::DeletePosition(PlayerControl &pc, unsigned song)
}
void
playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end)
playlist::DeleteRange(PlayerControl &pc, RangeArg range)
{
if (start >= queue.GetLength())
if (!range.CheckClip(GetLength()))
throw PlaylistError::BadRange();
if (end > queue.GetLength())
end = queue.GetLength();
if (start >= end)
if (range.IsEmpty())
return;
const DetachedSong *queued_song = GetQueuedSong();
do {
DeleteInternal(pc, --end, &queued_song);
} while (end != start);
DeleteInternal(pc, --range.end, &queued_song);
} while (range.end != range.start);
UpdateQueuedSong(pc, queued_song);
OnModified();
@@ -320,17 +315,17 @@ playlist::StaleSong(PlayerControl &pc, const char *uri) noexcept
}
void
playlist::MoveRange(PlayerControl &pc,
unsigned start, unsigned end, int to)
playlist::MoveRange(PlayerControl &pc, RangeArg range, int to)
{
if (!queue.IsValidPosition(start) || !queue.IsValidPosition(end - 1))
if (!queue.IsValidPosition(range.start) ||
!queue.IsValidPosition(range.end - 1))
throw PlaylistError::BadRange();
if ((to >= 0 && to + end - start - 1 >= GetLength()) ||
if ((to >= 0 && to + range.Count() - 1 >= GetLength()) ||
(to < 0 && unsigned(std::abs(to)) > GetLength()))
throw PlaylistError::BadRange();
if ((int)start == to)
if ((int)range.start == to)
/* nothing happens */
return;
@@ -347,26 +342,26 @@ playlist::MoveRange(PlayerControl &pc,
because there is no current song */
throw PlaylistError::BadRange();
if (start <= (unsigned)currentSong && (unsigned)currentSong < end)
if (range.Contains(currentSong))
/* no-op, can't be moved to offset of itself */
return;
to = (currentSong + std::abs(to)) % GetLength();
if (start < (unsigned)to)
to -= end - start;
if (range.start < (unsigned)to)
to -= range.Count();
}
queue.MoveRange(start, end, to);
queue.MoveRange(range.start, range.end, to);
if (!queue.random && current >= 0) {
/* update current */
if (start <= unsigned(current) && unsigned(current) < end)
current += unsigned(to) - start;
else if (unsigned(current) >= end &&
if (range.Contains(current))
current += unsigned(to) - range.start;
else if (unsigned(current) >= range.end &&
unsigned(current) <= unsigned(to))
current -= end - start;
current -= range.Count();
else if (unsigned(current) >= unsigned(to) &&
unsigned(current) < start)
current += end - start;
unsigned(current) < range.start)
current += range.Count();
}
UpdateQueuedSong(pc, queued_song);
@@ -380,17 +375,16 @@ playlist::MoveId(PlayerControl &pc, unsigned id1, int to)
if (song < 0)
throw PlaylistError::NoSuchSong();
MoveRange(pc, song, song + 1, to);
MoveRange(pc, RangeArg::Single(song), to);
}
void
playlist::Shuffle(PlayerControl &pc, unsigned start, unsigned end) noexcept
playlist::Shuffle(PlayerControl &pc, RangeArg range)
{
if (end > GetLength())
/* correct the "end" offset */
end = GetLength();
if (!range.CheckClip(GetLength()))
throw PlaylistError::BadRange();
if (start + 1 >= end)
if (range.HasAtLeast(2))
/* needs at least two entries. */
return;
@@ -398,17 +392,17 @@ playlist::Shuffle(PlayerControl &pc, unsigned start, unsigned end) noexcept
if (playing && current >= 0) {
unsigned current_position = queue.OrderToPosition(current);
if (current_position >= start && current_position < end) {
if (range.Contains(current_position)) {
/* put current playing song first */
queue.SwapPositions(start, current_position);
queue.SwapPositions(range.start, current_position);
if (queue.random) {
current = queue.PositionToOrder(start);
current = queue.PositionToOrder(range.start);
} else
current = start;
current = range.start;
/* start shuffle after the current song */
start++;
range.start++;
}
} else {
/* no playback currently: reset current */
@@ -416,7 +410,7 @@ playlist::Shuffle(PlayerControl &pc, unsigned start, unsigned end) noexcept
current = -1;
}
queue.ShuffleRange(start, end);
queue.ShuffleRange(range.start, range.end);
UpdateQueuedSong(pc, queued_song);
OnModified();

View File

@@ -76,12 +76,7 @@ queue_print_changes_info(Response &r, const Queue &queue,
unsigned start, unsigned end)
{
assert(start <= end);
if (start >= queue.GetLength())
return;
if (end > queue.GetLength())
end = queue.GetLength();
assert(end <= queue.GetLength());
for (unsigned i = start; i < end; i++)
if (queue.IsNewerAtPosition(i, version))
@@ -94,12 +89,7 @@ queue_print_changes_position(Response &r, const Queue &queue,
unsigned start, unsigned end)
{
assert(start <= end);
if (start >= queue.GetLength())
return;
if (end > queue.GetLength())
end = queue.GetLength();
assert(end <= queue.GetLength());
for (unsigned i = start; i < end; i++)
if (queue.IsNewerAtPosition(i, version))