1 /*****************************************************************************
2 * item.c : Playlist item creation/deletion/add/removal functions
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
30 #include <vlc_playlist.h>
31 #include "playlist_internal.h"
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,
41 static playlist_item_t *ItemToNode( playlist_t *, playlist_item_t *, bool );
43 /*****************************************************************************
44 * An input item has gained a subitem (Event Callback)
45 *****************************************************************************/
46 static void input_item_subitem_added( const vlc_event_t * p_event,
49 playlist_item_t *p_parent_playlist_item = user_data;
50 playlist_t * p_playlist = p_parent_playlist_item->p_playlist;
51 input_item_t * p_parent, * p_child;
52 playlist_item_t * p_child_in_category;
53 playlist_item_t * p_item_in_category;
56 p_parent = p_event->p_obj;
57 p_child = p_event->u.input_item_subitem_added.p_new_child;
60 b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
62 /* This part is really hakish, but this playlist system isn't simple */
63 /* First check if we haven't already added the item as we are
64 * listening using the onelevel and the category representent
65 * (Because of the playlist design) */
66 p_child_in_category = playlist_ItemFindFromInputAndRoot(
68 p_playlist->p_root_category,
69 false /* Only non-node */ );
71 if( !p_child_in_category )
73 /* Then, transform to a node if needed */
74 p_item_in_category = playlist_ItemFindFromInputAndRoot(
76 p_playlist->p_root_category,
77 false /* Only non-node */ );
78 if( !p_item_in_category )
80 /* Item may have been removed */
85 bool b_stop = p_item_in_category->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
88 p_item_in_category == get_current_status_item( p_playlist ) &&
89 p_item_in_category->i_children == -1;
91 /* If this item is already a node don't transform it */
92 if( p_item_in_category->i_children == -1 )
94 p_item_in_category = ItemToNode( p_playlist,
95 p_item_in_category, pl_Locked );
96 p_item_in_category->p_input->i_type = ITEM_TYPE_PLAYLIST;
99 int i_ret = playlist_BothAddInput( p_playlist, p_child,
101 PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
102 NULL, NULL, pl_Locked );
104 if( i_ret == VLC_SUCCESS && b_play )
108 p_item_in_category->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
110 playlist_Stop( p_playlist );
113 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
114 pl_Locked, p_item_in_category, NULL );
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,
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 );
133 /*****************************************************************************
134 * Listen to vlc_InputItemAddSubItem event
135 *****************************************************************************/
136 static void install_input_item_observer( playlist_item_t * p_item )
138 vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
139 vlc_event_attach( p_em, vlc_InputItemSubItemAdded,
140 input_item_subitem_added, 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 );
153 static void uninstall_input_item_observer( playlist_item_t * p_item )
155 vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
156 vlc_event_detach( p_em, vlc_InputItemSubItemAdded,
157 input_item_subitem_added, 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 );
170 /*****************************************************************************
171 * Playlist item creation
172 *****************************************************************************/
173 playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
174 input_item_t *p_input, bool install_observer )
176 playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
182 p_item->p_input = p_input;
183 vlc_gc_incref( p_item->p_input );
185 p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
187 p_item->p_parent = NULL;
188 p_item->i_children = -1;
189 p_item->pp_children = NULL;
191 p_item->p_playlist = p_playlist;
192 p_item->b_input_item_observer = install_observer;
194 if( install_observer )
195 install_input_item_observer( p_item );
200 /***************************************************************************
201 * Playlist item destruction
202 ***************************************************************************/
207 * \param p_item item to delete
208 * \return VLC_SUCCESS
210 int playlist_ItemRelease( playlist_item_t *p_item )
213 playlist_t *p_playlist = p_item->p_playlist;
216 /* Surprise, we can't actually do more because we
217 * don't do refcounting, or eauivalent.
218 * Because item are not only accessed by their id
219 * using playlist_item outside the PL_LOCK isn't safe.
220 * Most of the modules does that.
222 * Who wants to add proper memory management? */
223 if( p_item->b_input_item_observer )
224 uninstall_input_item_observer( p_item );
225 ARRAY_APPEND( pl_priv(p_playlist)->items_to_delete, p_item);
232 * Remove an input item when it appears from a root playlist item
233 * \param p_playlist playlist object
234 * \param p_input the input to delete
235 * \param p_root root playlist item
236 * \param b_do_stop must stop or not the playlist
237 * \return VLC_SUCCESS or VLC_EGENERIC
239 static int DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
240 playlist_item_t *p_root, bool b_do_stop )
244 for( i = 0 ; i< p_root->i_children ; i++ )
246 if( p_root->pp_children[i]->i_children == -1 &&
247 p_root->pp_children[i]->p_input == p_input )
249 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
252 else if( p_root->pp_children[i]->i_children >= 0 )
254 int i_ret = DeleteFromInput( p_playlist, p_input,
255 p_root->pp_children[i], b_do_stop );
256 if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
265 * Remove an input item when it appears from a root playlist item
266 * \param p_playlist playlist object
267 * \param p_input the input to delete
268 * \param p_root root playlist item
269 * \param b_locked TRUE if the playlist is locked
270 * \return VLC_SUCCESS or VLC_EGENERIC
272 int playlist_DeleteFromInputInParent( playlist_t *p_playlist,
273 input_item_t *p_item,
274 playlist_item_t *p_root, bool b_locked )
277 PL_LOCK_IF( !b_locked );
278 i_ret = DeleteFromInput( p_playlist, p_item, p_root, true );
279 PL_UNLOCK_IF( !b_locked );
286 * Remove an input item from ONELEVEL and CATEGORY
287 * \param p_playlist playlist object
288 * \param p_input the input to delete
289 * \param b_locked TRUE if the playlist is locked
290 * \return VLC_SUCCESS or VLC_ENOITEM
292 int playlist_DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
296 PL_LOCK_IF( !b_locked );
297 i_ret1 = DeleteFromInput( p_playlist, p_input,
298 p_playlist->p_root_category, true );
299 i_ret2 = DeleteFromInput( p_playlist, p_input,
300 p_playlist->p_root_onelevel, true );
301 PL_UNLOCK_IF( !b_locked );
302 return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
303 VLC_SUCCESS : VLC_ENOITEM;
309 * \param p_playlist playlist object
310 * \param b_locked TRUE if the playlist is locked
313 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
315 PL_LOCK_IF( !b_locked );
316 playlist_NodeEmpty( p_playlist, p_playlist->p_local_category, true );
317 playlist_NodeEmpty( p_playlist, p_playlist->p_local_onelevel, true );
318 PL_UNLOCK_IF( !b_locked );
322 * Delete playlist item
324 * Remove a playlist item from the playlist, given its id
325 * This function is to be used only by the playlist
326 * \param p_playlist playlist object
327 * \param i_id id of the item do delete
328 * \return VLC_SUCCESS or an error
330 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
333 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
334 if( !p_item ) return VLC_EGENERIC;
335 return DeleteInner( p_playlist, p_item, true );
338 /***************************************************************************
339 * Playlist item addition
340 ***************************************************************************/
344 * Add an item to the playlist or the media library
345 * \param p_playlist the playlist to add into
346 * \param psz_uri the mrl to add to the playlist
347 * \param psz_name a text giving a name or description of this item
348 * \param i_mode the mode used when adding
349 * \param i_pos the position in the playlist where to add. If this is
350 * PLAYLIST_END the item will be added at the end of the playlist
351 * regardless of its size
352 * \param b_playlist TRUE for playlist, FALSE for media library
353 * \param b_locked TRUE if the playlist is locked
354 * \return VLC_SUCCESS or a VLC error code
356 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
357 const char *psz_name, int i_mode, int i_pos,
358 bool b_playlist, bool b_locked )
360 return playlist_AddExt( p_playlist, psz_uri, psz_name,
361 i_mode, i_pos, -1, 0, NULL, 0, b_playlist, b_locked );
365 * Add a MRL into the playlist or the media library, duration and options given
367 * \param p_playlist the playlist to add into
368 * \param psz_uri the mrl to add to the playlist
369 * \param psz_name a text giving a name or description of this item
370 * \param i_mode the mode used when adding
371 * \param i_pos the position in the playlist where to add. If this is
372 * PLAYLIST_END the item will be added at the end of the playlist
373 * regardless of its size
374 * \param i_duration length of the item in milliseconds.
375 * \param i_options the number of options
376 * \param ppsz_options an array of options
377 * \param i_option_flags options flags
378 * \param b_playlist TRUE for playlist, FALSE for media library
379 * \param b_locked TRUE if the playlist is locked
380 * \return VLC_SUCCESS or a VLC error code
382 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
383 const char *psz_name, int i_mode, int i_pos,
385 int i_options, const char *const *ppsz_options,
386 unsigned i_option_flags,
387 bool b_playlist, bool b_locked )
390 input_item_t *p_input;
392 p_input = input_item_NewExt( p_playlist, psz_uri, psz_name,
393 i_options, ppsz_options, i_option_flags,
395 if( p_input == NULL )
397 i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
399 vlc_gc_decref( p_input );
404 * Add an input item to the playlist node
406 * \param p_playlist the playlist to add into
407 * \param p_input the input item to add
408 * \param i_mode the mode used when adding
409 * \param i_pos the position in the playlist where to add. If this is
410 * PLAYLIST_END the item will be added at the end of the playlist
411 * regardless of its size
412 * \param b_playlist TRUE for playlist, FALSE for media library
413 * \param b_locked TRUE if the playlist is locked
414 * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
416 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
417 int i_mode, int i_pos, bool b_playlist,
420 playlist_item_t *p_item_cat, *p_item_one;
421 if( p_playlist->b_die ) return VLC_EGENERIC;
422 if( !pl_priv(p_playlist)->b_doing_ml )
423 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
426 PL_LOCK_IF( !b_locked );
428 /* Add to ONELEVEL */
429 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input, false );
430 if( p_item_one == NULL ) return VLC_ENOMEM;
431 AddItem( p_playlist, p_item_one,
432 b_playlist ? p_playlist->p_local_onelevel :
433 p_playlist->p_ml_onelevel , i_mode, i_pos );
435 /* Add to CATEGORY */
436 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input, true );
437 if( p_item_cat == NULL ) return VLC_ENOMEM;
438 AddItem( p_playlist, p_item_cat,
439 b_playlist ? p_playlist->p_local_category :
440 p_playlist->p_ml_category , i_mode, i_pos );
442 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
444 PL_UNLOCK_IF( !b_locked );
451 * Add an input item to p_direct_parent in the category tree, and to the
452 * matching top category in onelevel
453 * \param p_playlist the playlist to add into
454 * \param p_input the input item to add
455 * \param p_direct_parent the parent item to add into
456 * \param i_mode the mode used when adding
457 * \param i_pos the position in the playlist where to add. If this is
458 * PLAYLIST_END the item will be added at the end of the playlist
459 * regardless of its size
460 * \param i_cat id of the items category
461 * \param i_one id of the item onelevel category
462 * \param b_locked TRUE if the playlist is locked
463 * \return VLC_SUCCESS if success, VLC_EGENERIC if fail, VLC_ENOMEM if OOM
465 int playlist_BothAddInput( playlist_t *p_playlist,
466 input_item_t *p_input,
467 playlist_item_t *p_direct_parent,
468 int i_mode, int i_pos,
469 int *i_cat, int *i_one, bool b_locked )
471 playlist_item_t *p_item_cat, *p_item_one, *p_up;
475 if( !vlc_object_alive( p_playlist ) )
478 PL_LOCK_IF( !b_locked );
480 /* Add to category */
481 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input, true );
482 if( p_item_cat == NULL ) return VLC_ENOMEM;
483 AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
485 /* Add to onelevel */
486 /**Â \todo make a faster case for ml import */
487 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input, false );
488 if( p_item_one == NULL ) return VLC_ENOMEM;
490 p_up = p_direct_parent;
491 while( p_up->p_parent != p_playlist->p_root_category )
493 p_up = p_up->p_parent;
495 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
497 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input ==
500 AddItem( p_playlist, p_item_one,
501 p_playlist->p_root_onelevel->pp_children[i_top],
506 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
508 if( i_cat ) *i_cat = p_item_cat->i_id;
509 if( i_one ) *i_one = p_item_one->i_id;
511 PL_UNLOCK_IF( !b_locked );
516 * Add an input item to a given node
518 * \param p_playlist the playlist to add into
519 * \param p_input the input item to add
520 * \param p_parent the parent item to add into
521 * \param i_mode the mode used when addin
522 * \param i_pos the position in the playlist where to add. If this is
523 * PLAYLIST_END the item will be added at the end of the playlist
524 * regardless of its size
525 * \param b_locked TRUE if the playlist is locked
526 * \return the new playlist item
528 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
529 input_item_t *p_input,
530 playlist_item_t *p_parent,
531 int i_mode, int i_pos,
534 playlist_item_t *p_item;
536 assert( p_parent && p_parent->i_children != -1 );
538 if( p_playlist->b_die )
540 PL_LOCK_IF( !b_locked );
542 p_item = playlist_ItemNewFromInput( p_playlist, p_input, true );
543 if( p_item == NULL ) return NULL;
544 AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
546 PL_UNLOCK_IF( !b_locked );
551 /*****************************************************************************
552 * Playlist item misc operations
553 *****************************************************************************/
558 * Transform an item to a node. Return the node in the category tree, or NULL
560 * This function must be entered without the playlist lock
561 * \param p_playlist the playlist object
562 * \param p_item the item to transform
563 * \param b_locked TRUE if the playlist is locked
564 * \return the item transform in a node
566 static playlist_item_t *ItemToNode( playlist_t *p_playlist,
567 playlist_item_t *p_item,
571 playlist_item_t *p_item_in_category;
573 * Find the input in CATEGORY.
575 * - change it to node
576 * - we'll return it at the end
577 * - If we are a direct child of onelevel root, change to node, else
578 * delete the input from ONELEVEL
579 * - If we don't find it, just change to node (we are probably in VLM)
582 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
583 * useful for later BothAddInput )
586 PL_LOCK_IF( !b_locked );
588 /* Fast track the media library, no time to loose */
589 if( p_item == p_playlist->p_ml_category ) {
590 PL_UNLOCK_IF( !b_locked );
594 /** \todo First look if we don't already have it */
595 p_item_in_category = playlist_ItemFindFromInputAndRoot(
596 p_playlist, p_item->p_input,
597 p_playlist->p_root_category,
600 if( p_item_in_category )
602 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
603 p_playlist, p_item->p_input,
604 p_playlist->p_root_onelevel,
606 assert( p_item_in_one );
608 /* We already have it, and there is nothing more to do */
609 ChangeToNode( p_playlist, p_item_in_category );
611 /* Item in one is a root, change it to node */
612 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
613 ChangeToNode( p_playlist, p_item_in_one );
616 playlist_item_t *p_status_item = get_current_status_item( p_playlist );
617 playlist_item_t *p_status_node = get_current_status_node( p_playlist );
618 if( p_item_in_one == p_status_item )
620 /* We're deleting the current playlist item. Update
621 * the playlist object to point at the previous item
622 * so the playlist won't be restarted */
623 playlist_item_t *p_prev_status_item = NULL;
625 while( i < p_status_node->i_children &&
626 p_status_node->pp_children[i] != p_status_item )
628 p_prev_status_item = p_status_node->pp_children[i];
631 if( i == p_status_node->i_children )
632 p_prev_status_item = NULL;
633 if( p_prev_status_item )
634 set_current_status_item( p_playlist, p_prev_status_item );
636 DeleteFromInput( p_playlist, p_item_in_one->p_input,
637 p_playlist->p_root_onelevel, false );
639 pl_priv(p_playlist)->b_reset_currently_playing = true;
640 vlc_cond_signal( &pl_priv(p_playlist)->signal );
641 var_SetAddress( p_playlist, "item-change", p_item_in_category->p_input );
642 var_SetAddress( p_playlist, "leaf-to-parent", p_item_in_category->p_input );
643 PL_UNLOCK_IF( !b_locked );
644 return p_item_in_category;
648 ChangeToNode( p_playlist, p_item );
649 PL_UNLOCK_IF( !b_locked );
655 * Find an item within a root, given its input id.
657 * \param p_playlist the playlist object
658 * \param p_item the input item
659 * \param p_root root playlist item
660 * \param b_items_only TRUE if we want the item himself
661 * \return the first found item, or NULL if not found
663 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
664 input_item_t *p_item,
665 playlist_item_t *p_root,
669 for( i = 0 ; i< p_root->i_children ; i++ )
671 if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
672 p_root->pp_children[i]->p_input == p_item )
674 return p_root->pp_children[i];
676 else if( p_root->pp_children[i]->i_children >= 0 )
678 playlist_item_t *p_search =
679 playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
680 p_root->pp_children[i],
682 if( p_search ) return p_search;
689 static int ItemIndex ( playlist_item_t *p_item )
691 for( int i = 0; i < p_item->p_parent->i_children; i++ )
692 if( p_item->p_parent->pp_children[i] == p_item ) return i;
699 * This function must be entered with the playlist lock
701 * \param p_playlist the playlist
702 * \param p_item the item to move
703 * \param p_node the new parent of the item
704 * \param i_newpos the new position under this new parent
705 * \return VLC_SUCCESS or an error
707 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
708 playlist_item_t *p_node, int i_newpos )
712 if( p_node->i_children == -1 ) return VLC_EGENERIC;
714 playlist_item_t *p_detach = p_item->p_parent;
715 int i_index = ItemIndex( p_item );
717 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );
719 if( p_detach == p_node && i_index < i_newpos )
722 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
723 p_item->p_parent = p_node;
725 pl_priv( p_playlist )->b_reset_currently_playing = true;
726 vlc_cond_signal( &pl_priv( p_playlist )->signal );
731 * Moves an array of items
733 * This function must be entered with the playlist lock
735 * \param p_playlist the playlist
736 * \param i_items the number of indexes to move
737 * \param pp_items the array of indexes to move
738 * \param p_node the target node
739 * \param i_newpos the target position under this node
740 * \return VLC_SUCCESS or an error
742 int playlist_TreeMoveMany( playlist_t *p_playlist,
743 int i_items, playlist_item_t **pp_items,
744 playlist_item_t *p_node, int i_newpos )
748 if ( p_node->i_children == -1 ) return VLC_EGENERIC;
751 for( i = 0; i < i_items; i++ )
753 playlist_item_t *p_item = pp_items[i];
754 int i_index = ItemIndex( p_item );
755 playlist_item_t *p_parent = p_item->p_parent;
756 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i_index );
757 if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
759 for( i = i_items - 1; i >= 0; i-- )
761 playlist_item_t *p_item = pp_items[i];
762 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
763 p_item->p_parent = p_node;
766 pl_priv( p_playlist )->b_reset_currently_playing = true;
767 vlc_cond_signal( &pl_priv( p_playlist )->signal );
772 * Send a notification that an item has been added to a node
774 * \param p_playlist the playlist object
775 * \param i_item_id id of the item added
776 * \param i_node_id id of the node in wich the item was added
777 * \param b_signal TRUE if the function must send a signal
780 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
781 int i_node_id, bool b_signal )
783 playlist_private_t *p_sys = pl_priv(p_playlist);
786 p_sys->b_reset_currently_playing = true;
788 vlc_cond_signal( &p_sys->signal );
791 add.i_item = i_item_id;
792 add.i_node = i_node_id;
795 val.p_address = &add;
797 var_Set( p_playlist, "playlist-item-append", val );
800 /***************************************************************************
801 * The following functions are local
802 ***************************************************************************/
804 /* Enqueue an item for preparsing, and play it, if needed */
805 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
806 playlist_item_t *p_item_cat,
807 playlist_item_t *p_item_one )
810 if( (i_mode & PLAYLIST_GO ) )
812 playlist_item_t *p_parent = p_item_one;
813 playlist_item_t *p_toplay = NULL;
816 if( p_parent == p_playlist->p_root_category )
818 p_toplay = p_item_cat; break;
820 else if( p_parent == p_playlist->p_root_onelevel )
822 p_toplay = p_item_one; break;
824 p_parent = p_parent->p_parent;
827 pl_priv(p_playlist)->request.b_request = true;
828 pl_priv(p_playlist)->request.i_skip = 0;
829 pl_priv(p_playlist)->request.p_item = p_toplay;
830 if( pl_priv(p_playlist)->p_input )
831 input_Stop( pl_priv(p_playlist)->p_input, true );
832 pl_priv(p_playlist)->request.i_status = PLAYLIST_RUNNING;
833 vlc_cond_signal( &pl_priv(p_playlist)->signal );
835 /* Preparse if PREPARSE or SPREPARSE & not enough meta */
836 char *psz_artist = input_item_GetArtist( p_item_cat->p_input );
837 char *psz_album = input_item_GetAlbum( p_item_cat->p_input );
838 if( pl_priv(p_playlist)->b_auto_preparse &&
839 (i_mode & PLAYLIST_PREPARSE ||
840 ( i_mode & PLAYLIST_SPREPARSE &&
841 ( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
843 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input, pl_Locked );
844 /* If we already have it, signal it */
845 else if( !EMPTY_STR( psz_artist ) && !EMPTY_STR( psz_album ) )
846 input_item_SetPreparsed( p_item_cat->p_input, true );
851 /* Add the playlist item to the requested node and fire a notification */
852 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
853 playlist_item_t *p_node, int i_mode, int i_pos )
856 ARRAY_APPEND(p_playlist->items, p_item);
857 ARRAY_APPEND(p_playlist->all_items, p_item);
859 if( i_pos == PLAYLIST_END )
860 playlist_NodeAppend( p_playlist, p_item, p_node );
862 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
864 if( !pl_priv(p_playlist)->b_doing_ml )
865 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
866 !( i_mode & PLAYLIST_NO_REBUILD ) );
869 /* Actually convert an item to a node */
870 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
873 if( p_item->i_children == -1 )
874 p_item->i_children = 0;
876 /* Remove it from the array of available items */
877 ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
879 ARRAY_REMOVE( p_playlist->items, i );
882 /* Do the actual removal */
883 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
887 int i_id = p_item->i_id;
890 if( p_item->i_children > -1 )
892 return playlist_NodeDelete( p_playlist, p_item, true, false );
894 pl_priv(p_playlist)->b_reset_currently_playing = true;
895 var_SetInteger( p_playlist, "playlist-item-deleted", i_id );
897 /* Remove the item from the bank */
898 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
900 ARRAY_REMOVE( p_playlist->all_items, i );
902 ARRAY_BSEARCH( p_playlist->items,->i_id, int, i_id, i );
904 ARRAY_REMOVE( p_playlist->items, i );
906 /* Check if it is the current item */
907 if( get_current_status_item( p_playlist ) == p_item )
909 /* Stop it if we have to */
912 playlist_Control( p_playlist, PLAYLIST_STOP, pl_Locked );
913 msg_Info( p_playlist, "stopping playback" );
915 /* In any case, this item can't be the next one to be played ! */
916 set_current_status_item( p_playlist, NULL );
919 ARRAY_BSEARCH( p_playlist->current,->i_id, int, i_id, i );
921 ARRAY_REMOVE( p_playlist->current, i );
923 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
925 /* Remove the item from its parent */
926 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
928 playlist_ItemRelease( p_item );