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 /*****************************************************************************
42 * An input item has gained a subitem (Event Callback)
43 *****************************************************************************/
44 static void input_item_subitem_added( const vlc_event_t * p_event,
47 playlist_item_t *p_parent_playlist_item = user_data;
48 playlist_t * p_playlist = p_parent_playlist_item->p_playlist;
49 input_item_t * p_parent, * p_child;
50 playlist_item_t * p_child_in_category;
51 playlist_item_t * p_item_in_category;
54 p_parent = p_event->p_obj;
55 p_child = p_event->u.input_item_subitem_added.p_new_child;
58 b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
60 /* This part is really hakish, but this playlist system isn't simple */
61 /* First check if we haven't already added the item as we are
62 * listening using the onelevel and the category representent
63 * (Because of the playlist design) */
64 p_child_in_category = playlist_ItemFindFromInputAndRoot(
65 p_playlist, p_child->i_id,
66 p_playlist->p_root_category,
67 false /* Only non-node */ );
69 if( !p_child_in_category )
71 /* Then, transform to a node if needed */
72 p_item_in_category = playlist_ItemFindFromInputAndRoot(
73 p_playlist, p_parent->i_id,
74 p_playlist->p_root_category,
75 false /* Only non-node */ );
76 if( !p_item_in_category )
78 /* Item may have been removed */
83 b_play = b_play && p_item_in_category == p_playlist->status.p_item;
85 /* If this item is already a node don't transform it */
86 if( p_item_in_category->i_children == -1 )
88 p_item_in_category = playlist_ItemToNode( p_playlist,
89 p_item_in_category, true );
90 p_item_in_category->p_input->i_type = ITEM_TYPE_PLAYLIST;
93 int i_ret = playlist_BothAddInput( p_playlist, p_child,
95 PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
98 if( i_ret == VLC_SUCCESS && b_play )
100 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
101 true, p_item_in_category, NULL );
109 /*****************************************************************************
110 * An input item's meta or duration has changed (Event Callback)
111 *****************************************************************************/
112 static void input_item_changed( const vlc_event_t * p_event,
116 playlist_item_t * p_item = user_data;
117 var_SetInteger( p_item->p_playlist, "item-change", p_item->i_id );
120 /*****************************************************************************
121 * Listen to vlc_InputItemAddSubItem event
122 *****************************************************************************/
123 static void install_input_item_observer( playlist_item_t * p_item )
125 vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
126 vlc_event_attach( p_em, vlc_InputItemSubItemAdded,
127 input_item_subitem_added, p_item );
128 vlc_event_attach( p_em, vlc_InputItemDurationChanged,
129 input_item_changed, p_item );
130 vlc_event_attach( p_em, vlc_InputItemMetaChanged,
131 input_item_changed, p_item );
132 vlc_event_attach( p_em, vlc_InputItemNameChanged,
133 input_item_changed, p_item );
134 vlc_event_attach( p_em, vlc_InputItemInfoChanged,
135 input_item_changed, p_item );
138 static void uninstall_input_item_observer( playlist_item_t * p_item )
140 vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
141 vlc_event_detach( p_em, vlc_InputItemSubItemAdded,
142 input_item_subitem_added, p_item );
143 vlc_event_detach( p_em, vlc_InputItemMetaChanged,
144 input_item_changed, p_item );
145 vlc_event_detach( p_em, vlc_InputItemDurationChanged,
146 input_item_changed, p_item );
147 vlc_event_detach( p_em, vlc_InputItemNameChanged,
148 input_item_changed, p_item );
149 vlc_event_detach( p_em, vlc_InputItemInfoChanged,
150 input_item_changed, p_item );
153 /*****************************************************************************
154 * Playlist item creation
155 *****************************************************************************/
156 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
158 const char *psz_name,
160 const char *const *ppsz_options,
161 int i_duration, int i_type )
163 input_item_t *p_input;
164 if( psz_uri == NULL ) return NULL;
165 p_input = input_ItemNewWithType( p_obj, psz_uri,
166 psz_name, i_options, ppsz_options,
167 i_duration, i_type );
168 return playlist_ItemNewFromInput( p_obj, p_input );
171 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
172 input_item_t *p_input )
174 DECMALLOC_NULL( p_item, playlist_item_t );
175 playlist_t *p_playlist = pl_Yield( p_obj );
177 p_item->p_input = p_input;
178 vlc_gc_incref( p_item->p_input );
180 p_item->i_id = ++p_playlist->i_last_playlist_id;
182 p_item->p_parent = NULL;
183 p_item->i_children = -1;
184 p_item->pp_children = NULL;
186 p_item->p_playlist = p_playlist;
188 install_input_item_observer( p_item );
190 pl_Release( p_item->p_playlist );
195 /***************************************************************************
196 * Playlist item destruction
197 ***************************************************************************/
202 * Delete a playlist item and detach its input item
203 * \param p_item item to delete
204 * \return VLC_SUCCESS
206 int playlist_ItemDelete( playlist_item_t *p_item )
208 uninstall_input_item_observer( p_item );
210 vlc_gc_decref( p_item->p_input );
218 * Remove an input item when it appears from a root playlist item
219 * \param p_playlist playlist object
220 * \param i_input_id id of the input to delete
221 * \param p_root root playlist item
222 * \param b_do_stop must stop or not the playlist
223 * \return VLC_SUCCESS or VLC_EGENERIC
225 static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
226 playlist_item_t *p_root, bool b_do_stop )
229 for( i = 0 ; i< p_root->i_children ; i++ )
231 if( p_root->pp_children[i]->i_children == -1 &&
232 p_root->pp_children[i]->p_input->i_id == i_input_id )
234 DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
237 else if( p_root->pp_children[i]->i_children >= 0 )
239 int i_ret = DeleteFromInput( p_playlist, i_input_id,
240 p_root->pp_children[i], b_do_stop );
241 if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
250 * Remove an input item when it appears from a root playlist item
251 * \param p_playlist playlist object
252 * \param i_input_id id of the input to delete
253 * \param p_root root playlist item
254 * \param b_locked TRUE if the playlist is locked
255 * \return VLC_SUCCESS or VLC_EGENERIC
257 int playlist_DeleteInputInParent( playlist_t *p_playlist, int i_input_id,
258 playlist_item_t *p_root, bool b_locked )
261 if( !b_locked ) PL_LOCK;
262 i_ret = DeleteFromInput( p_playlist, i_input_id,
264 if( !b_locked ) PL_UNLOCK;
271 * Remove an input item from ONELEVEL and CATEGORY
272 * \param p_playlist playlist object
273 * \param i_input_id id of the input to delete
274 * \param b_locked TRUE if the playlist is locked
275 * \return VLC_SUCCESS or VLC_ENOITEM
277 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
281 if( !b_locked ) PL_LOCK;
282 i_ret1 = DeleteFromInput( p_playlist, i_input_id,
283 p_playlist->p_root_category, true );
284 i_ret2 = DeleteFromInput( p_playlist, i_input_id,
285 p_playlist->p_root_onelevel, true );
286 if( !b_locked ) PL_UNLOCK;
287 return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
288 VLC_SUCCESS : VLC_ENOITEM;
294 * \param p_playlist playlist object
295 * \param b_locked TRUE if the playlist is locked
298 void playlist_Clear( playlist_t * p_playlist, bool b_locked )
300 if( !b_locked ) PL_LOCK;
301 playlist_NodeEmpty( p_playlist, p_playlist->p_local_category, true );
302 playlist_NodeEmpty( p_playlist, p_playlist->p_local_onelevel, true );
303 if( !b_locked ) PL_UNLOCK;
307 * Delete playlist item
309 * Remove a playlist item from the playlist, given its id
310 * This function is to be used only by the playlist
311 * \param p_playlist playlist object
312 * \param i_id id of the item do delete
313 * \return VLC_SUCCESS or an error
315 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
317 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,
319 if( !p_item ) return VLC_EGENERIC;
320 return DeleteInner( p_playlist, p_item, true );
323 /***************************************************************************
324 * Playlist item addition
325 ***************************************************************************/
329 * Add an item to the playlist or the media library
330 * \param p_playlist the playlist to add into
331 * \param psz_uri the mrl to add to the playlist
332 * \param psz_name a text giving a name or description of this item
333 * \param i_mode the mode used when adding
334 * \param i_pos the position in the playlist where to add. If this is
335 * PLAYLIST_END the item will be added at the end of the playlist
336 * regardless of its size
337 * \param b_playlist TRUE for playlist, FALSE for media library
338 * \param b_locked TRUE if the playlist is locked
339 * \return The id of the playlist item
341 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
342 const char *psz_name, int i_mode, int i_pos,
343 bool b_playlist, bool b_locked )
345 return playlist_AddExt( p_playlist, psz_uri, psz_name,
346 i_mode, i_pos, -1, NULL, 0, b_playlist, b_locked );
350 * Add a MRL into the playlist or the media library, duration and options given
352 * \param p_playlist the playlist to add into
353 * \param psz_uri the mrl to add to the playlist
354 * \param psz_name a text giving a name or description of this item
355 * \param i_mode the mode used when adding
356 * \param i_pos the position in the playlist where to add. If this is
357 * PLAYLIST_END the item will be added at the end of the playlist
358 * regardless of its size
359 * \param i_duration length of the item in milliseconds.
360 * \param ppsz_options an array of options
361 * \param i_options the number of options
362 * \param b_playlist TRUE for playlist, FALSE for media library
363 * \param b_locked TRUE if the playlist is locked
364 * \return The id of the playlist item
366 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
367 const char *psz_name, int i_mode, int i_pos,
368 mtime_t i_duration, const char *const *ppsz_options,
369 int i_options, bool b_playlist, bool b_locked )
372 input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
373 i_options, ppsz_options,
376 i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
378 int i_id = (i_ret == VLC_SUCCESS) ? p_input->i_id : -1;
380 vlc_gc_decref( p_input );
386 * Add an input item to the playlist node
388 * \param p_playlist the playlist to add into
389 * \param p_input the input item to add
390 * \param i_mode the mode used when adding
391 * \param i_pos the position in the playlist where to add. If this is
392 * PLAYLIST_END the item will be added at the end of the playlist
393 * regardless of its size
394 * \param b_playlist TRUE for playlist, FALSE for media library
395 * \param b_locked TRUE if the playlist is locked
396 * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
398 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
399 int i_mode, int i_pos, bool b_playlist,
402 playlist_item_t *p_item_cat, *p_item_one;
403 if( p_playlist->b_die ) return VLC_EGENERIC;
404 if( !p_playlist->b_doing_ml )
405 PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
408 if( !b_locked ) PL_LOCK;
410 /* Add to ONELEVEL */
411 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
412 if( p_item_one == NULL ) return VLC_ENOMEM;
413 AddItem( p_playlist, p_item_one,
414 b_playlist ? p_playlist->p_local_onelevel :
415 p_playlist->p_ml_onelevel , i_mode, i_pos );
417 /* Add to CATEGORY */
418 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
419 if( p_item_cat == NULL ) return VLC_ENOMEM;
420 AddItem( p_playlist, p_item_cat,
421 b_playlist ? p_playlist->p_local_category :
422 p_playlist->p_ml_category , i_mode, i_pos );
424 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
426 if( !b_locked ) PL_UNLOCK;
433 * Add an input item to p_direct_parent in the category tree, and to the
434 * matching top category in onelevel
435 * \param p_playlist the playlist to add into
436 * \param p_input the input item to add
437 * \param p_direct_parent the parent item to add into
438 * \param i_mode the mode used when adding
439 * \param i_pos the position in the playlist where to add. If this is
440 * PLAYLIST_END the item will be added at the end of the playlist
441 * regardless of its size
442 * \param i_cat id of the items category
443 * \param i_one id of the item onelevel category
444 * \param b_locked TRUE if the playlist is locked
445 * \return VLC_SUCCESS if success, VLC_EGENERIC if fail, VLC_ENOMEM if OOM
447 int playlist_BothAddInput( playlist_t *p_playlist,
448 input_item_t *p_input,
449 playlist_item_t *p_direct_parent,
450 int i_mode, int i_pos,
451 int *i_cat, int *i_one, bool b_locked )
453 playlist_item_t *p_item_cat, *p_item_one, *p_up;
456 if( p_playlist->b_die ) return VLC_EGENERIC;
457 if( !b_locked ) PL_LOCK;
459 /* Add to category */
460 p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
461 if( p_item_cat == NULL ) return VLC_ENOMEM;
462 AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
464 /* Add to onelevel */
465 /**Â \todo make a faster case for ml import */
466 p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
467 if( p_item_one == NULL ) return VLC_ENOMEM;
469 p_up = p_direct_parent;
470 while( p_up->p_parent != p_playlist->p_root_category )
472 p_up = p_up->p_parent;
474 for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
476 if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
477 p_up->p_input->i_id )
479 AddItem( p_playlist, p_item_one,
480 p_playlist->p_root_onelevel->pp_children[i_top],
485 GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
487 if( i_cat ) *i_cat = p_item_cat->i_id;
488 if( i_one ) *i_one = p_item_one->i_id;
490 if( !b_locked ) PL_UNLOCK;
495 * Add an input item to a given node
497 * \param p_playlist the playlist to add into
498 * \param p_input the input item to add
499 * \param p_parent the parent item to add into
500 * \param i_mode the mode used when addin
501 * \param i_pos the position in the playlist where to add. If this is
502 * PLAYLIST_END the item will be added at the end of the playlist
503 * regardless of its size
504 * \param b_locked TRUE if the playlist is locked
505 * \return the new playlist item
507 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
508 input_item_t *p_input,
509 playlist_item_t *p_parent,
510 int i_mode, int i_pos,
513 playlist_item_t *p_item;
515 assert( p_parent && p_parent->i_children != -1 );
517 if( p_playlist->b_die )
519 if( !b_locked ) PL_LOCK;
521 p_item = playlist_ItemNewFromInput( p_playlist, p_input );
522 if( p_item == NULL ) return NULL;
523 AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
525 if( !b_locked ) PL_UNLOCK;
530 /*****************************************************************************
531 * Playlist item misc operations
532 *****************************************************************************/
537 * Transform an item to a node. Return the node in the category tree, or NULL
539 * This function must be entered without the playlist lock
540 * \param p_playlist the playlist object
541 * \param p_item the item to transform
542 * \param b_locked TRUE if the playlist is locked
543 * \return the item transform in a node
545 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
546 playlist_item_t *p_item,
550 playlist_item_t *p_item_in_category;
552 * Find the input in CATEGORY.
554 * - change it to node
555 * - we'll return it at the end
556 * - If we are a direct child of onelevel root, change to node, else
557 * delete the input from ONELEVEL
558 * - If we don't find it, just change to node (we are probably in VLM)
561 * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
562 * useful for later BothAddInput )
565 if( !b_locked ) PL_LOCK;
567 /* Fast track the media library, no time to loose */
568 if( p_item == p_playlist->p_ml_category ) {
569 if( !b_locked ) PL_UNLOCK;
573 /** \todo First look if we don't already have it */
574 p_item_in_category = playlist_ItemFindFromInputAndRoot(
575 p_playlist, p_item->p_input->i_id,
576 p_playlist->p_root_category,
579 if( p_item_in_category )
581 playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
582 p_playlist, p_item->p_input->i_id,
583 p_playlist->p_root_onelevel,
585 assert( p_item_in_one );
587 /* We already have it, and there is nothing more to do */
588 ChangeToNode( p_playlist, p_item_in_category );
590 /* Item in one is a root, change it to node */
591 if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
592 ChangeToNode( p_playlist, p_item_in_one );
595 DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
596 p_playlist->p_root_onelevel, false );
598 p_playlist->b_reset_currently_playing = true;
599 vlc_cond_signal( &p_playlist->object_wait );
600 var_SetInteger( p_playlist, "item-change", p_item_in_category->
602 if( !b_locked ) PL_UNLOCK;
603 return p_item_in_category;
607 ChangeToNode( p_playlist, p_item );
608 if( !b_locked ) PL_UNLOCK;
614 * Find an item within a root, given its input id.
616 * \param p_playlist the playlist object
617 * \param i_input_id id of the input
618 * \param p_root root playlist item
619 * \param b_items_only TRUE if we want the item himself
620 * \return the first found item, or NULL if not found
622 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
624 playlist_item_t *p_root,
628 for( i = 0 ; i< p_root->i_children ; i++ )
630 if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
631 p_root->pp_children[i]->p_input->i_id == i_input_id )
633 return p_root->pp_children[i];
635 else if( p_root->pp_children[i]->i_children >= 0 )
637 playlist_item_t *p_search =
638 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
639 p_root->pp_children[i],
641 if( p_search ) return p_search;
648 static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
649 playlist_item_t *p_node, int i_newpos )
652 playlist_item_t *p_detach = p_item->p_parent;
655 if( p_node->i_children == -1 ) return VLC_EGENERIC;
657 for( j = 0; j < p_detach->i_children; j++ )
659 if( p_detach->pp_children[j] == p_item ) break;
661 REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
663 /* Attach to new parent */
664 INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
665 p_item->p_parent = p_node;
673 * This function must be entered with the playlist lock
675 * \param p_playlist the playlist
676 * \param p_item the item to move
677 * \param p_node the new parent of the item
678 * \param i_newpos the new position under this new parent
679 * \return VLC_SUCCESS or an error
681 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
682 playlist_item_t *p_node, int i_newpos )
685 /* Drop on a top level node. Move in the two trees */
686 if( p_node->p_parent == p_playlist->p_root_category ||
687 p_node->p_parent == p_playlist->p_root_onelevel )
689 /* Fixme: avoid useless lookups but we need some clean helpers */
691 /* Fixme: if we try to move a node on a top-level node, it will
692 * fail because the node doesn't exist in onelevel and we will
693 * do some shit in onelevel. We should recursively move all items
695 playlist_item_t *p_node_onelevel;
696 playlist_item_t *p_item_onelevel;
697 p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
698 p_node->p_input->i_id,
699 p_playlist->p_root_onelevel,
701 p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
702 p_item->p_input->i_id,
703 p_playlist->p_root_onelevel,
705 if( p_node_onelevel && p_item_onelevel )
706 TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
709 playlist_item_t *p_node_category;
710 playlist_item_t *p_item_category;
711 p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
712 p_node->p_input->i_id,
713 p_playlist->p_root_category,
715 p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
716 p_item->p_input->i_id,
717 p_playlist->p_root_category,
719 if( p_node_category && p_item_category )
720 TreeMove( p_playlist, p_item_category, p_node_category, 0 );
725 i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
726 p_playlist->b_reset_currently_playing = true;
727 vlc_cond_signal( &p_playlist->object_wait );
732 * Send a notification that an item has been added to a node
734 * \param p_playlist the playlist object
735 * \param i_item_id id of the item added
736 * \param i_node_id id of the node in wich the item was added
737 * \param b_signal TRUE if the function must send a signal
740 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
741 int i_node_id, bool b_signal )
744 playlist_add_t *p_add = (playlist_add_t *)malloc( sizeof( playlist_add_t) );
748 p_add->i_item = i_item_id;
749 p_add->i_node = i_node_id;
750 val.p_address = p_add;
751 p_playlist->b_reset_currently_playing = true;
753 vlc_cond_signal( &p_playlist->object_wait );
754 var_Set( p_playlist, "item-append", val );
758 /*****************************************************************************
759 * Playlist item accessors
760 *****************************************************************************/
763 * Set the name of a playlist item
765 * \param p_item the item
766 * \param psz_name the name
767 * \return VLC_SUCCESS or VLC_EGENERIC
769 int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
771 if( psz_name && p_item )
773 input_item_SetName( p_item->p_input, psz_name );
779 /***************************************************************************
780 * The following functions are local
781 ***************************************************************************/
783 /* Enqueue an item for preparsing, and play it, if needed */
784 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
785 playlist_item_t *p_item_cat,
786 playlist_item_t *p_item_one )
788 if( (i_mode & PLAYLIST_GO ) )
790 playlist_item_t *p_parent = p_item_one;
791 playlist_item_t *p_toplay = NULL;
794 if( p_parent == p_playlist->p_root_category )
796 p_toplay = p_item_cat; break;
798 else if( p_parent == p_playlist->p_root_onelevel )
800 p_toplay = p_item_one; break;
802 p_parent = p_parent->p_parent;
805 p_playlist->request.b_request = true;
806 p_playlist->request.i_skip = 0;
807 p_playlist->request.p_item = p_toplay;
808 if( p_playlist->p_input )
809 input_StopThread( p_playlist->p_input );
810 p_playlist->request.i_status = PLAYLIST_RUNNING;
811 vlc_cond_signal( &p_playlist->object_wait );
813 /* Preparse if PREPARSE or SPREPARSE & not enough meta */
814 char *psz_artist = input_item_GetArtist( p_item_cat->p_input );
815 char *psz_album = input_item_GetAlbum( p_item_cat->p_input );
816 if( p_playlist->b_auto_preparse &&
817 (i_mode & PLAYLIST_PREPARSE ||
818 ( i_mode & PLAYLIST_SPREPARSE &&
819 ( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
821 playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
822 /* If we already have it, signal it */
823 else if( !EMPTY_STR( psz_artist ) && !EMPTY_STR( psz_album ) )
824 input_item_SetPreparsed( p_item_cat->p_input, true );
829 /* Add the playlist item to the requested node and fire a notification */
830 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
831 playlist_item_t *p_node, int i_mode, int i_pos )
833 ARRAY_APPEND(p_playlist->items, p_item);
834 ARRAY_APPEND(p_playlist->all_items, p_item);
836 if( i_pos == PLAYLIST_END )
837 playlist_NodeAppend( p_playlist, p_item, p_node );
839 playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
841 if( !p_playlist->b_doing_ml )
842 playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
843 !( i_mode & PLAYLIST_NO_REBUILD ) );
846 /* Actually convert an item to a node */
847 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
850 if( p_item->i_children == -1 )
851 p_item->i_children = 0;
853 /* Remove it from the array of available items */
854 ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
856 ARRAY_REMOVE( p_playlist->items, i );
859 /* Do the actual removal */
860 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
864 int i_id = p_item->i_id;
865 bool b_delay_deletion = false;
867 if( p_item->i_children > -1 )
869 return playlist_NodeDelete( p_playlist, p_item, true, false );
871 p_playlist->b_reset_currently_playing = true;
872 var_SetInteger( p_playlist, "item-deleted", i_id );
874 /* Remove the item from the bank */
875 ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
877 ARRAY_REMOVE( p_playlist->all_items, i );
879 ARRAY_BSEARCH( p_playlist->items,->i_id, int, i_id, i );
881 ARRAY_REMOVE( p_playlist->items, i );
883 /* Check if it is the current item */
884 if( p_playlist->status.p_item == p_item )
886 /* Hack we don't call playlist_Control for lock reasons */
889 p_playlist->request.i_status = PLAYLIST_STOPPED;
890 p_playlist->request.b_request = true;
891 p_playlist->request.p_item = NULL;
892 msg_Info( p_playlist, "stopping playback" );
893 vlc_cond_signal( &p_playlist->object_wait );
895 b_delay_deletion = true;
898 PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
900 /* Remove the item from its parent */
901 playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
903 if( !b_delay_deletion )
904 playlist_ItemDelete( p_item );
907 PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
908 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;