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"
32 static void GuessType( input_item_t *p_item);
35 * Create a new item, without adding it to the playlist
37 * \param p_obj a vlc object (anyone will do)
38 * \param psz_uri the mrl of the item
39 * \param psz_name a text giving a name or description of the item
40 * \return the new item or NULL on failure
43 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
45 const char *psz_name )
47 return playlist_ItemNewWithType( p_obj, psz_uri,
48 psz_name, ITEM_TYPE_UNKNOWN );
51 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
56 playlist_item_t * p_item;
58 if( psz_uri == NULL) return NULL;
60 p_item = malloc( sizeof( playlist_item_t ) );
61 if( p_item == NULL ) return NULL;
63 memset( p_item, 0, sizeof( playlist_item_t ) );
65 p_item->input.psz_uri = strdup( psz_uri );
67 if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
68 else p_item->input.psz_name = strdup ( psz_uri );
70 p_item->input.i_type = i_type;
72 p_item->b_enabled = VLC_TRUE;
73 p_item->i_nb_played = 0;
75 p_item->i_children = -1;
76 p_item->pp_children = NULL;
79 p_item->i_flags |= PLAYLIST_SKIP_FLAG;
81 p_item->input.i_duration = -1;
82 p_item->input.ppsz_options = NULL;
83 p_item->input.i_options = 0;
85 vlc_mutex_init( p_obj, &p_item->input.lock );
87 if( p_item->input.i_type == ITEM_TYPE_UNKNOWN )
88 GuessType( &p_item->input );
90 playlist_ItemCreateCategory( p_item, _("General") );
96 * Deletes a playlist item
98 * \param p_item the item to delete
101 int playlist_ItemDelete( playlist_item_t *p_item )
103 vlc_mutex_lock( &p_item->input.lock );
105 if( p_item->input.psz_name ) free( p_item->input.psz_name );
106 if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
108 /* Free the info categories */
109 if( p_item->input.i_categories > 0 )
113 for( i = 0; i < p_item->input.i_categories; i++ )
115 info_category_t *p_category = p_item->input.pp_categories[i];
117 for( j = 0; j < p_category->i_infos; j++)
119 if( p_category->pp_infos[j]->psz_name )
121 free( p_category->pp_infos[j]->psz_name);
123 if( p_category->pp_infos[j]->psz_value )
125 free( p_category->pp_infos[j]->psz_value );
127 free( p_category->pp_infos[j] );
130 if( p_category->i_infos ) free( p_category->pp_infos );
131 if( p_category->psz_name ) free( p_category->psz_name );
135 free( p_item->input.pp_categories );
138 for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
140 free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
141 if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
144 for( ; p_item->i_parents > 0 ; )
146 struct item_parent_t *p_parent = p_item->pp_parents[0];
147 REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, 0 );
151 vlc_mutex_unlock( &p_item->input.lock );
152 vlc_mutex_destroy( &p_item->input.lock );
160 * Add a option to one item ( no need for p_playlist )
162 * \param p_item the item on which we want the info
163 * \param psz_option the option
164 * \return 0 on success
166 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
168 if( !psz_option ) return VLC_EGENERIC;
170 vlc_mutex_lock( &p_item->input.lock );
171 INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
172 p_item->input.i_options, strdup( psz_option ) );
173 vlc_mutex_unlock( &p_item->input.lock );
179 * Add a parent to an item
181 * \param p_item the item
182 * \param i_view the view in which the parent is
183 * \param p_parent the parent to add
186 int playlist_ItemAddParent( playlist_item_t *p_item, int i_view,
187 playlist_item_t *p_parent )
189 vlc_bool_t b_found = VLC_FALSE;
192 for( i= 0; i< p_item->i_parents ; i++ )
194 if( p_item->pp_parents[i]->i_view == i_view )
201 if( b_found == VLC_FALSE )
204 struct item_parent_t *p_ip = (struct item_parent_t *)
205 malloc(sizeof(struct item_parent_t) );
206 p_ip->i_view = i_view;
207 p_ip->p_parent = p_parent;
209 INSERT_ELEM( p_item->pp_parents,
210 p_item->i_parents, p_item->i_parents,
217 * Copy all parents from parent to child
219 int playlist_CopyParents( playlist_item_t *p_parent,
220 playlist_item_t *p_child )
223 for( i= 0 ; i< p_parent->i_parents; i ++ )
225 playlist_ItemAddParent( p_child,
226 p_parent->pp_parents[i]->i_view,
233 /**********************************************************************
234 * playlist_item_t structure accessors
235 * These functions give access to the fields of the playlist_item_t
237 **********************************************************************/
241 * Set the name of a playlist item
243 * \param p_item the item
244 * \param psz_name the new name
245 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
247 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
249 if( psz_name && p_item )
251 p_item->input.psz_name = strdup( psz_name );
258 * Set the duration of a playlist item
259 * This function must be entered with the item lock
261 * \param p_item the item
262 * \param i_duration the new duration
263 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
265 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
267 char psz_buffer[MSTRTIME_MAX_SIZE];
270 p_item->input.i_duration = i_duration;
271 if( i_duration != -1 )
273 secstotimestr( psz_buffer, (int)(i_duration/1000000) );
277 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
279 playlist_ItemAddInfo( p_item, _("General") , _("Duration"),
288 * Guess the type of the item using the beginning of the mrl */
289 static void GuessType( input_item_t *p_item)
292 static struct { char *psz_search; int i_type; } types_array[] =
294 { "http", ITEM_TYPE_NET },
295 { "dvd", ITEM_TYPE_DISC },
296 { "cdda", ITEM_TYPE_CDDA },
297 { "mms", ITEM_TYPE_NET },
298 { "rtsp", ITEM_TYPE_NET },
299 { "udp", ITEM_TYPE_NET },
300 { "rtp", ITEM_TYPE_NET },
301 { "vcd", ITEM_TYPE_DISC },
302 { "v4l", ITEM_TYPE_CARD },
303 { "dshow", ITEM_TYPE_CARD },
304 { "pvr", ITEM_TYPE_CARD },
305 { "dvb", ITEM_TYPE_CARD },
306 { "qpsk", ITEM_TYPE_CARD },
307 { "sdp", ITEM_TYPE_NET },
311 static struct { char *psz_search; int i_type; } exts_array[] =
313 { "mp3", ITEM_TYPE_AFILE },
317 for( i = 0; types_array[i].psz_search != NULL; i++ )
319 if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
320 strlen( types_array[i].psz_search ) ) )
322 p_item->i_type = types_array[i].i_type;
326 p_item->i_type = ITEM_TYPE_VFILE;