From a0ea8416f6303950844b1c09de0f837a3746d6c7 Mon Sep 17 00:00:00 2001 From: whatdoineed2do Date: Sun, 6 Feb 2022 19:23:06 +0000 Subject: [PATCH] [db] fix 'add next' when in queue shuffle mode (#1414) * [db] fix 'add next' when in queue shuffle mode * [db] review on 'add next' fix Co-authored-by: whatdoineed2do/Ray --- src/db.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/db.c b/src/db.c index 9ede24ee..47d73ca0 100644 --- a/src/db.c +++ b/src/db.c @@ -5161,6 +5161,8 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id, int queue_version; uint32_t queue_count; int pos; + int shuffle_pos; + bool append_to_queue; int ret; if (new_item_id) @@ -5190,24 +5192,34 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id, return 0; } - if (position < 0 || position > queue_count) + append_to_queue = (position < 0 || position > queue_count); + + if (append_to_queue) { pos = queue_count; + shuffle_pos = queue_count; } else { pos = position; + shuffle_pos = position; // Update pos for all items from the given position (make room for the new items in the queue) query = sqlite3_mprintf("UPDATE queue SET pos = pos + %d, queue_version = %d WHERE pos >= %d;", qp->results, queue_version, pos); ret = db_query_run(query, 1, 0); if (ret < 0) goto end_transaction; + + // and similary update on shuffle_pos + query = sqlite3_mprintf("UPDATE queue SET shuffle_pos = shuffle_pos + %d, queue_version = %d WHERE shuffle_pos >= %d;", qp->results, queue_version, pos); + ret = db_query_run(query, 1, 0); + if (ret < 0) + goto end_transaction; } while ((ret = db_query_fetch_file(&dbmfi, qp)) == 0) { - ret = queue_item_add_from_file(&dbmfi, pos, queue_count, queue_version); + ret = queue_item_add_from_file(&dbmfi, pos, shuffle_pos, queue_version); if (ret < 0) { @@ -5215,7 +5227,7 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id, break; } - DPRINTF(E_DBG, L_DB, "Added song id %s (%s) to queue with item id %d\n", dbmfi.id, dbmfi.title, ret); + DPRINTF(E_DBG, L_DB, "Added (pos=%d shuffle_pos=%d reshuffle=%d req position=%d) song id %s (%s) to queue with item id %d\n", pos, shuffle_pos, reshuffle, position, dbmfi.id, dbmfi.title, ret); if (new_item_id && *new_item_id == 0) *new_item_id = ret; @@ -5223,7 +5235,7 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id, (*count)++; pos++; - queue_count++; + shuffle_pos++; } if (ret > 0) @@ -5234,8 +5246,10 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id, if (ret < 0) goto end_transaction; - // Reshuffle after adding new items - if (reshuffle) + // Reshuffle after adding new items, if no queue position was specified - this + // case would indicate an 'add next' condition where if shuffling invalidates + // the tracks added to 'next' + if (append_to_queue && reshuffle) { ret = queue_reshuffle(item_id, queue_version); }