]> git.sesse.net Git - vlc/blob - src/playlist/item.c
playlist: restore stop and browse behavior like before
[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_play = var_CreateGetBool( p_playlist, "playlist-autostart" ) &&
67                   get_current_status_item( p_playlist ) == p_item;
68     bool b_stop = b_play && 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             p_up->p_parent == p_playlist->p_media_library )
87         {
88             if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
89             break;
90         }
91         p_up = p_up->p_parent;
92     }
93     if( b_flat )
94     {
95         playlist_DeleteItem( p_playlist, p_item, true );
96         p_item = playlist_InsertInputItemTree( p_playlist, p_parent,
97                                                p_new_root, pos, true );
98     }
99     else
100         p_item = playlist_InsertInputItemTree( p_playlist, p_item,
101                                                p_new_root, 0, false );
102
103     if( !b_flat ) var_SetAddress( p_playlist, "leaf-to-parent", p_input );
104
105     if( b_stop && !b_flat )
106     {
107         PL_UNLOCK;
108         playlist_Stop( p_playlist );
109         return;
110     }
111     else if( b_play )
112     {
113         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
114                           pl_Locked, get_current_status_node( p_playlist ), p_item );
115     }
116
117     PL_UNLOCK;
118 }
119 /*****************************************************************************
120  * An input item's meta or duration has changed (Event Callback)
121  *****************************************************************************/
122 static void input_item_changed( const vlc_event_t * p_event,
123                                 void * user_data )
124 {
125     playlist_item_t *p_item = user_data;
126     VLC_UNUSED( p_event );
127     var_SetAddress( p_item->p_playlist, "item-change", p_item->p_input );
128 }
129
130 /*****************************************************************************
131  * Listen to vlc_InputItemAddSubItem event
132  *****************************************************************************/
133 static void install_input_item_observer( playlist_item_t * p_item )
134 {
135     vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
136     vlc_event_attach( p_em, vlc_InputItemSubItemTreeAdded,
137                       input_item_add_subitem_tree, p_item );
138     vlc_event_attach( p_em, vlc_InputItemDurationChanged,
139                       input_item_changed, p_item );
140     vlc_event_attach( p_em, vlc_InputItemMetaChanged,
141                       input_item_changed, p_item );
142     vlc_event_attach( p_em, vlc_InputItemNameChanged,
143                       input_item_changed, p_item );
144     vlc_event_attach( p_em, vlc_InputItemInfoChanged,
145                       input_item_changed, p_item );
146     vlc_event_attach( p_em, vlc_InputItemErrorWhenReadingChanged,
147                       input_item_changed, p_item );
148 }
149
150 static void uninstall_input_item_observer( playlist_item_t * p_item )
151 {
152     vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
153     vlc_event_detach( p_em, vlc_InputItemSubItemTreeAdded,
154                       input_item_add_subitem_tree, p_item );
155     vlc_event_detach( p_em, vlc_InputItemMetaChanged,
156                       input_item_changed, p_item );
157     vlc_event_detach( p_em, vlc_InputItemDurationChanged,
158                       input_item_changed, p_item );
159     vlc_event_detach( p_em, vlc_InputItemNameChanged,
160                       input_item_changed, p_item );
161     vlc_event_detach( p_em, vlc_InputItemInfoChanged,
162                       input_item_changed, p_item );
163     vlc_event_detach( p_em, vlc_InputItemErrorWhenReadingChanged,
164                       input_item_changed, p_item );
165 }
166
167 /*****************************************************************************
168  * Playlist item creation
169  *****************************************************************************/
170 playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
171                                               input_item_t *p_input )
172 {
173     playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
174     if( !p_item )
175         return NULL;
176
177     assert( p_input );
178
179     p_item->p_input = p_input;
180     vlc_gc_incref( p_item->p_input );
181
182     p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
183
184     p_item->p_parent = NULL;
185     p_item->i_children = -1;
186     p_item->pp_children = NULL;
187     p_item->i_flags = 0;
188     p_item->p_playlist = p_playlist;
189
190     install_input_item_observer( p_item );
191
192     return p_item;
193 }
194
195 /***************************************************************************
196  * Playlist item destruction
197  ***************************************************************************/
198
199 /**
200  * Release an item
201  *
202  * \param p_item item to delete
203  * \return VLC_SUCCESS
204 */
205 int playlist_ItemRelease( playlist_item_t *p_item )
206 {
207     /* For the assert */
208     playlist_t *p_playlist = p_item->p_playlist;
209     PL_ASSERT_LOCKED;
210
211     /* Surprise, we can't actually do more because we
212      * don't do refcounting, or eauivalent.
213      * Because item are not only accessed by their id
214      * using playlist_item outside the PL_LOCK isn't safe.
215      * Most of the modules does that.
216      *
217      * Who wants to add proper memory management? */
218     uninstall_input_item_observer( p_item );
219     ARRAY_APPEND( pl_priv(p_playlist)->items_to_delete, p_item);
220     return VLC_SUCCESS;
221 }
222
223 /**
224  * Delete input item
225  *
226  * Remove an input item when it appears from a root playlist item
227  * \param p_playlist playlist object
228  * \param p_input the input to delete
229  * \param p_root root playlist item
230  * \param b_do_stop must stop or not the playlist
231  * \return VLC_SUCCESS or VLC_EGENERIC
232 */
233 static int DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
234                             playlist_item_t *p_root, bool b_do_stop )
235 {
236     PL_ASSERT_LOCKED;
237     playlist_item_t *p_item = playlist_ItemFindFromInputAndRoot(
238         p_playlist, p_input, p_root, false );
239     if( !p_item ) return VLC_EGENERIC;
240     return playlist_DeleteItem( p_playlist, p_item, b_do_stop );
241 }
242
243 /**
244  * Delete input item
245  *
246  * Remove an input item when it appears from a root playlist item
247  * \param p_playlist playlist object
248  * \param p_input the input to delete
249  * \param p_root root playlist item
250  * \param b_locked TRUE if the playlist is locked
251  * \return VLC_SUCCESS or VLC_EGENERIC
252  */
253 int playlist_DeleteFromInputInParent( playlist_t *p_playlist,
254                                       input_item_t *p_item,
255                                       playlist_item_t *p_root, bool b_locked )
256 {
257     int i_ret;
258     PL_LOCK_IF( !b_locked );
259     i_ret = DeleteFromInput( p_playlist, p_item, p_root, true );
260     PL_UNLOCK_IF( !b_locked );
261     return i_ret;
262 }
263
264 /**
265  * Delete from input
266  *
267  * Search anywhere in playlist for an an input item and delete it
268  * \param p_playlist playlist object
269  * \param p_input the input to delete
270  * \param b_locked TRUE if the playlist is locked
271  * \return VLC_SUCCESS or VLC_ENOITEM
272  */
273 int playlist_DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
274                               bool b_locked )
275 {
276     int i_ret;
277     PL_LOCK_IF( !b_locked );
278     i_ret = DeleteFromInput( p_playlist, p_input,
279                              p_playlist->p_root, true );
280     PL_UNLOCK_IF( !b_locked );
281     return ( i_ret == VLC_SUCCESS ? VLC_SUCCESS : VLC_ENOITEM );
282 }
283
284 /**
285  * Clear the playlist
286  *
287  * \param p_playlist playlist object
288  * \param b_locked TRUE if the playlist is locked
289  * \return nothing
290  */
291 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
292 {
293     PL_LOCK_IF( !b_locked );
294     playlist_NodeEmpty( p_playlist, p_playlist->p_playing, true );
295     PL_UNLOCK_IF( !b_locked );
296 }
297
298 /**
299  * Delete playlist item
300  *
301  * Remove a playlist item from the playlist, given its id
302  * This function is to be used only by the playlist
303  * \param p_playlist playlist object
304  * \param i_id id of the item do delete
305  * \return VLC_SUCCESS or an error
306  */
307 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
308 {
309     PL_ASSERT_LOCKED;
310     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
311     if( !p_item ) return VLC_EGENERIC;
312     return playlist_DeleteItem( p_playlist, p_item, true );
313 }
314
315 /***************************************************************************
316  * Playlist item addition
317  ***************************************************************************/
318 /**
319  * Playlist add
320  *
321  * Add an item to the playlist or the media library
322  * \param p_playlist the playlist to add into
323  * \param psz_uri the mrl to add to the playlist
324  * \param psz_name a text giving a name or description of this item
325  * \param i_mode the mode used when adding
326  * \param i_pos the position in the playlist where to add. If this is
327  *        PLAYLIST_END the item will be added at the end of the playlist
328  *        regardless of its size
329  * \param b_playlist TRUE for playlist, FALSE for media library
330  * \param b_locked TRUE if the playlist is locked
331  * \return VLC_SUCCESS or a VLC error code
332  */
333 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
334                   const char *psz_name, int i_mode, int i_pos,
335                   bool b_playlist, bool b_locked )
336 {
337     return playlist_AddExt( p_playlist, psz_uri, psz_name,
338                             i_mode, i_pos, -1, 0, NULL, 0, b_playlist, b_locked );
339 }
340
341 /**
342  * Add a MRL into the playlist or the media library, duration and options given
343  *
344  * \param p_playlist the playlist to add into
345  * \param psz_uri the mrl to add to the playlist
346  * \param psz_name a text giving a name or description of this item
347  * \param i_mode the mode used when adding
348  * \param i_pos the position in the playlist where to add. If this is
349  *        PLAYLIST_END the item will be added at the end of the playlist
350  *        regardless of its size
351  * \param i_duration length of the item in milliseconds.
352  * \param i_options the number of options
353  * \param ppsz_options an array of options
354  * \param i_option_flags options flags
355  * \param b_playlist TRUE for playlist, FALSE for media library
356  * \param b_locked TRUE if the playlist is locked
357  * \return VLC_SUCCESS or a VLC error code
358 */
359 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
360                      const char *psz_name, int i_mode, int i_pos,
361                      mtime_t i_duration,
362                      int i_options, const char *const *ppsz_options,
363                      unsigned i_option_flags,
364                      bool b_playlist, bool b_locked )
365 {
366     int i_ret;
367     input_item_t *p_input;
368
369     p_input = input_item_NewExt( p_playlist, psz_uri, psz_name,
370                                  i_options, ppsz_options, i_option_flags,
371                                  i_duration );
372     if( p_input == NULL )
373         return VLC_ENOMEM;
374     i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
375                                b_locked );
376     vlc_gc_decref( p_input );
377     return i_ret;
378 }
379
380 /**
381  * Add an input item to the playlist node
382  *
383  * \param p_playlist the playlist to add into
384  * \param p_input the input item to add
385  * \param i_mode the mode used when adding
386  * \param i_pos the position in the playlist where to add. If this is
387  *        PLAYLIST_END the item will be added at the end of the playlist
388  *        regardless of its size
389  * \param b_playlist TRUE for playlist, FALSE for media library
390  * \param b_locked TRUE if the playlist is locked
391  * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
392 */
393 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
394                        int i_mode, int i_pos, bool b_playlist,
395                        bool b_locked )
396 {
397     playlist_item_t *p_item;
398     if( p_playlist->b_die ) return VLC_EGENERIC;
399     if( !pl_priv(p_playlist)->b_doing_ml )
400         PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
401                                              p_input->psz_uri );
402
403     PL_LOCK_IF( !b_locked );
404
405     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
406     if( p_item == NULL ) return VLC_ENOMEM;
407     AddItem( p_playlist, p_item,
408              b_playlist ? p_playlist->p_playing :
409                           p_playlist->p_media_library , i_mode, i_pos );
410
411     GoAndPreparse( p_playlist, i_mode, p_item );
412
413     PL_UNLOCK_IF( !b_locked );
414     return VLC_SUCCESS;
415 }
416
417 /**
418  * Add an input item to a given node
419  *
420  * \param p_playlist the playlist to add into
421  * \param p_input the input item to add
422  * \param p_parent the parent item to add into
423  * \param i_mode the mode used when addin
424  * \param i_pos the position in the playlist where to add. If this is
425  *        PLAYLIST_END the item will be added at the end of the playlist
426  *        regardless of its size
427  * \param b_locked TRUE if the playlist is locked
428  * \return the new playlist item
429  */
430 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
431                                          input_item_t *p_input,
432                                          playlist_item_t *p_parent,
433                                          int i_mode, int i_pos,
434                                          bool b_locked )
435 {
436     playlist_item_t *p_item;
437     assert( p_input );
438     assert( p_parent && p_parent->i_children != -1 );
439
440     if( p_playlist->b_die )
441         return NULL;
442     PL_LOCK_IF( !b_locked );
443
444     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
445     if( p_item == NULL ) return NULL;
446     AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
447
448     PL_UNLOCK_IF( !b_locked );
449
450     return p_item;
451 }
452
453 /**
454  * Insert a tree of input items into a given playlist node
455  *
456  * \param p_playlist the playlist to insert into
457  * \param p_parent the receiving playlist node (can be an item)
458  * \param p_node the root of input item tree,
459           only it's contents will be inserted
460  * \param i_pos the position in the playlist where to insert. If this is
461  *        PLAYLIST_END the items will be added at the end of the playlist
462  *        regardless of its size
463  * \param b_flat TRUE if the new tree contents should be flattened into a list
464  * \return the first new leaf inserted (in playing order)
465  */
466 playlist_item_t *playlist_InsertInputItemTree (
467     playlist_t *p_playlist, playlist_item_t *p_parent,
468     input_item_node_t *p_node, int i_pos, bool b_flat )
469 {
470   playlist_item_t *p_first_leaf = NULL;
471   RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
472   return p_first_leaf;
473 }
474
475 /*****************************************************************************
476  * Playlist item misc operations
477  *****************************************************************************/
478
479 /**
480  * Item to node
481  *
482  * Transform an item to a node. Return the node in the category tree, or NULL
483  * if not found there
484  * This function must be entered without the playlist lock
485  * \param p_playlist the playlist object
486  * \param p_item the item to transform
487  * \param b_locked TRUE if the playlist is locked
488  * \return the item transform in a node
489  */
490 static playlist_item_t *ItemToNode( playlist_t *p_playlist,
491                                     playlist_item_t *p_item,
492                                     bool b_locked )
493 {
494     PL_LOCK_IF( !b_locked );
495
496     assert( p_item->p_parent );
497
498     bool b_flat = false;
499     playlist_item_t *p_up = p_item;
500     while( p_up->p_parent )
501     {
502         if( p_up->p_parent == p_playlist->p_playing ||
503             p_up->p_parent == p_playlist->p_media_library )
504         {
505             if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
506             break;
507         }
508         p_up = p_up->p_parent;
509     }
510
511     if( !b_flat )
512     {
513         ChangeToNode( p_playlist, p_item );
514         if( p_up == p_playlist->p_root )
515             var_SetAddress( p_playlist, "item-change", p_item->p_input );
516         PL_UNLOCK_IF( !b_locked );
517         return p_item;
518     }
519     else
520     {
521         playlist_item_t *p_status_item = get_current_status_item( p_playlist );
522         playlist_item_t *p_status_node = get_current_status_node( p_playlist );
523         if( p_item == p_status_item )
524         {
525             /* We're deleting the current playlist item. Update
526               * the playlist object to point at the previous item
527               * so the playlist won't be restarted */
528             playlist_item_t *p_prev_status_item = NULL;
529             int i = 0;
530             while( i < p_status_node->i_children &&
531                     p_status_node->pp_children[i] != p_status_item )
532             {
533                 p_prev_status_item = p_status_node->pp_children[i];
534                 i++;
535             }
536             if( i == p_status_node->i_children )
537                 p_prev_status_item = NULL;
538             if( p_prev_status_item )
539                 set_current_status_item( p_playlist, p_prev_status_item );
540         }
541
542         DeleteFromInput( p_playlist, p_item->p_input,
543                           p_playlist->p_root, false );
544
545         pl_priv(p_playlist)->b_reset_currently_playing = true;
546         vlc_cond_signal( &pl_priv(p_playlist)->signal );
547
548         PL_UNLOCK_IF( !b_locked );
549         return p_item->p_parent;
550     }
551 }
552
553 /**
554  * Find an item within a root, given its input id.
555  *
556  * \param p_playlist the playlist object
557  * \param p_item the input item
558  * \param p_root root playlist item
559  * \param b_items_only TRUE if we want the item himself
560  * \return the first found item, or NULL if not found
561  */
562 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
563                                                     input_item_t *p_item,
564                                                     playlist_item_t *p_root,
565                                                     bool b_items_only )
566 {
567     int i;
568     for( i = 0 ; i< p_root->i_children ; i++ )
569     {
570         if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
571             p_root->pp_children[i]->p_input == p_item )
572         {
573             return p_root->pp_children[i];
574         }
575         else if( p_root->pp_children[i]->i_children >= 0 )
576         {
577             playlist_item_t *p_search =
578                  playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
579                                                     p_root->pp_children[i],
580                                                     b_items_only );
581             if( p_search ) return p_search;
582         }
583     }
584     return NULL;
585 }
586
587
588 static int ItemIndex ( playlist_item_t *p_item )
589 {
590     for( int i = 0; i < p_item->p_parent->i_children; i++ )
591         if( p_item->p_parent->pp_children[i] == p_item ) return i;
592     return -1;
593 }
594
595 /**
596  * Moves an item
597  *
598  * This function must be entered with the playlist lock
599  *
600  * \param p_playlist the playlist
601  * \param p_item the item to move
602  * \param p_node the new parent of the item
603  * \param i_newpos the new position under this new parent
604  * \return VLC_SUCCESS or an error
605  */
606 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
607                        playlist_item_t *p_node, int i_newpos )
608 {
609     PL_ASSERT_LOCKED;
610
611     if( p_node->i_children == -1 ) return VLC_EGENERIC;
612
613     playlist_item_t *p_detach = p_item->p_parent;
614     int i_index = ItemIndex( p_item );
615
616     REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );
617
618     if( p_detach == p_node && i_index < i_newpos )
619         i_newpos--;
620
621     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
622     p_item->p_parent = p_node;
623
624     pl_priv( p_playlist )->b_reset_currently_playing = true;
625     vlc_cond_signal( &pl_priv( p_playlist )->signal );
626     return VLC_SUCCESS;
627 }
628
629 /**
630  * Moves an array of items
631  *
632  * This function must be entered with the playlist lock
633  *
634  * \param p_playlist the playlist
635  * \param i_items the number of indexes to move
636  * \param pp_items the array of indexes to move
637  * \param p_node the target node
638  * \param i_newpos the target position under this node
639  * \return VLC_SUCCESS or an error
640  */
641 int playlist_TreeMoveMany( playlist_t *p_playlist,
642                             int i_items, playlist_item_t **pp_items,
643                             playlist_item_t *p_node, int i_newpos )
644 {
645     PL_ASSERT_LOCKED;
646
647     if ( p_node->i_children == -1 ) return VLC_EGENERIC;
648
649     int i;
650     for( i = 0; i < i_items; i++ )
651     {
652         playlist_item_t *p_item = pp_items[i];
653         int i_index = ItemIndex( p_item );
654         playlist_item_t *p_parent = p_item->p_parent;
655         REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i_index );
656         if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
657     }
658     for( i = i_items - 1; i >= 0; i-- )
659     {
660         playlist_item_t *p_item = pp_items[i];
661         INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
662         p_item->p_parent = p_node;
663     }
664
665     pl_priv( p_playlist )->b_reset_currently_playing = true;
666     vlc_cond_signal( &pl_priv( p_playlist )->signal );
667     return VLC_SUCCESS;
668 }
669
670 /**
671  * Send a notification that an item has been added to a node
672  *
673  * \param p_playlist the playlist object
674  * \param i_item_id id of the item added
675  * \param i_node_id id of the node in wich the item was added
676  * \param b_signal TRUE if the function must send a signal
677  * \return nothing
678  */
679 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
680                              int i_node_id, bool b_signal )
681 {
682     playlist_private_t *p_sys = pl_priv(p_playlist);
683     PL_ASSERT_LOCKED;
684
685     p_sys->b_reset_currently_playing = true;
686     if( b_signal )
687         vlc_cond_signal( &p_sys->signal );
688
689     playlist_add_t add;
690     add.i_item = i_item_id;
691     add.i_node = i_node_id;
692
693     vlc_value_t val;
694     val.p_address = &add;
695
696     var_Set( p_playlist, "playlist-item-append", val );
697 }
698
699 /***************************************************************************
700  * The following functions are local
701  ***************************************************************************/
702
703 /* Enqueue an item for preparsing, and play it, if needed */
704 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
705                            playlist_item_t *p_item )
706 {
707     PL_ASSERT_LOCKED;
708     if( (i_mode & PLAYLIST_GO ) )
709     {
710         pl_priv(p_playlist)->request.b_request = true;
711         pl_priv(p_playlist)->request.i_skip = 0;
712         pl_priv(p_playlist)->request.p_item = p_item;
713         if( pl_priv(p_playlist)->p_input )
714             input_Stop( pl_priv(p_playlist)->p_input, true );
715         pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
716         vlc_cond_signal( &pl_priv(p_playlist)->signal );
717     }
718     /* Preparse if PREPARSE or SPREPARSE & not enough meta */
719     char *psz_artist = input_item_GetArtist( p_item->p_input );
720     char *psz_album = input_item_GetAlbum( p_item->p_input );
721     if( pl_priv(p_playlist)->b_auto_preparse &&
722           (i_mode & PLAYLIST_PREPARSE ||
723           ( i_mode & PLAYLIST_SPREPARSE &&
724             ( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
725           ) ) )
726         playlist_PreparseEnqueue( p_playlist, p_item->p_input, pl_Locked );
727     /* If we already have it, signal it */
728     else if( !EMPTY_STR( psz_artist ) && !EMPTY_STR( psz_album ) )
729         input_item_SetPreparsed( p_item->p_input, true );
730     free( psz_artist );
731     free( psz_album );
732 }
733
734 /* Add the playlist item to the requested node and fire a notification */
735 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
736                      playlist_item_t *p_node, int i_mode, int i_pos )
737 {
738     PL_ASSERT_LOCKED;
739     ARRAY_APPEND(p_playlist->items, p_item);
740     ARRAY_APPEND(p_playlist->all_items, p_item);
741
742     if( i_pos == PLAYLIST_END )
743         playlist_NodeAppend( p_playlist, p_item, p_node );
744     else
745         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
746
747     if( !pl_priv(p_playlist)->b_doing_ml )
748         playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
749                                  !( i_mode & PLAYLIST_NO_REBUILD ) );
750 }
751
752 /* Actually convert an item to a node */
753 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
754 {
755     int i;
756     if( p_item->i_children == -1 )
757         p_item->i_children = 0;
758
759     /* Remove it from the array of available items */
760     ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
761     if( i != -1 )
762         ARRAY_REMOVE( p_playlist->items, i );
763 }
764
765 /* Do the actual removal */
766 int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
767                         bool b_stop )
768 {
769     int i;
770     int i_id = p_item->i_id;
771     PL_ASSERT_LOCKED;
772
773     if( p_item->i_children > -1 )
774     {
775         return playlist_NodeDelete( p_playlist, p_item, true, false );
776     }
777
778     pl_priv(p_playlist)->b_reset_currently_playing = true;
779     var_SetInteger( p_playlist, "playlist-item-deleted", i_id );
780
781     /* Remove the item from the bank */
782     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
783     if( i != -1 )
784         ARRAY_REMOVE( p_playlist->all_items, i );
785
786     ARRAY_BSEARCH( p_playlist->items,->i_id, int, i_id, i );
787     if( i != -1 )
788         ARRAY_REMOVE( p_playlist->items, i );
789
790     /* Check if it is the current item */
791     if( get_current_status_item( p_playlist ) == p_item )
792     {
793         /* Stop it if we have to */
794         if( b_stop )
795         {
796             playlist_Control( p_playlist, PLAYLIST_STOP, pl_Locked );
797             msg_Info( p_playlist, "stopping playback" );
798         }
799         /* In any case, this item can't be the next one to be played ! */
800         set_current_status_item( p_playlist, NULL );
801     }
802
803     ARRAY_BSEARCH( p_playlist->current,->i_id, int, i_id, i );
804     if( i != -1 )
805         ARRAY_REMOVE( p_playlist->current, i );
806
807     PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
808
809     /* Remove the item from its parent */
810     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
811
812     playlist_ItemRelease( p_item );
813
814     return VLC_SUCCESS;
815 }
816
817 static int RecursiveAddIntoParent (
818     playlist_t *p_playlist, playlist_item_t *p_parent,
819     input_item_node_t *p_node, int i_pos, bool b_flat,
820     playlist_item_t **pp_first_leaf )
821 {
822   if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
823
824   if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
825
826   for( int i = 0; i < p_node->i_children; i++ )
827   {
828       playlist_item_t *p_child = NULL;
829       if( b_flat ? p_node->pp_children[i]->i_children == 0 : 1 )
830       {
831           p_child = playlist_NodeAddInput( p_playlist,
832                                  p_node->pp_children[i]->p_item,
833                                  p_parent,
834                                  PLAYLIST_INSERT, i_pos,
835                                  pl_Locked );
836           i_pos++;
837       }
838       if( p_node->pp_children[i]->i_children > 0 )
839       {
840           if( b_flat )
841           {
842               i_pos = RecursiveAddIntoParent(
843                                       p_playlist, p_parent,
844                                       p_node->pp_children[i], i_pos, true,
845                                       &p_child );
846           }
847           else
848           {
849               RecursiveAddIntoParent( p_playlist, p_child,
850                                       p_node->pp_children[i], 0, false,
851                                       &p_child );
852           }
853       }
854       assert( p_child != NULL );
855       if( i == 0 ) *pp_first_leaf = p_child;
856   }
857   return i_pos;
858 }