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