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