Skip to content

Commit a0ea841

Browse files
whatdoineed2dowhatdoineed2do/Ray
andauthored
[db] fix 'add next' when in queue shuffle mode (owntone#1414)
* [db] fix 'add next' when in queue shuffle mode * [db] review on 'add next' fix Co-authored-by: whatdoineed2do/Ray <[email protected]>
1 parent 26d7cf4 commit a0ea841

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/db.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5161,6 +5161,8 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
51615161
int queue_version;
51625162
uint32_t queue_count;
51635163
int pos;
5164+
int shuffle_pos;
5165+
bool append_to_queue;
51645166
int ret;
51655167

51665168
if (new_item_id)
@@ -5190,40 +5192,50 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
51905192
return 0;
51915193
}
51925194

5193-
if (position < 0 || position > queue_count)
5195+
append_to_queue = (position < 0 || position > queue_count);
5196+
5197+
if (append_to_queue)
51945198
{
51955199
pos = queue_count;
5200+
shuffle_pos = queue_count;
51965201
}
51975202
else
51985203
{
51995204
pos = position;
5205+
shuffle_pos = position;
52005206

52015207
// Update pos for all items from the given position (make room for the new items in the queue)
52025208
query = sqlite3_mprintf("UPDATE queue SET pos = pos + %d, queue_version = %d WHERE pos >= %d;", qp->results, queue_version, pos);
52035209
ret = db_query_run(query, 1, 0);
52045210
if (ret < 0)
52055211
goto end_transaction;
5212+
5213+
// and similary update on shuffle_pos
5214+
query = sqlite3_mprintf("UPDATE queue SET shuffle_pos = shuffle_pos + %d, queue_version = %d WHERE shuffle_pos >= %d;", qp->results, queue_version, pos);
5215+
ret = db_query_run(query, 1, 0);
5216+
if (ret < 0)
5217+
goto end_transaction;
52065218
}
52075219

52085220
while ((ret = db_query_fetch_file(&dbmfi, qp)) == 0)
52095221
{
5210-
ret = queue_item_add_from_file(&dbmfi, pos, queue_count, queue_version);
5222+
ret = queue_item_add_from_file(&dbmfi, pos, shuffle_pos, queue_version);
52115223

52125224
if (ret < 0)
52135225
{
52145226
DPRINTF(E_DBG, L_DB, "Failed to add song id %s (%s)\n", dbmfi.id, dbmfi.title);
52155227
break;
52165228
}
52175229

5218-
DPRINTF(E_DBG, L_DB, "Added song id %s (%s) to queue with item id %d\n", dbmfi.id, dbmfi.title, ret);
5230+
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);
52195231

52205232
if (new_item_id && *new_item_id == 0)
52215233
*new_item_id = ret;
52225234
if (count)
52235235
(*count)++;
52245236

52255237
pos++;
5226-
queue_count++;
5238+
shuffle_pos++;
52275239
}
52285240

52295241
if (ret > 0)
@@ -5234,8 +5246,10 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
52345246
if (ret < 0)
52355247
goto end_transaction;
52365248

5237-
// Reshuffle after adding new items
5238-
if (reshuffle)
5249+
// Reshuffle after adding new items, if no queue position was specified - this
5250+
// case would indicate an 'add next' condition where if shuffling invalidates
5251+
// the tracks added to 'next'
5252+
if (append_to_queue && reshuffle)
52395253
{
52405254
ret = queue_reshuffle(item_id, queue_version);
52415255
}

0 commit comments

Comments
 (0)