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