]> git.sesse.net Git - vlc/blob - src/playlist/item-ext.c
606223f3063b58aeaf1c3e0bb05fd37492792258
[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
430
431 /***********************************************************************
432  * Misc functions
433  ***********************************************************************/
434
435 /**
436  * Transform an item to a node
437  *
438  * \param p_playlist the playlist object
439  * \param p_item the item to transform
440  * \return nothing
441  */
442 void playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
443 {
444     int i = 0;
445     if( p_item->i_children == -1 )
446     {
447         p_item->i_children = 0;
448     }
449
450     vlc_mutex_lock( &p_playlist->object_lock );
451
452     /* Remove it from the array of available items */
453     for( i = 0 ; i < p_playlist->i_size ; i++ )
454     {
455         if( p_item == p_playlist->pp_items[i] )
456         {
457             REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
458         }
459     }
460     vlc_mutex_unlock( &p_playlist->object_lock );
461
462     /* Handle the parents
463      * Nothing to do ! */
464 }
465
466 /**
467  * delete an item from a playlist.
468  *
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
472  */
473 int playlist_Delete( playlist_t * p_playlist, int i_id )
474 {
475     vlc_value_t     val;
476     int             i;
477
478     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
479
480     if( p_item == NULL )
481     {
482         return VLC_EGENERIC;
483     }
484
485     /* Check if it is the current item */
486     if( p_playlist->status.p_item == p_item )
487     {
488         playlist_Control( p_playlist, PLAYLIST_STOP );
489         p_playlist->status.p_item = NULL;
490     }
491
492     vlc_mutex_lock( &p_playlist->object_lock );
493
494     msg_Dbg( p_playlist, "deleting playlist item `%s'",
495                           p_item->input.psz_name );
496
497     /* Remove the item from all its parent nodes */
498     for ( i= 0 ; i < p_item->i_parents ; i++ )
499     {
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 )
503         {
504             p_playlist->i_size--;
505         }
506     }
507
508     /* TODO : Update views */
509
510     playlist_ItemDelete( p_item );
511
512     vlc_mutex_unlock( &p_playlist->object_lock );
513
514     val.b_bool = VLC_TRUE;
515     var_Set( p_playlist, "intf-change", val );
516
517     return VLC_SUCCESS;
518 }
519
520 /**
521  * Clear all playlist items
522  *
523  * \param p_playlist the playlist to be cleared.
524  * \return returns 0
525  */
526 int playlist_Clear( playlist_t * p_playlist )
527 {
528     int i;
529     for( i = p_playlist->i_size; i > 0 ; i-- )
530     {
531         playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
532     }
533     return VLC_SUCCESS;
534 }
535
536
537 /**
538  * Disables a playlist item
539  *
540  * \param p_playlist the playlist to disable from.
541  * \param i_pos the position of the item to disable
542  * \return returns 0
543  */
544 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
545 {
546     if( !p_item ) return VLC_EGENERIC;
547
548     msg_Dbg( p_playlist, "disabling playlist item `%s'",
549                    p_item->input.psz_name );
550
551     if( p_item->i_flags & PLAYLIST_ENA_FLAG )
552     {
553         p_playlist->i_enabled--;
554     }
555     p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
556
557     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
558     return VLC_SUCCESS;
559 }
560
561 /**
562  * Enables a playlist item
563  *
564  * \param p_playlist the playlist to enable from.
565  * \param i_pos the position of the item to enable
566  * \return returns 0
567  */
568 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
569 {
570     if( !p_item ) return VLC_EGENERIC;
571
572     msg_Dbg( p_playlist, "enabling playlist item `%s'",
573                    p_item->input.psz_name );
574
575     if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
576     {
577         p_playlist->i_enabled++;
578     }
579     p_item->i_flags |= PLAYLIST_ENA_FLAG;
580
581     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
582     return VLC_SUCCESS;
583 }
584
585 /**
586  * Move an item in a playlist
587  *
588  * Move the item in the playlist with position i_pos before the current item
589  * at position i_newpos.
590  * \param p_playlist the playlist to move items in
591  * \param i_pos the position of the item to move
592  * \param i_newpos the position of the item that will be behind the moved item
593  *        after the move
594  * \return returns VLC_SUCCESS
595  */
596 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
597 {
598     vlc_value_t val;
599     vlc_mutex_lock( &p_playlist->object_lock );
600
601     /* take into account that our own row disappears. */
602     if( i_pos < i_newpos ) i_newpos--;
603
604     if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
605         i_newpos <= p_playlist->i_size )
606     {
607         playlist_item_t * temp;
608
609         msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
610                  p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
611
612         if( i_pos == p_playlist->i_index )
613         {
614             p_playlist->i_index = i_newpos;
615         }
616         else if( i_pos > p_playlist->i_index &&
617                  i_newpos <= p_playlist->i_index )
618         {
619             p_playlist->i_index++;
620         }
621         else if( i_pos < p_playlist->i_index &&
622                  i_newpos >= p_playlist->i_index )
623         {
624             p_playlist->i_index--;
625         }
626
627         if ( i_pos < i_newpos )
628         {
629             temp = p_playlist->pp_items[i_pos];
630             while ( i_pos < i_newpos )
631             {
632                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
633                 i_pos++;
634             }
635             p_playlist->pp_items[i_newpos] = temp;
636         }
637         else if ( i_pos > i_newpos )
638         {
639             temp = p_playlist->pp_items[i_pos];
640             while ( i_pos > i_newpos )
641             {
642                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
643                 i_pos--;
644             }
645             p_playlist->pp_items[i_newpos] = temp;
646         }
647     }
648
649     vlc_mutex_unlock( &p_playlist->object_lock );
650
651     val.b_bool = VLC_TRUE;
652     var_Set( p_playlist, "intf-change", val );
653
654     return VLC_SUCCESS;
655 }