]> git.sesse.net Git - vlc/blob - src/playlist/item.c
* playlist.c: better random algorithm: do not play an item if it has
[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 /**
33  * Create a new item, without adding it to the playlist
34  *
35  * \param psz_uri the mrl of the item
36  * \param psz_name a text giving a name or description of the item
37  * \return the new item or NULL on failure
38  */
39 playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
40                                       const char *psz_uri,
41                                       const char *psz_name )
42 {
43     playlist_item_t * p_item;
44
45     p_item = malloc( sizeof( playlist_item_t ) );
46     if( p_item == NULL ) return NULL;
47     if( psz_uri == NULL) return NULL;
48
49     memset( p_item, 0, sizeof( playlist_item_t ) );
50
51     p_item->input.psz_uri = strdup( psz_uri );
52
53     if( psz_name != NULL ) p_item->input.psz_name = strdup( psz_name );
54     else p_item->input.psz_name = strdup ( psz_uri );
55
56     p_item->b_enabled = VLC_TRUE;
57     p_item->i_group = PLAYLIST_TYPE_MANUAL;
58     p_item->i_nb_played = 0;
59
60     p_item->input.i_duration = -1;
61     p_item->input.ppsz_options = NULL;
62     p_item->input.i_options = 0;
63
64     vlc_mutex_init( p_obj, &p_item->input.lock );
65
66     playlist_ItemCreateCategory( p_item, _("General") );
67     return p_item;
68 }
69
70 /**
71  * Deletes a playlist item
72  *
73  * \param p_item the item to delete
74  * \return nothing
75  */
76 void playlist_ItemDelete( playlist_item_t *p_item )
77 {
78 #if 0
79     int i,j;
80 #endif
81
82     vlc_mutex_lock( &p_item->input.lock );
83
84     if( p_item->input.psz_name ) free( p_item->input.psz_name );
85     if( p_item->input.psz_uri ) free( p_item->input.psz_uri );
86
87 #if 0
88     /* Free the info categories. Welcome to the segfault factory */
89     if( p_item->i_categories > 0 )
90     {
91         for( i = 0; i < p_item->i_categories; i++ )
92         {
93             for( j= 0 ; j < p_item->pp_categories[i]->i_infos; j++)
94             {
95                 if( p_item->pp_categories[i]->pp_infos[j]->psz_name)
96                 {
97                     free( p_item->pp_categories[i]->
98                                   pp_infos[j]->psz_name);
99                 }
100                 if( p_item->pp_categories[i]->pp_infos[j]->psz_value)
101                 {
102                     free( p_item->pp_categories[i]->
103                                   pp_infos[j]->psz_value);
104                 }
105                 free( p_item->pp_categories[i]->pp_infos[j] );
106             }
107             if( p_item->pp_categories[i]->i_infos )
108                 free( p_item->pp_categories[i]->pp_infos );
109             if( p_item->pp_categories[i]->psz_name)
110             {
111                 free( p_item->pp_categories[i]->psz_name );
112             }
113             free( p_item->pp_categories[i] );
114         }
115         free( p_item->pp_categories );
116     }
117 #endif
118
119     vlc_mutex_unlock( &p_item->input.lock );
120     vlc_mutex_destroy( &p_item->input.lock );
121
122     free( p_item );
123 }
124
125 /**
126  * Add a playlist item into a playlist
127  *
128  * \param p_playlist the playlist to insert into
129  * \param p_item the playlist item to insert
130  * \param i_mode the mode used when adding
131  * \param i_pos the possition in the playlist where to add. If this is
132  *        PLAYLIST_END the item will be added at the end of the playlist
133  *        regardless of it's size
134  * \return The id of the playlist item
135  */
136 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
137                       int i_mode, int i_pos)
138 {
139     vlc_value_t val;
140
141     vlc_mutex_lock( &p_playlist->object_lock );
142
143     /*
144      * CHECK_INSERT : checks if the item is already enqued before
145      * enqueing it
146      */
147     if ( i_mode & PLAYLIST_CHECK_INSERT )
148     {
149          int j;
150
151          if ( p_playlist->pp_items )
152          {
153              for ( j = 0; j < p_playlist->i_size; j++ )
154              {
155                  if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
156                                p_item->input.psz_uri ) )
157                  {
158                       if ( p_item->input.psz_name )
159                       {
160                           free( p_item->input.psz_name );
161                       }
162                       if ( p_item->input.psz_uri )
163                       {
164                           free ( p_item->input.psz_uri );
165                       }
166                       free( p_item );
167                       vlc_mutex_unlock( &p_playlist->object_lock );
168                       return -1;
169                  }
170              }
171          }
172          i_mode &= ~PLAYLIST_CHECK_INSERT;
173          i_mode |= PLAYLIST_APPEND;
174     }
175
176     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
177              p_item->input.psz_name, p_item->input.psz_uri );
178
179     p_item->i_id = ++p_playlist->i_last_id;
180
181     /* Do a few boundary checks and allocate space for the item */
182     if( i_pos == PLAYLIST_END )
183     {
184         if( i_mode & PLAYLIST_INSERT )
185         {
186             i_mode &= ~PLAYLIST_INSERT;
187             i_mode |= PLAYLIST_APPEND;
188         }
189
190         i_pos = p_playlist->i_size - 1;
191     }
192
193     if( !(i_mode & PLAYLIST_REPLACE)
194          || i_pos < 0 || i_pos >= p_playlist->i_size )
195     {
196         /* Additional boundary checks */
197         if( i_mode & PLAYLIST_APPEND )
198         {
199             i_pos++;
200         }
201
202         if( i_pos < 0 )
203         {
204             i_pos = 0;
205         }
206         else if( i_pos > p_playlist->i_size )
207         {
208             i_pos = p_playlist->i_size;
209         }
210
211         INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos, p_item );
212         p_playlist->i_enabled ++;
213
214         if( p_playlist->i_index >= i_pos )
215         {
216             p_playlist->i_index++;
217         }
218     }
219     else
220     {
221         /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
222         if( p_playlist->pp_items[i_pos]->input.psz_name )
223         {
224             free( p_playlist->pp_items[i_pos]->input.psz_name );
225         }
226         if( p_playlist->pp_items[i_pos]->input.psz_uri )
227         {
228             free( p_playlist->pp_items[i_pos]->input.psz_uri );
229         }
230         /* XXX: what if the item is still in use? */
231         free( p_playlist->pp_items[i_pos] );
232         p_playlist->pp_items[i_pos] = p_item;
233     }
234
235     if( i_mode & PLAYLIST_GO )
236     {
237         p_playlist->i_index = i_pos;
238         if( p_playlist->p_input )
239         {
240             input_StopThread( p_playlist->p_input );
241         }
242         p_playlist->i_status = PLAYLIST_RUNNING;
243     }
244
245     vlc_mutex_unlock( &p_playlist->object_lock );
246
247     val.b_bool = VLC_TRUE;
248     var_Set( p_playlist, "intf-change", val );
249
250     return p_item->i_id;
251 }
252
253 /**
254  *  Add a option to one item ( no need for p_playlist )
255  *
256  * \param p_item the item on which we want the info
257  * \param psz_format the option
258  * \return 0 on success
259  */
260 int playlist_ItemAddOption( playlist_item_t *p_item, const char *psz_option )
261 {
262     if( !psz_option ) return VLC_EGENERIC;
263
264     vlc_mutex_lock( &p_item->input.lock );
265     INSERT_ELEM( p_item->input.ppsz_options, p_item->input.i_options,
266                  p_item->input.i_options, strdup( psz_option ) );
267     vlc_mutex_unlock( &p_item->input.lock );
268
269     return VLC_SUCCESS;
270 }