1 /*****************************************************************************
2 * item-ext.c : Exported playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: item-ext.c,v 1.7 2004/01/10 14:24:33 hartman Exp $
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() */
32 #include "vlc_playlist.h"
35 * Add a MRL into the playlist.
37 * \param p_playlist the playlist to add into
38 * \param psz_uri the mrl to add to the playlist
39 * \param psz_name a text giving a name or description of this item
40 * \param i_mode the mode used when adding
41 * \param i_pos the position in the playlist where to add. If this is
42 * PLAYLIST_END the item will be added at the end of the playlist
43 * regardless of it's size
44 * \param i_duration length of the item in milliseconds.
45 * \return the position of the new item
47 int playlist_AddWDuration( playlist_t *p_playlist, const char * psz_uri,
48 const char *psz_name, int i_mode, int i_pos,
51 playlist_item_t * p_item;
53 p_item = malloc( sizeof( playlist_item_t ) );
56 msg_Err( p_playlist, "out of memory" );
60 msg_Err( p_playlist, "Not adding NULL item");
63 p_item->psz_uri = strdup( psz_uri );
64 if( psz_name != NULL )
66 p_item->psz_name = strdup( psz_name );
70 p_item->psz_name = strdup ( psz_uri );
73 p_item->b_autodeletion = VLC_FALSE;
74 p_item->b_enabled = VLC_TRUE;
75 p_item->i_group = PLAYLIST_TYPE_MANUAL;
76 p_item->i_duration = i_duration;
78 p_item->pp_categories = NULL;
79 p_item->i_categories = 0;
81 playlist_CreateItemCategory( p_item, _("General") );
82 playlist_CreateItemCategory( p_item, _("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 position of the new 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_AddWDuration ( p_playlist, psz_uri, psz_name, i_mode, i_pos,
106 * Search the position of an item by its id
107 * \param p_playlist the playlist
108 * \param i_id the id to find
109 * \return the position, or -1 on failure
111 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
114 for( i = 0 ; i < p_playlist->i_size ; i++ )
116 if( p_playlist->pp_items[i]->i_id == i_id )
126 * Search an item by its id
127 * \param p_playlist the playlist
128 * \param i_id the id to find
129 * \return the item, or NULL on failure
131 playlist_item_t * playlist_GetItemById( playlist_t * p_playlist , int i_id )
134 for( i = 0 ; i < p_playlist->i_size ; i++ )
136 if( p_playlist->pp_items[i]->i_id == i_id )
138 return p_playlist->pp_items[i];
144 /**********************************************************************
145 * playlist_item_t structure accessors
146 * These functions give access to the fields of the playlist_item_t
148 **********************************************************************/
151 * Set the group of a playlist item
153 * \param p_playlist the playlist
154 * \param i_pos the postition of the item of which we change the group
155 * \param i_group the new group
156 * \return 0 on success, -1 on failure
158 int playlist_SetGroup( playlist_t *p_playlist, int i_pos, int i_group )
161 /* Check the existence of the playlist */
162 if( p_playlist == NULL)
166 /* Get a correct item */
167 if( i_pos >= 0 && i_pos < p_playlist->i_size )
170 else if( p_playlist->i_size > 0 )
172 i_pos = p_playlist->i_index;
179 psz_group = playlist_FindGroup( p_playlist , i_group );
180 if( psz_group != NULL)
182 p_playlist->pp_items[i_pos]->i_group = i_group ;
188 * Set the name of a playlist item
190 * \param p_playlist the playlist
191 * \param i_pos the position of the item of which we change the name
192 * \param psz_name the new name
193 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
195 int playlist_SetName( playlist_t *p_playlist, int i_pos, char *psz_name )
199 /* Check the existence of the playlist */
200 if( p_playlist == NULL)
204 /* Get a correct item */
205 if( i_pos >= 0 && i_pos < p_playlist->i_size )
208 else if( p_playlist->i_size > 0 )
210 i_pos = p_playlist->i_index;
217 if( p_playlist->pp_items[i_pos]->psz_name)
218 free( p_playlist->pp_items[i_pos]->psz_name );
221 p_playlist->pp_items[i_pos]->psz_name = strdup( psz_name );
224 var_Set( p_playlist, "item-change", val );
229 * Set the duration of a playlist item
231 * \param p_playlist the playlist
232 * \param i_pos the position of the item of which we change the duration
233 * \param i_duration the duration to set
234 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
236 int playlist_SetDuration( playlist_t *p_playlist, int i_pos, mtime_t i_duration )
238 char psz_buffer[MSTRTIME_MAX_SIZE];
241 /* Check the existence of the playlist */
242 if( p_playlist == NULL)
246 /* Get a correct item */
247 if( i_pos >= 0 && i_pos < p_playlist->i_size )
250 else if( p_playlist->i_size > 0 )
252 i_pos = p_playlist->i_index;
259 p_playlist->pp_items[i_pos]->i_duration = i_duration;
260 if( i_duration != -1 )
262 secstotimestr( psz_buffer, i_duration/1000000 );
266 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
268 playlist_AddInfo( p_playlist, i_pos, _("General") , _("Duration"),
272 var_Set( p_playlist, "item-change", val );
276 /**********************************************************************
277 * Actions on existing playlist items
278 **********************************************************************/
282 * delete an item from a playlist.
284 * \param p_playlist the playlist to remove from.
285 * \param i_pos the position of the item to remove
288 int playlist_Delete( playlist_t * p_playlist, int i_pos )
293 /* if i_pos is the current played item, playlist should stop playing it */
294 if( ( p_playlist->i_status == PLAYLIST_RUNNING) && (p_playlist->i_index == i_pos) )
296 playlist_Command( p_playlist, PLAYLIST_STOP, 0 );
299 vlc_mutex_lock( &p_playlist->object_lock );
300 if( i_pos >= 0 && i_pos < p_playlist->i_size )
302 playlist_item_t *p_item = p_playlist->pp_items[i_pos];
304 msg_Dbg( p_playlist, "deleting playlist item « %s »",
307 if( p_item->psz_name )
309 free( p_item->psz_name );
311 if( p_item->psz_uri )
313 free( p_item->psz_uri );
316 /* Free the info categories. Welcome to the segfault factory */
317 if( p_item->i_categories > 0 )
319 for( i = 0; i < p_item->i_categories; i++ )
321 for( j= 0 ; j < p_item->pp_categories[i]->i_infos; j++)
323 if( p_item->pp_categories[i]->pp_infos[j]->psz_name)
325 free( p_item->pp_categories[i]->
326 pp_infos[j]->psz_name);
328 if( p_item->pp_categories[i]->pp_infos[j]->psz_value)
330 free( p_item->pp_categories[i]->
331 pp_infos[j]->psz_value);
333 free( p_item->pp_categories[i]->pp_infos[j] );
335 if( p_item->pp_categories[i]->i_infos )
336 free( p_item->pp_categories[i]->pp_infos );
338 if( p_item->pp_categories[i]->psz_name)
340 free( p_item->pp_categories[i]->psz_name );
342 free( p_item->pp_categories[i] );
344 free( p_item->pp_categories );
347 /* XXX: what if the item is still in use? */
350 if( i_pos <= p_playlist->i_index )
352 p_playlist->i_index--;
355 /* Renumber the playlist */
356 REMOVE_ELEM( p_playlist->pp_items,
359 if( p_playlist->i_enabled > 0 )
360 p_playlist->i_enabled--;
363 vlc_mutex_unlock( &p_playlist->object_lock );
365 val.b_bool = VLC_TRUE;
366 var_Set( p_playlist, "intf-change", val );
374 * Clear all playlist items
376 * \param p_playlist the playlist to be cleared.
379 int playlist_Clear( playlist_t * p_playlist ) {
381 while( p_playlist->i_groups > 0 )
383 playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
386 while( p_playlist->i_size > 0 )
388 playlist_Delete( p_playlist, 0 );
395 * Disables a playlist item
397 * \param p_playlist the playlist to disable from.
398 * \param i_pos the position of the item to disable
401 int playlist_Disable( playlist_t * p_playlist, int i_pos )
404 vlc_mutex_lock( &p_playlist->object_lock );
407 if( i_pos >= 0 && i_pos < p_playlist->i_size )
409 msg_Dbg( p_playlist, "disabling playlist item « %s »",
410 p_playlist->pp_items[i_pos]->psz_name );
412 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
413 p_playlist->i_enabled--;
414 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
417 vlc_mutex_unlock( &p_playlist->object_lock );
420 var_Set( p_playlist, "item-change", val );
426 * Enables a playlist item
428 * \param p_playlist the playlist to enable from.
429 * \param i_pos the position of the item to enable
432 int playlist_Enable( playlist_t * p_playlist, int i_pos )
435 vlc_mutex_lock( &p_playlist->object_lock );
437 if( i_pos >= 0 && i_pos < p_playlist->i_size )
439 msg_Dbg( p_playlist, "enabling playlist item « %s »",
440 p_playlist->pp_items[i_pos]->psz_name );
442 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
443 p_playlist->i_enabled++;
445 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
448 vlc_mutex_unlock( &p_playlist->object_lock );
451 var_Set( p_playlist, "item-change", val );
457 * Disables a playlist group
459 * \param p_playlist the playlist to disable from.
460 * \param i_group the id of the group to disable
463 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
467 vlc_mutex_lock( &p_playlist->object_lock );
469 msg_Dbg(p_playlist,"Disabling group %i",i_group);
470 for( i = 0 ; i< p_playlist->i_size; i++ )
472 if( p_playlist->pp_items[i]->i_group == i_group )
474 msg_Dbg( p_playlist, "disabling playlist item « %s »",
475 p_playlist->pp_items[i]->psz_name );
477 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
478 p_playlist->i_enabled--;
480 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
482 var_Set( p_playlist, "item-change", val );
485 vlc_mutex_unlock( &p_playlist->object_lock );
491 * Enables a playlist group
493 * \param p_playlist the playlist to enable from.
494 * \param i_group the id of the group to enable
497 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
501 vlc_mutex_lock( &p_playlist->object_lock );
503 for( i = 0 ; i< p_playlist->i_size; i++ )
505 if( p_playlist->pp_items[i]->i_group == i_group )
507 msg_Dbg( p_playlist, "enabling playlist item « %s »",
508 p_playlist->pp_items[i]->psz_name );
510 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
511 p_playlist->i_enabled++;
513 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
515 var_Set( p_playlist, "item-change", val );
518 vlc_mutex_unlock( &p_playlist->object_lock );
524 * Move an item in a playlist
526 * Move the item in the playlist with position i_pos before the current item
527 * at position i_newpos.
528 * \param p_playlist the playlist to move items in
529 * \param i_pos the position of the item to move
530 * \param i_newpos the position of the item that will be behind the moved item
534 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
537 vlc_mutex_lock( &p_playlist->object_lock );
539 /* take into account that our own row disappears. */
540 if ( i_pos < i_newpos ) i_newpos--;
542 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
543 && i_newpos <= p_playlist->i_size )
545 playlist_item_t * temp;
547 msg_Dbg( p_playlist, "moving playlist item « %s » (%i -> %i)",
548 p_playlist->pp_items[i_pos]->psz_name, i_pos,
551 if( i_pos == p_playlist->i_index )
553 p_playlist->i_index = i_newpos;
555 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
557 p_playlist->i_index++;
559 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
561 p_playlist->i_index--;
564 if ( i_pos < i_newpos )
566 temp = p_playlist->pp_items[i_pos];
567 while ( i_pos < i_newpos )
569 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
572 p_playlist->pp_items[i_newpos] = temp;
574 else if ( i_pos > i_newpos )
576 temp = p_playlist->pp_items[i_pos];
577 while ( i_pos > i_newpos )
579 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
582 p_playlist->pp_items[i_newpos] = temp;
586 vlc_mutex_unlock( &p_playlist->object_lock );
588 val.b_bool = VLC_TRUE;
589 var_Set( p_playlist, "intf-change", val );