1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: item.c,v 1.4 2003/11/25 00:56:35 fenrir Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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() */
31 #include "vlc_playlist.h"
34 * Add an MRL to the playlist. This is a simplified version of
35 * playlist_AddExt inculded for convenince. It equals calling playlist_AddExt
36 * with psz_name == psz_target and i_duration == -1
39 int playlist_Add( playlist_t *p_playlist, const char *psz_target,
40 const char **ppsz_options, int i_options,
41 int i_mode, int i_pos )
43 return playlist_AddExt( p_playlist, psz_target, psz_target, -1,
44 ppsz_options, i_options, i_mode, i_pos );
48 * Add a MRL into the playlist.
50 * \param p_playlist the playlist to add into
51 * \param psz_uri the mrl to add to the playlist
52 * \param psz_name a text giving a name or description of this item
53 * \param i_duration a hint about the duration of this item, in miliseconds, or
55 * \param ppsz_options array of options
56 * \param i_options number of items in ppsz_options
57 * \param i_mode the mode used when adding
58 * \param i_pos the position in the playlist where to add. If this is
59 * PLAYLIST_END the item will be added at the end of the playlist
60 * regardless of it's size
61 * \return always returns 0
63 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
64 const char * psz_name, mtime_t i_duration,
65 const char **ppsz_options, int i_options, int i_mode,
68 playlist_item_t * p_item;
70 p_item = malloc( sizeof( playlist_item_t ) );
73 msg_Err( p_playlist, "out of memory" );
76 p_item->psz_name = strdup( psz_name );
77 p_item->psz_uri = strdup( psz_uri );
78 p_item->psz_author = strdup( "Arg" );
79 p_item->i_duration = i_duration;
82 p_item->b_autodeletion = VLC_FALSE;
83 p_item->b_enabled = VLC_TRUE;
84 p_item->i_group = PLAYLIST_TYPE_MANUAL;
86 p_item->ppsz_options = NULL;
87 p_item->i_options = i_options;
93 p_item->ppsz_options = malloc( i_options * sizeof(char *) );
94 for( i = 0; i < i_options; i++ )
96 p_item->ppsz_options[i] = strdup( ppsz_options[i] );
101 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
105 * Add a playlist item into a playlist
107 * \param p_playlist the playlist to insert into
108 * \param p_item the playlist item to insert
109 * \param i_mode the mode used when adding
110 * \param i_pos the possition in the playlist where to add. If this is
111 * PLAYLIST_END the item will be added at the end of the playlist
112 * regardless of it's size
113 * \return always returns 0
115 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
116 int i_mode, int i_pos)
120 vlc_mutex_lock( &p_playlist->object_lock );
123 * CHECK_INSERT : checks if the item is already enqued before
126 if ( i_mode & PLAYLIST_CHECK_INSERT )
130 if ( p_playlist->pp_items )
132 for ( j = 0; j < p_playlist->i_size; j++ )
134 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
136 if( p_item->psz_name )
138 free( p_item->psz_name );
140 if( p_item->psz_uri )
142 free( p_item->psz_uri );
144 if( p_item->i_options )
147 for( i_opt = 0; i_opt < p_item->i_options; i_opt++ )
149 free( p_item->ppsz_options[i_opt] );
151 free( p_item->ppsz_options );
153 if( p_item->psz_author )
155 free( p_item->psz_author );
158 vlc_mutex_unlock( &p_playlist->object_lock );
163 i_mode &= ~PLAYLIST_CHECK_INSERT;
164 i_mode |= PLAYLIST_APPEND;
168 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
170 /* Create the new playlist item */
173 /* Do a few boundary checks and allocate space for the item */
174 if( i_pos == PLAYLIST_END )
176 if( i_mode & PLAYLIST_INSERT )
178 i_mode &= ~PLAYLIST_INSERT;
179 i_mode |= PLAYLIST_APPEND;
182 i_pos = p_playlist->i_size - 1;
185 if( !(i_mode & PLAYLIST_REPLACE)
186 || i_pos < 0 || i_pos >= p_playlist->i_size )
188 /* Additional boundary checks */
189 if( i_mode & PLAYLIST_APPEND )
198 else if( i_pos > p_playlist->i_size )
200 i_pos = p_playlist->i_size;
203 INSERT_ELEM( p_playlist->pp_items,
207 p_playlist->i_enabled ++;
209 if( p_playlist->i_index >= i_pos )
211 p_playlist->i_index++;
216 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
217 if( p_playlist->pp_items[i_pos]->psz_name )
219 free( p_playlist->pp_items[i_pos]->psz_name );
221 if( p_playlist->pp_items[i_pos]->psz_uri )
223 free( p_playlist->pp_items[i_pos]->psz_uri );
225 if( p_playlist->pp_items[i_pos]->psz_author )
227 free( p_playlist->pp_items[i_pos]->psz_author );
229 /* XXX: what if the item is still in use? */
230 free( p_playlist->pp_items[i_pos] );
231 p_playlist->pp_items[i_pos] = p_item;
234 if( i_mode & PLAYLIST_GO )
236 p_playlist->i_index = i_pos;
237 if( p_playlist->p_input )
239 input_StopThread( p_playlist->p_input );
241 p_playlist->i_status = PLAYLIST_RUNNING;
244 vlc_mutex_unlock( &p_playlist->object_lock );
246 val.b_bool = VLC_TRUE;
247 var_Set( p_playlist, "intf-change", val );
253 * delete an item from a playlist.
255 * \param p_playlist the playlist to remove from.
256 * \param i_pos the position of the item to remove
259 int playlist_Delete( playlist_t * p_playlist, int i_pos )
263 /* if i_pos is the current played item, playlist should stop playing it */
264 if( ( p_playlist->i_status == PLAYLIST_RUNNING) && (p_playlist->i_index == i_pos) )
266 playlist_Command( p_playlist, PLAYLIST_STOP, 0 );
269 vlc_mutex_lock( &p_playlist->object_lock );
270 if( i_pos >= 0 && i_pos < p_playlist->i_size )
272 playlist_item_t *p_item = p_playlist->pp_items[i_pos];
274 msg_Dbg( p_playlist, "deleting playlist item « %s »",
277 if( p_item->psz_name )
279 free( p_item->psz_name );
281 if( p_item->psz_uri )
283 free( p_item->psz_uri );
285 if( p_item->psz_author )
287 free( p_item->psz_author );
289 if( p_item->i_options > 0 )
293 for( i = 0; i < p_item->i_options; i++ )
295 free( p_item->ppsz_options[i] );
298 free( p_item->ppsz_options );
301 /* XXX: what if the item is still in use? */
304 if( i_pos <= p_playlist->i_index )
306 p_playlist->i_index--;
309 /* Renumber the playlist */
310 REMOVE_ELEM( p_playlist->pp_items,
313 if( p_playlist->i_enabled > 0 )
314 p_playlist->i_enabled--;
317 vlc_mutex_unlock( &p_playlist->object_lock );
319 val.b_bool = VLC_TRUE;
320 var_Set( p_playlist, "intf-change", val );
326 * Disables a playlist item
328 * \param p_playlist the playlist to disable from.
329 * \param i_pos the position of the item to disable
332 int playlist_Disable( playlist_t * p_playlist, int i_pos )
335 vlc_mutex_lock( &p_playlist->object_lock );
338 if( i_pos >= 0 && i_pos < p_playlist->i_size )
340 msg_Dbg( p_playlist, "disabling playlist item « %s »",
341 p_playlist->pp_items[i_pos]->psz_name );
343 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
344 p_playlist->i_enabled--;
345 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
348 vlc_mutex_unlock( &p_playlist->object_lock );
350 val.b_bool = VLC_TRUE;
351 var_Set( p_playlist, "intf-change", val );
357 * Enables a playlist item
359 * \param p_playlist the playlist to enable from.
360 * \param i_pos the position of the item to enable
363 int playlist_Enable( playlist_t * p_playlist, int i_pos )
366 vlc_mutex_lock( &p_playlist->object_lock );
368 if( i_pos >= 0 && i_pos < p_playlist->i_size )
370 msg_Dbg( p_playlist, "enabling playlist item « %s »",
371 p_playlist->pp_items[i_pos]->psz_name );
373 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
374 p_playlist->i_enabled++;
376 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
379 vlc_mutex_unlock( &p_playlist->object_lock );
381 val.b_bool = VLC_TRUE;
382 var_Set( p_playlist, "intf-change", val );
388 * Disables a playlist group
390 * \param p_playlist the playlist to disable from.
391 * \param i_pos the id of the group to disable
394 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
398 vlc_mutex_lock( &p_playlist->object_lock );
400 msg_Dbg(p_playlist,"Disabling group %i",i_group);
401 for( i = 0 ; i< p_playlist->i_size; i++ )
403 if( p_playlist->pp_items[i]->i_group == i_group )
405 msg_Dbg( p_playlist, "disabling playlist item « %s »",
406 p_playlist->pp_items[i]->psz_name );
408 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
409 p_playlist->i_enabled--;
411 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
414 vlc_mutex_unlock( &p_playlist->object_lock );
416 val.b_bool = VLC_TRUE;
417 var_Set( p_playlist, "intf-change", val );
423 * Enables a playlist group
425 * \param p_playlist the playlist to enable from.
426 * \param i_pos the id of the group to enable
429 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
433 vlc_mutex_lock( &p_playlist->object_lock );
435 for( i = 0 ; i< p_playlist->i_size; i++ )
437 if( p_playlist->pp_items[i]->i_group == i_group )
439 msg_Dbg( p_playlist, "enabling playlist item « %s »",
440 p_playlist->pp_items[i]->psz_name );
442 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
443 p_playlist->i_enabled++;
445 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
448 vlc_mutex_unlock( &p_playlist->object_lock );
450 val.b_bool = VLC_TRUE;
451 var_Set( p_playlist, "intf-change", val );
457 * Move an item in a playlist
459 * Move the item in the playlist with position i_pos before the current item
460 * at position i_newpos.
461 * \param p_playlist the playlist to move items in
462 * \param i_pos the position of the item to move
463 * \param i_newpos the position of the item that will be behind the moved item
467 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
470 vlc_mutex_lock( &p_playlist->object_lock );
472 /* take into account that our own row disappears. */
473 if ( i_pos < i_newpos ) i_newpos--;
475 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
476 && i_newpos <= p_playlist->i_size )
478 playlist_item_t * temp;
480 msg_Dbg( p_playlist, "moving playlist item « %s » (%i -> %i)",
481 p_playlist->pp_items[i_pos]->psz_name, i_pos,
484 if( i_pos == p_playlist->i_index )
486 p_playlist->i_index = i_newpos;
488 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
490 p_playlist->i_index++;
492 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
494 p_playlist->i_index--;
497 if ( i_pos < i_newpos )
499 temp = p_playlist->pp_items[i_pos];
500 while ( i_pos < i_newpos )
502 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
505 p_playlist->pp_items[i_newpos] = temp;
507 else if ( i_pos > i_newpos )
509 temp = p_playlist->pp_items[i_pos];
510 while ( i_pos > i_newpos )
512 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
515 p_playlist->pp_items[i_newpos] = temp;
519 vlc_mutex_unlock( &p_playlist->object_lock );
521 val.b_bool = VLC_TRUE;
522 var_Set( p_playlist, "intf-change", val );