]> git.sesse.net Git - vlc/blob - src/playlist/item.c
playlist: only make The Playlist flat, not Media Library
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item creation/deletion/add/removal functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <assert.h>
30 #include <vlc_playlist.h>
31 #include "playlist_internal.h"
32
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,
36                            playlist_item_t * );
37 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
38
39 static playlist_item_t *ItemToNode( playlist_t *, playlist_item_t *, bool );
40
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 );
45
46 /*****************************************************************************
47  * An input item has gained subitems (Event Callback)
48  *****************************************************************************/
49
50 static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
51                                           void * user_data )
52 {
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;
56
57     PL_LOCK;
58
59     playlist_item_t *p_item =
60         playlist_ItemGetByInput( p_playlist, p_input );
61
62     assert( p_item != NULL );
63     playlist_item_t *p_parent = p_item->p_parent;
64     assert( p_parent != NULL );
65
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;
70
71     int pos = 0;
72     for( int i = 0; i < p_parent->i_children; i++ )
73     {
74         if( p_parent->pp_children[i] == p_item )
75         {
76             pos = i;
77             break;
78         }
79     }
80
81     bool b_flat = false;
82     playlist_item_t *p_up = p_item;
83     while( p_up->p_parent )
84     {
85         if( p_up->p_parent == p_playlist->p_playing )
86         {
87             if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
88             break;
89         }
90         p_up = p_up->p_parent;
91     }
92     if( b_flat )
93     {
94         playlist_DeleteItem( p_playlist, p_item, true );
95         p_item = playlist_InsertInputItemTree( p_playlist, p_parent,
96                                                p_new_root, pos, true );
97     }
98     else
99         p_item = playlist_InsertInputItemTree( p_playlist, p_item,
100                                                p_new_root, PLAYLIST_END, false );
101
102     if( !b_flat ) var_SetAddress( p_playlist, "leaf-to-parent", p_input );
103
104     if( b_current )
105     {
106         if( ( b_stop && !b_flat ) || !b_autostart )
107         {
108             PL_UNLOCK;
109             playlist_Stop( p_playlist );
110             return;
111         }
112         else
113         {
114             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
115                 pl_Locked, get_current_status_node( p_playlist ), p_item );
116         }
117     }
118
119     PL_UNLOCK;
120 }
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,
125                                 void * user_data )
126 {
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 );
130 }
131
132 /*****************************************************************************
133  * Listen to vlc_InputItemAddSubItem event
134  *****************************************************************************/
135 static void install_input_item_observer( playlist_item_t * p_item )
136 {
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 );
150 }
151
152 static void uninstall_input_item_observer( playlist_item_t * p_item )
153 {
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 );
167 }
168
169 /*****************************************************************************
170  * Playlist item creation
171  *****************************************************************************/
172 playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
173                                               input_item_t *p_input )
174 {
175     playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
176     if( !p_item )
177         return NULL;
178
179     assert( p_input );
180
181     p_item->p_input = p_input;
182     vlc_gc_incref( p_item->p_input );
183
184     p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
185
186     p_item->p_parent = NULL;
187     p_item->i_children = -1;
188     p_item->pp_children = NULL;
189     p_item->i_flags = 0;
190     p_item->p_playlist = p_playlist;
191
192     install_input_item_observer( p_item );
193
194     return p_item;
195 }
196
197 /***************************************************************************
198  * Playlist item destruction
199  ***************************************************************************/
200
201 /**
202  * Release an item
203  *
204  * \param p_item item to delete
205  * \return VLC_SUCCESS
206 */
207 int playlist_ItemRelease( playlist_item_t *p_item )
208 {
209     /* For the assert */
210     playlist_t *p_playlist = p_item->p_playlist;
211     PL_ASSERT_LOCKED;
212
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.
218      *
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);
222     return VLC_SUCCESS;
223 }
224
225 /**
226  * Delete input item
227  *
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
234 */
235 static int DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
236                             playlist_item_t *p_root, bool b_do_stop )
237 {
238     PL_ASSERT_LOCKED;
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 );
243 }
244
245 /**
246  * Delete input item
247  *
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
254  */
255 int playlist_DeleteFromInputInParent( playlist_t *p_playlist,
256                                       input_item_t *p_item,
257                                       playlist_item_t *p_root, bool b_locked )
258 {
259     int i_ret;
260     PL_LOCK_IF( !b_locked );
261     i_ret = DeleteFromInput( p_playlist, p_item, p_root, true );
262     PL_UNLOCK_IF( !b_locked );
263     return i_ret;
264 }
265
266 /**
267  * Delete from input
268  *
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
274  */
275 int playlist_DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
276                               bool b_locked )
277 {
278     int i_ret;
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 );
284 }
285
286 /**
287  * Clear the playlist
288  *
289  * \param p_playlist playlist object
290  * \param b_locked TRUE if the playlist is locked
291  * \return nothing
292  */
293 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
294 {
295     PL_LOCK_IF( !b_locked );
296     playlist_NodeEmpty( p_playlist, p_playlist->p_playing, true );
297     PL_UNLOCK_IF( !b_locked );
298 }
299
300 /**
301  * Delete playlist item
302  *
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
308  */
309 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
310 {
311     PL_ASSERT_LOCKED;
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 );
315 }
316
317 /***************************************************************************
318  * Playlist item addition
319  ***************************************************************************/
320 /**
321  * Playlist add
322  *
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
334  */
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 )
338 {
339     return playlist_AddExt( p_playlist, psz_uri, psz_name,
340                             i_mode, i_pos, -1, 0, NULL, 0, b_playlist, b_locked );
341 }
342
343 /**
344  * Add a MRL into the playlist or the media library, duration and options given
345  *
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
360 */
361 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
362                      const char *psz_name, int i_mode, int i_pos,
363                      mtime_t i_duration,
364                      int i_options, const char *const *ppsz_options,
365                      unsigned i_option_flags,
366                      bool b_playlist, bool b_locked )
367 {
368     int i_ret;
369     input_item_t *p_input;
370
371     p_input = input_item_NewExt( p_playlist, psz_uri, psz_name,
372                                  i_options, ppsz_options, i_option_flags,
373                                  i_duration );
374     if( p_input == NULL )
375         return VLC_ENOMEM;
376     i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
377                                b_locked );
378     vlc_gc_decref( p_input );
379     return i_ret;
380 }
381
382 /**
383  * Add an input item to the playlist node
384  *
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
394 */
395 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
396                        int i_mode, int i_pos, bool b_playlist,
397                        bool b_locked )
398 {
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,
403                                              p_input->psz_uri );
404
405     PL_LOCK_IF( !b_locked );
406
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 );
412
413     GoAndPreparse( p_playlist, i_mode, p_item );
414
415     PL_UNLOCK_IF( !b_locked );
416     return VLC_SUCCESS;
417 }
418
419 /**
420  * Add an input item to a given node
421  *
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
431  */
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,
436                                          bool b_locked )
437 {
438     playlist_item_t *p_item;
439     assert( p_input );
440     assert( p_parent && p_parent->i_children != -1 );
441
442     if( p_playlist->b_die )
443         return NULL;
444     PL_LOCK_IF( !b_locked );
445
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 );
449
450     GoAndPreparse( p_playlist, i_mode, p_item );
451
452     PL_UNLOCK_IF( !b_locked );
453
454     return p_item;
455 }
456
457 /**
458  * Insert a tree of input items into a given playlist node
459  *
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)
469  */
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 )
473 {
474   playlist_item_t *p_first_leaf = NULL;
475   RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
476   return p_first_leaf;
477 }
478
479 /*****************************************************************************
480  * Playlist item misc operations
481  *****************************************************************************/
482
483 /**
484  * Item to node
485  *
486  * Transform an item to a node. Return the node in the category tree, or NULL
487  * if not found there
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
493  */
494 static playlist_item_t *ItemToNode( playlist_t *p_playlist,
495                                     playlist_item_t *p_item,
496                                     bool b_locked )
497 {
498     PL_LOCK_IF( !b_locked );
499
500     assert( p_item->p_parent );
501
502     bool b_flat = false;
503     playlist_item_t *p_up = p_item;
504     while( p_up->p_parent )
505     {
506         if( p_up->p_parent == p_playlist->p_playing ||
507             p_up->p_parent == p_playlist->p_media_library )
508         {
509             if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
510             break;
511         }
512         p_up = p_up->p_parent;
513     }
514
515     if( !b_flat )
516     {
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 );
521         return p_item;
522     }
523     else
524     {
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 )
528         {
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;
533             int i = 0;
534             while( i < p_status_node->i_children &&
535                     p_status_node->pp_children[i] != p_status_item )
536             {
537                 p_prev_status_item = p_status_node->pp_children[i];
538                 i++;
539             }
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 );
544         }
545
546         DeleteFromInput( p_playlist, p_item->p_input,
547                           p_playlist->p_root, false );
548
549         pl_priv(p_playlist)->b_reset_currently_playing = true;
550         vlc_cond_signal( &pl_priv(p_playlist)->signal );
551
552         PL_UNLOCK_IF( !b_locked );
553         return p_item->p_parent;
554     }
555 }
556
557 /**
558  * Find an item within a root, given its input id.
559  *
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
565  */
566 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
567                                                     input_item_t *p_item,
568                                                     playlist_item_t *p_root,
569                                                     bool b_items_only )
570 {
571     int i;
572     for( i = 0 ; i< p_root->i_children ; i++ )
573     {
574         if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
575             p_root->pp_children[i]->p_input == p_item )
576         {
577             return p_root->pp_children[i];
578         }
579         else if( p_root->pp_children[i]->i_children >= 0 )
580         {
581             playlist_item_t *p_search =
582                  playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
583                                                     p_root->pp_children[i],
584                                                     b_items_only );
585             if( p_search ) return p_search;
586         }
587     }
588     return NULL;
589 }
590
591
592 static int ItemIndex ( playlist_item_t *p_item )
593 {
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;
596     return -1;
597 }
598
599 /**
600  * Moves an item
601  *
602  * This function must be entered with the playlist lock
603  *
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
609  */
610 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
611                        playlist_item_t *p_node, int i_newpos )
612 {
613     PL_ASSERT_LOCKED;
614
615     if( p_node->i_children == -1 ) return VLC_EGENERIC;
616
617     playlist_item_t *p_detach = p_item->p_parent;
618     int i_index = ItemIndex( p_item );
619
620     REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );
621
622     if( p_detach == p_node && i_index < i_newpos )
623         i_newpos--;
624
625     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
626     p_item->p_parent = p_node;
627
628     pl_priv( p_playlist )->b_reset_currently_playing = true;
629     vlc_cond_signal( &pl_priv( p_playlist )->signal );
630     return VLC_SUCCESS;
631 }
632
633 /**
634  * Moves an array of items
635  *
636  * This function must be entered with the playlist lock
637  *
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
644  */
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 )
648 {
649     PL_ASSERT_LOCKED;
650
651     if ( p_node->i_children == -1 ) return VLC_EGENERIC;
652
653     int i;
654     for( i = 0; i < i_items; i++ )
655     {
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--;
661     }
662     for( i = i_items - 1; i >= 0; i-- )
663     {
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;
667     }
668
669     pl_priv( p_playlist )->b_reset_currently_playing = true;
670     vlc_cond_signal( &pl_priv( p_playlist )->signal );
671     return VLC_SUCCESS;
672 }
673
674 /**
675  * Send a notification that an item has been added to a node
676  *
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
681  * \return nothing
682  */
683 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
684                              int i_node_id, bool b_signal )
685 {
686     playlist_private_t *p_sys = pl_priv(p_playlist);
687     PL_ASSERT_LOCKED;
688
689     p_sys->b_reset_currently_playing = true;
690     if( b_signal )
691         vlc_cond_signal( &p_sys->signal );
692
693     playlist_add_t add;
694     add.i_item = i_item_id;
695     add.i_node = i_node_id;
696
697     vlc_value_t val;
698     val.p_address = &add;
699
700     var_Set( p_playlist, "playlist-item-append", val );
701 }
702
703 /***************************************************************************
704  * The following functions are local
705  ***************************************************************************/
706
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 )
710 {
711     PL_ASSERT_LOCKED;
712     if( (i_mode & PLAYLIST_GO ) )
713     {
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 );
721     }
722     /* Preparse if no artist/album info, and hasn't been preparsed allready
723        and if user has some preparsing option (auto-preparse variable)
724        enabled*/
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 ) ) )
730           )
731         playlist_PreparseEnqueue( p_playlist, p_item->p_input );
732     free( psz_artist );
733     free( psz_album );
734 }
735
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 )
739 {
740     PL_ASSERT_LOCKED;
741     ARRAY_APPEND(p_playlist->items, p_item);
742     ARRAY_APPEND(p_playlist->all_items, p_item);
743
744     if( i_pos == PLAYLIST_END )
745         playlist_NodeAppend( p_playlist, p_item, p_node );
746     else
747         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
748
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 ) );
752 }
753
754 /* Actually convert an item to a node */
755 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
756 {
757     int i;
758     if( p_item->i_children == -1 )
759         p_item->i_children = 0;
760
761     /* Remove it from the array of available items */
762     ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
763     if( i != -1 )
764         ARRAY_REMOVE( p_playlist->items, i );
765 }
766
767 /* Do the actual removal */
768 int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
769                         bool b_stop )
770 {
771     int i;
772     int i_id = p_item->i_id;
773     PL_ASSERT_LOCKED;
774
775     if( p_item->i_children > -1 )
776     {
777         return playlist_NodeDelete( p_playlist, p_item, true, false );
778     }
779
780     pl_priv(p_playlist)->b_reset_currently_playing = true;
781     var_SetInteger( p_playlist, "playlist-item-deleted", i_id );
782
783     /* Remove the item from the bank */
784     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
785     if( i != -1 )
786         ARRAY_REMOVE( p_playlist->all_items, i );
787
788     ARRAY_BSEARCH( p_playlist->items,->i_id, int, i_id, i );
789     if( i != -1 )
790         ARRAY_REMOVE( p_playlist->items, i );
791
792     /* Check if it is the current item */
793     if( get_current_status_item( p_playlist ) == p_item )
794     {
795         /* Stop it if we have to */
796         if( b_stop )
797         {
798             playlist_Control( p_playlist, PLAYLIST_STOP, pl_Locked );
799             msg_Info( p_playlist, "stopping playback" );
800         }
801         /* In any case, this item can't be the next one to be played ! */
802         set_current_status_item( p_playlist, NULL );
803     }
804
805     ARRAY_BSEARCH( p_playlist->current,->i_id, int, i_id, i );
806     if( i != -1 )
807         ARRAY_REMOVE( p_playlist->current, i );
808
809     PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
810
811     /* Remove the item from its parent */
812     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
813
814     playlist_ItemRelease( p_item );
815
816     return VLC_SUCCESS;
817 }
818
819 static int RecursiveAddIntoParent (
820     playlist_t *p_playlist, playlist_item_t *p_parent,
821     input_item_node_t *p_node, int i_pos, bool b_flat,
822     playlist_item_t **pp_first_leaf )
823 {
824   if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
825
826   if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
827
828   for( int i = 0; i < p_node->i_children; i++ )
829   {
830       playlist_item_t *p_child = NULL;
831       if( b_flat ? p_node->pp_children[i]->i_children == 0 : 1 )
832       {
833           p_child = playlist_NodeAddInput( p_playlist,
834                                  p_node->pp_children[i]->p_item,
835                                  p_parent,
836                                  PLAYLIST_INSERT, i_pos,
837                                  pl_Locked );
838           i_pos++;
839       }
840       if( p_node->pp_children[i]->i_children > 0 )
841       {
842           if( b_flat )
843           {
844               i_pos = RecursiveAddIntoParent(
845                                       p_playlist, p_parent,
846                                       p_node->pp_children[i], i_pos, true,
847                                       &p_child );
848           }
849           else
850           {
851               RecursiveAddIntoParent( p_playlist, p_child,
852                                       p_node->pp_children[i], 0, false,
853                                       &p_child );
854           }
855       }
856       assert( p_child != NULL );
857       if( i == 0 ) *pp_first_leaf = p_child;
858   }
859   return i_pos;
860 }