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