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