@@ -5161,6 +5161,8 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
5161
5161
int queue_version ;
5162
5162
uint32_t queue_count ;
5163
5163
int pos ;
5164
+ int shuffle_pos ;
5165
+ bool append_to_queue ;
5164
5166
int ret ;
5165
5167
5166
5168
if (new_item_id )
@@ -5190,40 +5192,50 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
5190
5192
return 0 ;
5191
5193
}
5192
5194
5193
- if (position < 0 || position > queue_count )
5195
+ append_to_queue = (position < 0 || position > queue_count );
5196
+
5197
+ if (append_to_queue )
5194
5198
{
5195
5199
pos = queue_count ;
5200
+ shuffle_pos = queue_count ;
5196
5201
}
5197
5202
else
5198
5203
{
5199
5204
pos = position ;
5205
+ shuffle_pos = position ;
5200
5206
5201
5207
// Update pos for all items from the given position (make room for the new items in the queue)
5202
5208
query = sqlite3_mprintf ("UPDATE queue SET pos = pos + %d, queue_version = %d WHERE pos >= %d;" , qp -> results , queue_version , pos );
5203
5209
ret = db_query_run (query , 1 , 0 );
5204
5210
if (ret < 0 )
5205
5211
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 ;
5206
5218
}
5207
5219
5208
5220
while ((ret = db_query_fetch_file (& dbmfi , qp )) == 0 )
5209
5221
{
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 );
5211
5223
5212
5224
if (ret < 0 )
5213
5225
{
5214
5226
DPRINTF (E_DBG , L_DB , "Failed to add song id %s (%s)\n" , dbmfi .id , dbmfi .title );
5215
5227
break ;
5216
5228
}
5217
5229
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 );
5219
5231
5220
5232
if (new_item_id && * new_item_id == 0 )
5221
5233
* new_item_id = ret ;
5222
5234
if (count )
5223
5235
(* count )++ ;
5224
5236
5225
5237
pos ++ ;
5226
- queue_count ++ ;
5238
+ shuffle_pos ++ ;
5227
5239
}
5228
5240
5229
5241
if (ret > 0 )
@@ -5234,8 +5246,10 @@ db_queue_add_by_query(struct query_params *qp, char reshuffle, uint32_t item_id,
5234
5246
if (ret < 0 )
5235
5247
goto end_transaction ;
5236
5248
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 )
5239
5253
{
5240
5254
ret = queue_reshuffle (item_id , queue_version );
5241
5255
}
0 commit comments