1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: item.c,v 1.10 2004/01/05 12:59:43 zorglub Exp $
7 * Authors: Samuel Hocevar <sam@zoy.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() */
30 #include <vlc/input.h>
32 #include "vlc_playlist.h"
35 * Add a playlist item into a playlist
37 * \param p_playlist the playlist to insert into
38 * \param p_item the playlist item to insert
39 * \param i_mode the mode used when adding
40 * \param i_pos the possition in the playlist where to add. If this is
41 * PLAYLIST_END the item will be added at the end of the playlist
42 * regardless of it's size
43 * \return position of the new item
45 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
46 int i_mode, int i_pos)
50 vlc_mutex_lock( &p_playlist->object_lock );
53 * CHECK_INSERT : checks if the item is already enqued before
56 if ( i_mode & PLAYLIST_CHECK_INSERT )
60 if ( p_playlist->pp_items )
62 for ( j = 0; j < p_playlist->i_size; j++ )
64 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
66 if( p_item->psz_name )
68 free( p_item->psz_name );
72 free( p_item->psz_uri );
75 vlc_mutex_unlock( &p_playlist->object_lock );
80 i_mode &= ~PLAYLIST_CHECK_INSERT;
81 i_mode |= PLAYLIST_APPEND;
85 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
88 p_item->i_id = ++p_playlist->i_last_id;
90 /* Do a few boundary checks and allocate space for the item */
91 if( i_pos == PLAYLIST_END )
93 if( i_mode & PLAYLIST_INSERT )
95 i_mode &= ~PLAYLIST_INSERT;
96 i_mode |= PLAYLIST_APPEND;
99 i_pos = p_playlist->i_size - 1;
102 if( !(i_mode & PLAYLIST_REPLACE)
103 || i_pos < 0 || i_pos >= p_playlist->i_size )
105 /* Additional boundary checks */
106 if( i_mode & PLAYLIST_APPEND )
115 else if( i_pos > p_playlist->i_size )
117 i_pos = p_playlist->i_size;
120 INSERT_ELEM( p_playlist->pp_items,
124 p_playlist->i_enabled ++;
126 if( p_playlist->i_index >= i_pos )
128 p_playlist->i_index++;
133 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
134 if( p_playlist->pp_items[i_pos]->psz_name )
136 free( p_playlist->pp_items[i_pos]->psz_name );
138 if( p_playlist->pp_items[i_pos]->psz_uri )
140 free( p_playlist->pp_items[i_pos]->psz_uri );
142 /* XXX: what if the item is still in use? */
143 free( p_playlist->pp_items[i_pos] );
144 p_playlist->pp_items[i_pos] = p_item;
147 if( i_mode & PLAYLIST_GO )
149 p_playlist->i_index = i_pos;
150 if( p_playlist->p_input )
152 input_StopThread( p_playlist->p_input );
154 p_playlist->i_status = PLAYLIST_RUNNING;
157 vlc_mutex_unlock( &p_playlist->object_lock );
159 val.b_bool = VLC_TRUE;
160 var_Set( p_playlist, "intf-change", val );