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 *****************************************************************************/
25 #include "vlc_playlist.h"
26 #include "playlist_internal.h"
28 /************************************************************************
30 ************************************************************************/
31 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
32 playlist_item_t *p_root );
33 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
34 playlist_item_t *p_root );
36 playlist_item_t *GetNextItem( playlist_t *p_playlist,
37 playlist_item_t *p_root,
38 playlist_item_t *p_item );
39 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
40 playlist_item_t *p_item,
41 playlist_item_t *p_root );
44 * Create a playlist node
46 * \param p_playlist the playlist
47 * \paam psz_name the name of the node
48 * \param p_parent the parent node to attach to or NULL if no attach
49 * \param p_flags miscellaneous flags
50 * \return the new node
52 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
54 playlist_item_t *p_parent, int i_flags )
56 input_item_t *p_input;
57 playlist_item_t *p_item;
59 if( !psz_name ) psz_name = _("Undefined");
60 p_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL, psz_name,
61 0, NULL, -1, ITEM_TYPE_NODE );
62 p_input->i_id = ++p_playlist->i_last_input_id;
63 p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist), p_input );
64 p_item->i_children = 0;
66 if( p_item == NULL ) return NULL;
68 ARRAY_APPEND(p_playlist->all_items, p_item);
70 if( p_parent != NULL )
71 playlist_NodeAppend( p_playlist, p_item, p_parent );
72 playlist_SendAddNotify( p_playlist, p_item->i_id,
73 p_parent ? p_parent->i_id : -1,
74 !( i_flags & PLAYLIST_NO_REBUILD ));
79 * Remove all the children of a node
81 * This function must be entered with the playlist lock
83 * \param p_playlist the playlist
84 * \param p_root the node
85 * \param b_delete_items do we have to delete the children items ?
86 * \return VLC_SUCCESS or an error
88 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
89 vlc_bool_t b_delete_items )
92 if( p_root->i_children == -1 )
97 /* Delete the children */
98 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
100 if( p_root->pp_children[i]->i_children > -1 )
102 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
103 b_delete_items , VLC_FALSE );
105 else if( b_delete_items )
107 /* Delete the item here */
108 playlist_DeleteFromItemId( p_playlist,
109 p_root->pp_children[i]->i_id );
116 * Remove all the children of a node and removes the node
118 * \param p_playlist the playlist
119 * \param p_root the node
120 * \param b_delete_items do we have to delete the children items ?
121 * \return VLC_SUCCESS or an error
123 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
124 vlc_bool_t b_delete_items, vlc_bool_t b_force )
128 if( p_root->i_children == -1 )
133 /* Delete the children */
134 for( i = p_root->i_children - 1 ; i >= 0; i-- )
136 if( p_root->pp_children[i]->i_children > -1 )
138 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
139 b_delete_items , b_force );
141 else if( b_delete_items )
143 playlist_DeleteFromItemId( p_playlist,
144 p_root->pp_children[i]->i_id );
147 /* Delete the node */
148 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
154 var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
155 ARRAY_BSEARCH( p_playlist->all_items, ->p_input->i_id, int,
156 p_root->p_input->i_id, i );
158 ARRAY_REMOVE( p_playlist->all_items, i );
160 /* Remove the item from its parent */
161 if( p_root->p_parent )
162 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
164 playlist_ItemDelete( p_root );
171 * Adds an item to the children of a node
173 * \param p_playlist the playlist
174 * \param p_item the item to append
175 * \param p_parent the parent node
176 * \return VLC_SUCCESS or an error
178 int playlist_NodeAppend( playlist_t *p_playlist,
179 playlist_item_t *p_item,
180 playlist_item_t *p_parent )
182 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
185 int playlist_NodeInsert( playlist_t *p_playlist,
186 playlist_item_t *p_item,
187 playlist_item_t *p_parent,
191 assert( p_parent && p_parent->i_children != -1 );
192 if( i_position == -1 ) i_position = p_parent->i_children ;
194 INSERT_ELEM( p_parent->pp_children,
195 p_parent->i_children,
198 p_item->p_parent = p_parent;
203 * Deletes an item from the children of a node
205 * \param p_playlist the playlist
206 * \param p_item the item to remove
207 * \param p_parent the parent node
208 * \return VLC_SUCCESS or an error
210 int playlist_NodeRemoveItem( playlist_t *p_playlist,
211 playlist_item_t *p_item,
212 playlist_item_t *p_parent )
216 for(int i= 0; i< p_parent->i_children ; i++ )
218 if( p_parent->pp_children[i] == p_item )
220 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
229 * Count the children of a node
231 * \param p_playlist the playlist
232 * \param p_node the node
233 * \return the number of children
235 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
240 if( p_node->i_children == -1 )
243 i_nb = p_node->i_children;
244 for( i=0 ; i< p_node->i_children;i++ )
246 if( p_node->pp_children[i]->i_children == -1 )
249 i_nb += playlist_NodeChildrenCount( p_playlist,
250 p_node->pp_children[i] );
256 * Search a child of a node by its name
258 * \param p_node the node
259 * \param psz_search the name of the child to search
260 * \return the child item or NULL if not found or error
262 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
263 const char *psz_search )
267 if( p_node->i_children < 0 )
271 for( i = 0 ; i< p_node->i_children; i++ )
273 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
275 return p_node->pp_children[i];
282 * Create a pair of nodes in the category and onelevel trees.
283 * They share the same input ID.
284 * \todo really share the input item
285 * \param p_playlist the playlist
286 * \param psz_name the name of the nodes
287 * \param pp_node_cat pointer to return the node in category tree
288 * \param pp_node_one pointer to return the node in onelevel tree
289 * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
291 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
292 playlist_item_t **pp_node_cat,
293 playlist_item_t **pp_node_one,
294 vlc_bool_t b_for_sd )
296 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
297 p_playlist->p_root_category, 0 );
298 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
299 p_playlist->p_root_onelevel, 0 );
300 (*pp_node_one)->p_input->i_id = (*pp_node_cat)->p_input->i_id;
303 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
304 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
305 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
306 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
311 * Get the node in the preferred tree from a node in one of category
313 * For example, for the SAP node, it will return the node in the category
314 * tree if --playlist-tree is not set to never, because the SAP node prefers
317 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
318 playlist_item_t *p_node )
321 if( p_node->p_parent == p_playlist->p_root_category )
323 if( p_playlist->b_always_tree ||
324 p_node->p_input->b_prefers_tree ) return p_node;
325 for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
327 if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
328 p_node->p_input->i_id )
329 return p_playlist->p_root_onelevel->pp_children[i];
332 else if( p_node->p_parent == p_playlist->p_root_onelevel )
334 if( p_playlist->b_never_tree || !p_node->p_input->b_prefers_tree )
336 for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
338 if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
339 p_node->p_input->i_id )
340 return p_playlist->p_root_category->pp_children[i];
346 /**********************************************************************
347 * Tree walking functions
348 **********************************************************************/
350 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
351 playlist_item_t *p_root )
354 playlist_item_t *p_item;
355 for ( i = p_root->i_children - 1; i >= 0; i-- )
357 if( p_root->pp_children[i]->i_children == -1 )
358 return p_root->pp_children[i];
359 else if( p_root->pp_children[i]->i_children > 0)
361 p_item = playlist_GetLastLeaf( p_playlist,
362 p_root->pp_children[i] );
363 if ( p_item != NULL )
372 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
373 playlist_item_t *p_node,
374 playlist_item_t ***ppp_items )
377 playlist_item_t *p_next = NULL;
380 p_next = playlist_GetNextLeaf( p_playlist, p_node,
381 p_next, VLC_TRUE, VLC_FALSE );
383 INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
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 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
403 playlist_item_t *p_next;
405 assert( p_root && p_root->i_children != -1 );
407 PL_DEBUG2( "finding next of %s within %s",
408 PLI_NAME( p_item ), PLI_NAME( p_root ) );
410 /* Now, walk the tree until we find a suitable next item */
414 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
415 p_next = GetNextItem( p_playlist, p_root, p_next );
416 if( !p_next || p_next == p_root )
418 if( p_next->i_children == -1 )
420 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
421 b_ena_ok = VLC_FALSE;
422 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
423 b_unplayed_ok = VLC_FALSE;
424 if( b_ena_ok && b_unplayed_ok ) break;
427 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
432 * Finds the previous item to play
434 * \param p_playlist the playlist
435 * \param p_root the root node
436 * \param p_item the previous item (NULL if none )
437 * \return the next item to play, or NULL if none found
439 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
440 playlist_item_t *p_root,
441 playlist_item_t *p_item,
442 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
444 playlist_item_t *p_prev;
446 PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
447 PLI_NAME( p_root ) );
448 assert( p_root && p_root->i_children != -1 );
450 /* Now, walk the tree until we find a suitable previous item */
454 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
455 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
456 if( !p_prev || p_prev == p_root )
458 if( p_prev->i_children == -1 )
460 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
461 b_ena_ok = VLC_FALSE;
462 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
463 b_unplayed_ok = VLC_FALSE;
464 if( b_ena_ok && b_unplayed_ok ) break;
467 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
471 /************************************************************************
472 * Following functions are local
473 ***********************************************************************/
476 * Get the next item in the tree
477 * If p_item is NULL, return the first child of root
479 playlist_item_t *GetNextItem( playlist_t *p_playlist,
480 playlist_item_t *p_root,
481 playlist_item_t *p_item )
483 playlist_item_t *p_parent;
486 /* Node with children, get the first one */
487 if( p_item && p_item->i_children > 0 )
488 return p_item->pp_children[0];
491 p_parent = p_item->p_parent;
494 for( i= 0 ; i < p_parent->i_children ; i++ )
496 if( p_item == NULL || p_parent->pp_children[i] == p_item )
501 if( i+1 >= p_parent->i_children )
503 /* Was already the last sibling. Look for uncles */
504 PL_DEBUG2( "Current item is the last of the node,"
505 "looking for uncle from %s",
506 p_parent->p_input->psz_name );
508 if( p_parent == p_root )
510 PL_DEBUG2( "already at root" );
513 return GetNextUncle( p_playlist, p_item, p_root );
517 return p_parent->pp_children[i+1];
524 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
525 playlist_item_t *p_root )
527 playlist_item_t *p_parent = p_item->p_parent;
528 playlist_item_t *p_grandparent;
529 vlc_bool_t b_found = VLC_FALSE;
533 if( p_parent != NULL )
535 p_grandparent = p_parent->p_parent;
539 for( i = 0 ; i< p_grandparent->i_children ; i++ )
541 if( p_parent == p_grandparent->pp_children[i] )
543 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
544 p_parent->p_input->psz_name, i,
545 p_grandparent->p_input->psz_name );
550 if( b_found && i + 1 < p_grandparent->i_children )
552 return p_grandparent->pp_children[i+1];
554 /* Not found at root */
555 if( p_grandparent == p_root )
561 p_parent = p_grandparent;
562 p_grandparent = p_parent->p_parent;
566 /* We reached root */
570 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
571 playlist_item_t *p_root )
573 playlist_item_t *p_parent = p_item->p_parent;
574 playlist_item_t *p_grandparent;
575 vlc_bool_t b_found = VLC_FALSE;
579 if( p_parent != NULL )
581 p_grandparent = p_parent->p_parent;
585 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
587 if( p_parent == p_grandparent->pp_children[i] )
593 if( b_found && i - 1 > 0 )
595 return p_grandparent->pp_children[i-1];
597 /* Not found at root */
598 if( p_grandparent == p_root )
604 p_parent = p_grandparent;
605 p_grandparent = p_parent->p_parent;
609 /* We reached root */
614 /* Recursively search the tree for previous item */
615 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
616 playlist_item_t *p_root,
617 playlist_item_t *p_item )
619 playlist_item_t *p_parent;
622 /* Node with children, get the last one */
623 if( p_item && p_item->i_children > 0 )
624 return p_item->pp_children[p_item->i_children - 1];
626 /* Last child of its parent ? */
628 p_parent = p_item->p_parent;
631 msg_Err( p_playlist, "Get the last one" );
635 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
637 if( p_parent->pp_children[i] == p_item )
641 /* Was already the first sibling. Look for uncles */
642 PL_DEBUG2( "current item is the first of its node,"
643 "looking for uncle from %s",
644 p_parent->p_input->psz_name );
645 if( p_parent == p_root )
647 PL_DEBUG2( "already at root" );
650 return GetPrevUncle( p_playlist, p_item, p_root );
654 return p_parent->pp_children[i-1];
661 /* Dump the contents of a node */
662 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
670 msg_Dbg( p_playlist, "%s (%i)",
671 p_item->p_input->psz_name, p_item->i_children );
674 if( p_item->i_children == -1 )
679 for( i = 0; i< p_item->i_children; i++ )
681 memset( str, 32, 512 );
682 sprintf( str + 2 * i_level , "%s (%i)",
683 p_item->pp_children[i]->p_input->psz_name,
684 p_item->pp_children[i]->i_children );
685 msg_Dbg( p_playlist, "%s",str );
686 if( p_item->pp_children[i]->i_children >= 0 )
688 playlist_NodeDump( p_playlist, p_item->pp_children[i],