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