]> git.sesse.net Git - vlc/blob - src/playlist/item.c
Remove playlist info accessors (as they now belong to input_item) and use vlc_input_i...
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include "vlc_input.h"
31 #include "vlc_playlist.h"
32
33 static void GuessType( input_item_t *p_item);
34
35 /**
36  * Create a new item, without adding it to the playlist
37  *
38  * \param p_obj a vlc object (anyone will do)
39  * \param psz_uri the mrl of the item
40  * \param psz_name a text giving a name or description of the item
41  * \return the new item or NULL on failure
42  */
43
44 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
45                                       const char *psz_uri,
46                                       const char *psz_name )
47 {
48     return playlist_ItemNewWithType( p_obj, psz_uri,
49                                      psz_name, ITEM_TYPE_UNKNOWN );
50 }
51
52 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
53                                             const char *psz_uri,
54                                             const char *psz_name,
55                                             int i_type )
56 {
57     playlist_item_t * p_item;
58
59     if( psz_uri == NULL) return NULL;
60
61     p_item = malloc( sizeof( playlist_item_t ) );
62     if( p_item == NULL ) return NULL;
63
64     memset( p_item, 0, sizeof( playlist_item_t ) );
65
66     p_item->input.psz_uri = strdup( psz_uri );
67
68     if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
69     else p_item->input.psz_name = strdup ( psz_uri );
70
71     p_item->input.i_type = i_type;
72
73     p_item->b_enabled = VLC_TRUE;
74     p_item->i_nb_played = 0;
75
76     p_item->i_children = -1;
77     p_item->pp_children = NULL;
78
79     p_item->i_flags = 0;
80     p_item->i_flags |= PLAYLIST_SKIP_FLAG;
81
82     p_item->input.i_duration = -1;
83     p_item->input.ppsz_options = NULL;
84     p_item->input.i_options = 0;
85
86     vlc_mutex_init( p_obj, &p_item->input.lock );
87
88     if( p_item->input.i_type == ITEM_TYPE_UNKNOWN )
89         GuessType( &p_item->input );
90
91     return p_item;
92 }
93
94 /**
95  * Deletes a playlist item
96  *
97  * \param p_item the item to delete
98  * \return nothing
99  */
100 int playlist_ItemDelete( playlist_item_t *p_item )
101 {
102     vlc_mutex_lock( &p_item->input.lock );
103
104     if( p_item->input.psz_name ) free( p_item->input.psz_name );
105     if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
106
107     /* Free the info categories */
108     if( p_item->input.i_categories > 0 )
109     {
110         int i, j;
111
112         for( i = 0; i < p_item->input.i_categories; i++ )
113         {
114             info_category_t *p_category = p_item->input.pp_categories[i];
115
116             for( j = 0; j < p_category->i_infos; j++)
117             {
118                 if( p_category->pp_infos[j]->psz_name )
119                 {
120                     free( p_category->pp_infos[j]->psz_name);
121                 }
122                 if( p_category->pp_infos[j]->psz_value )
123                 {
124                     free( p_category->pp_infos[j]->psz_value );
125                 }
126                 free( p_category->pp_infos[j] );
127             }
128
129             if( p_category->i_infos ) free( p_category->pp_infos );
130             if( p_category->psz_name ) free( p_category->psz_name );
131             free( p_category );
132         }
133
134         free( p_item->input.pp_categories );
135     }
136
137     for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
138     {
139         free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
140         if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
141     }
142
143     for( ; p_item->i_parents > 0 ; )
144     {
145         struct item_parent_t *p_parent =  p_item->pp_parents[0];
146         REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, 0 );
147         free( p_parent );
148     }
149
150     vlc_mutex_unlock( &p_item->input.lock );
151     vlc_mutex_destroy( &p_item->input.lock );
152
153     free( p_item );
154
155     return VLC_SUCCESS;
156 }
157
158 /**
159  *  Add a option to one item ( no need for p_playlist )
160  *
161  * \param p_item the item on which we want the info
162  * \param psz_option the option
163  * \return 0 on success
164  */
165 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
166 {
167     if( !psz_option ) return VLC_EGENERIC;
168
169     vlc_mutex_lock( &p_item->input.lock );
170     INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
171                  p_item->input.i_options, strdup( psz_option ) );
172     vlc_mutex_unlock( &p_item->input.lock );
173
174     return VLC_SUCCESS;
175 }
176
177 /**
178  * Add a parent to an item
179  *
180  * \param p_item the item
181  * \param i_view the view in which the parent is
182  * \param p_parent the parent to add
183  * \return nothing
184  */
185 int playlist_ItemAddParent( playlist_item_t *p_item, int i_view,
186                             playlist_item_t *p_parent )
187 {
188    vlc_bool_t b_found = VLC_FALSE;
189    int i;
190
191    for( i= 0; i< p_item->i_parents ; i++ )
192    {
193        if( p_item->pp_parents[i]->i_view == i_view )
194
195        {
196            b_found = VLC_TRUE;
197            break;
198        }
199    }
200    if( b_found == VLC_FALSE )
201    {
202
203        struct item_parent_t *p_ip = (struct item_parent_t *)
204                malloc(sizeof(struct item_parent_t) );
205        p_ip->i_view = i_view;
206        p_ip->p_parent = p_parent;
207
208        INSERT_ELEM( p_item->pp_parents,
209                     p_item->i_parents, p_item->i_parents,
210                     p_ip );
211    }
212    return VLC_SUCCESS;
213 }
214
215 /**
216  * Copy all parents from parent to child
217  */
218 int playlist_CopyParents( playlist_item_t *p_parent,
219                            playlist_item_t *p_child )
220 {
221     int i=0;
222     for( i= 0 ; i< p_parent->i_parents; i ++ )
223     {
224         playlist_ItemAddParent( p_child,
225                                 p_parent->pp_parents[i]->i_view,
226                                 p_parent );
227     }
228     return VLC_SUCCESS;
229 }
230
231
232 /**********************************************************************
233  * playlist_item_t structure accessors
234  * These functions give access to the fields of the playlist_item_t
235  * structure
236  **********************************************************************/
237
238
239 /**
240  * Set the name of a playlist item
241  *
242  * \param p_item the item
243  * \param psz_name the new name
244  * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
245  */
246 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
247 {
248     if( psz_name && p_item )
249     {
250         p_item->input.psz_name = strdup( psz_name );
251         return VLC_SUCCESS;
252     }
253     return VLC_EGENERIC;
254 }
255
256 /**
257  * Set the duration of a playlist item
258  * This function must be entered with the item lock
259  *
260  * \param p_item the item
261  * \param i_duration the new duration
262  * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
263  */
264 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
265 {
266     char psz_buffer[MSTRTIME_MAX_SIZE];
267     if( p_item )
268     {
269         p_item->input.i_duration = i_duration;
270         if( i_duration != -1 )
271         {
272             secstotimestr( psz_buffer, (int)(i_duration/1000000) );
273         }
274         else
275         {
276             memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
277         }
278         vlc_input_item_AddInfo( &p_item->input, _("General") , _("Duration"),
279                                 "%s", psz_buffer );
280
281         return VLC_SUCCESS;
282     }
283     return VLC_EGENERIC;
284 }
285
286 /*
287  * Guess the type of the item using the beginning of the mrl */
288 static void GuessType( input_item_t *p_item)
289 {
290     int i;
291     static struct { char *psz_search; int i_type; }  types_array[] =
292     {
293         { "http", ITEM_TYPE_NET },
294         { "dvd", ITEM_TYPE_DISC },
295         { "cdda", ITEM_TYPE_CDDA },
296         { "mms", ITEM_TYPE_NET },
297         { "rtsp", ITEM_TYPE_NET },
298         { "udp", ITEM_TYPE_NET },
299         { "rtp", ITEM_TYPE_NET },
300         { "vcd", ITEM_TYPE_DISC },
301         { "v4l", ITEM_TYPE_CARD },
302         { "dshow", ITEM_TYPE_CARD },
303         { "pvr", ITEM_TYPE_CARD },
304         { "dvb", ITEM_TYPE_CARD },
305         { "qpsk", ITEM_TYPE_CARD },
306         { "sdp", ITEM_TYPE_NET },
307         { NULL, 0 }
308     };
309
310     static struct { char *psz_search; int i_type; } exts_array[] =
311     {
312         { "mp3", ITEM_TYPE_AFILE },
313         { NULL, 0 }
314     };
315
316     for( i = 0; types_array[i].psz_search != NULL; i++ )
317     {
318         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
319                       strlen( types_array[i].psz_search ) ) )
320         {
321             p_item->i_type = types_array[i].i_type;
322             return;
323         }
324     }
325     p_item->i_type = ITEM_TYPE_VFILE;
326 }