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