1 /*****************************************************************************
2 * tree.c : Playlist tree walking functions
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
32 /************************************************************************
34 ************************************************************************/
35 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
36 playlist_item_t *p_root );
37 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
38 playlist_item_t *p_root );
40 playlist_item_t *GetNextItem( playlist_t *p_playlist,
41 playlist_item_t *p_root,
42 playlist_item_t *p_item );
43 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
44 playlist_item_t *p_item,
45 playlist_item_t *p_root );
48 * Create a playlist node
50 * \param p_playlist the playlist
51 * \param psz_name the name of the node
52 * \param p_parent the parent node to attach to or NULL if no attach
53 * \param p_flags miscellaneous flags
54 * \param p_input the input_item to attach to or NULL if it has to be created
55 * \return the new node
57 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
59 playlist_item_t *p_parent, int i_flags,
60 input_item_t *p_input )
62 input_item_t *p_new_input = NULL;
63 playlist_item_t *p_item;
66 if( !psz_name ) psz_name = _("Undefined");
69 p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL,
70 psz_name, 0, NULL, -1, ITEM_TYPE_NODE );
71 p_item = playlist_ItemNewFromInput( p_playlist,
72 p_input ? p_input : p_new_input );
74 vlc_gc_decref( p_new_input );
76 if( p_item == NULL ) return NULL;
77 p_item->i_children = 0;
79 ARRAY_APPEND(p_playlist->all_items, p_item);
81 if( p_parent != NULL )
82 playlist_NodeAppend( p_playlist, p_item, p_parent );
83 playlist_SendAddNotify( p_playlist, p_item->i_id,
84 p_parent ? p_parent->i_id : -1,
85 !( i_flags & PLAYLIST_NO_REBUILD ));
90 * Remove all the children of a node
92 * This function must be entered with the playlist lock
94 * \param p_playlist the playlist
95 * \param p_root the node
96 * \param b_delete_items do we have to delete the children items ?
97 * \return VLC_SUCCESS or an error
99 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
100 bool b_delete_items )
104 if( p_root->i_children == -1 )
109 /* Delete the children */
110 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
112 if( p_root->pp_children[i]->i_children > -1 )
114 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
115 b_delete_items , false );
117 else if( b_delete_items )
119 /* Delete the item here */
120 playlist_DeleteFromItemId( p_playlist,
121 p_root->pp_children[i]->i_id );
128 * Remove all the children of a node and removes the node
130 * \param p_playlist the playlist
131 * \param p_root the node
132 * \param b_delete_items do we have to delete the children items ?
133 * \return VLC_SUCCESS or an error
135 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
136 bool b_delete_items, bool b_force )
141 if( p_root->i_children == -1 )
146 /* Delete the children */
147 for( i = p_root->i_children - 1 ; i >= 0; i-- )
149 if( p_root->pp_children[i]->i_children > -1 )
151 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
152 b_delete_items , b_force );
154 else if( b_delete_items )
156 playlist_DeleteFromItemId( p_playlist,
157 p_root->pp_children[i]->i_id );
160 /* Delete the node */
161 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
167 var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
168 ARRAY_BSEARCH( p_playlist->all_items, ->i_id, int,
171 ARRAY_REMOVE( p_playlist->all_items, i );
173 /* Remove the item from its parent */
174 if( p_root->p_parent )
175 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
177 playlist_ItemRelease( p_root );
184 * Adds an item to the children of a node
186 * \param p_playlist the playlist
187 * \param p_item the item to append
188 * \param p_parent the parent node
189 * \return VLC_SUCCESS or an error
191 int playlist_NodeAppend( playlist_t *p_playlist,
192 playlist_item_t *p_item,
193 playlist_item_t *p_parent )
195 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
198 int playlist_NodeInsert( playlist_t *p_playlist,
199 playlist_item_t *p_item,
200 playlist_item_t *p_parent,
205 assert( p_parent && p_parent->i_children != -1 );
206 if( i_position == -1 ) i_position = p_parent->i_children ;
208 INSERT_ELEM( p_parent->pp_children,
209 p_parent->i_children,
212 p_item->p_parent = p_parent;
217 * Deletes an item from the children of a node
219 * \param p_playlist the playlist
220 * \param p_item the item to remove
221 * \param p_parent the parent node
222 * \return VLC_SUCCESS or an error
224 int playlist_NodeRemoveItem( playlist_t *p_playlist,
225 playlist_item_t *p_item,
226 playlist_item_t *p_parent )
231 for(int i= 0; i< p_parent->i_children ; i++ )
233 if( p_parent->pp_children[i] == p_item )
235 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
244 * Count the children of a node
246 * \param p_playlist the playlist
247 * \param p_node the node
248 * \return the number of children
250 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
256 if( p_node->i_children == -1 )
259 i_nb = p_node->i_children;
260 for( i=0 ; i< p_node->i_children;i++ )
262 if( p_node->pp_children[i]->i_children == -1 )
265 i_nb += playlist_NodeChildrenCount( p_playlist,
266 p_node->pp_children[i] );
272 * Search a child of a node by its name
274 * \param p_node the node
275 * \param psz_search the name of the child to search
276 * \return the child item or NULL if not found or error
278 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
279 const char *psz_search )
281 playlist_t * p_playlist = p_node->p_playlist; /* For assert_locked */
282 (void)p_playlist; /* in NDEBUG case */
286 if( p_node->i_children < 0 )
290 for( i = 0 ; i< p_node->i_children; i++ )
292 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
294 return p_node->pp_children[i];
301 * Create a pair of nodes in the category and onelevel trees.
302 * They share the same input item.
303 * \param p_playlist the playlist
304 * \param psz_name the name of the nodes
305 * \param pp_node_cat pointer to return the node in category tree
306 * \param pp_node_one pointer to return the node in onelevel tree
307 * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
309 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
310 playlist_item_t **pp_node_cat,
311 playlist_item_t **pp_node_one,
315 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
316 p_playlist->p_root_category, 0, NULL );
317 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
318 p_playlist->p_root_onelevel, 0,
319 (*pp_node_cat)->p_input );
322 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
323 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
324 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
325 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
330 * Get the node in the preferred tree from a node in one of category
333 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
334 playlist_item_t *p_node )
338 if( p_node->p_parent == p_playlist->p_root_category )
340 if( p_playlist->b_tree )
342 for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
344 if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
345 p_node->p_input->i_id )
346 return p_playlist->p_root_onelevel->pp_children[i];
349 else if( p_node->p_parent == p_playlist->p_root_onelevel )
351 if( !p_playlist->b_tree )
353 for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
355 if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
356 p_node->p_input->i_id )
357 return p_playlist->p_root_category->pp_children[i];
363 /**********************************************************************
364 * Tree walking functions
365 **********************************************************************/
367 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
368 playlist_item_t *p_root )
372 playlist_item_t *p_item;
373 for ( i = p_root->i_children - 1; i >= 0; i-- )
375 if( p_root->pp_children[i]->i_children == -1 )
376 return p_root->pp_children[i];
377 else if( p_root->pp_children[i]->i_children > 0)
379 p_item = playlist_GetLastLeaf( p_playlist,
380 p_root->pp_children[i] );
381 if ( p_item != NULL )
391 * Finds the next item to play
393 * \param p_playlist the playlist
394 * \param p_root the root node
395 * \param p_item the previous item (NULL if none )
396 * \return the next item to play, or NULL if none found
398 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
399 playlist_item_t *p_root,
400 playlist_item_t *p_item,
401 bool b_ena, bool b_unplayed )
404 playlist_item_t *p_next;
406 assert( p_root && p_root->i_children != -1 );
408 PL_DEBUG2( "finding next of %s within %s",
409 PLI_NAME( p_item ), PLI_NAME( p_root ) );
411 /* Now, walk the tree until we find a suitable next item */
415 bool b_ena_ok = true, b_unplayed_ok = true;
416 p_next = GetNextItem( p_playlist, p_root, p_next );
417 if( !p_next || p_next == p_root )
419 if( p_next->i_children == -1 )
421 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
423 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
424 b_unplayed_ok = false;
425 if( b_ena_ok && b_unplayed_ok ) break;
428 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
433 * Finds the previous item to play
435 * \param p_playlist the playlist
436 * \param p_root the root node
437 * \param p_item the previous item (NULL if none )
438 * \return the next item to play, or NULL if none found
440 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
441 playlist_item_t *p_root,
442 playlist_item_t *p_item,
443 bool b_ena, bool b_unplayed )
446 playlist_item_t *p_prev;
448 PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
449 PLI_NAME( p_root ) );
450 assert( p_root && p_root->i_children != -1 );
452 /* Now, walk the tree until we find a suitable previous item */
456 bool b_ena_ok = true, b_unplayed_ok = true;
457 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
458 if( !p_prev || p_prev == p_root )
460 if( p_prev->i_children == -1 )
462 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
464 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
465 b_unplayed_ok = false;
466 if( b_ena_ok && b_unplayed_ok ) break;
469 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
473 /************************************************************************
474 * Following functions are local
475 ***********************************************************************/
478 * Get the next item in the tree
479 * If p_item is NULL, return the first child of root
481 playlist_item_t *GetNextItem( playlist_t *p_playlist,
482 playlist_item_t *p_root,
483 playlist_item_t *p_item )
485 playlist_item_t *p_parent;
488 /* Node with children, get the first one */
489 if( p_item && p_item->i_children > 0 )
490 return p_item->pp_children[0];
493 p_parent = p_item->p_parent;
496 for( i= 0 ; i < p_parent->i_children ; i++ )
498 if( p_item == NULL || p_parent->pp_children[i] == p_item )
503 if( i+1 >= p_parent->i_children )
505 /* Was already the last sibling. Look for uncles */
506 PL_DEBUG2( "Current item is the last of the node,"
507 "looking for uncle from %s",
508 p_parent->p_input->psz_name );
510 if( p_parent == p_root )
512 PL_DEBUG2( "already at root" );
515 return GetNextUncle( p_playlist, p_item, p_root );
519 return p_parent->pp_children[i+1];
526 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
527 playlist_item_t *p_root )
529 playlist_item_t *p_parent = p_item->p_parent;
530 playlist_item_t *p_grandparent;
531 bool b_found = false;
535 if( p_parent != NULL )
537 p_grandparent = p_parent->p_parent;
538 while( p_grandparent )
541 for( i = 0 ; i< p_grandparent->i_children ; i++ )
543 if( p_parent == p_grandparent->pp_children[i] )
545 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
546 p_parent->p_input->psz_name, i,
547 p_grandparent->p_input->psz_name );
552 if( b_found && i + 1 < p_grandparent->i_children )
554 return p_grandparent->pp_children[i+1];
556 /* Not found at root */
557 if( p_grandparent == p_root )
563 p_parent = p_grandparent;
564 p_grandparent = p_parent->p_parent;
568 /* We reached root */
572 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
573 playlist_item_t *p_root )
575 playlist_item_t *p_parent = p_item->p_parent;
576 playlist_item_t *p_grandparent;
577 bool b_found = false;
581 if( p_parent != NULL )
583 p_grandparent = p_parent->p_parent;
587 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
589 if( p_parent == p_grandparent->pp_children[i] )
595 if( b_found && i - 1 > 0 )
597 return p_grandparent->pp_children[i-1];
599 /* Not found at root */
600 if( p_grandparent == p_root )
606 p_parent = p_grandparent;
607 p_grandparent = p_parent->p_parent;
611 /* We reached root */
616 /* Recursively search the tree for previous item */
617 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
618 playlist_item_t *p_root,
619 playlist_item_t *p_item )
621 playlist_item_t *p_parent;
624 /* Node with children, get the last one */
625 if( p_item && p_item->i_children > 0 )
626 return p_item->pp_children[p_item->i_children - 1];
628 /* Last child of its parent ? */
630 p_parent = p_item->p_parent;
633 msg_Err( p_playlist, "Get the last one" );
637 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
639 if( p_parent->pp_children[i] == p_item )
643 /* Was already the first sibling. Look for uncles */
644 PL_DEBUG2( "current item is the first of its node,"
645 "looking for uncle from %s",
646 p_parent->p_input->psz_name );
647 if( p_parent == p_root )
649 PL_DEBUG2( "already at root" );
652 return GetPrevUncle( p_playlist, p_item, p_root );
656 return p_parent->pp_children[i-1];