From 1ec7ad683e3d948ed477b0f6a6a021cfffe7ab42 Mon Sep 17 00:00:00 2001 From: chme Date: Sat, 5 Mar 2016 14:02:06 +0100 Subject: [PATCH] [queue] Fix moving a song to the last position in the queue failed --- src/queue.c | 168 ++++++++++++++++++---------------------------------- 1 file changed, 56 insertions(+), 112 deletions(-) diff --git a/src/queue.c b/src/queue.c index 8e74e639..136b2941 100644 --- a/src/queue.c +++ b/src/queue.c @@ -698,6 +698,46 @@ queue_add_after(struct queue *queue, struct queue_item *item, unsigned int item_ queue_add_afteritem(queue, item, item_prev); } +static void +queue_move_item_before_item(struct queue *queue, struct queue_item *item, struct queue_item *item_next, char shuffle) +{ + if (!item_next) + { + // If item_next is NULL the item should be inserted at the end of the queue (directly before the head item) + item_next = queue->head; + } + + // Remove item from the queue + if (shuffle) + { + item->shuffle_prev->shuffle_next = item->shuffle_next; + item->shuffle_next->shuffle_prev = item->shuffle_prev; + } + else + { + item->prev->next = item->next; + item->next->prev = item->prev; + } + + // Insert item into the queue before the item at the target postion + if (shuffle) + { + item_next->shuffle_prev->shuffle_next = item; + item->shuffle_prev = item_next->shuffle_prev; + + item_next->shuffle_prev = item; + item->shuffle_next = item_next; + } + else + { + item_next->prev->next = item; + item->prev = item_next->prev; + + item_next->prev = item; + item->next = item_next; + } +} + /* * Moves the item at from_pos to to_pos in the play-queue (shuffle = 0) or shuffle-queue (shuffle = 1) * @@ -726,48 +766,15 @@ queue_move_bypos(struct queue *queue, unsigned int item_id, unsigned int from_po // Get the item at the target position item_next = queueitem_get_bypos(queue, item_id, (to_offset + 1), shuffle); - if (!item_next) - { - DPRINTF(E_LOG, L_PLAYER, "Invalid position given to move items\n"); - return; - } - // Remove item from the queue - if (shuffle) - { - item->shuffle_prev->shuffle_next = item->shuffle_next; - item->shuffle_next->shuffle_prev = item->shuffle_prev; - } - else - { - item->prev->next = item->next; - item->next->prev = item->prev; - } - - // Insert item into the queue before the item at the target postion - if (shuffle) - { - item_next->shuffle_prev->shuffle_next = item; - item->shuffle_prev = item_next->shuffle_prev; - - item_next->shuffle_prev = item; - item->shuffle_next = item_next; - } - else - { - item_next->prev->next = item; - item->prev = item_next->prev; - - item_next->prev = item; - item->next = item_next; - } + queue_move_item_before_item(queue, item, item_next, shuffle); } void queue_move_byindex(struct queue *queue, unsigned int from_pos, unsigned int to_pos, char shuffle) { struct queue_item *item; - struct queue_item *itemnext; + struct queue_item *item_next; if (from_pos == to_pos) return; @@ -780,46 +787,16 @@ queue_move_byindex(struct queue *queue, unsigned int from_pos, unsigned int to_p return; } + // Check if the index of the item to move is lower than the target index + // If that is the case, increment the target position, because the given to_pos + // is based on the queue without the moved item. + if (from_pos < to_pos) + to_pos++; + // Get the item at the target position - itemnext = queueitem_get_byindex(queue, to_pos, shuffle); - if (!itemnext) - { - DPRINTF(E_LOG, L_PLAYER, "Invalid position given to move items\n"); - return; - } + item_next = queueitem_get_byindex(queue, to_pos, shuffle); - if (to_pos > from_pos) - itemnext = item_next(itemnext, shuffle); - - // Remove item from the queue - if (shuffle) - { - item->shuffle_prev->shuffle_next = item->shuffle_next; - item->shuffle_next->shuffle_prev = item->shuffle_prev; - } - else - { - item->prev->next = item->next; - item->next->prev = item->prev; - } - - // Insert item into the queue before the item at the target postion - if (shuffle) - { - itemnext->shuffle_prev->shuffle_next = item; - item->shuffle_prev = itemnext->shuffle_prev; - - itemnext->shuffle_prev = item; - item->shuffle_next = itemnext; - } - else - { - itemnext->prev->next = item; - item->prev = itemnext->prev; - - itemnext->prev = item; - item->next = itemnext; - } + queue_move_item_before_item(queue, item, item_next, shuffle); } /* @@ -836,7 +813,7 @@ queue_move_byitemid(struct queue *queue, unsigned int item_id, unsigned int to_p { struct queue_item *item; struct queue_item *item_next; - int index; + int from_pos; // Get the item to be moved item = queueitem_get_byitemid(queue, item_id); @@ -846,57 +823,24 @@ queue_move_byitemid(struct queue *queue, unsigned int item_id, unsigned int to_p return; } - index = queue_index_byitemid(queue, item_id, shuffle); + from_pos = queue_index_byitemid(queue, item_id, shuffle); - if (index == to_pos) + if (from_pos == to_pos) { - DPRINTF(E_DBG, L_PLAYER, "Ignore moving item %d from index %d to %d\n", item_id, index, to_pos); + DPRINTF(E_DBG, L_PLAYER, "Ignore moving item %d from index %d to %d\n", item_id, from_pos, to_pos); return; } // Check if the index of the item to move is lower than the target index // If that is the case, increment the target position, because the given to_pos // is based on the queue without the moved item. - if (index < to_pos) + if (from_pos < to_pos) to_pos++; // Get the item at the target position item_next = queueitem_get_byindex(queue, to_pos, shuffle); - if (!item_next) - { - DPRINTF(E_LOG, L_PLAYER, "Invalid position given to move items\n"); - return; - } - // Remove item from the queue - if (shuffle) - { - item->shuffle_prev->shuffle_next = item->shuffle_next; - item->shuffle_next->shuffle_prev = item->shuffle_prev; - } - else - { - item->prev->next = item->next; - item->next->prev = item->prev; - } - - // Insert item into the queue before the item at the target postion - if (shuffle) - { - item_next->shuffle_prev->shuffle_next = item; - item->shuffle_prev = item_next->shuffle_prev; - - item_next->shuffle_prev = item; - item->shuffle_next = item_next; - } - else - { - item_next->prev->next = item; - item->prev = item_next->prev; - - item_next->prev = item; - item->next = item_next; - } + queue_move_item_before_item(queue, item, item_next, shuffle); } /*