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