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