1 /*****************************************************************************
2 * item.c : Playlist item creation/deletion/add/removal functions
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #include <vlc_common.h>
30 #include <vlc_playlist.h>
31 #include "playlist_internal.h"
33 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
34 playlist_item_t *p_node, int i_mode, int i_pos );
35 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
37 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
39 static playlist_item_t *ItemToNode( playlist_t *, playlist_item_t *, bool );
41 static int RecursiveAddIntoParent (
42 playlist_t *p_playlist, playlist_item_t *p_parent,
43 input_item_node_t *p_node, int i_pos, bool b_flat,
44 playlist_item_t **pp_first_leaf );
46 /*****************************************************************************
47 * An input item has gained subitems (Event Callback)
48 *****************************************************************************/
50 static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
53 input_item_t *p_input = p_event->p_obj;
54 playlist_t *p_playlist = (( playlist_item_t* ) user_data)->p_playlist;
55 input_item_node_t *p_new_root = p_event->u.input_item_subitem_tree_added.p_root;
59 playlist_item_t *p_item =
60 playlist_ItemGetByInput( p_playlist, p_input );
62 assert( p_item != NULL );
63 playlist_item_t *p_parent = p_item->p_parent;
64 assert( p_parent != NULL );
66 bool b_current = get_current_status_item( p_playlist ) == p_item;
67 bool b_autostart = var_CreateGetBool( p_playlist, "playlist-autostart" );
68 bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
69 p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
72 for( int i = 0; i < p_parent->i_children; i++ )
74 if( p_parent->pp_children[i] == p_item )
82 playlist_item_t *p_up = p_item;
83 while( p_up->p_parent )
85 if( p_up->p_parent == p_playlist->p_playing )
87 if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
90 p_up = p_up->p_parent;
94 playlist_DeleteItem( p_playlist, p_item, true );
95 p_item = playlist_InsertInputItemTree( p_playlist, p_parent,
96 p_new_root, pos, true );
99 p_item = playlist_InsertInputItemTree( p_playlist, p_item,
100 p_new_root, PLAYLIST_END, false );
102 if( !b_flat ) var_SetAddress( p_playlist, "leaf-to-parent", p_input );
106 if( ( b_stop && !b_flat ) || !b_autostart )
109 playlist_Stop( p_playlist );
114 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
115 pl_Locked, get_current_status_node( p_playlist ), p_item );
121 /*****************************************************************************
122 * An input item's meta or duration has changed (Event Callback)
123 *****************************************************************************/
124 static void input_item_changed( const vlc_event_t * p_event,
127 playlist_item_t *p_item = user_data;
128 VLC_UNUSED( p_event );
129 var_SetAddress( p_item->p_playlist, "item-change", p_item->p_input );
132 /*****************************************************************************
133 * Listen to vlc_InputItemAddSubItem event
134 *****************************************************************************/
135 static void install_input_item_observer( playlist_item_t * p_item )
137 vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
138 vlc_event_attach( p_em, vlc_InputItemSubItemTreeAdded,
139 input_item_add_subitem_tree, p_item );
140 vlc_event_attach( p_em, vlc_InputItemDurationChanged,
141 input_item_changed, p_item );
142 vlc_event_attach( p_em, vlc_InputItemMetaChanged,
143 input_item_changed, p_item );
144 vlc_event_attach( p_em, vlc_InputItemNameChanged,
145 input_item_changed, p_item );
146 vlc_event_attach( p_em, vlc_InputItemInfoChanged,
147 input_item_changed, p_item );
148 vlc_event_attach( p_em, vlc_InputItemErrorWhenReadingChanged,
149 input_item_changed, p_item );
152 static void uninstall_input_item_observer( playlist_item_t * p_item )
154 vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
155 vlc_event_detach( p_em, vlc_InputItemSubItemTreeAdded,
156 input_item_add_subitem_tree, p_item );
157 vlc_event_detach( p_em, vlc_InputItemMetaChanged,
158 input_item_changed, p_item );
159 vlc_event_detach( p_em, vlc_InputItemDurationChanged,
160 input_item_changed, p_item );
161 vlc_event_detach( p_em, vlc_InputItemNameChanged,
162 input_item_changed, p_item );
163 vlc_event_detach( p_em, vlc_InputItemInfoChanged,
164 input_item_changed, p_item );
165 vlc_event_detach( p_em, vlc_InputItemErrorWhenReadingChanged,
166 input_item_changed, p_item );
169 /*****************************************************************************
170 * Playlist item creation
171 *****************************************************************************/
172 playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
173 input_item_t *p_input )
175 playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
181 p_item->p_input = p_input;
182 vlc_gc_incref( p_item->p_input );
184 p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
186 p_item->p_parent = NULL;
187 p_item->i_children = -1;
188 p_item->pp_children = NULL;
190 p_item->p_playlist = p_playlist;
192 install_input_item_observer( p_item );
197 /***************************************************************************
198 * Playlist item destruction
199 ***************************************************************************/
204 * \param p_item item to delete
205 * \return VLC_SUCCESS
207 int playlist_ItemRelease( playlist_item_t *p_item )
210 playlist_t *p_playlist = p_item->p_playlist;
213 /* Surprise, we can't actually do more because we
214 * don't do refcounting, or eauivalent.
215 * Because item are not only accessed by their id
216 * using playlist_item outside the PL_LOCK isn't safe.
217 * Most of the modules does that.
219 * Who wants to add proper memory management? */
220 uninstall_input_item_observer( p_item );
221 ARRAY_APPEND( pl_priv(p_playlist)->items_to_delete, p_item);
228 * Remove an input item when it appears from a root playlist item
229 * \param p_playlist playlist object
230 * \param p_input the input to delete
231 * \param p_root root playlist item
232 * \param b_do_stop must stop or not the playlist
233 * \return VLC_SUCCESS or VLC_EGENERIC
235 static int DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
236 playlist_item_t *p_root, bool b_do_stop )
239 playlist_item_t *p_item = playlist_ItemFindFromInputAndRoot(
240 p_playlist, p_input, p_root, false );
241 if( !p_item ) return VLC_EGENERIC;
242 return playlist_DeleteItem( p_playlist, p_item, b_do_stop );
248 * Remove an input item when it appears from a root playlist item
249 * \param p_playlist playlist object
250 * \param p_input the input to delete
251 * \param p_root root playlist item
252 * \param b_locked TRUE if the playlist is locked
253 * \return VLC_SUCCESS or VLC_EGENERIC
255 int playlist_DeleteFromInputInParent( playlist_t *p_playlist,
256 input_item_t *p_item,
257 playlist_item_t *p_root, bool b_locked )
260 PL_LOCK_IF( !b_locked );
261 i_ret = DeleteFromInput( p_playlist, p_item, p_root, true );
262 PL_UNLOCK_IF( !b_locked );
269 * Search anywhere in playlist for an an input item and delete it
270 * \param p_playlist playlist object
271 * \param p_input the input to delete
272 * \param b_locked TRUE if the playlist is locked
273 * \return VLC_SUCCESS or VLC_ENOITEM
275 int playlist_DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
279 PL_LOCK_IF( !b_locked );
280 i_ret = DeleteFromInput( p_playlist, p_input,
281 p_playlist->p_root, true );
282 PL_UNLOCK_IF( !b_locked );
283 return ( i_ret == VLC_SUCCESS ? VLC_SUCCESS : VLC_ENOITEM );
289 * \param p_playlist playlist object
290 * \param b_locked TRUE if the playlist is locked
293 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
295 PL_LOCK_IF( !b_locked );
296 playlist_NodeEmpty( p_playlist, p_playlist->p_playing, true );
297 PL_UNLOCK_IF( !b_locked );
301 * Delete playlist item
303 * Remove a playlist item from the playlist, given its id
304 * This function is to be used only by the playlist
305 * \param p_playlist playlist object
306 * \param i_id id of the item do delete
307 * \return VLC_SUCCESS or an error
309 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
312 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
313 if( !p_item ) return VLC_EGENERIC;
314 return playlist_DeleteItem( p_playlist, p_item, true );
317 /***************************************************************************
318 * Playlist item addition
319 ***************************************************************************/
323 * Add an item to the playlist or the media library
324 * \param p_playlist the playlist to add into
325 * \param psz_uri the mrl to add to the playlist
326 * \param psz_name a text giving a name or description of this item
327 * \param i_mode the mode used when adding
328 * \param i_pos the position in the playlist where to add. If this is
329 * PLAYLIST_END the item will be added at the end of the playlist
330 * regardless of its size
331 * \param b_playlist TRUE for playlist, FALSE for media library
332 * \param b_locked TRUE if the playlist is locked
333 * \return VLC_SUCCESS or a VLC error code
335 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
336 const char *psz_name, int i_mode, int i_pos,
337 bool b_playlist, bool b_locked )
339 return playlist_AddExt( p_playlist, psz_uri, psz_name,
340 i_mode, i_pos, -1, 0, NULL, 0, b_playlist, b_locked );
344 * Add a MRL into the playlist or the media library, duration and options given
346 * \param p_playlist the playlist to add into
347 * \param psz_uri the mrl to add to the playlist
348 * \param psz_name a text giving a name or description of this item
349 * \param i_mode the mode used when adding
350 * \param i_pos the position in the playlist where to add. If this is
351 * PLAYLIST_END the item will be added at the end of the playlist
352 * regardless of its size
353 * \param i_duration length of the item in milliseconds.
354 * \param i_options the number of options
355 * \param ppsz_options an array of options
356 * \param i_option_flags options flags
357 * \param b_playlist TRUE for playlist, FALSE for media library
358 * \param b_locked TRUE if the playlist is locked
359 * \return VLC_SUCCESS or a VLC error code
361 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
362 const char *psz_name, int i_mode, int i_pos,
364 int i_options, const char *const *ppsz_options,
365 unsigned i_option_flags,
366 bool b_playlist, bool b_locked )
369 input_item_t *p_input;
371 p_input = input_item_NewExt( p_playlist, psz_uri, psz_name,
372 i_options, ppsz_options, i_option_flags,
374 if( p_input == NULL )
376 i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
378 vlc_gc_decref( p_input );
383 * Add an input item to the playlist node
385 * \param p_playlist the playlist to add into
386 * \param p_input the input item to add
387 * \param i_mode the mode used when adding
388 * \param i_pos the position in the playlist where to add. If this is
389 * PLAYLIST_END the item will be added at the end of the playlist
390 * regardless of its size
391 * \param b_playlist TRUE for playlist, FALSE for media library
392 * \param b_locked TRUE if the playlist is locked
393 * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
395 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
396 int i_mode, int i_pos, bool b_playlist,
399 playlist_item_t *p_item;
400 if( p_playlist->b_die ) return VLC_EGENERIC;
401 if( !pl_priv(p_playlist)->b_doing_ml )
402 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
405 PL_LOCK_IF( !b_locked );
407 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
408 if( p_item == NULL ) return VLC_ENOMEM;
409 AddItem( p_playlist, p_item,
410 b_playlist ? p_playlist->p_playing :
411 p_playlist->p_media_library , i_mode, i_pos );
413 GoAndPreparse( p_playlist, i_mode, p_item );
415 PL_UNLOCK_IF( !b_locked );
420 * Add an input item to a given node
422 * \param p_playlist the playlist to add into
423 * \param p_input the input item to add
424 * \param p_parent the parent item to add into
425 * \param i_mode the mode used when addin
426 * \param i_pos the position in the playlist where to add. If this is
427 * PLAYLIST_END the item will be added at the end of the playlist
428 * regardless of its size
429 * \param b_locked TRUE if the playlist is locked
430 * \return the new playlist item
432 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
433 input_item_t *p_input,
434 playlist_item_t *p_parent,
435 int i_mode, int i_pos,
438 playlist_item_t *p_item;
440 assert( p_parent && p_parent->i_children != -1 );
442 if( p_playlist->b_die )
444 PL_LOCK_IF( !b_locked );
446 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
447 if( p_item == NULL ) return NULL;
448 AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
450 GoAndPreparse( p_playlist, i_mode, p_item );
452 PL_UNLOCK_IF( !b_locked );
458 * Insert a tree of input items into a given playlist node
460 * \param p_playlist the playlist to insert into
461 * \param p_parent the receiving playlist node (can be an item)
462 * \param p_node the root of input item tree,
463 only it's contents will be inserted
464 * \param i_pos the position in the playlist where to insert. If this is
465 * PLAYLIST_END the items will be added at the end of the playlist
466 * regardless of its size
467 * \param b_flat TRUE if the new tree contents should be flattened into a list
468 * \return the first new leaf inserted (in playing order)
470 playlist_item_t *playlist_InsertInputItemTree (
471 playlist_t *p_playlist, playlist_item_t *p_parent,
472 input_item_node_t *p_node, int i_pos, bool b_flat )
474 playlist_item_t *p_first_leaf = NULL;
475 RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
479 /*****************************************************************************
480 * Playlist item misc operations
481 *****************************************************************************/
486 * Transform an item to a node. Return the node in the category tree, or NULL
488 * This function must be entered without the playlist lock
489 * \param p_playlist the playlist object
490 * \param p_item the item to transform
491 * \param b_locked TRUE if the playlist is locked
492 * \return the item transform in a node
494 static playlist_item_t *ItemToNode( playlist_t *p_playlist,
495 playlist_item_t *p_item,
498 PL_LOCK_IF( !b_locked );
500 assert( p_item->p_parent );
503 playlist_item_t *p_up = p_item;
504 while( p_up->p_parent )
506 if( p_up->p_parent == p_playlist->p_playing ||
507 p_up->p_parent == p_playlist->p_media_library )
509 if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
512 p_up = p_up->p_parent;
517 ChangeToNode( p_playlist, p_item );
518 if( p_up == p_playlist->p_root )
519 var_SetAddress( p_playlist, "item-change", p_item->p_input );
520 PL_UNLOCK_IF( !b_locked );
525 playlist_item_t *p_status_item = get_current_status_item( p_playlist );
526 playlist_item_t *p_status_node = get_current_status_node( p_playlist );
527 if( p_item == p_status_item )
529 /* We're deleting the current playlist item. Update
530 * the playlist object to point at the previous item
531 * so the playlist won't be restarted */
532 playlist_item_t *p_prev_status_item = NULL;
534 while( i < p_status_node->i_children &&
535 p_status_node->pp_children[i] != p_status_item )
537 p_prev_status_item = p_status_node->pp_children[i];
540 if( i == p_status_node->i_children )
541 p_prev_status_item = NULL;
542 if( p_prev_status_item )
543 set_current_status_item( p_playlist, p_prev_status_item );
546 DeleteFromInput( p_playlist, p_item->p_input,
547 p_playlist->p_root, false );
549 pl_priv(p_playlist)->b_reset_currently_playing = true;
550 vlc_cond_signal( &pl_priv(p_playlist)->signal );
552 PL_UNLOCK_IF( !b_locked );
553 return p_item->p_parent;
558 * Find an item within a root, given its input id.
560 * \param p_playlist the playlist object
561 * \param p_item the input item
562 * \param p_root root playlist item
563 * \param b_items_only TRUE if we want the item himself
564 * \return the first found item, or NULL if not found
566 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
567 input_item_t *p_item,
568 playlist_item_t *p_root,
572 for( i = 0 ; i< p_root->i_children ; i++ )
574 if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
575 p_root->pp_children[i]->p_input == p_item )
577 return p_root->pp_children[i];
579 else if( p_root->pp_children[i]->i_children >= 0 )
581 playlist_item_t *p_search =
582 playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
583 p_root->pp_children[i],
585 if( p_search ) return p_search;
592 static int ItemIndex ( playlist_item_t *p_item )
594 for( int i = 0; i < p_item->p_parent->i_children; i++ )
595 if( p_item->p_parent->pp_children[i] == p_item ) return i;
602 * This function must be entered with the playlist lock
604 * \param p_playlist the playlist
605 * \param p_item the item to move
606 * \param p_node the new parent of the item
607 * \param i_newpos the new position under this new parent
608 * \return VLC_SUCCESS or an error
610 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
611 playlist_item_t *p_node, int i_newpos )
615 if( p_node->i_children == -1 ) return VLC_EGENERIC;
617 playlist_item_t *p_detach = p_item->p_parent;
618 int i_index = ItemIndex( p_item );
620 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );
622 if( p_detach == p_node && i_index < i_newpos )
625 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
626 p_item->p_parent = p_node;
628 pl_priv( p_playlist )->b_reset_currently_playing = true;
629 vlc_cond_signal( &pl_priv( p_playlist )->signal );
634 * Moves an array of items
636 * This function must be entered with the playlist lock
638 * \param p_playlist the playlist
639 * \param i_items the number of indexes to move
640 * \param pp_items the array of indexes to move
641 * \param p_node the target node
642 * \param i_newpos the target position under this node
643 * \return VLC_SUCCESS or an error
645 int playlist_TreeMoveMany( playlist_t *p_playlist,
646 int i_items, playlist_item_t **pp_items,
647 playlist_item_t *p_node, int i_newpos )
651 if ( p_node->i_children == -1 ) return VLC_EGENERIC;
654 for( i = 0; i < i_items; i++ )
656 playlist_item_t *p_item = pp_items[i];
657 int i_index = ItemIndex( p_item );
658 playlist_item_t *p_parent = p_item->p_parent;
659 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i_index );
660 if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
662 for( i = i_items - 1; i >= 0; i-- )
664 playlist_item_t *p_item = pp_items[i];
665 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
666 p_item->p_parent = p_node;
669 pl_priv( p_playlist )->b_reset_currently_playing = true;
670 vlc_cond_signal( &pl_priv( p_playlist )->signal );
675 * Send a notification that an item has been added to a node
677 * \param p_playlist the playlist object
678 * \param i_item_id id of the item added
679 * \param i_node_id id of the node in wich the item was added
680 * \param b_signal TRUE if the function must send a signal
683 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
684 int i_node_id, bool b_signal )
686 playlist_private_t *p_sys = pl_priv(p_playlist);
689 p_sys->b_reset_currently_playing = true;
691 vlc_cond_signal( &p_sys->signal );
694 add.i_item = i_item_id;
695 add.i_node = i_node_id;
698 val.p_address = &add;
700 var_Set( p_playlist, "playlist-item-append", val );
703 /***************************************************************************
704 * The following functions are local
705 ***************************************************************************/
707 /* Enqueue an item for preparsing, and play it, if needed */
708 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
709 playlist_item_t *p_item )
712 if( (i_mode & PLAYLIST_GO ) )
714 pl_priv(p_playlist)->request.b_request = true;
715 pl_priv(p_playlist)->request.i_skip = 0;
716 pl_priv(p_playlist)->request.p_item = p_item;
717 if( pl_priv(p_playlist)->p_input )
718 input_Stop( pl_priv(p_playlist)->p_input, true );
719 pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
720 vlc_cond_signal( &pl_priv(p_playlist)->signal );
722 /* Preparse if no artist/album info, and hasn't been preparsed allready
723 and if user has some preparsing option (auto-preparse variable)
725 char *psz_artist = input_item_GetArtist( p_item->p_input );
726 char *psz_album = input_item_GetAlbum( p_item->p_input );
727 if( pl_priv(p_playlist)->b_auto_preparse &&
728 input_item_IsPreparsed( p_item->p_input ) == false &&
729 ( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
731 playlist_PreparseEnqueue( p_playlist, p_item->p_input );
736 /* Add the playlist item to the requested node and fire a notification */
737 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
738 playlist_item_t *p_node, int i_mode, int i_pos )
741 ARRAY_APPEND(p_playlist->items, p_item);
742 ARRAY_APPEND(p_playlist->all_items, p_item);
744 if( i_pos == PLAYLIST_END )
745 playlist_NodeAppend( p_playlist, p_item, p_node );
747 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
749 if( !pl_priv(p_playlist)->b_doing_ml )
750 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
751 !( i_mode & PLAYLIST_NO_REBUILD ) );
754 /* Actually convert an item to a node */
755 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
758 if( p_item->i_children != -1 ) return;
760 p_item->i_children = 0;
762 input_item_t *p_input = p_item->p_input;
763 vlc_mutex_lock( &p_input->lock );
764 p_input->i_type = ITEM_TYPE_NODE;
765 vlc_mutex_unlock( &p_input->lock );
767 var_SetAddress( p_playlist, "item-change", p_item->p_input );
769 /* Remove it from the array of available items */
770 ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
772 ARRAY_REMOVE( p_playlist->items, i );
775 /* Do the actual removal */
776 int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
780 int i_id = p_item->i_id;
783 if( p_item->i_children > -1 )
785 return playlist_NodeDelete( p_playlist, p_item, true, false );
788 pl_priv(p_playlist)->b_reset_currently_playing = true;
789 var_SetInteger( p_playlist, "playlist-item-deleted", i_id );
791 /* Remove the item from the bank */
792 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
794 ARRAY_REMOVE( p_playlist->all_items, i );
796 ARRAY_BSEARCH( p_playlist->items,->i_id, int, i_id, i );
798 ARRAY_REMOVE( p_playlist->items, i );
800 /* Check if it is the current item */
801 if( get_current_status_item( p_playlist ) == p_item )
803 /* Stop it if we have to */
806 playlist_Control( p_playlist, PLAYLIST_STOP, pl_Locked );
807 msg_Info( p_playlist, "stopping playback" );
809 /* In any case, this item can't be the next one to be played ! */
810 set_current_status_item( p_playlist, NULL );
813 ARRAY_BSEARCH( p_playlist->current,->i_id, int, i_id, i );
815 ARRAY_REMOVE( p_playlist->current, i );
817 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
819 /* Remove the item from its parent */
820 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
822 playlist_ItemRelease( p_item );
827 static int RecursiveAddIntoParent (
828 playlist_t *p_playlist, playlist_item_t *p_parent,
829 input_item_node_t *p_node, int i_pos, bool b_flat,
830 playlist_item_t **pp_first_leaf )
832 if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
834 if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
836 for( int i = 0; i < p_node->i_children; i++ )
838 playlist_item_t *p_child = NULL;
839 if( b_flat ? p_node->pp_children[i]->i_children == 0 : 1 )
841 p_child = playlist_NodeAddInput( p_playlist,
842 p_node->pp_children[i]->p_item,
844 PLAYLIST_INSERT, i_pos,
848 if( p_node->pp_children[i]->i_children > 0 )
852 i_pos = RecursiveAddIntoParent(
853 p_playlist, p_parent,
854 p_node->pp_children[i], i_pos, true,
859 RecursiveAddIntoParent( p_playlist, p_child,
860 p_node->pp_children[i], 0, false,
864 assert( p_child != NULL );
865 if( i == 0 ) *pp_first_leaf = p_child;