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