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