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