1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: item.c,v 1.1 2003/10/29 18:00:46 zorglub 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( "" );
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 = (char **)malloc( i_options * sizeof(char *) );
94 for( i = 0; i < i_options; i++ )
95 p_item->ppsz_options[i] = strdup( ppsz_options[i] );
99 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
103 * Add a playlist item into a playlist
105 * \param p_playlist the playlist to insert into
106 * \param p_item the playlist item to insert
107 * \param i_mode the mode used when adding
108 * \param i_pos the possition in the playlist where to add. If this is
109 * PLAYLIST_END the item will be added at the end of the playlist
110 * regardless of it's size
111 * \return always returns 0
113 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
114 int i_mode, int i_pos)
118 vlc_mutex_lock( &p_playlist->object_lock );
121 * CHECK_INSERT : checks if the item is already enqued before
124 if ( i_mode & PLAYLIST_CHECK_INSERT )
128 if ( p_playlist->pp_items )
130 for ( j = 0; j < p_playlist->i_size; j++ )
132 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
134 if( p_item->psz_name )
136 free( p_item->psz_name );
138 if( p_item->psz_uri )
140 free( p_item->psz_uri );
143 vlc_mutex_unlock( &p_playlist->object_lock );
148 i_mode &= ~PLAYLIST_CHECK_INSERT;
149 i_mode |= PLAYLIST_APPEND;
153 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
155 /* Create the new playlist item */
158 /* Do a few boundary checks and allocate space for the item */
159 if( i_pos == PLAYLIST_END )
161 if( i_mode & PLAYLIST_INSERT )
163 i_mode &= ~PLAYLIST_INSERT;
164 i_mode |= PLAYLIST_APPEND;
167 i_pos = p_playlist->i_size - 1;
170 if( !(i_mode & PLAYLIST_REPLACE)
171 || i_pos < 0 || i_pos >= p_playlist->i_size )
173 /* Additional boundary checks */
174 if( i_mode & PLAYLIST_APPEND )
183 else if( i_pos > p_playlist->i_size )
185 i_pos = p_playlist->i_size;
188 INSERT_ELEM( p_playlist->pp_items,
192 p_playlist->i_enabled ++;
194 if( p_playlist->i_index >= i_pos )
196 p_playlist->i_index++;
201 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
202 if( p_playlist->pp_items[i_pos]->psz_name )
204 free( p_playlist->pp_items[i_pos]->psz_name );
206 if( p_playlist->pp_items[i_pos]->psz_uri )
208 free( p_playlist->pp_items[i_pos]->psz_uri );
210 /* XXX: what if the item is still in use? */
211 free( p_playlist->pp_items[i_pos] );
212 p_playlist->pp_items[i_pos] = p_item;
215 if( i_mode & PLAYLIST_GO )
217 p_playlist->i_index = i_pos;
218 if( p_playlist->p_input )
220 input_StopThread( p_playlist->p_input );
222 p_playlist->i_status = PLAYLIST_RUNNING;
225 vlc_mutex_unlock( &p_playlist->object_lock );
227 val.b_bool = VLC_TRUE;
228 var_Set( p_playlist, "intf-change", val );
234 * delete an item from a playlist.
236 * \param p_playlist the playlist to remove from.
237 * \param i_pos the position of the item to remove
240 int playlist_Delete( playlist_t * p_playlist, int i_pos )
243 vlc_mutex_lock( &p_playlist->object_lock );
245 if( i_pos >= 0 && i_pos < p_playlist->i_size )
247 msg_Dbg( p_playlist, "deleting playlist item « %s »",
248 p_playlist->pp_items[i_pos]->psz_name );
250 if( p_playlist->pp_items[i_pos]->psz_name )
252 free( p_playlist->pp_items[i_pos]->psz_name );
254 if( p_playlist->pp_items[i_pos]->psz_uri )
256 free( p_playlist->pp_items[i_pos]->psz_uri );
258 if( p_playlist->pp_items[i_pos]->psz_author )
260 free( p_playlist->pp_items[i_pos]->psz_author );
262 if( p_playlist->pp_items[i_pos]->i_options )
266 for( i = 0; i < p_playlist->pp_items[i_pos]->i_options; i++ )
267 free( p_playlist->pp_items[i_pos]->ppsz_options[i] );
269 free( p_playlist->pp_items[i_pos]->ppsz_options );
272 /* XXX: what if the item is still in use? */
273 free( p_playlist->pp_items[i_pos] );
275 if( i_pos <= p_playlist->i_index )
277 p_playlist->i_index--;
280 /* Renumber the playlist */
281 REMOVE_ELEM( p_playlist->pp_items,
284 if( p_playlist->i_enabled > 0 )
285 p_playlist->i_enabled--;
288 vlc_mutex_unlock( &p_playlist->object_lock );
290 val.b_bool = VLC_TRUE;
291 var_Set( p_playlist, "intf-change", val );
297 * Disables a playlist item
299 * \param p_playlist the playlist to disable from.
300 * \param i_pos the position of the item to disable
303 int playlist_Disable( playlist_t * p_playlist, int i_pos )
306 vlc_mutex_lock( &p_playlist->object_lock );
309 if( i_pos >= 0 && i_pos < p_playlist->i_size )
311 msg_Dbg( p_playlist, "disabling playlist item « %s »",
312 p_playlist->pp_items[i_pos]->psz_name );
314 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
315 p_playlist->i_enabled--;
316 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
319 vlc_mutex_unlock( &p_playlist->object_lock );
321 val.b_bool = VLC_TRUE;
322 var_Set( p_playlist, "intf-change", val );
328 * Enables a playlist item
330 * \param p_playlist the playlist to enable from.
331 * \param i_pos the position of the item to enable
334 int playlist_Enable( playlist_t * p_playlist, int i_pos )
337 vlc_mutex_lock( &p_playlist->object_lock );
339 if( i_pos >= 0 && i_pos < p_playlist->i_size )
341 msg_Dbg( p_playlist, "enabling playlist item « %s »",
342 p_playlist->pp_items[i_pos]->psz_name );
344 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
345 p_playlist->i_enabled++;
347 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
350 vlc_mutex_unlock( &p_playlist->object_lock );
352 val.b_bool = VLC_TRUE;
353 var_Set( p_playlist, "intf-change", val );
359 * Disables a playlist group
361 * \param p_playlist the playlist to disable from.
362 * \param i_pos the id of the group to disable
365 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
369 vlc_mutex_lock( &p_playlist->object_lock );
371 msg_Dbg(p_playlist,"Disabling group %i",i_group);
372 for( i = 0 ; i< p_playlist->i_size; i++ )
374 if( p_playlist->pp_items[i]->i_group == i_group )
376 msg_Dbg( p_playlist, "disabling playlist item « %s »",
377 p_playlist->pp_items[i]->psz_name );
379 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
380 p_playlist->i_enabled--;
382 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
385 vlc_mutex_unlock( &p_playlist->object_lock );
387 val.b_bool = VLC_TRUE;
388 var_Set( p_playlist, "intf-change", val );
394 * Enables a playlist group
396 * \param p_playlist the playlist to enable from.
397 * \param i_pos the id of the group to enable
400 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
404 vlc_mutex_lock( &p_playlist->object_lock );
406 for( i = 0 ; i< p_playlist->i_size; i++ )
408 if( p_playlist->pp_items[i]->i_group == i_group )
410 msg_Dbg( p_playlist, "enabling playlist item « %s »",
411 p_playlist->pp_items[i]->psz_name );
413 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
414 p_playlist->i_enabled++;
416 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
419 vlc_mutex_unlock( &p_playlist->object_lock );
421 val.b_bool = VLC_TRUE;
422 var_Set( p_playlist, "intf-change", val );
428 * Move an item in a playlist
430 * Move the item in the playlist with position i_pos before the current item
431 * at position i_newpos.
432 * \param p_playlist the playlist to move items in
433 * \param i_pos the position of the item to move
434 * \param i_newpos the position of the item that will be behind the moved item
438 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
441 vlc_mutex_lock( &p_playlist->object_lock );
443 /* take into account that our own row disappears. */
444 if ( i_pos < i_newpos ) i_newpos--;
446 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
447 && i_newpos <= p_playlist->i_size )
449 playlist_item_t * temp;
451 msg_Dbg( p_playlist, "moving playlist item « %s » (%i -> %i)",
452 p_playlist->pp_items[i_pos]->psz_name, i_pos,
455 if( i_pos == p_playlist->i_index )
457 p_playlist->i_index = i_newpos;
459 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
461 p_playlist->i_index++;
463 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
465 p_playlist->i_index--;
468 if ( i_pos < i_newpos )
470 temp = p_playlist->pp_items[i_pos];
471 while ( i_pos < i_newpos )
473 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
476 p_playlist->pp_items[i_newpos] = temp;
478 else if ( i_pos > i_newpos )
480 temp = p_playlist->pp_items[i_pos];
481 while ( i_pos > i_newpos )
483 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
486 p_playlist->pp_items[i_newpos] = temp;
490 vlc_mutex_unlock( &p_playlist->object_lock );
492 val.b_bool = VLC_TRUE;
493 var_Set( p_playlist, "intf-change", val );