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