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_FIRST_SORTED )
185 playlist_ViewEmpty( p_playlist, i_view, VLC_FALSE );
189 case VIEW_S_AUTHOR: i_sort_type = SORT_AUTHOR;break;
190 case VIEW_S_GENRE: i_sort_type = SORT_GENRE;break;
191 default: i_sort_type = SORT_AUTHOR;
193 playlist_NodeGroup( p_playlist, i_view, p_view->p_root,
194 p_playlist->pp_items,p_playlist->i_size,
195 i_sort_type, ORDER_NORMAL );
206 * \param p_playlist the playlist
207 * \param i_id the id to find
208 * \return the found view or NULL if not found
210 playlist_view_t *playlist_ViewFind( playlist_t *p_playlist, int i_id )
213 for( i=0 ; i< p_playlist->i_views ; i++ )
215 if( p_playlist->pp_views[i]->i_id == i_id )
217 return p_playlist->pp_views[i];
224 int playlist_ViewEmpty( playlist_t *p_playlist, int i_view,
225 vlc_bool_t b_delete_items )
227 playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
234 return playlist_NodeEmpty( p_playlist, p_view->p_root, b_delete_items );
237 /**********************************************************************
238 * Exported Nodes management functions
239 **********************************************************************/
244 * Create a playlist node
246 * \param p_playlist the playlist
247 * \paam psz_name the name of the node
248 * \param p_parent the parent node to attach to or NULL if no attach
249 * \return the new node
251 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, int i_view,
253 playlist_item_t *p_parent )
255 /* Create the item */
256 playlist_item_t *p_item = (playlist_item_t *)malloc(
257 sizeof( playlist_item_t ) );
259 playlist_add_t *p_add;
265 p_add = (playlist_add_t*)malloc( sizeof(playlist_add_t) );
271 vlc_input_item_Init( VLC_OBJECT(p_playlist), &p_item->input );
273 if( psz_name != NULL )
275 p_item->input.psz_name = strdup( psz_name );
279 p_item->input.psz_name = strdup( _("Undefined") );
282 p_item->input.psz_uri = NULL;
284 p_item->b_enabled = VLC_TRUE;
285 p_item->i_nb_played = 0;
289 p_item->i_children = 0;
290 p_item->pp_children = NULL;
292 p_item->input.i_duration = -1;
293 p_item->input.ppsz_options = NULL;
294 p_item->input.i_options = 0;
295 p_item->input.i_categories = 0;
296 p_item->input.pp_categories = NULL;
297 p_item->input.i_id = ++p_playlist->i_last_id;
299 p_item->input.i_type = ITEM_TYPE_NODE;
301 p_item->pp_parents = NULL;
302 p_item->i_parents = 0;
304 p_item->i_flags |= PLAYLIST_SKIP_FLAG; /* Default behaviour */
306 vlc_mutex_init( p_playlist, &p_item->input.lock );
308 INSERT_ELEM( p_playlist->pp_all_items,
309 p_playlist->i_all_size,
310 p_playlist->i_all_size,
313 if( p_parent != NULL )
315 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
318 p_add->i_node = p_parent ? p_parent->input.i_id : -1;
319 p_add->i_item = p_item->input.i_id;
320 p_add->i_view = i_view;
321 val.p_address = p_add;
322 var_Set( p_playlist, "item-append", val);
330 * Remove all the children of a node
332 * This function must be entered with the playlist lock
334 * \param p_playlist the playlist
335 * \param p_root the node
336 * \param b_delete_items do we have to delete the children items ?
337 * \return VLC_SUCCESS or an error
339 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
340 vlc_bool_t b_delete_items )
343 if( p_root->i_children == -1 )
348 /* Delete the children */
349 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
351 if( p_root->pp_children[i]->i_children > -1 )
353 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
354 b_delete_items , VLC_FALSE );
356 else if( b_delete_items )
358 /* Delete the item here */
359 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
366 * Remove all the children of a node and removes the node
368 * \param p_playlist the playlist
369 * \param p_root the node
370 * \param b_delete_items do we have to delete the children items ?
371 * \return VLC_SUCCESS or an error
373 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
374 vlc_bool_t b_delete_items, vlc_bool_t b_force )
376 int i, i_top, i_bottom;
377 if( p_root->i_children == -1 )
382 /* Delete the children */
383 for( i = p_root->i_children - 1 ; i >= 0; i-- )
385 if( p_root->pp_children[i]->i_children > -1 )
387 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
388 b_delete_items , b_force );
390 else if( b_delete_items )
392 /* Delete the item here */
393 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
396 /* Delete the node */
397 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
402 for( i = 0 ; i< p_root->i_parents; i++ )
404 playlist_NodeRemoveItem( p_playlist, p_root,
405 p_root->pp_parents[i]->p_parent );
407 var_SetInteger( p_playlist, "item-deleted", p_root->input.i_id );
409 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
411 while( p_playlist->pp_all_items[i]->input.i_id != p_root->input.i_id &&
414 if( p_playlist->pp_all_items[i]->input.i_id < p_root->input.i_id )
422 i = i_bottom + ( i_top - i_bottom ) / 2;
424 if( p_playlist->pp_all_items[i]->input.i_id == p_root->input.i_id )
426 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
428 playlist_ItemDelete( p_root );
435 * Adds an item to the childs of a node
437 * \param p_playlist the playlist
438 * \param i_view the view of the node ( needed for parent search )
439 * \param p_item the item to append
440 * \param p_parent the parent node
441 * \return VLC_SUCCESS or an error
443 int playlist_NodeAppend( playlist_t *p_playlist,
445 playlist_item_t *p_item,
446 playlist_item_t *p_parent )
448 return playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, -1 );
451 int playlist_NodeInsert( playlist_t *p_playlist,
453 playlist_item_t *p_item,
454 playlist_item_t *p_parent,
458 vlc_bool_t b_found = VLC_FALSE;
459 if( !p_parent || p_parent->i_children == -1 )
461 msg_Err( p_playlist, "invalid node" );
465 if( i_position == -1 ) i_position = p_parent->i_children ;
467 INSERT_ELEM( p_parent->pp_children,
468 p_parent->i_children,
472 /* Add the parent to the array */
473 for( i= 0; i< p_item->i_parents ; i++ )
475 if( p_item->pp_parents[i]->i_view == i_view )
481 if( b_found == VLC_FALSE )
483 struct item_parent_t *p_ip = (struct item_parent_t *)
484 malloc(sizeof(struct item_parent_t) );
485 p_ip->i_view = i_view;
486 p_ip->p_parent = p_parent;
488 INSERT_ELEM( p_item->pp_parents,
489 p_item->i_parents, p_item->i_parents,
493 /* Let the interface know this has been updated */
494 p_parent->i_serial++;
499 * Deletes an item from the children of a node
501 * \param p_playlist the playlist
502 * \param p_item the item to remove
503 * \param p_parent the parent node
504 * \return VLC_SUCCESS or an error
506 int playlist_NodeRemoveItem( playlist_t *p_playlist,
507 playlist_item_t *p_item,
508 playlist_item_t *p_parent )
511 if( !p_parent || p_parent->i_children == -1 )
513 msg_Err( p_playlist, "invalid node" );
516 for( i= 0; i< p_parent->i_children ; i++ )
518 if( p_parent->pp_children[i] == p_item )
520 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
524 /* Let the interface know this has been updated */
525 p_parent->i_serial++;
532 * Count the children of a node
534 * \param p_playlist the playlist
535 * \param p_node the node
536 * \return the number of children
538 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
542 if( p_node->i_children == -1 )
547 for( i=0 ; i< p_node->i_children;i++ )
549 if( p_node->pp_children[i]->i_children == -1 )
555 i_nb += playlist_NodeChildrenCount( p_playlist,
556 p_node->pp_children[i] );
563 * Search a child of a node by its name
565 * \param p_node the node
566 * \param psz_search the name of the child to search
567 * \return the child item or NULL if not found or error
569 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
570 const char *psz_search )
574 if( p_node->i_children < 0 )
578 for( i = 0 ; i< p_node->i_children; i++ )
580 if( !strcmp( p_node->pp_children[i]->input.psz_name, psz_search ) )
582 return p_node->pp_children[i];
589 /**********************************************************************
591 **********************************************************************/
594 * Finds the next item to play
596 * \param p_playlist the playlist
597 * \param i_view the view
598 * \param p_root the root node
599 * \param p_node the node we are playing from
600 * \param p_item the item we were playing (NULL if none )
601 * \return the next item to play, or NULL if none found
603 playlist_item_t *playlist_FindNextFromParent( playlist_t *p_playlist,
604 int i_view, /* FIXME: useless */
605 playlist_item_t *p_root,
606 playlist_item_t *p_node,
607 playlist_item_t *p_item )
609 playlist_item_t *p_search, *p_next;
611 #ifdef PLAYLIST_DEBUG
614 msg_Dbg( p_playlist, "finding next of %s within %s",
615 p_item->input.psz_name, p_node->input.psz_name );
619 msg_Dbg( p_playlist, "finding something to play within %s",
620 p_node->input.psz_name );
625 if( !p_node || p_node->i_children == -1 )
627 msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
631 /* Find the parent node of the item */
634 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
635 if( p_search == NULL )
637 msg_Err( p_playlist, "parent node not found" );
646 /* Now, go up the tree until we find a suitable next item */
647 p_next = playlist_RecursiveFindNext( p_playlist,i_view,
648 p_node, p_item, p_search );
650 /* Not found, do we go past p_node ? */
653 if( p_playlist->b_go_next )
655 p_next = playlist_RecursiveFindNext( p_playlist, i_view,
656 p_root, p_item, p_search );
661 /* OK, we could continue, so set our current node to the root */
662 p_playlist->status.p_node = p_root;
673 * Finds the previous item to play
675 * \param p_playlist the playlist
676 * \param i_view the view
677 * \param p_root the root node
678 * \param p_node the node we are playing from
679 * \param p_item the item we were playing (NULL if none )
680 * \return the next item to play, or NULL if none found
682 playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
684 playlist_item_t *p_root,
685 playlist_item_t *p_node,
686 playlist_item_t *p_item )
688 playlist_item_t *p_search, *p_next;
690 #ifdef PLAYLIST_DEBUG
693 msg_Dbg( p_playlist, "Finding prev of %s within %s",
694 p_item->input.psz_name, p_node->input.psz_name );
698 msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
702 if( !p_node || p_node->i_children == -1 )
704 msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
708 /* Find the parent node of the item */
711 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
712 if( p_search == NULL )
714 msg_Err( p_playlist, "parent node not found" );
723 /* Now, go up the tree until we find a suitable next item */
724 p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
725 p_node, p_item, p_search );
729 if( p_playlist->b_go_next )
731 p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
732 p_root, p_item, p_search );
737 /* OK, we could continue, so set our current node to the root */
738 p_playlist->status.p_node = p_root;
748 /************************************************************************
749 * Following functions are local
750 ***********************************************************************/
753 /* Recursively search the tree for next item */
754 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
756 playlist_item_t *p_root,
757 playlist_item_t *p_item,
758 playlist_item_t *p_parent )
761 playlist_item_t *p_parent_parent;
763 for( i= 0 ; i < p_parent->i_children ; i++ )
765 if( p_parent->pp_children[i] == p_item || p_item == NULL )
771 #ifdef PLAYLIST_DEBUG
772 msg_Dbg( p_playlist,"Current item found, child %i of %s",
773 i , p_parent->input.psz_name );
775 /* We found our item */
776 if( i+1 >= p_parent->i_children )
779 #ifdef PLAYLIST_DEBUG
780 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
781 p_parent->input.psz_name );
783 if( p_parent == p_root )
785 /* Hmm, seems it's the end for you, guy ! */
789 /* Go up one level */
790 p_parent_parent = playlist_FindDirectParent( p_playlist,
792 if( p_parent_parent == NULL )
794 msg_Warn( p_playlist, "Unable to find parent !");
797 return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
798 p_parent, p_parent_parent );
802 if( p_parent->pp_children[i+1]->i_children == -1 )
804 /* Cool, we have found a real item to play */
805 #ifdef PLAYLIST_DEBUG
806 msg_Dbg( p_playlist, "Playing child %i of %s",
807 i+1 , p_parent->input.psz_name );
809 return p_parent->pp_children[i+1];
811 else if( p_parent->pp_children[i+1]->i_children > 0 )
813 /* Select the first child of this node */
814 #ifdef PLAYLIST_DEBUG
815 msg_Dbg( p_playlist, "%s is a node with children, "
817 p_parent->pp_children[i+1]->input.psz_name);
819 if( p_parent->pp_children[i+1]->pp_children[0]
822 /* first child is a node ! */
823 return playlist_RecursiveFindNext( p_playlist, i_view,
825 p_parent->pp_children[i+1]->pp_children[0]);
827 return p_parent->pp_children[i+1]->pp_children[0];
831 /* This node has no child... We must continue */
832 #ifdef PLAYLIST_DEBUG
833 msg_Dbg( p_playlist, "%s is a node with no children",
834 p_parent->pp_children[i+1]->input.psz_name);
836 p_item = p_parent->pp_children[i+1];
841 /* Just in case :) */
845 /* Recursively search the tree for previous item */
846 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
848 playlist_item_t *p_root,
849 playlist_item_t *p_item,
850 playlist_item_t *p_parent )
853 playlist_item_t *p_parent_parent;
855 for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
857 if( p_parent->pp_children[i] == p_item || p_item == NULL )
863 #ifdef PLAYLIST_DEBUG
864 msg_Dbg( p_playlist,"Current item found, child %i of %s",
865 i , p_parent->input.psz_name );
867 /* We found our item */
871 #ifdef PLAYLIST_DEBUG
872 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
873 p_parent->input.psz_name );
875 if( p_parent == p_root )
877 /* Hmm, seems it's the end for you, guy ! */
880 /* Go up one level */
881 p_parent_parent = playlist_FindDirectParent( p_playlist,
883 return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
884 p_parent, p_parent_parent );
888 if( p_parent->pp_children[i-1]->i_children == -1 )
890 /* Cool, we have found a real item to play */
891 #ifdef PLAYLIST_DEBUG
892 msg_Dbg( p_playlist, "Playing child %i of %s",
893 i-1, p_parent->input.psz_name );
895 return p_parent->pp_children[i-1];
897 else if( p_parent->pp_children[i-1]->i_children > 0 )
899 /* Select the last child of this node */
900 #ifdef PLAYLIST_DEBUG
901 msg_Dbg( p_playlist, "%s is a node with children,"
903 p_parent->pp_children[i-1]->input.psz_name);
905 if( p_parent->pp_children[i-1]->pp_children[p_parent->
906 pp_children[i-1]->i_children-1]->i_children >= 0 )
908 /* Last child is a node */
909 return playlist_RecursiveFindPrev( p_playlist, i_view,
911 p_parent->pp_children[i-1]->pp_children[
912 p_parent->pp_children[i-1]->i_children-1]);
914 return p_parent->pp_children[i-1]->pp_children[
915 p_parent->pp_children[i-1]->i_children-1];
919 /* This node has no child... We must continue */
920 #ifdef PLAYLIST_DEBUG
921 msg_Dbg( p_playlist, "%s is a node with no children",
922 p_parent->pp_children[i-1]->input.psz_name);
924 p_item = p_parent->pp_children[i-1];
932 /* This function returns the parent of an item in a view */
933 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
934 playlist_item_t *p_item,
938 for( i= 0; i< p_item->i_parents ; i++ )
940 if( p_item->pp_parents[i]->i_view == i_view )
942 return p_item->pp_parents[i]->p_parent;
949 #ifdef PLAYLIST_DEBUG
950 /* This function dumps a node : to be used only for debug*/
951 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
959 msg_Dbg( p_playlist, "%s (%i)",
960 p_item->input.psz_name, p_item->i_children );
963 if( p_item->i_children == -1 )
968 for( i = 0; i< p_item->i_children; i++ )
970 memset( str, 32, 512 );
971 sprintf( str + 2 * i_level , "%s (%i)",
972 p_item->pp_children[i]->input.psz_name,
973 p_item->pp_children[i]->i_children );
974 msg_Dbg( p_playlist, "%s",str );
975 if( p_item->pp_children[i]->i_children >= 0 )
977 playlist_NodeDump( p_playlist, p_item->pp_children[i],