]> git.sesse.net Git - vlc/blob - src/playlist/item.c
include/vlc_playlist.h: Export playlist_DeleteInputInParent.
[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 #include <vlc/vlc.h>
25 #include <assert.h>
26 #include <vlc_playlist.h>
27 #include "playlist_internal.h"
28
29 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
30                      playlist_item_t *p_node, int i_mode, int i_pos );
31 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
32                            playlist_item_t *, playlist_item_t * );
33 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
34 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
35                         vlc_bool_t b_stop );
36
37 /*****************************************************************************
38  * An input item has gained a subitem (Event Callback)
39  *****************************************************************************/
40 static void input_item_subitem_added( const vlc_event_t * p_event,
41                                       void * user_data )
42 {
43     playlist_t * p_playlist = user_data;
44     input_item_t * p_parent, * p_child;
45     vlc_bool_t b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
46     playlist_item_t *p_item_in_category;
47     playlist_item_t *p_current;
48
49     p_parent = p_event->p_obj;
50     p_child = p_event->u.input_item_subitem_added.p_new_child;
51
52     p_current = playlist_ItemGetByInput( p_playlist, p_parent, VLC_FALSE );
53     p_item_in_category = playlist_ItemToNode( p_playlist, p_current,
54                                               VLC_FALSE );
55     if(!p_item_in_category)
56         p_item_in_category = p_current;
57
58     b_play = b_play && p_current == p_playlist->status.p_item;
59
60     playlist_NodeAddInput( p_playlist, p_child, p_item_in_category, 
61         PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
62         VLC_FALSE );
63
64     if( b_play )
65     {
66         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
67                           VLC_TRUE, p_item_in_category, NULL );
68         vlc_object_release( p_playlist );
69     }
70 }
71
72 /*****************************************************************************
73  * Listen to vlc_InputItemAddSubItem event
74  *****************************************************************************/
75 static void install_input_item_observer( playlist_t * p_playlist,
76                                          input_item_t * p_input )
77 {
78     msg_Dbg( p_playlist, "Listening to %s with %p", p_input->psz_name, p_playlist);
79
80     vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
81                       input_item_subitem_added,
82                       p_playlist );
83 }
84
85 static void uninstall_input_item_observer( playlist_t * p_playlist,
86                                            input_item_t * p_input )
87 {
88     msg_Dbg( p_playlist, "Not Listening to %s with %p", p_input->psz_name, p_playlist);
89
90     vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
91                       input_item_subitem_added,
92                       p_playlist );
93                       
94 }
95
96 /*****************************************************************************
97  * Playlist item creation
98  *****************************************************************************/
99 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
100                                             const char *psz_uri,
101                                             const char *psz_name,
102                                             int i_options,
103                                             const char *const *ppsz_options,
104                                             int i_duration, int i_type )
105 {
106     input_item_t *p_input;
107     if( psz_uri == NULL ) return NULL;
108     p_input = input_ItemNewWithType( p_obj, psz_uri,
109                                      psz_name, i_options, ppsz_options,
110                                      i_duration, i_type );
111     return playlist_ItemNewFromInput( p_obj, p_input );
112 }
113
114 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
115                                               input_item_t *p_input )
116 {
117     DECMALLOC_NULL( p_item, playlist_item_t );
118     playlist_t *p_playlist = pl_Yield( p_obj );
119
120     p_item->p_input = p_input;
121     vlc_gc_incref( p_item->p_input );
122
123     p_item->i_id = ++p_playlist->i_last_playlist_id;
124
125     p_item->p_parent = NULL;
126     p_item->i_children = -1;
127     p_item->pp_children = NULL;
128     p_item->i_flags = 0;
129     p_item->p_playlist = p_playlist;
130
131     install_input_item_observer( p_playlist, p_input );
132
133     pl_Release( p_item->p_playlist );
134
135     return p_item;
136 }
137
138 /***************************************************************************
139  * Playlist item destruction
140  ***************************************************************************/
141
142 /** Delete a playlist item and detach its input item */
143 int playlist_ItemDelete( playlist_item_t *p_item )
144 {
145     uninstall_input_item_observer( p_item->p_playlist, p_item->p_input );
146
147     vlc_gc_decref( p_item->p_input );
148     free( p_item );
149     return VLC_SUCCESS;
150 }
151
152 /** Remove an input item when it appears from a root playlist item */
153 static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
154                             playlist_item_t *p_root, vlc_bool_t b_do_stop )
155 {
156     int i;
157     for( i = 0 ; i< p_root->i_children ; i++ )
158     {
159         if( p_root->pp_children[i]->i_children == -1 &&
160             p_root->pp_children[i]->p_input->i_id == i_input_id )
161         {
162             DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
163             return VLC_SUCCESS;
164         }
165         else if( p_root->pp_children[i]->i_children >= 0 )
166         {
167             int i_ret = DeleteFromInput( p_playlist, i_input_id,
168                                          p_root->pp_children[i], b_do_stop );
169             if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
170         }
171     }
172     return VLC_EGENERIC;
173 }
174
175 /** Remove an input item when it appears from a root playlist item */
176 int playlist_DeleteInputInParent( playlist_t *p_playlist, int i_input_id,
177                                   playlist_item_t *p_root, vlc_bool_t b_locked )
178 {
179     int i_ret;
180     if( !b_locked ) PL_LOCK;
181     i_ret = DeleteFromInput( p_playlist, i_input_id,
182                              p_root, VLC_TRUE );
183     if( !b_locked ) PL_UNLOCK;
184     return i_ret;
185 }
186
187 /** Remove an input item from ONELEVEL and CATEGORY */
188 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
189                               vlc_bool_t b_locked )
190 {
191     int i_ret1, i_ret2;
192     if( !b_locked ) PL_LOCK;
193     i_ret1 = DeleteFromInput( p_playlist, i_input_id,
194                              p_playlist->p_root_category, VLC_TRUE );
195     i_ret2 = DeleteFromInput( p_playlist, i_input_id,
196                      p_playlist->p_root_onelevel, VLC_TRUE );
197     if( !b_locked ) PL_UNLOCK;
198     return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
199                             VLC_SUCCESS : VLC_ENOITEM;
200 }
201
202 void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
203 {
204     if( !b_locked ) PL_LOCK;
205     playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
206     playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
207     if( !b_locked ) PL_UNLOCK;
208 }
209
210 /** Remove a playlist item from the playlist, given its id
211  * This function is to be used only by the playlist */
212 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
213 {
214     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,
215                                                     VLC_TRUE );
216     if( !p_item ) return VLC_EGENERIC;
217     return DeleteInner( p_playlist, p_item, VLC_TRUE );
218 }
219
220 /***************************************************************************
221  * Playlist item addition
222  ***************************************************************************/
223 /** Add an item to the playlist or the media library
224  * \param p_playlist the playlist to add into
225  * \param psz_uri the mrl to add to the playlist
226  * \param psz_name a text giving a name or description of this item
227  * \param i_mode the mode used when adding
228  * \param i_pos the position in the playlist where to add. If this is
229  *        PLAYLIST_END the item will be added at the end of the playlist
230  *        regardless of its size
231  * \param b_playlist TRUE for playlist, FALSE for media library
232  * \return The id of the playlist item
233  */
234 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
235                   const char *psz_name, int i_mode, int i_pos,
236                   vlc_bool_t b_playlist, vlc_bool_t b_locked )
237 {
238     return playlist_AddExt( p_playlist, psz_uri, psz_name,
239                             i_mode, i_pos, -1, NULL, 0, b_playlist, b_locked );
240 }
241
242 /**
243  * Add a MRL into the playlist or the media library, duration and options given
244  *
245  * \param p_playlist the playlist to add into
246  * \param psz_uri the mrl to add to the playlist
247  * \param psz_name a text giving a name or description of this item
248  * \param i_mode the mode used when adding
249  * \param i_pos the position in the playlist where to add. If this is
250  *        PLAYLIST_END the item will be added at the end of the playlist
251  *        regardless of its size
252  * \param i_duration length of the item in milliseconds.
253  * \param ppsz_options an array of options
254  * \param i_options the number of options
255  * \param b_playlist TRUE for playlist, FALSE for media library
256  * \return The id of the playlist item
257 */
258 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
259                      const char *psz_name, int i_mode, int i_pos,
260                      mtime_t i_duration, const char *const *ppsz_options,
261                      int i_options, vlc_bool_t b_playlist, vlc_bool_t b_locked )
262 {
263     int i_ret;
264     input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
265                                               i_options, ppsz_options,
266                                               i_duration );
267
268     i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
269                                b_locked );
270     if( i_ret == VLC_SUCCESS )
271         return p_input->i_id;
272     return -1;
273 }
274
275 /** Add an input item to the playlist node */
276 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
277                        int i_mode, int i_pos, vlc_bool_t b_playlist,
278                        vlc_bool_t b_locked )
279 {
280     playlist_item_t *p_item_cat, *p_item_one;
281
282     if( !p_playlist->b_doing_ml )
283         PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
284                                              p_input->psz_uri );
285
286     if( !b_locked ) PL_LOCK;
287
288     /* Add to ONELEVEL */
289     p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
290     if( p_item_one == NULL ) return VLC_ENOMEM;
291     AddItem( p_playlist, p_item_one,
292              b_playlist ? p_playlist->p_local_onelevel :
293                           p_playlist->p_ml_onelevel , i_mode, i_pos );
294
295     /* Add to CATEGORY */
296     p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
297     if( p_item_cat == NULL ) return VLC_ENOMEM;
298     AddItem( p_playlist, p_item_cat,
299              b_playlist ? p_playlist->p_local_category :
300                           p_playlist->p_ml_category , i_mode, i_pos );
301
302     GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
303
304     if( !b_locked ) PL_UNLOCK;
305     return VLC_SUCCESS;
306 }
307
308 /** Add an input item to p_direct_parent in the category tree, and to the
309  *  matching top category in onelevel **/
310 int playlist_BothAddInput( playlist_t *p_playlist,
311                            input_item_t *p_input,
312                            playlist_item_t *p_direct_parent,
313                            int i_mode, int i_pos,
314                            int *i_cat, int *i_one, vlc_bool_t b_locked )
315 {
316     playlist_item_t *p_item_cat, *p_item_one, *p_up;
317     int i_top;
318     assert( p_input );
319     if( !b_locked ) PL_LOCK;
320
321     /* Add to category */
322     p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
323     if( p_item_cat == NULL ) return VLC_ENOMEM;
324     AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
325
326     /* Add to onelevel */
327     /** \todo make a faster case for ml import */
328     p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
329     if( p_item_one == NULL ) return VLC_ENOMEM;
330
331     p_up = p_direct_parent;
332     while( p_up->p_parent != p_playlist->p_root_category )
333     {
334         p_up = p_up->p_parent;
335     }
336     for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
337     {
338         if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
339                              p_up->p_input->i_id )
340         {
341             AddItem( p_playlist, p_item_one,
342                      p_playlist->p_root_onelevel->pp_children[i_top],
343                      i_mode, i_pos );
344             break;
345         }
346     }
347     GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
348
349     if( i_cat ) *i_cat = p_item_cat->i_id;
350     if( i_one ) *i_one = p_item_one->i_id;
351
352     if( !b_locked ) PL_UNLOCK;
353     return VLC_SUCCESS;
354 }
355
356 /** Add an input item to a given node */
357 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
358                                          input_item_t *p_input,
359                                          playlist_item_t *p_parent,
360                                          int i_mode, int i_pos,
361                                          vlc_bool_t b_locked )
362 {
363     playlist_item_t *p_item;
364     assert( p_input );
365     assert( p_parent && p_parent->i_children != -1 );
366
367     if( !b_locked ) PL_LOCK;
368
369     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
370     if( p_item == NULL ) return NULL;
371     AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
372
373     if( !b_locked ) PL_UNLOCK;
374
375     return p_item;
376 }
377
378 /*****************************************************************************
379  * Playlist item misc operations
380  *****************************************************************************/
381
382 /**
383  * Transform an item to a node. Return the node in the category tree, or NULL
384  * if not found there
385  * This function must be entered without the playlist lock
386  */
387 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
388                                       playlist_item_t *p_item,
389                                       vlc_bool_t b_locked )
390 {
391
392     playlist_item_t *p_item_in_category;
393     /* What we do
394      * Find the input in CATEGORY.
395      *  - If we find it
396      *    - change it to node
397      *    - we'll return it at the end
398      *    - If we are a direct child of onelevel root, change to node, else
399      *      delete the input from ONELEVEL
400      *  - If we don't find it, just change to node (we are probably in VLM)
401      *    and return NULL
402      *
403      * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
404      * useful for later BothAddInput )
405      */
406
407     if( !b_locked ) PL_LOCK;
408
409     /* Fast track the media library, no time to loose */
410     if( p_item == p_playlist->p_ml_category ) {
411         if( !b_locked ) PL_UNLOCK;
412         return p_item;
413     }
414
415     /** \todo First look if we don't already have it */
416     p_item_in_category = playlist_ItemFindFromInputAndRoot(
417                                             p_playlist, p_item->p_input->i_id,
418                                             p_playlist->p_root_category,
419                                             VLC_TRUE );
420
421     if( p_item_in_category )
422     {
423         playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
424                                             p_playlist, p_item->p_input->i_id,
425                                             p_playlist->p_root_onelevel,
426                                             VLC_TRUE );
427         /* We already have it, and there is nothing more to do */
428         ChangeToNode( p_playlist, p_item_in_category );
429
430         if( !p_item_in_one )
431         {
432             if( !b_locked ) PL_UNLOCK;
433             return p_item_in_category;
434         }
435
436         /* Item in one is a root, change it to node */
437         if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
438             ChangeToNode( p_playlist, p_item_in_one );
439         else
440         {
441             DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
442                              p_playlist->p_root_onelevel, VLC_FALSE );
443         }
444         p_playlist->b_reset_currently_playing = VLC_TRUE;
445         vlc_cond_signal( &p_playlist->object_wait );
446         var_SetInteger( p_playlist, "item-change", p_item_in_category->
447                                                         p_input->i_id );
448         if( !b_locked ) PL_UNLOCK;
449         return p_item_in_category;
450     }
451     else
452     {
453         ChangeToNode( p_playlist, p_item );
454         if( !b_locked ) PL_UNLOCK;
455         return NULL;
456     }
457 }
458
459 /** Find an item within a root, given its input id.
460  * \return the first found item, or NULL if not found
461  */
462 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
463                                                     int i_input_id,
464                                                     playlist_item_t *p_root,
465                                                     vlc_bool_t b_items_only )
466 {
467     int i;
468     for( i = 0 ; i< p_root->i_children ; i++ )
469     {
470         if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
471             p_root->pp_children[i]->p_input->i_id == i_input_id )
472         {
473             return p_root->pp_children[i];
474         }
475         else if( p_root->pp_children[i]->i_children >= 0 )
476         {
477             playlist_item_t *p_search =
478                  playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
479                                                     p_root->pp_children[i],
480                                                     b_items_only );
481             if( p_search ) return p_search;
482         }
483     }
484     return NULL;
485 }
486
487
488 static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
489                      playlist_item_t *p_node, int i_newpos )
490 {
491     int j;
492     playlist_item_t *p_detach = p_item->p_parent;
493     (void)p_playlist;
494
495     if( p_node->i_children == -1 ) return VLC_EGENERIC;
496
497     for( j = 0; j < p_detach->i_children; j++ )
498     {
499          if( p_detach->pp_children[j] == p_item ) break;
500     }
501     REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
502
503     /* Attach to new parent */
504     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
505     p_item->p_parent = p_node;
506
507     return VLC_SUCCESS;
508 }
509
510 /**
511  * Moves an item
512  *
513  * This function must be entered with the playlist lock
514  *
515  * \param p_playlist the playlist
516  * \param p_item the item to move
517  * \param p_node the new parent of the item
518  * \param i_newpos the new position under this new parent
519  * \return VLC_SUCCESS or an error
520  */
521 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
522                        playlist_item_t *p_node, int i_newpos )
523 {
524     int i_ret;
525     /* Drop on a top level node. Move in the two trees */
526     if( p_node->p_parent == p_playlist->p_root_category ||
527         p_node->p_parent == p_playlist->p_root_onelevel )
528     {
529         /* Fixme: avoid useless lookups but we need some clean helpers */
530         {
531             /* Fixme: if we try to move a node on a top-level node, it will
532              * fail because the node doesn't exist in onelevel and we will
533              * do some shit in onelevel. We should recursively move all items
534              * within the node */
535             playlist_item_t *p_node_onelevel;
536             playlist_item_t *p_item_onelevel;
537             p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
538                                                 p_node->p_input->i_id,
539                                                 p_playlist->p_root_onelevel,
540                                                 VLC_FALSE );
541             p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
542                                                 p_item->p_input->i_id,
543                                                 p_playlist->p_root_onelevel,
544                                                 VLC_FALSE );
545             if( p_node_onelevel && p_item_onelevel )
546                 TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
547         }
548         {
549             playlist_item_t *p_node_category;
550             playlist_item_t *p_item_category;
551             p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
552                                                 p_node->p_input->i_id,
553                                                 p_playlist->p_root_category,
554                                                 VLC_FALSE );
555             p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
556                                                 p_item->p_input->i_id,
557                                                 p_playlist->p_root_category,
558                                                 VLC_FALSE );
559             if( p_node_category && p_item_category )
560                 TreeMove( p_playlist, p_item_category, p_node_category, 0 );
561         }
562         i_ret = VLC_SUCCESS;
563     }
564     else
565         i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
566     p_playlist->b_reset_currently_playing = VLC_TRUE;
567     vlc_cond_signal( &p_playlist->object_wait );
568     return i_ret;
569 }
570
571 /** Send a notification that an item has been added to a node */
572 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
573                              int i_node_id, vlc_bool_t b_signal )
574 {
575     vlc_value_t val;
576     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
577     p_add->i_item = i_item_id;
578     p_add->i_node = i_node_id;
579     val.p_address = p_add;
580     p_playlist->b_reset_currently_playing = VLC_TRUE;
581     if( b_signal )
582         vlc_cond_signal( &p_playlist->object_wait );
583     var_Set( p_playlist, "item-append", val );
584     free( p_add );
585 }
586
587 /*****************************************************************************
588  * Playlist item accessors
589  *****************************************************************************/
590
591 /** Set the name of a playlist item */
592 int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
593 {
594     if( psz_name && p_item )
595     {
596         input_ItemSetName( p_item->p_input, psz_name );
597         return VLC_SUCCESS;
598     }
599     return VLC_EGENERIC;
600 }
601
602 /***************************************************************************
603  * The following functions are local
604  ***************************************************************************/
605
606 /* Enqueue an item for preparsing, and play it, if needed */
607 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
608                            playlist_item_t *p_item_cat,
609                            playlist_item_t *p_item_one )
610 {
611     if( (i_mode & PLAYLIST_GO ) )
612     {
613         playlist_item_t *p_parent = p_item_one;
614         playlist_item_t *p_toplay = NULL;
615         while( p_parent )
616         {
617             if( p_parent == p_playlist->p_root_category )
618             {
619                 p_toplay = p_item_cat; break;
620             }
621             else if( p_parent == p_playlist->p_root_onelevel )
622             {
623                 p_toplay = p_item_one; break;
624             }
625             p_parent = p_parent->p_parent;
626         }
627         assert( p_toplay );
628         p_playlist->request.b_request = VLC_TRUE;
629         p_playlist->request.i_skip = 0;
630         p_playlist->request.p_item = p_toplay;
631         if( p_playlist->p_input )
632             input_StopThread( p_playlist->p_input );
633         p_playlist->request.i_status = PLAYLIST_RUNNING;
634         vlc_cond_signal( &p_playlist->object_wait );
635     }
636     /* Preparse if PREPARSE or SPREPARSE & not enough meta */
637     if( p_playlist->b_auto_preparse &&
638           (i_mode & PLAYLIST_PREPARSE ||
639           ( i_mode & PLAYLIST_SPREPARSE &&
640             ( EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) ||
641             ( EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) ) )
642           ) ) )
643         playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
644     /* If we already have it, signal it */
645     else if( !EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) &&
646              !EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) )
647         input_item_SetPreparsed( p_item_cat->p_input, VLC_TRUE );
648 }
649
650 /* Add the playlist item to the requested node and fire a notification */
651 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
652                      playlist_item_t *p_node, int i_mode, int i_pos )
653 {
654     ARRAY_APPEND(p_playlist->items, p_item);
655     ARRAY_APPEND(p_playlist->all_items, p_item);
656     p_playlist->i_enabled ++;
657
658     if( i_pos == PLAYLIST_END )
659         playlist_NodeAppend( p_playlist, p_item, p_node );
660     else
661         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
662
663     if( !p_playlist->b_doing_ml )
664         playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
665                                  !( i_mode & PLAYLIST_NO_REBUILD ) );
666 }
667
668 /* Actually convert an item to a node */
669 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
670 {
671     int i;
672     if( p_item->i_children == -1 )
673         p_item->i_children = 0;
674
675     /* Remove it from the array of available items */
676     ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
677     if( i != -1 )
678         ARRAY_REMOVE( p_playlist->items, i );
679 }
680
681 /* Do the actual removal */
682 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
683                         vlc_bool_t b_stop )
684 {
685     int i;
686     int i_id = p_item->i_id;
687     vlc_bool_t b_delay_deletion = VLC_FALSE;
688
689     if( p_item->i_children > -1 )
690     {
691         return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
692     }
693     p_playlist->b_reset_currently_playing = VLC_TRUE;
694     var_SetInteger( p_playlist, "item-deleted", i_id );
695
696     /* Remove the item from the bank */
697     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
698     if( i != -1 )
699         ARRAY_REMOVE( p_playlist->all_items, i );
700
701     /* Check if it is the current item */
702     if( p_playlist->status.p_item == p_item )
703     {
704         /* Hack we don't call playlist_Control for lock reasons */
705         if( b_stop )
706         {
707             p_playlist->request.i_status = PLAYLIST_STOPPED;
708             p_playlist->request.b_request = VLC_TRUE;
709             p_playlist->request.p_item = NULL;
710             msg_Info( p_playlist, "stopping playback" );
711             vlc_cond_signal( &p_playlist->object_wait );
712         }
713         b_delay_deletion = VLC_TRUE;
714     }
715
716     PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
717
718     /* Remove the item from its parent */
719     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
720
721     if( !b_delay_deletion )
722         playlist_ItemDelete( p_item );
723     else
724     {
725         PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
726         p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
727     }
728
729     return VLC_SUCCESS;
730 }