1 /*****************************************************************************
2 * item-ext.c : Playlist item management functions (act on the playlist)
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
24 #include <stdlib.h> /* free(), strtol() */
25 #include <stdio.h> /* sprintf() */
26 #include <string.h> /* strerror() */
29 #include <vlc/input.h>
31 #include "vlc_playlist.h"
33 /***************************************************************************
34 * Item creation/addition functions
35 ***************************************************************************/
38 * Add a MRL into the playlist, duration and options given
40 * \param p_playlist the playlist to add into
41 * \param psz_uri the mrl to add to the playlist
42 * \param psz_name a text giving a name or description of this item
43 * \param i_mode the mode used when adding
44 * \param i_pos the position in the playlist where to add. If this is
45 * PLAYLIST_END the item will be added at the end of the playlist
46 * regardless of it's size
47 * \param i_duration length of the item in milliseconds.
48 * \param ppsz_options an array of options
49 * \param i_options the number of options
50 * \return The id of the playlist item
52 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
53 const char *psz_name, int i_mode, int i_pos,
54 mtime_t i_duration, const char **ppsz_options,
57 playlist_item_t *p_item;
58 p_item = playlist_ItemNew( p_playlist , psz_uri, psz_name );
62 msg_Err( p_playlist, "unable to add item to playlist" );
66 p_item->input.i_duration = i_duration;
67 p_item->input.i_options = i_options;
68 p_item->input.ppsz_options = NULL;
70 for( p_item->input.i_options = 0; p_item->input.i_options < i_options;
71 p_item->input.i_options++ )
73 if( !p_item->input.i_options )
75 p_item->input.ppsz_options = malloc( i_options * sizeof(char *) );
76 if( !p_item->input.ppsz_options ) break;
79 p_item->input.ppsz_options[p_item->input.i_options] =
80 strdup( ppsz_options[p_item->input.i_options] );
83 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
87 * Add a MRL into the playlist.
89 * \param p_playlist the playlist to add into
90 * \param psz_uri the mrl to add to the playlist
91 * \param psz_name a text giving a name or description of this item
92 * \param i_mode the mode used when adding
93 * \param i_pos the position in the playlist where to add. If this is
94 * PLAYLIST_END the item will be added at the end of the playlist
95 * regardless of it's size
96 * \return The id of the playlist item
98 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
99 const char *psz_name, int i_mode, int i_pos )
101 return playlist_AddExt( p_playlist, psz_uri, psz_name, i_mode, i_pos,
106 * Add a playlist item into a playlist
108 * \param p_playlist the playlist to insert into
109 * \param p_item the playlist item to insert
110 * \param i_mode the mode used when adding
111 * \param i_pos the possition in the playlist where to add. If this is
112 * PLAYLIST_END the item will be added at the end of the playlist
113 * regardless of it's size
114 * \return The id of the playlist item
116 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
117 int i_mode, int i_pos)
120 vlc_bool_t b_end = VLC_FALSE;
121 playlist_view_t *p_view;
123 vlc_mutex_lock( &p_playlist->object_lock );
126 * CHECK_INSERT : checks if the item is already enqued before
130 /* That should not change */
131 if ( i_mode & PLAYLIST_CHECK_INSERT )
135 if ( p_playlist->pp_items )
137 for ( j = 0; j < p_playlist->i_size; j++ )
139 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
140 p_item->input.psz_uri ) )
142 playlist_ItemDelete( p_item );
143 vlc_mutex_unlock( &p_playlist->object_lock );
148 i_mode &= ~PLAYLIST_CHECK_INSERT;
149 i_mode |= PLAYLIST_APPEND;
152 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
153 p_item->input.psz_name, p_item->input.psz_uri );
155 p_item->input.i_id = ++p_playlist->i_last_id;
157 /* Do a few boundary checks and allocate space for the item */
158 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, p_playlist->i_size, i_pos, p_item );
189 p_playlist->i_enabled ++;
191 /* We update the ALL view directly */
192 playlist_ViewUpdate( p_playlist, VIEW_ALL );
194 /* Add the item to the General category */
195 if( b_end == VLC_TRUE )
197 playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
198 p_playlist->p_general );
202 playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
203 p_playlist->p_general, i_pos );
205 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
206 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
208 /* Also add the item to the "simple" view */
209 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
211 if( b_end == VLC_TRUE )
213 playlist_NodeAppend( p_playlist, VIEW_SIMPLE,p_item,
218 playlist_NodeInsert( p_playlist, VIEW_SIMPLE,p_item,
219 p_view->p_root, i_pos );
222 /* FIXME : Update sorted views */
224 if( p_playlist->i_index >= i_pos )
226 p_playlist->i_index++;
231 msg_Err( p_playlist, "Insert mode not implemented" );
234 if( i_mode & PLAYLIST_GO )
236 p_playlist->request.b_request = VLC_TRUE;
238 p_playlist->request.i_view = VIEW_SIMPLE;
239 p_playlist->request.p_node = p_view->p_root;
240 p_playlist->request.p_item = p_item;
242 if( p_playlist->p_input )
244 input_StopThread( p_playlist->p_input );
246 p_playlist->status.i_status = PLAYLIST_RUNNING;
249 vlc_mutex_unlock( &p_playlist->object_lock );
251 val.b_bool = VLC_TRUE;
252 var_Set( p_playlist, "intf-change", val );
254 return p_item->input.i_id;
259 * Add a playlist item to a given node (in the category view )
261 * \param p_playlist the playlist to insert into
262 * \param p_item the playlist item to insert
263 * \param i_view the view for which to add or TODO: ALL_VIEWS
264 * \param p_parent the parent node
265 * \param i_mode the mode used when adding
266 * \param i_pos the possition in the node where to add. If this is
267 * PLAYLIST_END the item will be added at the end of the node
268 ** \return The id of the playlist item
270 int playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
271 int i_view,playlist_item_t *p_parent,
272 int i_mode, int i_pos)
276 playlist_view_t *p_view;
278 vlc_mutex_lock( &p_playlist->object_lock );
281 if( !p_parent || p_parent->i_children == -1 )
283 msg_Err( p_playlist, "invalid node" );
287 * CHECK_INSERT : checks if the item is already enqued before
290 if ( i_mode & PLAYLIST_CHECK_INSERT )
294 if ( p_playlist->pp_items )
296 for ( j = 0; j < p_playlist->i_size; j++ )
298 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
299 p_item->input.psz_uri ) )
301 playlist_ItemDelete( p_item );
302 vlc_mutex_unlock( &p_playlist->object_lock );
307 i_mode &= ~PLAYLIST_CHECK_INSERT;
308 i_mode |= PLAYLIST_APPEND;
311 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
312 p_item->input.psz_name, p_item->input.psz_uri );
314 p_item->input.i_id = ++p_playlist->i_last_id;
316 /* First, add the item at the right position in the item bank */
318 //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
319 i_position = p_playlist->i_size ;
321 INSERT_ELEM( p_playlist->pp_items,
325 p_playlist->i_enabled ++;
327 /* TODO: Handle modes */
328 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
330 /* We update the ALL view directly */
331 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
332 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
333 playlist_ViewUpdate( p_playlist, VIEW_ALL );
335 /* TODO : Update sorted views*/
337 if( i_mode & PLAYLIST_GO )
339 p_playlist->request.b_request = VLC_TRUE;
340 p_playlist->request.i_view = VIEW_CATEGORY;
341 p_playlist->request.p_node = p_parent;
342 p_playlist->request.p_item = p_item;
343 if( p_playlist->p_input )
345 input_StopThread( p_playlist->p_input );
347 p_playlist->status.i_status = PLAYLIST_RUNNING;
350 vlc_mutex_unlock( &p_playlist->object_lock );
352 val.b_bool = VLC_TRUE;
353 var_Set( p_playlist, "intf-change", val );
355 return p_item->input.i_id;
358 /***************************************************************************
359 * Item search functions
360 ***************************************************************************/
363 * Search the position of an item by its id
364 * This function must be entered with the playlist lock
366 * \param p_playlist the playlist
367 * \param i_id the id to find
368 * \return the position, or VLC_EGENERIC on failure
370 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
373 for( i = 0 ; i < p_playlist->i_size ; i++ )
375 if( p_playlist->pp_items[i]->input.i_id == i_id )
385 * Search an item by its position
386 * This function must be entered with the playlist lock
388 * \param p_playlist the playlist
389 * \param i_pos the position of the item to find
390 * \return the item, or NULL on failure
392 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
394 if( i_pos >= 0 && i_pos < p_playlist->i_size)
396 return p_playlist->pp_items[i_pos];
398 else if( p_playlist->i_size > 0)
400 return p_playlist->pp_items[p_playlist->i_index];
409 * Search an item by its id
411 * \param p_playlist the playlist
412 * \param i_id the id to find
413 * \return the item, or NULL on failure
415 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
418 for( i = 0 ; i < p_playlist->i_size ; i++ )
420 if( p_playlist->pp_items[i]->input.i_id == i_id )
422 return p_playlist->pp_items[i];
431 /***********************************************************************
433 ***********************************************************************/
436 * Transform an item to a node
438 * \param p_playlist the playlist object
439 * \param p_item the item to transform
442 void playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
445 if( p_item->i_children == -1 )
447 p_item->i_children = 0;
450 vlc_mutex_lock( &p_playlist->object_lock );
452 /* Remove it from the array of available items */
453 for( i = 0 ; i < p_playlist->i_size ; i++ )
455 if( p_item == p_playlist->pp_items[i] )
457 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
460 vlc_mutex_unlock( &p_playlist->object_lock );
462 /* Handle the parents
467 * delete an item from a playlist.
469 * \param p_playlist the playlist to remove from.
470 * \param i_id the identifier of the item to delete
471 * \return returns VLC_SUCCESS or an error
473 int playlist_Delete( playlist_t * p_playlist, int i_id )
478 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
485 /* Check if it is the current item */
486 if( p_playlist->status.p_item == p_item )
488 playlist_Control( p_playlist, PLAYLIST_STOP );
489 p_playlist->status.p_item = NULL;
492 vlc_mutex_lock( &p_playlist->object_lock );
494 msg_Dbg( p_playlist, "deleting playlist item `%s'",
495 p_item->input.psz_name );
497 /* Remove the item from all its parent nodes */
498 for ( i= 0 ; i < p_item->i_parents ; i++ )
500 playlist_NodeRemoveItem( p_playlist, p_item,
501 p_item->pp_parents[i]->p_parent );
502 if( p_item->pp_parents[i]->i_view == VIEW_ALL )
504 p_playlist->i_size--;
508 /* TODO : Update views */
510 playlist_ItemDelete( p_item );
512 vlc_mutex_unlock( &p_playlist->object_lock );
514 val.b_bool = VLC_TRUE;
515 var_Set( p_playlist, "intf-change", val );
521 * Clear all playlist items
523 * \param p_playlist the playlist to be cleared.
526 int playlist_Clear( playlist_t * p_playlist )
529 for( i = p_playlist->i_size; i > 0 ; i-- )
531 playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
538 * Disables a playlist item
540 * \param p_playlist the playlist to disable from.
541 * \param i_pos the position of the item to disable
544 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
548 if( !p_item ) return VLC_EGENERIC;
550 msg_Dbg( p_playlist, "disabling playlist item `%s'",
551 p_item->input.psz_name );
553 if( p_item->i_flags & PLAYLIST_ENA_FLAG )
555 p_playlist->i_enabled--;
557 p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
559 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
564 * Enables a playlist item
566 * \param p_playlist the playlist to enable from.
567 * \param i_pos the position of the item to enable
570 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
574 if( !p_item ) return VLC_EGENERIC;
576 msg_Dbg( p_playlist, "enabling playlist item `%s'",
577 p_item->input.psz_name );
579 if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
581 p_playlist->i_enabled++;
583 p_item->i_flags |= PLAYLIST_ENA_FLAG;
585 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
590 * Move an item in a playlist
592 * Move the item in the playlist with position i_pos before the current item
593 * at position i_newpos.
594 * \param p_playlist the playlist to move items in
595 * \param i_pos the position of the item to move
596 * \param i_newpos the position of the item that will be behind the moved item
598 * \return returns VLC_SUCCESS
600 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
603 vlc_mutex_lock( &p_playlist->object_lock );
605 /* take into account that our own row disappears. */
606 if( i_pos < i_newpos ) i_newpos--;
608 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
609 i_newpos <= p_playlist->i_size )
611 playlist_item_t * temp;
613 msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
614 p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
616 if( i_pos == p_playlist->i_index )
618 p_playlist->i_index = i_newpos;
620 else if( i_pos > p_playlist->i_index &&
621 i_newpos <= p_playlist->i_index )
623 p_playlist->i_index++;
625 else if( i_pos < p_playlist->i_index &&
626 i_newpos >= p_playlist->i_index )
628 p_playlist->i_index--;
631 if ( i_pos < i_newpos )
633 temp = p_playlist->pp_items[i_pos];
634 while ( i_pos < i_newpos )
636 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
639 p_playlist->pp_items[i_newpos] = temp;
641 else if ( i_pos > i_newpos )
643 temp = p_playlist->pp_items[i_pos];
644 while ( i_pos > i_newpos )
646 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
649 p_playlist->pp_items[i_newpos] = temp;
653 vlc_mutex_unlock( &p_playlist->object_lock );
655 val.b_bool = VLC_TRUE;
656 var_Set( p_playlist, "intf-change", val );