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