1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: item.c,v 1.2 2003/11/22 12:35:17 sigmunau 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 );
142 if( p_item->i_options )
145 for( i_opt = 0; i_opt < p_item->i_options; i_opt++ )
147 free( p_item->ppsz_options[i_opt] );
149 free( p_item->ppsz_options );
151 if( p_item->psz_author )
153 free( p_item->psz_author );
156 vlc_mutex_unlock( &p_playlist->object_lock );
161 i_mode &= ~PLAYLIST_CHECK_INSERT;
162 i_mode |= PLAYLIST_APPEND;
166 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
168 /* Create the new playlist item */
171 /* Do a few boundary checks and allocate space for the item */
172 if( i_pos == PLAYLIST_END )
174 if( i_mode & PLAYLIST_INSERT )
176 i_mode &= ~PLAYLIST_INSERT;
177 i_mode |= PLAYLIST_APPEND;
180 i_pos = p_playlist->i_size - 1;
183 if( !(i_mode & PLAYLIST_REPLACE)
184 || i_pos < 0 || i_pos >= p_playlist->i_size )
186 /* Additional boundary checks */
187 if( i_mode & PLAYLIST_APPEND )
196 else if( i_pos > p_playlist->i_size )
198 i_pos = p_playlist->i_size;
201 INSERT_ELEM( p_playlist->pp_items,
205 p_playlist->i_enabled ++;
207 if( p_playlist->i_index >= i_pos )
209 p_playlist->i_index++;
214 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
215 if( p_playlist->pp_items[i_pos]->psz_name )
217 free( p_playlist->pp_items[i_pos]->psz_name );
219 if( p_playlist->pp_items[i_pos]->psz_uri )
221 free( p_playlist->pp_items[i_pos]->psz_uri );
223 /* XXX: what if the item is still in use? */
224 free( p_playlist->pp_items[i_pos] );
225 p_playlist->pp_items[i_pos] = p_item;
228 if( i_mode & PLAYLIST_GO )
230 p_playlist->i_index = i_pos;
231 if( p_playlist->p_input )
233 input_StopThread( p_playlist->p_input );
235 p_playlist->i_status = PLAYLIST_RUNNING;
238 vlc_mutex_unlock( &p_playlist->object_lock );
240 val.b_bool = VLC_TRUE;
241 var_Set( p_playlist, "intf-change", val );
247 * delete an item from a playlist.
249 * \param p_playlist the playlist to remove from.
250 * \param i_pos the position of the item to remove
253 int playlist_Delete( playlist_t * p_playlist, int i_pos )
256 vlc_mutex_lock( &p_playlist->object_lock );
258 if( i_pos >= 0 && i_pos < p_playlist->i_size )
260 msg_Dbg( p_playlist, "deleting playlist item « %s »",
261 p_playlist->pp_items[i_pos]->psz_name );
263 if( p_playlist->pp_items[i_pos]->psz_name )
265 free( p_playlist->pp_items[i_pos]->psz_name );
267 if( p_playlist->pp_items[i_pos]->psz_uri )
269 free( p_playlist->pp_items[i_pos]->psz_uri );
271 if( p_playlist->pp_items[i_pos]->psz_author )
273 free( p_playlist->pp_items[i_pos]->psz_author );
275 if( p_playlist->pp_items[i_pos]->i_options )
279 for( i = 0; i < p_playlist->pp_items[i_pos]->i_options; i++ )
280 free( p_playlist->pp_items[i_pos]->ppsz_options[i] );
282 free( p_playlist->pp_items[i_pos]->ppsz_options );
285 /* XXX: what if the item is still in use? */
286 free( p_playlist->pp_items[i_pos] );
288 if( i_pos <= p_playlist->i_index )
290 p_playlist->i_index--;
293 /* Renumber the playlist */
294 REMOVE_ELEM( p_playlist->pp_items,
297 if( p_playlist->i_enabled > 0 )
298 p_playlist->i_enabled--;
301 vlc_mutex_unlock( &p_playlist->object_lock );
303 val.b_bool = VLC_TRUE;
304 var_Set( p_playlist, "intf-change", val );
310 * Disables a playlist item
312 * \param p_playlist the playlist to disable from.
313 * \param i_pos the position of the item to disable
316 int playlist_Disable( playlist_t * p_playlist, int i_pos )
319 vlc_mutex_lock( &p_playlist->object_lock );
322 if( i_pos >= 0 && i_pos < p_playlist->i_size )
324 msg_Dbg( p_playlist, "disabling playlist item « %s »",
325 p_playlist->pp_items[i_pos]->psz_name );
327 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
328 p_playlist->i_enabled--;
329 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
332 vlc_mutex_unlock( &p_playlist->object_lock );
334 val.b_bool = VLC_TRUE;
335 var_Set( p_playlist, "intf-change", val );
341 * Enables a playlist item
343 * \param p_playlist the playlist to enable from.
344 * \param i_pos the position of the item to enable
347 int playlist_Enable( playlist_t * p_playlist, int i_pos )
350 vlc_mutex_lock( &p_playlist->object_lock );
352 if( i_pos >= 0 && i_pos < p_playlist->i_size )
354 msg_Dbg( p_playlist, "enabling playlist item « %s »",
355 p_playlist->pp_items[i_pos]->psz_name );
357 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
358 p_playlist->i_enabled++;
360 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
363 vlc_mutex_unlock( &p_playlist->object_lock );
365 val.b_bool = VLC_TRUE;
366 var_Set( p_playlist, "intf-change", val );
372 * Disables a playlist group
374 * \param p_playlist the playlist to disable from.
375 * \param i_pos the id of the group to disable
378 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
382 vlc_mutex_lock( &p_playlist->object_lock );
384 msg_Dbg(p_playlist,"Disabling group %i",i_group);
385 for( i = 0 ; i< p_playlist->i_size; i++ )
387 if( p_playlist->pp_items[i]->i_group == i_group )
389 msg_Dbg( p_playlist, "disabling playlist item « %s »",
390 p_playlist->pp_items[i]->psz_name );
392 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
393 p_playlist->i_enabled--;
395 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
398 vlc_mutex_unlock( &p_playlist->object_lock );
400 val.b_bool = VLC_TRUE;
401 var_Set( p_playlist, "intf-change", val );
407 * Enables a playlist group
409 * \param p_playlist the playlist to enable from.
410 * \param i_pos the id of the group to enable
413 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
417 vlc_mutex_lock( &p_playlist->object_lock );
419 for( i = 0 ; i< p_playlist->i_size; i++ )
421 if( p_playlist->pp_items[i]->i_group == i_group )
423 msg_Dbg( p_playlist, "enabling playlist item « %s »",
424 p_playlist->pp_items[i]->psz_name );
426 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
427 p_playlist->i_enabled++;
429 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
432 vlc_mutex_unlock( &p_playlist->object_lock );
434 val.b_bool = VLC_TRUE;
435 var_Set( p_playlist, "intf-change", val );
441 * Move an item in a playlist
443 * Move the item in the playlist with position i_pos before the current item
444 * at position i_newpos.
445 * \param p_playlist the playlist to move items in
446 * \param i_pos the position of the item to move
447 * \param i_newpos the position of the item that will be behind the moved item
451 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
454 vlc_mutex_lock( &p_playlist->object_lock );
456 /* take into account that our own row disappears. */
457 if ( i_pos < i_newpos ) i_newpos--;
459 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
460 && i_newpos <= p_playlist->i_size )
462 playlist_item_t * temp;
464 msg_Dbg( p_playlist, "moving playlist item « %s » (%i -> %i)",
465 p_playlist->pp_items[i_pos]->psz_name, i_pos,
468 if( i_pos == p_playlist->i_index )
470 p_playlist->i_index = i_newpos;
472 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
474 p_playlist->i_index++;
476 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
478 p_playlist->i_index--;
481 if ( i_pos < i_newpos )
483 temp = p_playlist->pp_items[i_pos];
484 while ( i_pos < i_newpos )
486 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
489 p_playlist->pp_items[i_newpos] = temp;
491 else if ( i_pos > i_newpos )
493 temp = p_playlist->pp_items[i_pos];
494 while ( i_pos > i_newpos )
496 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
499 p_playlist->pp_items[i_newpos] = temp;
503 vlc_mutex_unlock( &p_playlist->object_lock );
505 val.b_bool = VLC_TRUE;
506 var_Set( p_playlist, "intf-change", val );