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));
258 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 INSERT_ELEM( p_playlist->pp_all_items,
296 p_playlist->i_all_size,
297 p_playlist->i_all_size,
300 if( p_parent != NULL )
302 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
305 p_add->p_node = p_parent;
306 p_add->p_item = p_item;
307 p_add->i_view = i_view;
308 val.p_address = p_add;
309 var_Set( p_playlist, "item-append", val);
317 * Remove all the children of a node
319 * This function must be entered with the playlist lock
321 * \param p_playlist the playlist
322 * \param p_root the node
323 * \param b_delete_items do we have to delete the children items ?
324 * \return VLC_SUCCESS or an error
326 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
327 vlc_bool_t b_delete_items )
330 if( p_root->i_children == -1 )
335 /* Delete the children */
336 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
338 if( p_root->pp_children[i]->i_children > -1 )
340 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
341 b_delete_items , VLC_FALSE );
343 else if( b_delete_items )
345 /* Delete the item here */
346 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
353 * Remove all the children of a node and removes the node
355 * \param p_playlist the playlist
356 * \param p_root the node
357 * \param b_delete_items do we have to delete the children items ?
358 * \return VLC_SUCCESS or an error
360 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
361 vlc_bool_t b_delete_items, vlc_bool_t b_force )
364 if( p_root->i_children == -1 )
369 /* Delete the children */
370 for( i = p_root->i_children - 1 ; i >= 0; i-- )
372 if( p_root->pp_children[i]->i_children > -1 )
374 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
375 b_delete_items , b_force );
377 else if( b_delete_items )
379 /* Delete the item here */
380 playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
383 /* Delete the node */
384 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
389 for( i = 0 ; i< p_root->i_parents; i++ )
391 playlist_NodeRemoveItem( p_playlist, p_root,
392 p_root->pp_parents[i]->p_parent );
394 var_SetInteger( p_playlist, "item-deleted", p_root->input.i_id );
395 playlist_ItemDelete( p_root );
402 * Adds an item to the childs of a node
404 * \param p_playlist the playlist
405 * \param i_view the view of the node ( needed for parent search )
406 * \param p_item the item to append
407 * \param p_parent the parent node
408 * \return VLC_SUCCESS or an error
410 int playlist_NodeAppend( playlist_t *p_playlist,
412 playlist_item_t *p_item,
413 playlist_item_t *p_parent )
415 return playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, -1 );
418 int playlist_NodeInsert( playlist_t *p_playlist,
420 playlist_item_t *p_item,
421 playlist_item_t *p_parent,
425 vlc_bool_t b_found = VLC_FALSE;
426 if( !p_parent || p_parent->i_children == -1 )
428 msg_Err( p_playlist, "invalid node" );
432 if( i_position == -1 ) i_position = p_parent->i_children ;
434 INSERT_ELEM( p_parent->pp_children,
435 p_parent->i_children,
439 /* Add the parent to the array */
440 for( i= 0; i< p_item->i_parents ; i++ )
442 if( p_item->pp_parents[i]->i_view == i_view )
448 if( b_found == VLC_FALSE )
450 struct item_parent_t *p_ip = (struct item_parent_t *)
451 malloc(sizeof(struct item_parent_t) );
452 p_ip->i_view = i_view;
453 p_ip->p_parent = p_parent;
455 INSERT_ELEM( p_item->pp_parents,
456 p_item->i_parents, p_item->i_parents,
460 /* Let the interface know this has been updated */
461 p_parent->i_serial++;
466 * Deletes an item from the children of a node
468 * \param p_playlist the playlist
469 * \param p_item the item to remove
470 * \param p_parent the parent node
471 * \return VLC_SUCCESS or an error
473 int playlist_NodeRemoveItem( playlist_t *p_playlist,
474 playlist_item_t *p_item,
475 playlist_item_t *p_parent )
478 if( !p_parent || p_parent->i_children == -1 )
480 msg_Err( p_playlist, "invalid node" );
483 for( i= 0; i< p_parent->i_children ; i++ )
485 if( p_parent->pp_children[i] == p_item )
487 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
491 /* Let the interface know this has been updated */
492 p_parent->i_serial++;
499 * Count the children of a node
501 * \param p_playlist the playlist
502 * \param p_node the node
503 * \return the number of children
505 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
509 if( p_node->i_children == -1 )
514 for( i=0 ; i< p_node->i_children;i++ )
516 if( p_node->pp_children[i]->i_children == -1 )
522 i_nb += playlist_NodeChildrenCount( p_playlist,
523 p_node->pp_children[i] );
530 * Search a child of a node by its name
532 * \param p_node the node
533 * \param psz_search the name of the child to search
534 * \return the child item or NULL if not found or error
536 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
537 const char *psz_search )
541 if( p_node->i_children < 0 )
545 for( i = 0 ; i< p_node->i_children; i++ )
547 if( !strcmp( p_node->pp_children[i]->input.psz_name, psz_search ) )
549 return p_node->pp_children[i];
556 /**********************************************************************
558 **********************************************************************/
561 * Finds the next item to play
563 * \param p_playlist the playlist
564 * \param i_view the view
565 * \param p_root the root node
566 * \param p_node the node we are playing from
567 * \param p_item the item we were playing (NULL if none )
568 * \return the next item to play, or NULL if none found
570 playlist_item_t *playlist_FindNextFromParent( playlist_t *p_playlist,
571 int i_view, /* FIXME: useless */
572 playlist_item_t *p_root,
573 playlist_item_t *p_node,
574 playlist_item_t *p_item )
576 playlist_item_t *p_search, *p_next;
578 #ifdef PLAYLIST_DEBUG
581 msg_Dbg( p_playlist, "finding next of %s within %s",
582 p_item->input.psz_name, p_node->input.psz_name );
586 msg_Dbg( p_playlist, "finding something to play within %s",
587 p_node->input.psz_name );
592 if( !p_node || p_node->i_children == -1 )
594 msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
598 /* Find the parent node of the item */
601 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
602 if( p_search == NULL )
604 msg_Err( p_playlist, "parent node not found" );
613 /* Now, go up the tree until we find a suitable next item */
614 p_next = playlist_RecursiveFindNext( p_playlist,i_view,
615 p_node, p_item, p_search );
617 /* Not found, do we go past p_node ? */
620 if( p_playlist->b_go_next )
622 p_next = playlist_RecursiveFindNext( p_playlist, i_view,
623 p_root, p_item, p_search );
628 /* OK, we could continue, so set our current node to the root */
629 p_playlist->status.p_node = p_root;
640 * Finds the previous item to play
642 * \param p_playlist the playlist
643 * \param i_view the view
644 * \param p_root the root node
645 * \param p_node the node we are playing from
646 * \param p_item the item we were playing (NULL if none )
647 * \return the next item to play, or NULL if none found
649 playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
651 playlist_item_t *p_root,
652 playlist_item_t *p_node,
653 playlist_item_t *p_item )
655 playlist_item_t *p_search, *p_next;
657 #ifdef PLAYLIST_DEBUG
660 msg_Dbg( p_playlist, "Finding prev of %s within %s",
661 p_item->input.psz_name, p_node->input.psz_name );
665 msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
669 if( !p_node || p_node->i_children == -1 )
671 msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
675 /* Find the parent node of the item */
678 p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
679 if( p_search == NULL )
681 msg_Err( p_playlist, "parent node not found" );
690 /* Now, go up the tree until we find a suitable next item */
691 p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
692 p_node, p_item, p_search );
696 if( p_playlist->b_go_next )
698 p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
699 p_root, p_item, p_search );
704 /* OK, we could continue, so set our current node to the root */
705 p_playlist->status.p_node = p_root;
715 /************************************************************************
716 * Following functions are local
717 ***********************************************************************/
720 /* Recursively search the tree for next item */
721 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
723 playlist_item_t *p_root,
724 playlist_item_t *p_item,
725 playlist_item_t *p_parent )
728 playlist_item_t *p_parent_parent;
730 for( i= 0 ; i < p_parent->i_children ; i++ )
732 if( p_parent->pp_children[i] == p_item || p_item == NULL )
738 #ifdef PLAYLIST_DEBUG
739 msg_Dbg( p_playlist,"Current item found, child %i of %s",
740 i , p_parent->input.psz_name );
742 /* We found our item */
743 if( i+1 >= p_parent->i_children )
746 #ifdef PLAYLIST_DEBUG
747 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
748 p_parent->input.psz_name );
750 if( p_parent == p_root )
752 /* Hmm, seems it's the end for you, guy ! */
756 /* Go up one level */
757 p_parent_parent = playlist_FindDirectParent( p_playlist,
759 if( p_parent_parent == NULL )
761 msg_Warn( p_playlist, "Unable to find parent !");
764 return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
765 p_parent, p_parent_parent );
769 if( p_parent->pp_children[i+1]->i_children == -1 )
771 /* Cool, we have found a real item to play */
772 #ifdef PLAYLIST_DEBUG
773 msg_Dbg( p_playlist, "Playing child %i of %s",
774 i+1 , p_parent->input.psz_name );
776 return p_parent->pp_children[i+1];
778 else if( p_parent->pp_children[i+1]->i_children > 0 )
780 /* Select the first child of this node */
781 #ifdef PLAYLIST_DEBUG
782 msg_Dbg( p_playlist, "%s is a node with children, "
784 p_parent->pp_children[i+1]->input.psz_name);
786 if( p_parent->pp_children[i+1]->pp_children[0]
789 /* first child is a node ! */
790 return playlist_RecursiveFindNext( p_playlist, i_view,
792 p_parent->pp_children[i+1]->pp_children[0]);
794 return p_parent->pp_children[i+1]->pp_children[0];
798 /* This node has no child... We must continue */
799 #ifdef PLAYLIST_DEBUG
800 msg_Dbg( p_playlist, "%s is a node with no children",
801 p_parent->pp_children[i+1]->input.psz_name);
803 p_item = p_parent->pp_children[i+1];
808 /* Just in case :) */
812 /* Recursively search the tree for previous item */
813 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
815 playlist_item_t *p_root,
816 playlist_item_t *p_item,
817 playlist_item_t *p_parent )
820 playlist_item_t *p_parent_parent;
822 for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
824 if( p_parent->pp_children[i] == p_item || p_item == NULL )
830 #ifdef PLAYLIST_DEBUG
831 msg_Dbg( p_playlist,"Current item found, child %i of %s",
832 i , p_parent->input.psz_name );
834 /* We found our item */
838 #ifdef PLAYLIST_DEBUG
839 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
840 p_parent->input.psz_name );
842 if( p_parent == p_root )
844 /* Hmm, seems it's the end for you, guy ! */
847 /* Go up one level */
848 p_parent_parent = playlist_FindDirectParent( p_playlist,
850 return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
851 p_parent, p_parent_parent );
855 if( p_parent->pp_children[i-1]->i_children == -1 )
857 /* Cool, we have found a real item to play */
858 #ifdef PLAYLIST_DEBUG
859 msg_Dbg( p_playlist, "Playing child %i of %s",
860 i-1, p_parent->input.psz_name );
862 return p_parent->pp_children[i-1];
864 else if( p_parent->pp_children[i-1]->i_children > 0 )
866 /* Select the last child of this node */
867 #ifdef PLAYLIST_DEBUG
868 msg_Dbg( p_playlist, "%s is a node with children,"
870 p_parent->pp_children[i-1]->input.psz_name);
872 if( p_parent->pp_children[i-1]->pp_children[p_parent->
873 pp_children[i-1]->i_children-1]->i_children >= 0 )
875 /* Last child is a node */
876 return playlist_RecursiveFindPrev( p_playlist, i_view,
878 p_parent->pp_children[i-1]->pp_children[
879 p_parent->pp_children[i-1]->i_children-1]);
881 return p_parent->pp_children[i-1]->pp_children[
882 p_parent->pp_children[i-1]->i_children-1];
886 /* This node has no child... We must continue */
887 #ifdef PLAYLIST_DEBUG
888 msg_Dbg( p_playlist, "%s is a node with no children",
889 p_parent->pp_children[i-1]->input.psz_name);
891 p_item = p_parent->pp_children[i-1];
899 /* This function returns the parent of an item in a view */
900 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
901 playlist_item_t *p_item,
905 for( i= 0; i< p_item->i_parents ; i++ )
907 if( p_item->pp_parents[i]->i_view == i_view )
909 return p_item->pp_parents[i]->p_parent;
916 #ifdef PLAYLIST_DEBUG
917 /* This function dumps a node : to be used only for debug*/
918 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
926 msg_Dbg( p_playlist, "%s (%i)",
927 p_item->input.psz_name, p_item->i_children );
930 if( p_item->i_children == -1 )
935 for( i = 0; i< p_item->i_children; i++ )
937 memset( str, 32, 512 );
938 sprintf( str + 2 * i_level , "%s (%i)",
939 p_item->pp_children[i]->input.psz_name,
940 p_item->pp_children[i]->i_children );
941 msg_Dbg( p_playlist, "%s",str );
942 if( p_item->pp_children[i]->i_children >= 0 )
944 playlist_NodeDump( p_playlist, p_item->pp_children[i],