]> git.sesse.net Git - vlc/blob - src/playlist/item-ext.c
Add directory in wxWidgets
[vlc] / src / playlist / item-ext.c
1 /*****************************************************************************
2  * item-ext.c : Playlist item management functions (act on the playlist)
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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() */
27
28 #include <vlc/vlc.h>
29 #include <vlc/input.h>
30
31 #include "vlc_playlist.h"
32
33 /***************************************************************************
34  * Item creation/addition functions
35  ***************************************************************************/
36
37 /**
38  * Add a MRL into the playlist, duration and options given
39  *
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
51 */
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,
55                      int i_options )
56 {
57     playlist_item_t *p_item;
58     p_item = playlist_ItemNew( p_playlist , psz_uri, psz_name );
59
60     if( p_item == NULL )
61     {
62         msg_Err( p_playlist, "unable to add item to playlist" );
63         return -1;
64     }
65
66     p_item->input.i_duration = i_duration;
67     p_item->input.i_options = i_options;
68     p_item->input.ppsz_options = NULL;
69
70     for( p_item->input.i_options = 0; p_item->input.i_options < i_options;
71          p_item->input.i_options++ )
72     {
73         if( !p_item->input.i_options )
74         {
75             p_item->input.ppsz_options = malloc( i_options * sizeof(char *) );
76             if( !p_item->input.ppsz_options ) break;
77         }
78
79         p_item->input.ppsz_options[p_item->input.i_options] =
80             strdup( ppsz_options[p_item->input.i_options] );
81     }
82
83     return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
84 }
85
86 /**
87  * Add a MRL into the playlist.
88  *
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
97 */
98 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
99                   const char *psz_name, int i_mode, int i_pos )
100 {
101     return playlist_AddExt( p_playlist, psz_uri, psz_name, i_mode, i_pos,
102                             -1, NULL, 0 );
103 }
104
105 /**
106  * Add a playlist item into a playlist
107  *
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
115  */
116 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
117                       int i_mode, int i_pos)
118 {
119     vlc_value_t val;
120     vlc_bool_t b_end = VLC_FALSE;
121     playlist_view_t *p_view = NULL;
122
123     vlc_mutex_lock( &p_playlist->object_lock );
124
125     /*
126      * CHECK_INSERT : checks if the item is already enqued before
127      * enqueing it
128      */
129
130     /* That should not change */
131     if ( i_mode & PLAYLIST_CHECK_INSERT )
132     {
133          int j;
134
135         if ( p_playlist->pp_items )
136         {
137             for ( j = 0; j < p_playlist->i_size; j++ )
138             {
139                 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
140                                p_item->input.psz_uri ) )
141                 {
142                     playlist_ItemDelete( p_item );
143                     vlc_mutex_unlock( &p_playlist->object_lock );
144                     return -1;
145                 }
146              }
147          }
148          i_mode &= ~PLAYLIST_CHECK_INSERT;
149          i_mode |= PLAYLIST_APPEND;
150     }
151
152     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
153              p_item->input.psz_name, p_item->input.psz_uri );
154
155     p_item->input.i_id = ++p_playlist->i_last_id;
156
157     /* Do a few boundary checks and allocate space for the item */
158     if( i_pos == PLAYLIST_END )
159     {
160         b_end = VLC_TRUE;
161         if( i_mode & PLAYLIST_INSERT )
162         {
163             i_mode &= ~PLAYLIST_INSERT;
164             i_mode |= PLAYLIST_APPEND;
165         }
166
167         i_pos = p_playlist->i_size - 1;
168     }
169
170     if( !(i_mode & PLAYLIST_REPLACE)
171          || i_pos < 0 || i_pos >= p_playlist->i_size )
172     {
173         /* Additional boundary checks */
174         if( i_mode & PLAYLIST_APPEND )
175         {
176             i_pos++;
177         }
178
179         if( i_pos < 0 )
180         {
181             i_pos = 0;
182         }
183         else if( i_pos > p_playlist->i_size )
184         {
185             i_pos = p_playlist->i_size;
186         }
187
188         INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos, p_item );
189         p_playlist->i_enabled ++;
190
191         /* We update the ALL view directly */
192         playlist_ViewUpdate( p_playlist, VIEW_ALL );
193
194         /* Add the item to the General category */
195         if( b_end == VLC_TRUE )
196         {
197             playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
198                                  p_playlist->p_general );
199         }
200         else
201         {
202             playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
203                                  p_playlist->p_general, i_pos );
204         }
205         p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
206         playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
207
208         /* Also add the item to the "simple" view */
209         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
210
211         if( b_end == VLC_TRUE )
212         {
213             playlist_NodeAppend( p_playlist, VIEW_SIMPLE,p_item,
214                                   p_view->p_root );
215         }
216         else
217         {
218             playlist_NodeInsert( p_playlist, VIEW_SIMPLE,p_item,
219                                   p_view->p_root, i_pos );
220         }
221
222         /* FIXME : Update sorted views */
223
224         if( p_playlist->i_index >= i_pos )
225         {
226             p_playlist->i_index++;
227         }
228     }
229     else
230     {
231         msg_Err( p_playlist, "Insert mode not implemented" );
232     }
233
234     if( (i_mode & PLAYLIST_GO ) && p_view )
235     {
236         p_playlist->request.b_request = VLC_TRUE;
237         /* FIXME ... */
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;
241
242         if( p_playlist->p_input )
243         {
244             input_StopThread( p_playlist->p_input );
245         }
246         p_playlist->status.i_status = PLAYLIST_RUNNING;
247     }
248
249     vlc_mutex_unlock( &p_playlist->object_lock );
250
251     val.b_bool = VLC_TRUE;
252     var_Set( p_playlist, "intf-change", val );
253
254     return p_item->input.i_id;
255 }
256
257
258 /**
259  * Add a playlist item to a given node (in the category view )
260  *
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
269  */
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)
273 {
274     vlc_value_t val;
275     int i_position;
276     playlist_view_t *p_view;
277
278     vlc_mutex_lock( &p_playlist->object_lock );
279
280     /* Sanity checks */
281     if( !p_parent || p_parent->i_children == -1 )
282     {
283         msg_Err( p_playlist, "invalid node" );
284     }
285
286     /*
287      * CHECK_INSERT : checks if the item is already enqued before
288      * enqueing it
289      */
290     if ( i_mode & PLAYLIST_CHECK_INSERT )
291     {
292          int j;
293
294         if ( p_playlist->pp_items )
295         {
296             for ( j = 0; j < p_playlist->i_size; j++ )
297             {
298                 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
299                               p_item->input.psz_uri ) )
300                 {
301                     playlist_ItemDelete( p_item );
302                     vlc_mutex_unlock( &p_playlist->object_lock );
303                     return -1;
304                 }
305             }
306         }
307         i_mode &= ~PLAYLIST_CHECK_INSERT;
308         i_mode |= PLAYLIST_APPEND;
309     }
310
311     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
312              p_item->input.psz_name, p_item->input.psz_uri );
313
314     p_item->input.i_id = ++p_playlist->i_last_id;
315
316     /* First, add the item at the right position in the item bank */
317     /* WHY THAT ? */
318      //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
319     i_position = p_playlist->i_size ;
320
321     INSERT_ELEM( p_playlist->pp_items,
322                  p_playlist->i_size,
323                  i_position,
324                  p_item );
325     p_playlist->i_enabled ++;
326
327     /* TODO: Handle modes */
328     playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
329
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 );
334
335     /* TODO : Update sorted views*/
336
337     if( i_mode & PLAYLIST_GO )
338     {
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 )
344         {
345             input_StopThread( p_playlist->p_input );
346         }
347         p_playlist->status.i_status = PLAYLIST_RUNNING;
348     }
349
350     vlc_mutex_unlock( &p_playlist->object_lock );
351
352     val.b_bool = VLC_TRUE;
353     var_Set( p_playlist, "intf-change", val );
354
355     return p_item->input.i_id;
356 }
357
358 /***************************************************************************
359  * Item search functions
360  ***************************************************************************/
361
362 /**
363  * Search the position of an item by its id
364  * This function must be entered with the playlist lock
365  *
366  * \param p_playlist the playlist
367  * \param i_id the id to find
368  * \return the position, or VLC_EGENERIC on failure
369  */
370 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
371 {
372     int i;
373     for( i =  0 ; i < p_playlist->i_size ; i++ )
374     {
375         if( p_playlist->pp_items[i]->input.i_id == i_id )
376         {
377             return i;
378         }
379     }
380     return VLC_EGENERIC;
381 }
382
383
384 /**
385  * Search an item by its position
386  * This function must be entered with the playlist lock
387  *
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
391  */
392 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
393 {
394     if( i_pos >= 0 && i_pos < p_playlist->i_size)
395     {
396         return p_playlist->pp_items[i_pos];
397     }
398     else if( p_playlist->i_size > 0)
399     {
400         return p_playlist->pp_items[p_playlist->i_index];
401     }
402     else
403     {
404         return NULL;
405     }
406 }
407
408 /**
409  * Search an item by its id
410  *
411  * \param p_playlist the playlist
412  * \param i_id the id to find
413  * \return the item, or NULL on failure
414  */
415 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
416 {
417     int i;
418     for( i =  0 ; i < p_playlist->i_size ; i++ )
419     {
420         if( p_playlist->pp_items[i]->input.i_id == i_id )
421         {
422             return p_playlist->pp_items[i];
423         }
424     }
425     return NULL;
426 }
427
428 /**
429  * Search an item by its input_item_t
430  *
431  * \param p_playlist the playlist
432  * \param p_item the input_item_t to find
433  * \return the item, or NULL on failure
434  */
435 playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
436                                         input_item_t *p_item )
437 {
438     int i;
439     for( i =  0 ; i < p_playlist->i_size ; i++ )
440     {
441         if( &p_playlist->pp_items[i]->input == p_item )
442         {
443             return p_playlist->pp_items[i];
444         }
445     }
446     return NULL;
447 }
448
449
450
451 /***********************************************************************
452  * Misc functions
453  ***********************************************************************/
454
455 /**
456  * Transform an item to a node
457  *
458  * \param p_playlist the playlist object
459  * \param p_item the item to transform
460  * \return nothing
461  */
462 void playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
463 {
464     int i = 0;
465     if( p_item->i_children == -1 )
466     {
467         p_item->i_children = 0;
468     }
469
470     vlc_mutex_lock( &p_playlist->object_lock );
471
472     /* Remove it from the array of available items */
473     for( i = 0 ; i < p_playlist->i_size ; i++ )
474     {
475         if( p_item == p_playlist->pp_items[i] )
476         {
477             REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
478         }
479     }
480     vlc_mutex_unlock( &p_playlist->object_lock );
481
482     /* Handle the parents
483      * Nothing to do ! */
484 }
485
486 /**
487  * delete an item from a playlist.
488  *
489  * \param p_playlist the playlist to remove from.
490  * \param i_id the identifier of the item to delete
491  * \return returns VLC_SUCCESS or an error
492  */
493 int playlist_Delete( playlist_t * p_playlist, int i_id )
494 {
495     vlc_value_t     val;
496     int             i;
497
498     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
499
500     if( p_item == NULL )
501     {
502         return VLC_EGENERIC;
503     }
504
505     /* Check if it is the current item */
506     if( p_playlist->status.p_item == p_item )
507     {
508         playlist_Control( p_playlist, PLAYLIST_STOP );
509         p_playlist->status.p_item = NULL;
510     }
511
512     vlc_mutex_lock( &p_playlist->object_lock );
513
514     msg_Dbg( p_playlist, "deleting playlist item `%s'",
515                           p_item->input.psz_name );
516
517     /* Remove the item from all its parent nodes */
518     for ( i= 0 ; i < p_item->i_parents ; i++ )
519     {
520         playlist_NodeRemoveItem( p_playlist, p_item,
521                                  p_item->pp_parents[i]->p_parent );
522         if( p_item->pp_parents[i]->i_view == VIEW_ALL )
523         {
524             p_playlist->i_size--;
525         }
526     }
527
528     /* TODO : Update views */
529
530     playlist_ItemDelete( p_item );
531
532     vlc_mutex_unlock( &p_playlist->object_lock );
533
534     val.b_bool = VLC_TRUE;
535     var_Set( p_playlist, "intf-change", val );
536
537     return VLC_SUCCESS;
538 }
539
540 /**
541  * Clear all playlist items
542  *
543  * \param p_playlist the playlist to be cleared.
544  * \return returns 0
545  */
546 int playlist_Clear( playlist_t * p_playlist )
547 {
548     int i;
549     for( i = p_playlist->i_size; i > 0 ; i-- )
550     {
551         playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
552     }
553     return VLC_SUCCESS;
554 }
555
556
557 /**
558  * Disables a playlist item
559  *
560  * \param p_playlist the playlist to disable from.
561  * \param i_pos the position of the item to disable
562  * \return returns 0
563  */
564 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
565 {
566     if( !p_item ) return VLC_EGENERIC;
567
568     msg_Dbg( p_playlist, "disabling playlist item `%s'",
569                    p_item->input.psz_name );
570
571     if( p_item->i_flags & PLAYLIST_ENA_FLAG )
572     {
573         p_playlist->i_enabled--;
574     }
575     p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
576
577     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
578     return VLC_SUCCESS;
579 }
580
581 /**
582  * Enables a playlist item
583  *
584  * \param p_playlist the playlist to enable from.
585  * \param i_pos the position of the item to enable
586  * \return returns 0
587  */
588 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
589 {
590     if( !p_item ) return VLC_EGENERIC;
591
592     msg_Dbg( p_playlist, "enabling playlist item `%s'",
593                    p_item->input.psz_name );
594
595     if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
596     {
597         p_playlist->i_enabled++;
598     }
599     p_item->i_flags |= PLAYLIST_ENA_FLAG;
600
601     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
602     return VLC_SUCCESS;
603 }
604
605 /**
606  * Move an item in a playlist
607  *
608  * Move the item in the playlist with position i_pos before the current item
609  * at position i_newpos.
610  * \param p_playlist the playlist to move items in
611  * \param i_pos the position of the item to move
612  * \param i_newpos the position of the item that will be behind the moved item
613  *        after the move
614  * \return returns VLC_SUCCESS
615  */
616 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
617 {
618     vlc_value_t val;
619     vlc_mutex_lock( &p_playlist->object_lock );
620
621     /* take into account that our own row disappears. */
622     if( i_pos < i_newpos ) i_newpos--;
623
624     if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
625         i_newpos <= p_playlist->i_size )
626     {
627         playlist_item_t * temp;
628
629         msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
630                  p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
631
632         if( i_pos == p_playlist->i_index )
633         {
634             p_playlist->i_index = i_newpos;
635         }
636         else if( i_pos > p_playlist->i_index &&
637                  i_newpos <= p_playlist->i_index )
638         {
639             p_playlist->i_index++;
640         }
641         else if( i_pos < p_playlist->i_index &&
642                  i_newpos >= p_playlist->i_index )
643         {
644             p_playlist->i_index--;
645         }
646
647         if ( i_pos < i_newpos )
648         {
649             temp = p_playlist->pp_items[i_pos];
650             while ( i_pos < i_newpos )
651             {
652                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
653                 i_pos++;
654             }
655             p_playlist->pp_items[i_newpos] = temp;
656         }
657         else if ( i_pos > i_newpos )
658         {
659             temp = p_playlist->pp_items[i_pos];
660             while ( i_pos > i_newpos )
661             {
662                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
663                 i_pos--;
664             }
665             p_playlist->pp_items[i_newpos] = temp;
666         }
667     }
668
669     vlc_mutex_unlock( &p_playlist->object_lock );
670
671     val.b_bool = VLC_TRUE;
672     var_Set( p_playlist, "intf-change", val );
673
674     return VLC_SUCCESS;
675 }