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