1 /*****************************************************************************
2 * view.c : Playlist views functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: item.c 7997 2004-06-18 11:35:45Z sigmunau $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
23 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
28 #include <vlc/input.h>
30 #include "vlc_playlist.h"
34 /************************************************************************
36 ************************************************************************/
39 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
40 playlist_item_t *, int );
42 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
44 playlist_item_t *p_root,
45 playlist_item_t *p_item,
46 playlist_item_t *p_parent );
48 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
50 playlist_item_t *p_root,
51 playlist_item_t *p_item,
52 playlist_item_t *p_parent );
54 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
57 /**********************************************************************
58 * Exported View management functions
59 **********************************************************************/
64 * \param p_playlist a playlist object
65 * \param i_id the view identifier
66 * \return the new view or NULL on failure
68 playlist_view_t * playlist_ViewCreate( playlist_t *p_playlist, int i_id,
71 playlist_view_t * p_view;
73 msg_Dbg( p_playlist, "creating view %i",i_id );
75 p_view = malloc( sizeof( playlist_view_t ) );
77 memset( p_view, 0, sizeof( playlist_view_t ) );
79 p_view->p_root = playlist_NodeCreate( p_playlist, i_id, NULL, NULL );
81 p_view->psz_name = psz_name ? strdup( psz_name ) : strdup(_("Undefined") );
87 * Creates a new view and add it to the list
89 * This function must be entered without the playlist lock
91 * \param p_playlist a playlist object
92 * \param i_id the view identifier
93 * \return VLC_SUCCESS or an error
95 int playlist_ViewInsert( playlist_t *p_playlist, int i_id, char *psz_name )
97 playlist_view_t *p_view =
98 playlist_ViewCreate( p_playlist, i_id , psz_name );
101 msg_Err( p_playlist, "Creation failed" );
105 vlc_mutex_lock( &p_playlist->object_lock );
107 INSERT_ELEM( p_playlist->pp_views, p_playlist->i_views,
108 p_playlist->i_views, p_view );
110 vlc_mutex_unlock( &p_playlist->object_lock );
118 * This function must be entered wit the playlist lock
120 * \param p_view the view to delete
123 int playlist_ViewDelete( playlist_t *p_playlist,playlist_view_t *p_view )
125 playlist_NodeDelete( p_playlist, p_view->p_root, VLC_TRUE, VLC_TRUE );
126 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, 0 );
131 * Dumps the content of a view
133 * \param p_playlist the playlist
134 * \param p_view the view to dump
137 int playlist_ViewDump( playlist_t *p_playlist, playlist_view_t *p_view )
139 msg_Dbg( p_playlist, "dumping view %i",p_view->i_id );
140 playlist_NodeDump( p_playlist,p_view->p_root, 1 );
145 * Counts the items of a view
147 * \param p_playlist the playlist
148 * \param p_view the view to count
149 * \return the number of items
151 int playlist_ViewItemCount( playlist_t *p_playlist,
152 playlist_view_t *p_view )
154 return playlist_NodeChildrenCount( p_playlist, p_view->p_root );
159 * Updates a view. Only make sense for "sorted" and "ALL" views
161 * \param p_playlist the playlist
162 * \param i_view the view to update
165 int playlist_ViewUpdate( playlist_t *p_playlist, int i_view)
167 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
174 if( i_view == VIEW_ALL )
176 p_view->p_root->i_children = p_playlist->i_size;
177 p_view->p_root->pp_children = p_playlist->pp_items;
180 /* Handle update of sorted views here */
181 if( i_view == VIEW_S_AUTHOR )
183 playlist_ViewEmpty( p_playlist, i_view, VLC_FALSE );
185 playlist_NodeGroup( p_playlist, i_view, p_view->p_root,
186 p_playlist->pp_items,p_playlist->i_size,
187 SORT_AUTHOR, ORDER_NORMAL );
198 * \param p_playlist the playlist
199 * \param i_id the id to find
200 * \return the found view or NULL if not found
202 playlist_view_t *playlist_ViewFind( playlist_t *p_playlist, int i_id )
205 for( i=0 ; i< p_playlist->i_views ; i++ )
207 if( p_playlist->pp_views[i]->i_id == i_id )
209 return p_playlist->pp_views[i];
216 int playlist_ViewEmpty( playlist_t *p_playlist, int i_view,
217 vlc_bool_t b_delete_items )
219 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
226 return playlist_NodeEmpty( p_playlist, p_view->p_root, b_delete_items );
229 /**********************************************************************
230 * Exported Nodes management functions
231 **********************************************************************/
236 * Create a playlist node
238 * \param p_playlist the playlist
239 * \paam psz_name the name of the node
240 * \param p_parent the parent node to attach to or NULL if no attach
241 * \return the new node
243 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, int i_view,
245 playlist_item_t *p_parent )
247 /* Create the item */
248 playlist_item_t *p_item = (playlist_item_t *)malloc(
249 sizeof( playlist_item_t ) );
251 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(playlist_add_t));
253 vlc_input_item_Init( VLC_OBJECT(p_playlist), &p_item->input );
259 if( psz_name != NULL )
261 p_item->input.psz_name = strdup( psz_name );
265 p_item->input.psz_name = strdup( _("Undefined") );
268 p_item->input.psz_uri = NULL;
270 p_item->b_enabled = VLC_TRUE;
271 p_item->i_nb_played = 0;
275 p_item->i_children = 0;
276 p_item->pp_children = NULL;
278 p_item->input.i_duration = -1;
279 p_item->input.ppsz_options = NULL;
280 p_item->input.i_options = 0;
281 p_item->input.i_categories = 0;
282 p_item->input.pp_categories = NULL;
283 p_item->input.i_id = ++p_playlist->i_last_id;
285 p_item->input.i_type = ITEM_TYPE_NODE;
287 p_item->pp_parents = NULL;
288 p_item->i_parents = 0;
290 p_item->i_flags |= PLAYLIST_SKIP_FLAG; /* Default behaviour */
292 vlc_mutex_init( p_playlist, &p_item->input.lock );
294 if( p_parent != NULL )
296 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
299 p_add->p_node = p_parent;
300 p_add->p_item = p_item;
301 p_add->i_view = i_view;
302 val.p_address = p_add;
303 var_Set( p_playlist, "item-append", val);
311 * Remove all the children of a node
313 * This function must be entered with the playlist lock
315 * \param p_playlist the playlist
316 * \param p_root the node
317 * \param b_delete_items do we have to delete the children items ?
318 * \return VLC_SUCCESS or an error
320 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
321 vlc_bool_t b_delete_items )
324 if( p_root->i_children == -1 )
329 /* Delete the children */
330 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
332 if( p_root->pp_children[i]->i_children > -1 )
334 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
335 b_delete_items , VLC_FALSE );
337 else if( b_delete_items )
339 /* Delete the item here */
340 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
347 * Remove all the children of a node and removes the node
349 * \param p_playlist the playlist
350 * \param p_root the node
351 * \param b_delete_items do we have to delete the children items ?
352 * \return VLC_SUCCESS or an error
354 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
355 vlc_bool_t b_delete_items, vlc_bool_t b_force )
358 if( p_root->i_children == -1 )
363 /* Delete the children */
364 for( i = p_root->i_children - 1 ; i >= 0; i-- )
366 if( p_root->pp_children[i]->i_children > -1 )
368 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
369 b_delete_items , b_force );
371 else if( b_delete_items )
373 /* Delete the item here */
374 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
377 /* Delete the node */
378 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
383 for( i = 0 ; i< p_root->i_parents; i++ )
385 playlist_NodeRemoveItem( p_playlist, p_root,
386 p_root->pp_parents[i]->p_parent );
388 var_SetInteger( p_playlist, "item-deleted", p_root->input.i_id );
389 playlist_ItemDelete( p_root );
396 * Adds an item to the childs of a node
398 * \param p_playlist the playlist
399 * \param i_view the view of the node ( needed for parent search )
400 * \param p_item the item to append
401 * \param p_parent the parent node
402 * \return VLC_SUCCESS or an error
404 int playlist_NodeAppend( playlist_t *p_playlist,
406 playlist_item_t *p_item,
407 playlist_item_t *p_parent )
409 return playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, -1 );
412 int playlist_NodeInsert( playlist_t *p_playlist,
414 playlist_item_t *p_item,
415 playlist_item_t *p_parent,
419 vlc_bool_t b_found = VLC_FALSE;
420 if( !p_parent || p_parent->i_children == -1 )
422 msg_Err( p_playlist, "invalid node" );
426 if( i_position == -1 ) i_position = p_parent->i_children ;
428 INSERT_ELEM( p_parent->pp_children,
429 p_parent->i_children,
433 /* Add the parent to the array */
434 for( i= 0; i< p_item->i_parents ; i++ )
436 if( p_item->pp_parents[i]->i_view == i_view )
442 if( b_found == VLC_FALSE )
444 struct item_parent_t *p_ip = (struct item_parent_t *)
445 malloc(sizeof(struct item_parent_t) );
446 p_ip->i_view = i_view;
447 p_ip->p_parent = p_parent;
449 INSERT_ELEM( p_item->pp_parents,
450 p_item->i_parents, p_item->i_parents,
454 /* Let the interface know this has been updated */
455 p_parent->i_serial++;
460 * Deletes an item from the children of a node
462 * \param p_playlist the playlist
463 * \param p_item the item to remove
464 * \param p_parent the parent node
465 * \return VLC_SUCCESS or an error
467 int playlist_NodeRemoveItem( playlist_t *p_playlist,
468 playlist_item_t *p_item,
469 playlist_item_t *p_parent )
472 if( !p_parent || p_parent->i_children == -1 )
474 msg_Err( p_playlist, "invalid node" );
477 for( i= 0; i< p_parent->i_children ; i++ )
479 if( p_parent->pp_children[i] == p_item )
481 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
485 /* Let the interface know this has been updated */
486 p_parent->i_serial++;
493 * Count the children of a node
495 * \param p_playlist the playlist
496 * \param p_node the node
497 * \return the number of children
499 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
503 if( p_node->i_children == -1 )
508 for( i=0 ; i< p_node->i_children;i++ )
510 if( p_node->pp_children[i]->i_children == -1 )
516 i_nb += playlist_NodeChildrenCount( p_playlist,
517 p_node->pp_children[i] );
524 * Search a child of a node by its name
526 * \param p_node the node
527 * \param psz_search the name of the child to search
528 * \return the child item or NULL if not found or error
530 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
531 const char *psz_search )
535 if( p_node->i_children < 0 )
539 for( i = 0 ; i< p_node->i_children; i++ )
541 if( !strcmp( p_node->pp_children[i]->input.psz_name, psz_search ) )
543 return p_node->pp_children[i];
550 /**********************************************************************
552 **********************************************************************/
555 * Finds the next item to play
557 * \param p_playlist the playlist
558 * \param i_view the view
559 * \param p_root the root node
560 * \param p_node the node we are playing from
561 * \param p_item the item we were playing (NULL if none )
562 * \return the next item to play, or NULL if none found
564 playlist_item_t *playlist_FindNextFromParent( playlist_t *p_playlist,
565 int i_view, /* FIXME: useless */
566 playlist_item_t *p_root,
567 playlist_item_t *p_node,
568 playlist_item_t *p_item )
570 playlist_item_t *p_search, *p_next;
572 #ifdef PLAYLIST_DEBUG
575 msg_Dbg( p_playlist, "finding next of %s within %s",
576 p_item->input.psz_name, p_node->input.psz_name );
580 msg_Dbg( p_playlist, "finding something to play within %s",
581 p_node->input.psz_name );
586 if( !p_node || p_node->i_children == -1 )
588 msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
592 /* Find the parent node of the item */
595 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
596 if( p_search == NULL )
598 msg_Err( p_playlist, "parent node not found" );
607 /* Now, go up the tree until we find a suitable next item */
608 p_next = playlist_RecursiveFindNext( p_playlist,i_view,
609 p_node, p_item, p_search );
611 /* Not found, do we go past p_node ? */
614 if( p_playlist->b_go_next )
616 p_next = playlist_RecursiveFindNext( p_playlist, i_view,
617 p_root, p_item, p_search );
622 /* OK, we could continue, so set our current node to the root */
623 p_playlist->status.p_node = p_root;
634 * Finds the previous item to play
636 * \param p_playlist the playlist
637 * \param i_view the view
638 * \param p_root the root node
639 * \param p_node the node we are playing from
640 * \param p_item the item we were playing (NULL if none )
641 * \return the next item to play, or NULL if none found
643 playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
645 playlist_item_t *p_root,
646 playlist_item_t *p_node,
647 playlist_item_t *p_item )
649 playlist_item_t *p_search, *p_next;
651 #ifdef PLAYLIST_DEBUG
654 msg_Dbg( p_playlist, "Finding prev of %s within %s",
655 p_item->input.psz_name, p_node->input.psz_name );
659 msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
663 if( !p_node || p_node->i_children == -1 )
665 msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
669 /* Find the parent node of the item */
672 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
673 if( p_search == NULL )
675 msg_Err( p_playlist, "parent node not found" );
684 /* Now, go up the tree until we find a suitable next item */
685 p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
686 p_node, p_item, p_search );
690 if( p_playlist->b_go_next )
692 p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
693 p_root, p_item, p_search );
698 /* OK, we could continue, so set our current node to the root */
699 p_playlist->status.p_node = p_root;
709 /************************************************************************
710 * Following functions are local
711 ***********************************************************************/
714 /* Recursively search the tree for next item */
715 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
717 playlist_item_t *p_root,
718 playlist_item_t *p_item,
719 playlist_item_t *p_parent )
722 playlist_item_t *p_parent_parent;
724 for( i= 0 ; i < p_parent->i_children ; i++ )
726 if( p_parent->pp_children[i] == p_item || p_item == NULL )
732 #ifdef PLAYLIST_DEBUG
733 msg_Dbg( p_playlist,"Current item found, child %i of %s",
734 i , p_parent->input.psz_name );
736 /* We found our item */
737 if( i+1 >= p_parent->i_children )
740 #ifdef PLAYLIST_DEBUG
741 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
742 p_parent->input.psz_name );
744 if( p_parent == p_root )
746 /* Hmm, seems it's the end for you, guy ! */
750 /* Go up one level */
751 p_parent_parent = playlist_FindDirectParent( p_playlist,
753 if( p_parent_parent == NULL )
755 msg_Warn( p_playlist, "Unable to find parent !");
758 return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
759 p_parent, p_parent_parent );
763 if( p_parent->pp_children[i+1]->i_children == -1 )
765 /* Cool, we have found a real item to play */
766 #ifdef PLAYLIST_DEBUG
767 msg_Dbg( p_playlist, "Playing child %i of %s",
768 i+1 , p_parent->input.psz_name );
770 return p_parent->pp_children[i+1];
772 else if( p_parent->pp_children[i+1]->i_children > 0 )
774 /* Select the first child of this node */
775 #ifdef PLAYLIST_DEBUG
776 msg_Dbg( p_playlist, "%s is a node with children, "
778 p_parent->pp_children[i+1]->input.psz_name);
780 if( p_parent->pp_children[i+1]->pp_children[0]
783 /* first child is a node ! */
784 return playlist_RecursiveFindNext( p_playlist, i_view,
786 p_parent->pp_children[i+1]->pp_children[0]);
788 return p_parent->pp_children[i+1]->pp_children[0];
792 /* This node has no child... We must continue */
793 #ifdef PLAYLIST_DEBUG
794 msg_Dbg( p_playlist, "%s is a node with no children",
795 p_parent->pp_children[i+1]->input.psz_name);
797 p_item = p_parent->pp_children[i+1];
802 /* Just in case :) */
806 /* Recursively search the tree for previous item */
807 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
809 playlist_item_t *p_root,
810 playlist_item_t *p_item,
811 playlist_item_t *p_parent )
814 playlist_item_t *p_parent_parent;
816 for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
818 if( p_parent->pp_children[i] == p_item || p_item == NULL )
824 #ifdef PLAYLIST_DEBUG
825 msg_Dbg( p_playlist,"Current item found, child %i of %s",
826 i , p_parent->input.psz_name );
828 /* We found our item */
832 #ifdef PLAYLIST_DEBUG
833 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
834 p_parent->input.psz_name );
836 if( p_parent == p_root )
838 /* Hmm, seems it's the end for you, guy ! */
841 /* Go up one level */
842 p_parent_parent = playlist_FindDirectParent( p_playlist,
844 return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
845 p_parent, p_parent_parent );
849 if( p_parent->pp_children[i-1]->i_children == -1 )
851 /* Cool, we have found a real item to play */
852 #ifdef PLAYLIST_DEBUG
853 msg_Dbg( p_playlist, "Playing child %i of %s",
854 i-1, p_parent->input.psz_name );
856 return p_parent->pp_children[i-1];
858 else if( p_parent->pp_children[i-1]->i_children > 0 )
860 /* Select the last child of this node */
861 #ifdef PLAYLIST_DEBUG
862 msg_Dbg( p_playlist, "%s is a node with children,"
864 p_parent->pp_children[i-1]->input.psz_name);
866 if( p_parent->pp_children[i-1]->pp_children[p_parent->
867 pp_children[i-1]->i_children-1]->i_children >= 0 )
869 /* Last child is a node */
870 return playlist_RecursiveFindPrev( p_playlist, i_view,
872 p_parent->pp_children[i-1]->pp_children[
873 p_parent->pp_children[i-1]->i_children-1]);
875 return p_parent->pp_children[i-1]->pp_children[
876 p_parent->pp_children[i-1]->i_children-1];
880 /* This node has no child... We must continue */
881 #ifdef PLAYLIST_DEBUG
882 msg_Dbg( p_playlist, "%s is a node with no children",
883 p_parent->pp_children[i-1]->input.psz_name);
885 p_item = p_parent->pp_children[i-1];
893 /* This function returns the parent of an item in a view */
894 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
895 playlist_item_t *p_item,
899 for( i= 0; i< p_item->i_parents ; i++ )
901 if( p_item->pp_parents[i]->i_view == i_view )
903 return p_item->pp_parents[i]->p_parent;
910 /* This function dumps a node */
911 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
919 msg_Dbg( p_playlist, "%s (%i)",
920 p_item->input.psz_name, p_item->i_children );
923 if( p_item->i_children == -1 )
928 for( i = 0; i< p_item->i_children; i++ )
930 memset( str, 32, 512 );
931 sprintf( str + 2 * i_level , "%s (%i)",
932 p_item->pp_children[i]->input.psz_name,
933 p_item->pp_children[i]->i_children );
934 msg_Dbg( p_playlist, "%s",str );
935 if( p_item->pp_children[i]->i_children >= 0 )
937 playlist_NodeDump( p_playlist, p_item->pp_children[i],