1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: item.c,v 1.14 2004/02/08 18:17:22 gbazin 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"
39 * Add a playlist item into a playlist
41 * \param p_playlist the playlist to insert into
42 * \param p_item the playlist item to insert
43 * \param i_mode the mode used when adding
44 * \param i_pos the possition in the playlist where to add. If this is
45 * PLAYLIST_END the item will be added at the end of the playlist
46 * regardless of it's size
47 * \return The id of the playlist item
49 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
50 int i_mode, int i_pos)
54 vlc_mutex_lock( &p_playlist->object_lock );
57 * CHECK_INSERT : checks if the item is already enqued before
60 if ( i_mode & PLAYLIST_CHECK_INSERT )
64 if ( p_playlist->pp_items )
66 for ( j = 0; j < p_playlist->i_size; j++ )
68 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
70 if( p_item->psz_name )
72 free( p_item->psz_name );
76 free( p_item->psz_uri );
79 vlc_mutex_unlock( &p_playlist->object_lock );
84 i_mode &= ~PLAYLIST_CHECK_INSERT;
85 i_mode |= PLAYLIST_APPEND;
89 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
92 p_item->i_id = ++p_playlist->i_last_id;
94 /* Do a few boundary checks and allocate space for the item */
95 if( i_pos == PLAYLIST_END )
97 if( i_mode & PLAYLIST_INSERT )
99 i_mode &= ~PLAYLIST_INSERT;
100 i_mode |= PLAYLIST_APPEND;
103 i_pos = p_playlist->i_size - 1;
106 if( !(i_mode & PLAYLIST_REPLACE)
107 || i_pos < 0 || i_pos >= p_playlist->i_size )
109 /* Additional boundary checks */
110 if( i_mode & PLAYLIST_APPEND )
119 else if( i_pos > p_playlist->i_size )
121 i_pos = p_playlist->i_size;
124 INSERT_ELEM( p_playlist->pp_items,
128 p_playlist->i_enabled ++;
130 if( p_playlist->i_index >= i_pos )
132 p_playlist->i_index++;
137 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
138 if( p_playlist->pp_items[i_pos]->psz_name )
140 free( p_playlist->pp_items[i_pos]->psz_name );
142 if( p_playlist->pp_items[i_pos]->psz_uri )
144 free( p_playlist->pp_items[i_pos]->psz_uri );
146 /* XXX: what if the item is still in use? */
147 free( p_playlist->pp_items[i_pos] );
148 p_playlist->pp_items[i_pos] = p_item;
151 if( i_mode & PLAYLIST_GO )
153 p_playlist->i_index = i_pos;
154 if( p_playlist->p_input )
156 input_StopThread( p_playlist->p_input );
158 p_playlist->i_status = PLAYLIST_RUNNING;
161 vlc_mutex_unlock( &p_playlist->object_lock );
163 val.b_bool = VLC_TRUE;
164 var_Set( p_playlist, "intf-change", val );
170 * Add a option to one item ( no need for p_playlist )
172 * \param p_item the item on which we want the info
173 * \param psz_format the option
174 * \return 0 on success
176 int playlist_ItemAddOption( playlist_item_t *p_item,
177 const char *psz_option )
179 if( !psz_option ) return VLC_EGENERIC;
181 INSERT_ELEM( p_item->ppsz_options, p_item->i_options, p_item->i_options,
182 strdup( psz_option ) );