]> git.sesse.net Git - vlc/blob - src/playlist/item.c
playlist/item.c, include/vlc_playlist.h: introduced playlist_ItemCopy
[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     p_item->i_flags |= PLAYLIST_SAVE_FLAG;
82
83     p_item->input.i_duration = -1;
84     p_item->input.ppsz_options = NULL;
85     p_item->input.i_options = 0;
86
87     vlc_mutex_init( p_obj, &p_item->input.lock );
88
89     if( p_item->input.i_type == ITEM_TYPE_UNKNOWN )
90         GuessType( &p_item->input );
91
92     return p_item;
93 }
94
95 /**
96  * Copy a playlist item
97  *
98  * Creates a new item with name, mrl and meta infor like the
99  * source. Does not copy children for node type items.
100  * \param p_obj any vlc object, needed for mutex init
101  * \param p_item the item to copy
102  * \return pointer to the new item, or NULL on error
103  * \note function takes the lock on p_item
104  */
105 playlist_item_t *__playlist_ItemCopy( vlc_object_t *p_obj,
106                                       playlist_item_t *p_item )
107 {
108     playlist_item_t *p_res;
109     int i;
110     vlc_mutex_lock( &p_item->input.lock );
111
112     p_res = malloc( sizeof( playlist_item_t ) );
113     if( p_res == NULL )
114     {
115         vlc_mutex_unlock( &p_item->input.lock );
116         return NULL;
117     }
118
119     memcpy( p_res, p_item, sizeof(playlist_item_t) );
120     vlc_mutex_init( p_obj, &p_res->input.lock );
121     p_res->input.ppsz_options = malloc( p_item->input.i_options * sizeof(char*));
122     for( i = 0; i < p_item->input.i_options; i++ )
123     {
124         p_res->input.ppsz_options[i] = strdup( p_item->input.ppsz_options[i] );
125     }
126     if( p_item->i_children != -1 )
127     {
128         msg_Warn( p_obj, "not copying playlist items children" );
129         p_res->i_children = -1;
130         p_res->pp_children = NULL;
131     }
132     p_res->i_parents = 0;
133     p_res->pp_parents = NULL;
134     
135     if( p_item->input.psz_name )
136         p_res->input.psz_name = strdup( p_item->input.psz_name );
137     if( p_item->input.psz_uri )
138         p_res->input.psz_uri = strdup( p_item->input.psz_uri );
139     
140     if( p_item->input.i_es )
141     {
142         p_res->input.es = (es_format_t**)malloc( p_item->input.i_es * sizeof(es_format_t*));
143         for( i = 0; i < p_item->input.i_es; i++ )
144         {
145             p_res->input.es[ i ] = (es_format_t*)malloc(sizeof(es_format_t*));
146             es_format_Copy( p_res->input.es[ i ],
147                          p_item->input.es[ i ] );
148         }
149     }
150     if( p_item->input.i_categories )
151     {
152         p_res->input.pp_categories = NULL;
153         p_res->input.i_categories = 0;
154         for( i = 0; i < p_item->input.i_categories; i++ )
155         {
156             info_category_t *p_incat;
157             p_incat = p_item->input.pp_categories[i];
158             if( p_incat->i_infos )
159             {
160                 int j;
161                 for( j = 0; j < p_incat->i_infos; j++ )
162                 {
163                     vlc_input_item_AddInfo( &p_res->input, p_incat->psz_name,
164                                             p_incat->pp_infos[j]->psz_name,
165                                             "%s", /* to be safe */
166                                             p_incat->pp_infos[j]->psz_value );
167                 }
168             }
169         }
170     }
171
172     vlc_mutex_unlock( &p_item->input.lock );
173     return p_res;
174 }
175
176 /**
177  * Deletes a playlist item
178  *
179  * \param p_item the item to delete
180  * \return nothing
181  */
182 int playlist_ItemDelete( playlist_item_t *p_item )
183 {
184     vlc_mutex_lock( &p_item->input.lock );
185
186     if( p_item->input.psz_name ) free( p_item->input.psz_name );
187     if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
188
189     /* Free the info categories */
190     if( p_item->input.i_categories > 0 )
191     {
192         int i, j;
193
194         for( i = 0; i < p_item->input.i_categories; i++ )
195         {
196             info_category_t *p_category = p_item->input.pp_categories[i];
197
198             for( j = 0; j < p_category->i_infos; j++)
199             {
200                 if( p_category->pp_infos[j]->psz_name )
201                 {
202                     free( p_category->pp_infos[j]->psz_name);
203                 }
204                 if( p_category->pp_infos[j]->psz_value )
205                 {
206                     free( p_category->pp_infos[j]->psz_value );
207                 }
208                 free( p_category->pp_infos[j] );
209             }
210
211             if( p_category->i_infos ) free( p_category->pp_infos );
212             if( p_category->psz_name ) free( p_category->psz_name );
213             free( p_category );
214         }
215
216         free( p_item->input.pp_categories );
217     }
218
219     for( ; p_item->input.i_options > 0; p_item->input.i_options-- )
220     {
221         free( p_item->input.ppsz_options[p_item->input.i_options - 1] );
222         if( p_item->input.i_options == 1 ) free( p_item->input.ppsz_options );
223     }
224
225     for( ; p_item->i_parents > 0 ; )
226     {
227         struct item_parent_t *p_parent =  p_item->pp_parents[0];
228         REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, 0 );
229         free( p_parent );
230     }
231
232     vlc_mutex_unlock( &p_item->input.lock );
233     vlc_mutex_destroy( &p_item->input.lock );
234
235     free( p_item );
236
237     return VLC_SUCCESS;
238 }
239
240 /**
241  *  Add a option to one item ( no need for p_playlist )
242  *
243  * \param p_item the item on which we want the info
244  * \param psz_option the option
245  * \return 0 on success
246  */
247 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
248 {
249     if( !psz_option ) return VLC_EGENERIC;
250
251     vlc_mutex_lock( &p_item->input.lock );
252     INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
253                  p_item->input.i_options, strdup( psz_option ) );
254     vlc_mutex_unlock( &p_item->input.lock );
255
256     return VLC_SUCCESS;
257 }
258
259 /**
260  * Add a parent to an item
261  *
262  * \param p_item the item
263  * \param i_view the view in which the parent is
264  * \param p_parent the parent to add
265  * \return nothing
266  */
267 int playlist_ItemAddParent( playlist_item_t *p_item, int i_view,
268                             playlist_item_t *p_parent )
269 {
270    vlc_bool_t b_found = VLC_FALSE;
271    int i;
272
273    for( i= 0; i< p_item->i_parents ; i++ )
274    {
275        if( p_item->pp_parents[i]->i_view == i_view )
276
277        {
278            b_found = VLC_TRUE;
279            break;
280        }
281    }
282    if( b_found == VLC_FALSE )
283    {
284
285        struct item_parent_t *p_ip = (struct item_parent_t *)
286                malloc(sizeof(struct item_parent_t) );
287        p_ip->i_view = i_view;
288        p_ip->p_parent = p_parent;
289
290        INSERT_ELEM( p_item->pp_parents,
291                     p_item->i_parents, p_item->i_parents,
292                     p_ip );
293    }
294    return VLC_SUCCESS;
295 }
296
297 /**
298  * Copy all parents from parent to child
299  */
300 int playlist_CopyParents( playlist_item_t *p_parent,
301                            playlist_item_t *p_child )
302 {
303     int i=0;
304     for( i= 0 ; i< p_parent->i_parents; i ++ )
305     {
306         playlist_ItemAddParent( p_child,
307                                 p_parent->pp_parents[i]->i_view,
308                                 p_parent );
309     }
310     return VLC_SUCCESS;
311 }
312
313
314 /**********************************************************************
315  * playlist_item_t structure accessors
316  * These functions give access to the fields of the playlist_item_t
317  * structure
318  **********************************************************************/
319
320
321 /**
322  * Set the name of a playlist item
323  *
324  * \param p_item the item
325  * \param psz_name the new name
326  * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
327  */
328 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
329 {
330     if( psz_name && p_item )
331     {
332         p_item->input.psz_name = strdup( psz_name );
333         return VLC_SUCCESS;
334     }
335     return VLC_EGENERIC;
336 }
337
338 /**
339  * Set the duration of a playlist item
340  * This function must be entered with the item lock
341  *
342  * \param p_item the item
343  * \param i_duration the new duration
344  * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
345  */
346 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
347 {
348     char psz_buffer[MSTRTIME_MAX_SIZE];
349     if( p_item )
350     {
351         p_item->input.i_duration = i_duration;
352         if( i_duration != -1 )
353         {
354             secstotimestr( psz_buffer, (int)(i_duration/1000000) );
355         }
356         else
357         {
358             memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
359         }
360         vlc_input_item_AddInfo( &p_item->input, _("General") , _("Duration"),
361                                 "%s", psz_buffer );
362
363         return VLC_SUCCESS;
364     }
365     return VLC_EGENERIC;
366 }
367
368 /*
369  * Guess the type of the item using the beginning of the mrl */
370 static void GuessType( input_item_t *p_item)
371 {
372     int i;
373     static struct { char *psz_search; int i_type; }  types_array[] =
374     {
375         { "http", ITEM_TYPE_NET },
376         { "dvd", ITEM_TYPE_DISC },
377         { "cdda", ITEM_TYPE_CDDA },
378         { "mms", ITEM_TYPE_NET },
379         { "rtsp", ITEM_TYPE_NET },
380         { "udp", ITEM_TYPE_NET },
381         { "rtp", ITEM_TYPE_NET },
382         { "vcd", ITEM_TYPE_DISC },
383         { "v4l", ITEM_TYPE_CARD },
384         { "dshow", ITEM_TYPE_CARD },
385         { "pvr", ITEM_TYPE_CARD },
386         { "dvb", ITEM_TYPE_CARD },
387         { "qpsk", ITEM_TYPE_CARD },
388         { "sdp", ITEM_TYPE_NET },
389         { NULL, 0 }
390     };
391
392     static struct { char *psz_search; int i_type; } exts_array[] =
393     {
394         { "mp3", ITEM_TYPE_AFILE },
395         { NULL, 0 }
396     };
397
398     for( i = 0; types_array[i].psz_search != NULL; i++ )
399     {
400         if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
401                       strlen( types_array[i].psz_search ) ) )
402         {
403             p_item->i_type = types_array[i].i_type;
404             return;
405         }
406     }
407     p_item->i_type = ITEM_TYPE_VFILE;
408 }