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