1 /*****************************************************************************
2 * item-ext.c : Exported playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: item-ext.c,v 1.2 2004/01/05 14:42:14 zorglub 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 * \return the position of the new item
46 int playlist_Add( playlist_t *p_playlist, const char * psz_uri,
47 const char *psz_name, int i_mode, int i_pos )
49 playlist_item_t * p_item;
51 p_item = malloc( sizeof( playlist_item_t ) );
54 msg_Err( p_playlist, "out of memory" );
58 msg_Err( p_playlist, "Not adding NULL item");
61 p_item->psz_uri = strdup( psz_uri );
62 if( psz_name != NULL )
64 p_item->psz_name = strdup( psz_name );
68 p_item->psz_name = strdup ( psz_uri );
71 p_item->b_autodeletion = VLC_FALSE;
72 p_item->b_enabled = VLC_TRUE;
73 p_item->i_group = PLAYLIST_TYPE_MANUAL;
74 p_item->i_duration = -1;
76 p_item->pp_categories = NULL;
77 p_item->i_categories = 0;
79 playlist_CreateItemCategory( p_item, _("General") );
80 playlist_CreateItemCategory( p_item, _("Options") );
81 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
85 * Search the position of an item by its id
86 * \param p_playlist the playlist
87 * \param i_id the id to find
88 * \return the position, or -1 on failure
90 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
93 for( i = 0 ; i < p_playlist->i_size ; i++ )
95 if( p_playlist->pp_items[i]->i_id == i_id )
105 * Search an item by its id
106 * \param p_playlist the playlist
107 * \param i_id the id to find
108 * \return the item, or NULL on failure
110 playlist_item_t * playlist_GetItemById( playlist_t * p_playlist , int i_id )
113 for( i = 0 ; i < p_playlist->i_size ; i++ )
115 if( p_playlist->pp_items[i]->i_id == i_id )
117 return p_playlist->pp_items[i];
123 /**********************************************************************
124 * playlist_item_t structure accessors
125 * These functions give access to the fields of the playlist_item_t
127 **********************************************************************/
130 * Set the group of a playlist item
132 * \param p_playlist the playlist
133 * \param i_item the item of which we change the group
134 * \param i_group the new group
135 * \return 0 on success, -1 on failure
137 int playlist_SetGroup( playlist_t *p_playlist, int i_item, int i_group )
140 /* Check the existence of the playlist */
141 if( p_playlist == NULL)
145 /* Get a correct item */
146 if( i_item >= 0 && i_item < p_playlist->i_size )
149 else if( p_playlist->i_size > 0 )
151 i_item = p_playlist->i_index;
158 psz_group = playlist_FindGroup( p_playlist , i_group );
159 if( psz_group != NULL)
161 p_playlist->pp_items[i_item]->i_group = i_group ;
167 * Set the name of a playlist item
169 * \param p_playlist the playlist
170 * \param i_item the item of which we change the name
171 * \param psz_name the new name
172 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
174 int playlist_SetName( playlist_t *p_playlist, int i_item, char *psz_name )
178 /* Check the existence of the playlist */
179 if( p_playlist == NULL)
183 /* Get a correct item */
184 if( i_item >= 0 && i_item < p_playlist->i_size )
187 else if( p_playlist->i_size > 0 )
189 i_item = p_playlist->i_index;
196 if( p_playlist->pp_items[i_item]->psz_name)
197 free( p_playlist->pp_items[i_item]->psz_name );
200 p_playlist->pp_items[i_item]->psz_name = strdup( psz_name );
203 var_Set( p_playlist, "item-change", val );
208 * Set the duration of a playlist item
210 * \param p_playlist the playlist
211 * \param i_item the item of which we change the name
212 * \param i_duration the duration to set
213 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
215 int playlist_SetDuration( playlist_t *p_playlist, int i_item, int i_duration )
217 char psz_buffer[MSTRTIME_MAX_SIZE];
220 /* Check the existence of the playlist */
221 if( p_playlist == NULL)
225 /* Get a correct item */
226 if( i_item >= 0 && i_item < p_playlist->i_size )
229 else if( p_playlist->i_size > 0 )
231 i_item = p_playlist->i_index;
238 p_playlist->pp_items[i_item]->i_duration = i_duration;
239 if( i_duration != -1 )
241 secstotimestr( psz_buffer, i_duration/1000000 );
245 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
247 playlist_AddInfo( p_playlist, i_item, _("General") , _("Duration"),
251 var_Set( p_playlist, "item-change", val );
255 /**********************************************************************
256 * Actions on existing playlist items
257 **********************************************************************/
261 * delete an item from a playlist.
263 * \param p_playlist the playlist to remove from.
264 * \param i_pos the position of the item to remove
267 int playlist_Delete( playlist_t * p_playlist, int i_pos )
272 /* if i_pos is the current played item, playlist should stop playing it */
273 if( ( p_playlist->i_status == PLAYLIST_RUNNING) && (p_playlist->i_index == i_pos) )
275 playlist_Command( p_playlist, PLAYLIST_STOP, 0 );
278 vlc_mutex_lock( &p_playlist->object_lock );
279 if( i_pos >= 0 && i_pos < p_playlist->i_size )
281 playlist_item_t *p_item = p_playlist->pp_items[i_pos];
283 msg_Dbg( p_playlist, "deleting playlist item « %s »",
286 if( p_item->psz_name )
288 free( p_item->psz_name );
290 if( p_item->psz_uri )
292 free( p_item->psz_uri );
295 /* Free the info categories. Welcome to the segfault factory */
296 if( p_item->i_categories > 0 )
298 for( i = 0; i < p_item->i_categories; i++ )
300 for( j= 0 ; j < p_item->pp_categories[i]->i_infos; j++)
302 if( p_item->pp_categories[i]->pp_infos[j]->psz_name)
304 free( p_item->pp_categories[i]->
305 pp_infos[j]->psz_name);
307 if( p_item->pp_categories[i]->pp_infos[j]->psz_value)
309 free( p_item->pp_categories[i]->
310 pp_infos[j]->psz_value);
312 free( p_item->pp_categories[i]->pp_infos[j] );
314 if( p_item->pp_categories[i]->psz_name)
316 free( p_item->pp_categories[i]->psz_name );
318 free( p_item->pp_categories[i] );
322 /* XXX: what if the item is still in use? */
325 if( i_pos <= p_playlist->i_index )
327 p_playlist->i_index--;
330 /* Renumber the playlist */
331 REMOVE_ELEM( p_playlist->pp_items,
334 if( p_playlist->i_enabled > 0 )
335 p_playlist->i_enabled--;
338 vlc_mutex_unlock( &p_playlist->object_lock );
340 val.b_bool = VLC_TRUE;
341 var_Set( p_playlist, "intf-change", val );
347 * Disables a playlist item
349 * \param p_playlist the playlist to disable from.
350 * \param i_pos the position of the item to disable
353 int playlist_Disable( playlist_t * p_playlist, int i_pos )
356 vlc_mutex_lock( &p_playlist->object_lock );
359 if( i_pos >= 0 && i_pos < p_playlist->i_size )
361 msg_Dbg( p_playlist, "disabling playlist item « %s »",
362 p_playlist->pp_items[i_pos]->psz_name );
364 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
365 p_playlist->i_enabled--;
366 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
369 vlc_mutex_unlock( &p_playlist->object_lock );
372 var_Set( p_playlist, "item-change", val );
378 * Enables a playlist item
380 * \param p_playlist the playlist to enable from.
381 * \param i_pos the position of the item to enable
384 int playlist_Enable( playlist_t * p_playlist, int i_pos )
387 vlc_mutex_lock( &p_playlist->object_lock );
389 if( i_pos >= 0 && i_pos < p_playlist->i_size )
391 msg_Dbg( p_playlist, "enabling playlist item « %s »",
392 p_playlist->pp_items[i_pos]->psz_name );
394 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
395 p_playlist->i_enabled++;
397 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
400 vlc_mutex_unlock( &p_playlist->object_lock );
403 var_Set( p_playlist, "item-change", val );
409 * Disables a playlist group
411 * \param p_playlist the playlist to disable from.
412 * \param i_pos the id of the group to disable
415 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
419 vlc_mutex_lock( &p_playlist->object_lock );
421 msg_Dbg(p_playlist,"Disabling group %i",i_group);
422 for( i = 0 ; i< p_playlist->i_size; i++ )
424 if( p_playlist->pp_items[i]->i_group == i_group )
426 msg_Dbg( p_playlist, "disabling playlist item « %s »",
427 p_playlist->pp_items[i]->psz_name );
429 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
430 p_playlist->i_enabled--;
432 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
434 var_Set( p_playlist, "item-change", val );
437 vlc_mutex_unlock( &p_playlist->object_lock );
443 * Enables a playlist group
445 * \param p_playlist the playlist to enable from.
446 * \param i_pos the id of the group to enable
449 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
453 vlc_mutex_lock( &p_playlist->object_lock );
455 for( i = 0 ; i< p_playlist->i_size; i++ )
457 if( p_playlist->pp_items[i]->i_group == i_group )
459 msg_Dbg( p_playlist, "enabling playlist item « %s »",
460 p_playlist->pp_items[i]->psz_name );
462 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
463 p_playlist->i_enabled++;
465 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
467 var_Set( p_playlist, "item-change", val );
470 vlc_mutex_unlock( &p_playlist->object_lock );
476 * Move an item in a playlist
478 * Move the item in the playlist with position i_pos before the current item
479 * at position i_newpos.
480 * \param p_playlist the playlist to move items in
481 * \param i_pos the position of the item to move
482 * \param i_newpos the position of the item that will be behind the moved item
486 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
489 vlc_mutex_lock( &p_playlist->object_lock );
491 /* take into account that our own row disappears. */
492 if ( i_pos < i_newpos ) i_newpos--;
494 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
495 && i_newpos <= p_playlist->i_size )
497 playlist_item_t * temp;
499 msg_Dbg( p_playlist, "moving playlist item « %s » (%i -> %i)",
500 p_playlist->pp_items[i_pos]->psz_name, i_pos,
503 if( i_pos == p_playlist->i_index )
505 p_playlist->i_index = i_newpos;
507 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
509 p_playlist->i_index++;
511 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
513 p_playlist->i_index--;
516 if ( i_pos < i_newpos )
518 temp = p_playlist->pp_items[i_pos];
519 while ( i_pos < i_newpos )
521 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
524 p_playlist->pp_items[i_newpos] = temp;
526 else if ( i_pos > i_newpos )
528 temp = p_playlist->pp_items[i_pos];
529 while ( i_pos > i_newpos )
531 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
534 p_playlist->pp_items[i_newpos] = temp;
538 vlc_mutex_unlock( &p_playlist->object_lock );
540 val.b_bool = VLC_TRUE;
541 var_Set( p_playlist, "intf-change", val );