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