1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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() */
28 #include <vlc/input.h>
30 #include "vlc_playlist.h"
33 * Create a new item, without adding it to the playlist
35 * \param p_obj a vlc object (anyone will do)
36 * \param psz_uri the mrl of the item
37 * \param psz_name a text giving a name or description of the item
38 * \return the new item or NULL on failure
40 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
42 const char *psz_name )
44 playlist_item_t * p_item;
46 p_item = malloc( sizeof( playlist_item_t ) );
47 if( p_item == NULL ) return NULL;
48 if( psz_uri == NULL) return NULL;
50 memset( p_item, 0, sizeof( playlist_item_t ) );
52 p_item->input.psz_uri = strdup( psz_uri );
54 if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
55 else p_item->input.psz_name = strdup ( psz_uri );
57 p_item->b_enabled = VLC_TRUE;
58 p_item->i_group = PLAYLIST_TYPE_MANUAL;
59 p_item->i_nb_played = 0;
61 p_item->input.i_duration = -1;
62 p_item->input.ppsz_options = NULL;
63 p_item->input.i_options = 0;
65 vlc_mutex_init( p_obj, &p_item->input.lock );
67 playlist_ItemCreateCategory( p_item, _("General") );
72 * Deletes a playlist item
74 * \param p_item the item to delete
77 void playlist_ItemDelete( playlist_item_t *p_item )
79 vlc_mutex_lock( &p_item->input.lock );
81 if( p_item->input.psz_name ) free( p_item->input.psz_name );
82 if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
84 /* Free the info categories */
85 if( p_item->input.i_categories > 0 )
89 for( i = 0; i < p_item->input.i_categories; i++ )
91 info_category_t *p_category = p_item->input.pp_categories[i];
93 for( j = 0; j < p_category->i_infos; j++)
95 if( p_category->pp_infos[j]->psz_name )
97 free( p_category->pp_infos[j]->psz_name);
99 if( p_category->pp_infos[j]->psz_value )
101 free( p_category->pp_infos[j]->psz_value );
103 free( p_category->pp_infos[j] );
106 if( p_category->i_infos ) free( p_category->pp_infos );
107 if( p_category->psz_name ) free( p_category->psz_name );
111 free( p_item->input.pp_categories );
114 for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
116 free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
117 if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
120 vlc_mutex_unlock( &p_item->input.lock );
121 vlc_mutex_destroy( &p_item->input.lock );
127 * Add a playlist item into a playlist
129 * \param p_playlist the playlist to insert into
130 * \param p_item the playlist item to insert
131 * \param i_mode the mode used when adding
132 * \param i_pos the possition in the playlist where to add. If this is
133 * PLAYLIST_END the item will be added at the end of the playlist
134 * regardless of it's size
135 * \return The id of the playlist item
137 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
138 int i_mode, int i_pos)
142 vlc_mutex_lock( &p_playlist->object_lock );
145 * CHECK_INSERT : checks if the item is already enqued before
148 if ( i_mode & PLAYLIST_CHECK_INSERT )
152 if ( p_playlist->pp_items )
154 for ( j = 0; j < p_playlist->i_size; j++ )
156 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
157 p_item->input.psz_uri ) )
159 if ( p_item->input.psz_name )
161 free( p_item->input.psz_name );
163 if ( p_item->input.psz_uri )
165 free ( p_item->input.psz_uri );
168 vlc_mutex_unlock( &p_playlist->object_lock );
173 i_mode &= ~PLAYLIST_CHECK_INSERT;
174 i_mode |= PLAYLIST_APPEND;
177 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
178 p_item->input.psz_name, p_item->input.psz_uri );
180 p_item->i_id = ++p_playlist->i_last_id;
182 /* Do a few boundary checks and allocate space for the item */
183 if( i_pos == PLAYLIST_END )
185 if( i_mode & PLAYLIST_INSERT )
187 i_mode &= ~PLAYLIST_INSERT;
188 i_mode |= PLAYLIST_APPEND;
191 i_pos = p_playlist->i_size - 1;
194 if( !(i_mode & PLAYLIST_REPLACE)
195 || i_pos < 0 || i_pos >= p_playlist->i_size )
197 /* Additional boundary checks */
198 if( i_mode & PLAYLIST_APPEND )
207 else if( i_pos > p_playlist->i_size )
209 i_pos = p_playlist->i_size;
212 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos, p_item );
213 p_playlist->i_enabled ++;
215 if( p_playlist->i_index >= i_pos )
217 p_playlist->i_index++;
222 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
223 if( p_playlist->pp_items[i_pos]->input.psz_name )
225 free( p_playlist->pp_items[i_pos]->input.psz_name );
227 if( p_playlist->pp_items[i_pos]->input.psz_uri )
229 free( p_playlist->pp_items[i_pos]->input.psz_uri );
231 /* XXX: what if the item is still in use? */
232 free( p_playlist->pp_items[i_pos] );
233 p_playlist->pp_items[i_pos] = p_item;
236 if( i_mode & PLAYLIST_GO )
238 p_playlist->i_index = i_pos;
239 if( p_playlist->p_input )
241 input_StopThread( p_playlist->p_input );
243 p_playlist->i_status = PLAYLIST_RUNNING;
246 vlc_mutex_unlock( &p_playlist->object_lock );
248 val.b_bool = VLC_TRUE;
249 var_Set( p_playlist, "intf-change", val );
255 * Add a option to one item ( no need for p_playlist )
257 * \param p_item the item on which we want the info
258 * \param psz_option the option
259 * \return 0 on success
261 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
263 if( !psz_option ) return VLC_EGENERIC;
265 vlc_mutex_lock( &p_item->input.lock );
266 INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
267 p_item->input.i_options, strdup( psz_option ) );
268 vlc_mutex_unlock( &p_item->input.lock );