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 i_pos position of the node in the parent, PLAYLIST_END to append to end.
54 * \param p_flags miscellaneous flags
55 * \param p_input the input_item to attach to or NULL if it has to be created
56 * \return the new node
58 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
60 playlist_item_t *p_parent, int i_pos,
61 int i_flags, input_item_t *p_input )
63 input_item_t *p_new_input = NULL;
64 playlist_item_t *p_item;
67 if( !psz_name ) psz_name = _("Undefined");
70 p_new_input = input_item_NewWithType( VLC_OBJECT(p_playlist), NULL,
71 psz_name, 0, NULL, 0, -1, ITEM_TYPE_NODE );
72 p_item = playlist_ItemNewFromInput( p_playlist,
73 p_input ? p_input : p_new_input );
75 vlc_gc_decref( p_new_input );
77 if( p_item == NULL ) return NULL;
78 p_item->i_children = 0;
80 ARRAY_APPEND(p_playlist->all_items, p_item);
82 if( p_parent != NULL )
83 playlist_NodeInsert( p_playlist, p_item, p_parent,
84 i_pos == PLAYLIST_END ? -1 : i_pos );
85 playlist_SendAddNotify( p_playlist, p_item->i_id,
86 p_parent ? p_parent->i_id : -1,
87 !( i_flags & PLAYLIST_NO_REBUILD ));
89 p_item->i_flags |= i_flags;
95 * Remove all the children of a node
97 * This function must be entered with the playlist lock
99 * \param p_playlist the playlist
100 * \param p_root the node
101 * \param b_delete_items do we have to delete the children items ?
102 * \return VLC_SUCCESS or an error
104 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
105 bool b_delete_items )
109 if( p_root->i_children == -1 )
114 /* Delete the children */
115 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
117 if( p_root->pp_children[i]->i_children > -1 )
119 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
120 b_delete_items , false );
122 else if( b_delete_items )
124 /* Delete the item here */
125 playlist_DeleteFromItemId( p_playlist,
126 p_root->pp_children[i]->i_id );
133 * Remove all the children of a node and removes the node
135 * \param p_playlist the playlist
136 * \param p_root the node
137 * \param b_delete_items do we have to delete the children items ?
138 * \return VLC_SUCCESS or an error
140 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
141 bool b_delete_items, bool b_force )
146 if( p_root->i_children == -1 )
151 /* Delete the children */
152 for( i = p_root->i_children - 1 ; i >= 0; i-- )
154 if( p_root->pp_children[i]->i_children > -1 )
156 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
157 b_delete_items , b_force );
159 else if( b_delete_items )
161 playlist_DeleteItem( p_playlist, p_root->pp_children[i], true );
164 /* Delete the node */
165 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
171 var_SetInteger( p_playlist, "playlist-item-deleted", p_root->i_id );
172 ARRAY_BSEARCH( p_playlist->all_items, ->i_id, int,
175 ARRAY_REMOVE( p_playlist->all_items, i );
177 /* Remove the item from its parent */
178 if( p_root->p_parent )
179 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
181 playlist_ItemRelease( p_root );
188 * Adds an item to the children of a node
190 * \param p_playlist the playlist
191 * \param p_item the item to append
192 * \param p_parent the parent node
193 * \return VLC_SUCCESS or an error
195 int playlist_NodeAppend( playlist_t *p_playlist,
196 playlist_item_t *p_item,
197 playlist_item_t *p_parent )
199 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
202 int playlist_NodeInsert( playlist_t *p_playlist,
203 playlist_item_t *p_item,
204 playlist_item_t *p_parent,
209 assert( p_parent && p_parent->i_children != -1 );
210 if( i_position == -1 ) i_position = p_parent->i_children ;
211 assert( i_position <= p_parent->i_children);
213 INSERT_ELEM( p_parent->pp_children,
214 p_parent->i_children,
217 p_item->p_parent = p_parent;
222 * Deletes an item from the children of a node
224 * \param p_playlist the playlist
225 * \param p_item the item to remove
226 * \param p_parent the parent node
227 * \return VLC_SUCCESS or an error
229 int playlist_NodeRemoveItem( playlist_t *p_playlist,
230 playlist_item_t *p_item,
231 playlist_item_t *p_parent )
236 for(int i= 0; i< p_parent->i_children ; i++ )
238 if( p_parent->pp_children[i] == p_item )
240 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
248 * Search a child of a node by its name
250 * \param p_node the node
251 * \param psz_search the name of the child to search
252 * \return the child item or NULL if not found or error
254 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
255 const char *psz_search )
257 playlist_t * p_playlist = p_node->p_playlist; /* For assert_locked */
261 if( p_node->i_children < 0 )
265 for( i = 0 ; i< p_node->i_children; i++ )
267 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
269 return p_node->pp_children[i];
275 /**********************************************************************
276 * Tree walking functions
277 **********************************************************************/
279 * Finds the next item to play
281 * \param p_playlist the playlist
282 * \param p_root the root node
283 * \param p_item the previous item (NULL if none )
284 * \return the next item to play, or NULL if none found
286 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
287 playlist_item_t *p_root,
288 playlist_item_t *p_item,
289 bool b_ena, bool b_unplayed )
292 playlist_item_t *p_next;
294 assert( p_root && p_root->i_children != -1 );
296 PL_DEBUG2( "finding next of %s within %s",
297 PLI_NAME( p_item ), PLI_NAME( p_root ) );
299 /* Now, walk the tree until we find a suitable next item */
303 bool b_ena_ok = true, b_unplayed_ok = true;
304 p_next = GetNextItem( p_playlist, p_root, p_next );
305 if( !p_next || p_next == p_root )
307 if( p_next->i_children == -1 )
309 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
311 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
312 b_unplayed_ok = false;
313 if( b_ena_ok && b_unplayed_ok ) break;
316 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
321 * Finds the previous item to play
323 * \param p_playlist the playlist
324 * \param p_root the root node
325 * \param p_item the previous item (NULL if none )
326 * \return the next item to play, or NULL if none found
328 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
329 playlist_item_t *p_root,
330 playlist_item_t *p_item,
331 bool b_ena, bool b_unplayed )
334 playlist_item_t *p_prev;
336 PL_DEBUG2( "finding previous of %s within %s", PLI_NAME( p_item ),
337 PLI_NAME( p_root ) );
338 assert( p_root && p_root->i_children != -1 );
340 /* Now, walk the tree until we find a suitable previous item */
344 bool b_ena_ok = true, b_unplayed_ok = true;
345 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
346 if( !p_prev || p_prev == p_root )
348 if( p_prev->i_children == -1 )
350 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
352 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
353 b_unplayed_ok = false;
354 if( b_ena_ok && b_unplayed_ok ) break;
357 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
361 /************************************************************************
362 * Following functions are local
363 ***********************************************************************/
366 * Get the next item in the tree
367 * If p_item is NULL, return the first child of root
369 playlist_item_t *GetNextItem( playlist_t *p_playlist,
370 playlist_item_t *p_root,
371 playlist_item_t *p_item )
373 /* If the item is NULL, return the firt child of root */
376 if( p_root->i_children > 0 )
377 return p_root->pp_children[0];
382 /* Node with children, get the first one */
383 if( p_item->i_children > 0 )
384 return p_item->pp_children[0];
386 playlist_item_t* p_parent = p_item->p_parent;
387 for( int i = 0 ; i < p_parent->i_children ; i++ )
389 if( p_parent->pp_children[i] == p_item )
391 // Return the next children
392 if( i + 1 < p_parent->i_children )
393 return p_parent->pp_children[i+1];
394 // We are the least one, so try to have uncles
397 PL_DEBUG2( "Current item is the last of the node,"
398 "looking for uncle from %s",
399 p_parent->p_input->psz_name );
400 if( p_parent == p_root )
402 PL_DEBUG2( "already at root" );
406 return GetNextUncle( p_playlist, p_item, p_root );
413 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
414 playlist_item_t *p_root )
416 playlist_item_t *p_parent = p_item->p_parent;
417 playlist_item_t *p_grandparent;
418 bool b_found = false;
422 if( p_parent != NULL )
424 p_grandparent = p_parent->p_parent;
425 while( p_grandparent )
428 for( i = 0 ; i< p_grandparent->i_children ; i++ )
430 if( p_parent == p_grandparent->pp_children[i] )
432 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
433 p_parent->p_input->psz_name, i,
434 p_grandparent->p_input->psz_name );
439 if( b_found && i + 1 < p_grandparent->i_children )
441 return p_grandparent->pp_children[i+1];
443 /* Not found at root */
444 if( p_grandparent == p_root )
450 p_parent = p_grandparent;
451 p_grandparent = p_parent->p_parent;
455 /* We reached root */
459 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
460 playlist_item_t *p_root )
462 playlist_item_t *p_parent = p_item->p_parent;
463 playlist_item_t *p_grandparent;
464 bool b_found = false;
468 if( p_parent != NULL )
470 p_grandparent = p_parent->p_parent;
474 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
476 if( p_parent == p_grandparent->pp_children[i] )
482 if( b_found && i - 1 > 0 )
484 return p_grandparent->pp_children[i-1];
486 /* Not found at root */
487 if( p_grandparent == p_root )
493 p_parent = p_grandparent;
494 p_grandparent = p_parent->p_parent;
498 /* We reached root */
503 /* Recursively search the tree for previous item */
504 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
505 playlist_item_t *p_root,
506 playlist_item_t *p_item )
508 playlist_item_t *p_parent;
511 /* Node with children, get the last one */
512 if( p_item && p_item->i_children > 0 )
513 return p_item->pp_children[p_item->i_children - 1];
515 /* Last child of its parent ? */
517 p_parent = p_item->p_parent;
520 msg_Err( p_playlist, "Get the last one" );
524 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
526 if( p_parent->pp_children[i] == p_item )
530 /* Was already the first sibling. Look for uncles */
531 PL_DEBUG2( "current item is the first of its node,"
532 "looking for uncle from %s",
533 p_parent->p_input->psz_name );
534 if( p_parent == p_root )
536 PL_DEBUG2( "already at root" );
539 return GetPrevUncle( p_playlist, p_item, p_root );
543 return p_parent->pp_children[i-1];