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 *****************************************************************************/
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;
63 playlist_item_t *p_item;
65 if( !psz_name ) psz_name = _("Undefined");
68 p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL,
69 psz_name, 0, NULL, -1, ITEM_TYPE_NODE );
70 p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist),
71 p_input ? p_input : p_new_input );
73 vlc_gc_decref( p_new_input );
75 if( p_item == NULL ) return NULL;
76 p_item->i_children = 0;
78 ARRAY_APPEND(p_playlist->all_items, p_item);
80 if( p_parent != NULL )
81 playlist_NodeAppend( p_playlist, p_item, p_parent );
82 playlist_SendAddNotify( p_playlist, p_item->i_id,
83 p_parent ? p_parent->i_id : -1,
84 !( i_flags & PLAYLIST_NO_REBUILD ));
89 * Remove all the children of a node
91 * This function must be entered with the playlist lock
93 * \param p_playlist the playlist
94 * \param p_root the node
95 * \param b_delete_items do we have to delete the children items ?
96 * \return VLC_SUCCESS or an error
98 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
102 if( p_root->i_children == -1 )
107 /* Delete the children */
108 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
110 if( p_root->pp_children[i]->i_children > -1 )
112 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
113 b_delete_items , false );
115 else if( b_delete_items )
117 /* Delete the item here */
118 playlist_DeleteFromItemId( p_playlist,
119 p_root->pp_children[i]->i_id );
126 * Remove all the children of a node and removes the node
128 * \param p_playlist the playlist
129 * \param p_root the node
130 * \param b_delete_items do we have to delete the children items ?
131 * \return VLC_SUCCESS or an error
133 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
134 bool b_delete_items, bool b_force )
138 if( p_root->i_children == -1 )
143 /* Delete the children */
144 for( i = p_root->i_children - 1 ; i >= 0; i-- )
146 if( p_root->pp_children[i]->i_children > -1 )
148 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
149 b_delete_items , b_force );
151 else if( b_delete_items )
153 playlist_DeleteFromItemId( p_playlist,
154 p_root->pp_children[i]->i_id );
157 /* Delete the node */
158 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
164 var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
165 ARRAY_BSEARCH( p_playlist->all_items, ->i_id, int,
168 ARRAY_REMOVE( p_playlist->all_items, i );
170 /* Remove the item from its parent */
171 if( p_root->p_parent )
172 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
174 playlist_ItemDelete( p_root );
181 * Adds an item to the children of a node
183 * \param p_playlist the playlist
184 * \param p_item the item to append
185 * \param p_parent the parent node
186 * \return VLC_SUCCESS or an error
188 int playlist_NodeAppend( playlist_t *p_playlist,
189 playlist_item_t *p_item,
190 playlist_item_t *p_parent )
192 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
195 int playlist_NodeInsert( playlist_t *p_playlist,
196 playlist_item_t *p_item,
197 playlist_item_t *p_parent,
201 assert( p_parent && p_parent->i_children != -1 );
202 if( i_position == -1 ) i_position = p_parent->i_children ;
204 INSERT_ELEM( p_parent->pp_children,
205 p_parent->i_children,
208 p_item->p_parent = p_parent;
213 * Deletes an item from the children of a node
215 * \param p_playlist the playlist
216 * \param p_item the item to remove
217 * \param p_parent the parent node
218 * \return VLC_SUCCESS or an error
220 int playlist_NodeRemoveItem( playlist_t *p_playlist,
221 playlist_item_t *p_item,
222 playlist_item_t *p_parent )
226 for(int i= 0; i< p_parent->i_children ; i++ )
228 if( p_parent->pp_children[i] == p_item )
230 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
239 * Count the children of a node
241 * \param p_playlist the playlist
242 * \param p_node the node
243 * \return the number of children
245 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
250 if( p_node->i_children == -1 )
253 i_nb = p_node->i_children;
254 for( i=0 ; i< p_node->i_children;i++ )
256 if( p_node->pp_children[i]->i_children == -1 )
259 i_nb += playlist_NodeChildrenCount( p_playlist,
260 p_node->pp_children[i] );
266 * Search a child of a node by its name
268 * \param p_node the node
269 * \param psz_search the name of the child to search
270 * \return the child item or NULL if not found or error
272 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
273 const char *psz_search )
277 if( p_node->i_children < 0 )
281 for( i = 0 ; i< p_node->i_children; i++ )
283 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
285 return p_node->pp_children[i];
292 * Create a pair of nodes in the category and onelevel trees.
293 * They share the same input item.
294 * \param p_playlist the playlist
295 * \param psz_name the name of the nodes
296 * \param pp_node_cat pointer to return the node in category tree
297 * \param pp_node_one pointer to return the node in onelevel tree
298 * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
300 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
301 playlist_item_t **pp_node_cat,
302 playlist_item_t **pp_node_one,
305 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
306 p_playlist->p_root_category, 0, NULL );
307 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
308 p_playlist->p_root_onelevel, 0,
309 (*pp_node_cat)->p_input );
312 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
313 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
314 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
315 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
320 * Get the node in the preferred tree from a node in one of category
322 * For example, for the SAP node, it will return the node in the category
323 * tree if --playlist-tree is not set to never, because the SAP node prefers
326 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
327 playlist_item_t *p_node )
330 if( p_node->p_parent == p_playlist->p_root_category )
332 if( p_playlist->b_always_tree )
334 for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
336 if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
337 p_node->p_input->i_id )
338 return p_playlist->p_root_onelevel->pp_children[i];
341 else if( p_node->p_parent == p_playlist->p_root_onelevel )
343 if( p_playlist->b_never_tree )
345 for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
347 if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
348 p_node->p_input->i_id )
349 return p_playlist->p_root_category->pp_children[i];
355 /**********************************************************************
356 * Tree walking functions
357 **********************************************************************/
359 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
360 playlist_item_t *p_root )
363 playlist_item_t *p_item;
364 for ( i = p_root->i_children - 1; i >= 0; i-- )
366 if( p_root->pp_children[i]->i_children == -1 )
367 return p_root->pp_children[i];
368 else if( p_root->pp_children[i]->i_children > 0)
370 p_item = playlist_GetLastLeaf( p_playlist,
371 p_root->pp_children[i] );
372 if ( p_item != NULL )
381 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
382 playlist_item_t *p_node,
383 playlist_item_t ***ppp_items )
386 playlist_item_t *p_next = NULL;
389 p_next = playlist_GetNextLeaf( p_playlist, p_node,
390 p_next, true, false );
392 INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
400 * Finds the next item to play
402 * \param p_playlist the playlist
403 * \param p_root the root node
404 * \param p_item the previous item (NULL if none )
405 * \return the next item to play, or NULL if none found
407 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
408 playlist_item_t *p_root,
409 playlist_item_t *p_item,
410 bool b_ena, bool b_unplayed )
412 playlist_item_t *p_next;
414 assert( p_root && p_root->i_children != -1 );
416 PL_DEBUG2( "finding next of %s within %s",
417 PLI_NAME( p_item ), PLI_NAME( p_root ) );
419 /* Now, walk the tree until we find a suitable next item */
423 bool b_ena_ok = true, b_unplayed_ok = true;
424 p_next = GetNextItem( p_playlist, p_root, p_next );
425 if( !p_next || p_next == p_root )
427 if( p_next->i_children == -1 )
429 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
431 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
432 b_unplayed_ok = false;
433 if( b_ena_ok && b_unplayed_ok ) break;
436 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
441 * Finds the previous item to play
443 * \param p_playlist the playlist
444 * \param p_root the root node
445 * \param p_item the previous item (NULL if none )
446 * \return the next item to play, or NULL if none found
448 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
449 playlist_item_t *p_root,
450 playlist_item_t *p_item,
451 bool b_ena, bool b_unplayed )
453 playlist_item_t *p_prev;
455 PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
456 PLI_NAME( p_root ) );
457 assert( p_root && p_root->i_children != -1 );
459 /* Now, walk the tree until we find a suitable previous item */
463 bool b_ena_ok = true, b_unplayed_ok = true;
464 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
465 if( !p_prev || p_prev == p_root )
467 if( p_prev->i_children == -1 )
469 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
471 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
472 b_unplayed_ok = false;
473 if( b_ena_ok && b_unplayed_ok ) break;
476 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
480 /************************************************************************
481 * Following functions are local
482 ***********************************************************************/
485 * Get the next item in the tree
486 * If p_item is NULL, return the first child of root
488 playlist_item_t *GetNextItem( playlist_t *p_playlist,
489 playlist_item_t *p_root,
490 playlist_item_t *p_item )
492 playlist_item_t *p_parent;
495 /* Node with children, get the first one */
496 if( p_item && p_item->i_children > 0 )
497 return p_item->pp_children[0];
500 p_parent = p_item->p_parent;
503 for( i= 0 ; i < p_parent->i_children ; i++ )
505 if( p_item == NULL || p_parent->pp_children[i] == p_item )
510 if( i+1 >= p_parent->i_children )
512 /* Was already the last sibling. Look for uncles */
513 PL_DEBUG2( "Current item is the last of the node,"
514 "looking for uncle from %s",
515 p_parent->p_input->psz_name );
517 if( p_parent == p_root )
519 PL_DEBUG2( "already at root" );
522 return GetNextUncle( p_playlist, p_item, p_root );
526 return p_parent->pp_children[i+1];
533 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
534 playlist_item_t *p_root )
536 playlist_item_t *p_parent = p_item->p_parent;
537 playlist_item_t *p_grandparent;
538 bool b_found = false;
542 if( p_parent != NULL )
544 p_grandparent = p_parent->p_parent;
545 while( p_grandparent )
548 for( i = 0 ; i< p_grandparent->i_children ; i++ )
550 if( p_parent == p_grandparent->pp_children[i] )
552 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
553 p_parent->p_input->psz_name, i,
554 p_grandparent->p_input->psz_name );
559 if( b_found && i + 1 < p_grandparent->i_children )
561 return p_grandparent->pp_children[i+1];
563 /* Not found at root */
564 if( p_grandparent == p_root )
570 p_parent = p_grandparent;
571 p_grandparent = p_parent->p_parent;
575 /* We reached root */
579 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
580 playlist_item_t *p_root )
582 playlist_item_t *p_parent = p_item->p_parent;
583 playlist_item_t *p_grandparent;
584 bool b_found = false;
588 if( p_parent != NULL )
590 p_grandparent = p_parent->p_parent;
594 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
596 if( p_parent == p_grandparent->pp_children[i] )
602 if( b_found && i - 1 > 0 )
604 return p_grandparent->pp_children[i-1];
606 /* Not found at root */
607 if( p_grandparent == p_root )
613 p_parent = p_grandparent;
614 p_grandparent = p_parent->p_parent;
618 /* We reached root */
623 /* Recursively search the tree for previous item */
624 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
625 playlist_item_t *p_root,
626 playlist_item_t *p_item )
628 playlist_item_t *p_parent;
631 /* Node with children, get the last one */
632 if( p_item && p_item->i_children > 0 )
633 return p_item->pp_children[p_item->i_children - 1];
635 /* Last child of its parent ? */
637 p_parent = p_item->p_parent;
640 msg_Err( p_playlist, "Get the last one" );
644 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
646 if( p_parent->pp_children[i] == p_item )
650 /* Was already the first sibling. Look for uncles */
651 PL_DEBUG2( "current item is the first of its node,"
652 "looking for uncle from %s",
653 p_parent->p_input->psz_name );
654 if( p_parent == p_root )
656 PL_DEBUG2( "already at root" );
659 return GetPrevUncle( p_playlist, p_item, p_root );
663 return p_parent->pp_children[i-1];