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