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