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 p_view = malloc( sizeof( playlist_view_t ) );
75 memset( p_view, 0, sizeof( playlist_view_t ) );
77 p_view->p_root = playlist_NodeCreate( p_playlist, i_id, NULL, NULL );
79 p_view->psz_name = psz_name ? strdup( psz_name ) : strdup(_("Undefined") );
85 * Creates a new view and add it to the list
87 * This function must be entered without the playlist lock
89 * \param p_playlist a playlist object
90 * \param i_id the view identifier
91 * \return VLC_SUCCESS or an error
93 int playlist_ViewInsert( playlist_t *p_playlist, int i_id, char *psz_name )
95 playlist_view_t *p_view =
96 playlist_ViewCreate( p_playlist, i_id , psz_name );
99 msg_Err( p_playlist, "Creation failed" );
103 vlc_mutex_lock( &p_playlist->object_lock );
105 INSERT_ELEM( p_playlist->pp_views, p_playlist->i_views,
106 p_playlist->i_views, p_view );
108 vlc_mutex_unlock( &p_playlist->object_lock );
116 * This function must be entered wit the playlist lock
118 * \param p_view the view to delete
121 int playlist_ViewDelete( playlist_t *p_playlist,playlist_view_t *p_view )
123 playlist_NodeDelete( p_playlist, p_view->p_root, VLC_TRUE, VLC_TRUE );
124 REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, 0 );
130 * Dumps the content of a view
132 * \param p_playlist the playlist
133 * \param p_view the view to dump
136 int playlist_ViewDump( playlist_t *p_playlist, playlist_view_t *p_view )
138 #ifdef PLAYLIST_DEBUG
139 msg_Dbg( p_playlist, "dumping view %i",p_view->i_id );
140 playlist_NodeDump( p_playlist,p_view->p_root, 1 );
146 * Counts the items of a view
148 * \param p_playlist the playlist
149 * \param p_view the view to count
150 * \return the number of items
152 int playlist_ViewItemCount( playlist_t *p_playlist,
153 playlist_view_t *p_view )
155 return playlist_NodeChildrenCount( p_playlist, p_view->p_root );
160 * Updates a view. Only make sense for "sorted" and "ALL" views
162 * \param p_playlist the playlist
163 * \param i_view the view to update
166 int playlist_ViewUpdate( playlist_t *p_playlist, int i_view)
168 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
175 if( i_view == VIEW_ALL )
177 p_view->p_root->i_children = p_playlist->i_size;
178 p_view->p_root->pp_children = p_playlist->pp_items;
181 /* Handle update of sorted views here */
182 if( i_view == VIEW_S_AUTHOR )
184 playlist_ViewEmpty( p_playlist, i_view, VLC_FALSE );
186 playlist_NodeGroup( p_playlist, i_view, p_view->p_root,
187 p_playlist->pp_items,p_playlist->i_size,
188 SORT_AUTHOR, ORDER_NORMAL );
199 * \param p_playlist the playlist
200 * \param i_id the id to find
201 * \return the found view or NULL if not found
203 playlist_view_t *playlist_ViewFind( playlist_t *p_playlist, int i_id )
206 for( i=0 ; i< p_playlist->i_views ; i++ )
208 if( p_playlist->pp_views[i]->i_id == i_id )
210 return p_playlist->pp_views[i];
217 int playlist_ViewEmpty( playlist_t *p_playlist, int i_view,
218 vlc_bool_t b_delete_items )
220 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
227 return playlist_NodeEmpty( p_playlist, p_view->p_root, b_delete_items );
230 /**********************************************************************
231 * Exported Nodes management functions
232 **********************************************************************/
237 * Create a playlist node
239 * \param p_playlist the playlist
240 * \paam psz_name the name of the node
241 * \param p_parent the parent node to attach to or NULL if no attach
242 * \return the new node
244 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, int i_view,
246 playlist_item_t *p_parent )
248 /* Create the item */
249 playlist_item_t *p_item = (playlist_item_t *)malloc(
250 sizeof( playlist_item_t ) );
252 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(playlist_add_t));
254 vlc_input_item_Init( VLC_OBJECT(p_playlist), &p_item->input );
260 if( psz_name != NULL )
262 p_item->input.psz_name = strdup( psz_name );
266 p_item->input.psz_name = strdup( _("Undefined") );
269 p_item->input.psz_uri = NULL;
271 p_item->b_enabled = VLC_TRUE;
272 p_item->i_nb_played = 0;
276 p_item->i_children = 0;
277 p_item->pp_children = NULL;
279 p_item->input.i_duration = -1;
280 p_item->input.ppsz_options = NULL;
281 p_item->input.i_options = 0;
282 p_item->input.i_categories = 0;
283 p_item->input.pp_categories = NULL;
284 p_item->input.i_id = ++p_playlist->i_last_id;
286 p_item->input.i_type = ITEM_TYPE_NODE;
288 p_item->pp_parents = NULL;
289 p_item->i_parents = 0;
291 p_item->i_flags |= PLAYLIST_SKIP_FLAG; /* Default behaviour */
293 vlc_mutex_init( p_playlist, &p_item->input.lock );
295 if( p_parent != NULL )
297 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
300 p_add->p_node = p_parent;
301 p_add->p_item = p_item;
302 p_add->i_view = i_view;
303 val.p_address = p_add;
304 var_Set( p_playlist, "item-append", val);
312 * Remove all the children of a node
314 * This function must be entered with the playlist lock
316 * \param p_playlist the playlist
317 * \param p_root the node
318 * \param b_delete_items do we have to delete the children items ?
319 * \return VLC_SUCCESS or an error
321 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
322 vlc_bool_t b_delete_items )
325 if( p_root->i_children == -1 )
330 /* Delete the children */
331 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
333 if( p_root->pp_children[i]->i_children > -1 )
335 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
336 b_delete_items , VLC_FALSE );
338 else if( b_delete_items )
340 /* Delete the item here */
341 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
348 * Remove all the children of a node and removes the node
350 * \param p_playlist the playlist
351 * \param p_root the node
352 * \param b_delete_items do we have to delete the children items ?
353 * \return VLC_SUCCESS or an error
355 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
356 vlc_bool_t b_delete_items, vlc_bool_t b_force )
359 if( p_root->i_children == -1 )
364 /* Delete the children */
365 for( i = p_root->i_children - 1 ; i >= 0; i-- )
367 if( p_root->pp_children[i]->i_children > -1 )
369 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
370 b_delete_items , b_force );
372 else if( b_delete_items )
374 /* Delete the item here */
375 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
378 /* Delete the node */
379 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
384 for( i = 0 ; i< p_root->i_parents; i++ )
386 playlist_NodeRemoveItem( p_playlist, p_root,
387 p_root->pp_parents[i]->p_parent );
389 var_SetInteger( p_playlist, "item-deleted", p_root->input.i_id );
390 playlist_ItemDelete( p_root );
397 * Adds an item to the childs of a node
399 * \param p_playlist the playlist
400 * \param i_view the view of the node ( needed for parent search )
401 * \param p_item the item to append
402 * \param p_parent the parent node
403 * \return VLC_SUCCESS or an error
405 int playlist_NodeAppend( playlist_t *p_playlist,
407 playlist_item_t *p_item,
408 playlist_item_t *p_parent )
410 return playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, -1 );
413 int playlist_NodeInsert( playlist_t *p_playlist,
415 playlist_item_t *p_item,
416 playlist_item_t *p_parent,
420 vlc_bool_t b_found = VLC_FALSE;
421 if( !p_parent || p_parent->i_children == -1 )
423 msg_Err( p_playlist, "invalid node" );
427 if( i_position == -1 ) i_position = p_parent->i_children ;
429 INSERT_ELEM( p_parent->pp_children,
430 p_parent->i_children,
434 /* Add the parent to the array */
435 for( i= 0; i< p_item->i_parents ; i++ )
437 if( p_item->pp_parents[i]->i_view == i_view )
443 if( b_found == VLC_FALSE )
445 struct item_parent_t *p_ip = (struct item_parent_t *)
446 malloc(sizeof(struct item_parent_t) );
447 p_ip->i_view = i_view;
448 p_ip->p_parent = p_parent;
450 INSERT_ELEM( p_item->pp_parents,
451 p_item->i_parents, p_item->i_parents,
455 /* Let the interface know this has been updated */
456 p_parent->i_serial++;
461 * Deletes an item from the children of a node
463 * \param p_playlist the playlist
464 * \param p_item the item to remove
465 * \param p_parent the parent node
466 * \return VLC_SUCCESS or an error
468 int playlist_NodeRemoveItem( playlist_t *p_playlist,
469 playlist_item_t *p_item,
470 playlist_item_t *p_parent )
473 if( !p_parent || p_parent->i_children == -1 )
475 msg_Err( p_playlist, "invalid node" );
478 for( i= 0; i< p_parent->i_children ; i++ )
480 if( p_parent->pp_children[i] == p_item )
482 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
486 /* Let the interface know this has been updated */
487 p_parent->i_serial++;
494 * Count the children of a node
496 * \param p_playlist the playlist
497 * \param p_node the node
498 * \return the number of children
500 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
504 if( p_node->i_children == -1 )
509 for( i=0 ; i< p_node->i_children;i++ )
511 if( p_node->pp_children[i]->i_children == -1 )
517 i_nb += playlist_NodeChildrenCount( p_playlist,
518 p_node->pp_children[i] );
525 * Search a child of a node by its name
527 * \param p_node the node
528 * \param psz_search the name of the child to search
529 * \return the child item or NULL if not found or error
531 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
532 const char *psz_search )
536 if( p_node->i_children < 0 )
540 for( i = 0 ; i< p_node->i_children; i++ )
542 if( !strcmp( p_node->pp_children[i]->input.psz_name, psz_search ) )
544 return p_node->pp_children[i];
551 /**********************************************************************
553 **********************************************************************/
556 * Finds the next item to play
558 * \param p_playlist the playlist
559 * \param i_view the view
560 * \param p_root the root node
561 * \param p_node the node we are playing from
562 * \param p_item the item we were playing (NULL if none )
563 * \return the next item to play, or NULL if none found
565 playlist_item_t *playlist_FindNextFromParent( playlist_t *p_playlist,
566 int i_view, /* FIXME: useless */
567 playlist_item_t *p_root,
568 playlist_item_t *p_node,
569 playlist_item_t *p_item )
571 playlist_item_t *p_search, *p_next;
573 #ifdef PLAYLIST_DEBUG
576 msg_Dbg( p_playlist, "finding next of %s within %s",
577 p_item->input.psz_name, p_node->input.psz_name );
581 msg_Dbg( p_playlist, "finding something to play within %s",
582 p_node->input.psz_name );
587 if( !p_node || p_node->i_children == -1 )
589 msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
593 /* Find the parent node of the item */
596 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
597 if( p_search == NULL )
599 msg_Err( p_playlist, "parent node not found" );
608 /* Now, go up the tree until we find a suitable next item */
609 p_next = playlist_RecursiveFindNext( p_playlist,i_view,
610 p_node, p_item, p_search );
612 /* Not found, do we go past p_node ? */
615 if( p_playlist->b_go_next )
617 p_next = playlist_RecursiveFindNext( p_playlist, i_view,
618 p_root, p_item, p_search );
623 /* OK, we could continue, so set our current node to the root */
624 p_playlist->status.p_node = p_root;
635 * Finds the previous item to play
637 * \param p_playlist the playlist
638 * \param i_view the view
639 * \param p_root the root node
640 * \param p_node the node we are playing from
641 * \param p_item the item we were playing (NULL if none )
642 * \return the next item to play, or NULL if none found
644 playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
646 playlist_item_t *p_root,
647 playlist_item_t *p_node,
648 playlist_item_t *p_item )
650 playlist_item_t *p_search, *p_next;
652 #ifdef PLAYLIST_DEBUG
655 msg_Dbg( p_playlist, "Finding prev of %s within %s",
656 p_item->input.psz_name, p_node->input.psz_name );
660 msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
664 if( !p_node || p_node->i_children == -1 )
666 msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
670 /* Find the parent node of the item */
673 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
674 if( p_search == NULL )
676 msg_Err( p_playlist, "parent node not found" );
685 /* Now, go up the tree until we find a suitable next item */
686 p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
687 p_node, p_item, p_search );
691 if( p_playlist->b_go_next )
693 p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
694 p_root, p_item, p_search );
699 /* OK, we could continue, so set our current node to the root */
700 p_playlist->status.p_node = p_root;
710 /************************************************************************
711 * Following functions are local
712 ***********************************************************************/
715 /* Recursively search the tree for next item */
716 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
718 playlist_item_t *p_root,
719 playlist_item_t *p_item,
720 playlist_item_t *p_parent )
723 playlist_item_t *p_parent_parent;
725 for( i= 0 ; i < p_parent->i_children ; i++ )
727 if( p_parent->pp_children[i] == p_item || p_item == NULL )
733 #ifdef PLAYLIST_DEBUG
734 msg_Dbg( p_playlist,"Current item found, child %i of %s",
735 i , p_parent->input.psz_name );
737 /* We found our item */
738 if( i+1 >= p_parent->i_children )
741 #ifdef PLAYLIST_DEBUG
742 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
743 p_parent->input.psz_name );
745 if( p_parent == p_root )
747 /* Hmm, seems it's the end for you, guy ! */
751 /* Go up one level */
752 p_parent_parent = playlist_FindDirectParent( p_playlist,
754 if( p_parent_parent == NULL )
756 msg_Warn( p_playlist, "Unable to find parent !");
759 return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
760 p_parent, p_parent_parent );
764 if( p_parent->pp_children[i+1]->i_children == -1 )
766 /* Cool, we have found a real item to play */
767 #ifdef PLAYLIST_DEBUG
768 msg_Dbg( p_playlist, "Playing child %i of %s",
769 i+1 , p_parent->input.psz_name );
771 return p_parent->pp_children[i+1];
773 else if( p_parent->pp_children[i+1]->i_children > 0 )
775 /* Select the first child of this node */
776 #ifdef PLAYLIST_DEBUG
777 msg_Dbg( p_playlist, "%s is a node with children, "
779 p_parent->pp_children[i+1]->input.psz_name);
781 if( p_parent->pp_children[i+1]->pp_children[0]
784 /* first child is a node ! */
785 return playlist_RecursiveFindNext( p_playlist, i_view,
787 p_parent->pp_children[i+1]->pp_children[0]);
789 return p_parent->pp_children[i+1]->pp_children[0];
793 /* This node has no child... We must continue */
794 #ifdef PLAYLIST_DEBUG
795 msg_Dbg( p_playlist, "%s is a node with no children",
796 p_parent->pp_children[i+1]->input.psz_name);
798 p_item = p_parent->pp_children[i+1];
803 /* Just in case :) */
807 /* Recursively search the tree for previous item */
808 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
810 playlist_item_t *p_root,
811 playlist_item_t *p_item,
812 playlist_item_t *p_parent )
815 playlist_item_t *p_parent_parent;
817 for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
819 if( p_parent->pp_children[i] == p_item || p_item == NULL )
825 #ifdef PLAYLIST_DEBUG
826 msg_Dbg( p_playlist,"Current item found, child %i of %s",
827 i , p_parent->input.psz_name );
829 /* We found our item */
833 #ifdef PLAYLIST_DEBUG
834 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
835 p_parent->input.psz_name );
837 if( p_parent == p_root )
839 /* Hmm, seems it's the end for you, guy ! */
842 /* Go up one level */
843 p_parent_parent = playlist_FindDirectParent( p_playlist,
845 return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
846 p_parent, p_parent_parent );
850 if( p_parent->pp_children[i-1]->i_children == -1 )
852 /* Cool, we have found a real item to play */
853 #ifdef PLAYLIST_DEBUG
854 msg_Dbg( p_playlist, "Playing child %i of %s",
855 i-1, p_parent->input.psz_name );
857 return p_parent->pp_children[i-1];
859 else if( p_parent->pp_children[i-1]->i_children > 0 )
861 /* Select the last child of this node */
862 #ifdef PLAYLIST_DEBUG
863 msg_Dbg( p_playlist, "%s is a node with children,"
865 p_parent->pp_children[i-1]->input.psz_name);
867 if( p_parent->pp_children[i-1]->pp_children[p_parent->
868 pp_children[i-1]->i_children-1]->i_children >= 0 )
870 /* Last child is a node */
871 return playlist_RecursiveFindPrev( p_playlist, i_view,
873 p_parent->pp_children[i-1]->pp_children[
874 p_parent->pp_children[i-1]->i_children-1]);
876 return p_parent->pp_children[i-1]->pp_children[
877 p_parent->pp_children[i-1]->i_children-1];
881 /* This node has no child... We must continue */
882 #ifdef PLAYLIST_DEBUG
883 msg_Dbg( p_playlist, "%s is a node with no children",
884 p_parent->pp_children[i-1]->input.psz_name);
886 p_item = p_parent->pp_children[i-1];
894 /* This function returns the parent of an item in a view */
895 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
896 playlist_item_t *p_item,
900 for( i= 0; i< p_item->i_parents ; i++ )
902 if( p_item->pp_parents[i]->i_view == i_view )
904 return p_item->pp_parents[i]->p_parent;
911 #ifdef PLAYLIST_DEBUG
912 /* This function dumps a node : to be used only for debug*/
913 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
921 msg_Dbg( p_playlist, "%s (%i)",
922 p_item->input.psz_name, p_item->i_children );
925 if( p_item->i_children == -1 )
930 for( i = 0; i< p_item->i_children; i++ )
932 memset( str, 32, 512 );
933 sprintf( str + 2 * i_level , "%s (%i)",
934 p_item->pp_children[i]->input.psz_name,
935 p_item->pp_children[i]->i_children );
936 msg_Dbg( p_playlist, "%s",str );
937 if( p_item->pp_children[i]->i_children >= 0 )
939 playlist_NodeDump( p_playlist, p_item->pp_children[i],