]> git.sesse.net Git - vlc/blob - src/playlist/item-ext.c
6b6830a7e8e64ee6ae6f26e5f845069728bca81d
[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 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
124
125     vlc_mutex_lock( &p_playlist->object_lock );
126
127     /*
128      * CHECK_INSERT : checks if the item is already enqued before
129      * enqueing it
130      */
131
132     /* That should not change */
133     if ( i_mode & PLAYLIST_CHECK_INSERT )
134     {
135          int j;
136
137         if ( p_playlist->pp_items )
138         {
139             for ( j = 0; j < p_playlist->i_size; j++ )
140             {
141                 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
142                                p_item->input.psz_uri ) )
143                 {
144                     playlist_ItemDelete( p_item );
145                     vlc_mutex_unlock( &p_playlist->object_lock );
146                     return -1;
147                 }
148              }
149          }
150          i_mode &= ~PLAYLIST_CHECK_INSERT;
151          i_mode |= PLAYLIST_APPEND;
152     }
153
154     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
155              p_item->input.psz_name, p_item->input.psz_uri );
156
157     p_item->input.i_id = ++p_playlist->i_last_id;
158
159     /* Do a few boundary checks and allocate space for the item */
160     if( i_pos == PLAYLIST_END )
161     {
162         b_end = VLC_TRUE;
163         if( i_mode & PLAYLIST_INSERT )
164         {
165             i_mode &= ~PLAYLIST_INSERT;
166             i_mode |= PLAYLIST_APPEND;
167         }
168
169         i_pos = p_playlist->i_size - 1;
170     }
171
172     if( !(i_mode & PLAYLIST_REPLACE)
173          || i_pos < 0 || i_pos >= p_playlist->i_size )
174     {
175         /* Additional boundary checks */
176         if( i_mode & PLAYLIST_APPEND )
177         {
178             i_pos++;
179         }
180
181         if( i_pos < 0 )
182         {
183             i_pos = 0;
184         }
185         else if( i_pos > p_playlist->i_size )
186         {
187             i_pos = p_playlist->i_size;
188         }
189
190         INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos, p_item );
191         INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
192                      p_playlist->i_all_size, p_item );
193         p_playlist->i_enabled ++;
194
195         /* We update the ALL view directly */
196         playlist_ViewUpdate( p_playlist, VIEW_ALL );
197
198         /* Add the item to the General category */
199         if( b_end == VLC_TRUE )
200         {
201             playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
202                                  p_playlist->p_general );
203             p_add->i_item = p_item->input.i_id;
204             p_add->i_node = p_playlist->p_general->input.i_id;
205             p_add->i_view = VIEW_CATEGORY;
206             val.p_address = p_add;
207             var_Set( p_playlist, "item-append", val );
208         }
209         else
210         {
211             playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
212                                  p_playlist->p_general, i_pos );
213         }
214
215
216         p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
217         playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
218
219         /* FIXME : Update sorted views */
220
221         if( p_playlist->i_index >= i_pos )
222         {
223             p_playlist->i_index++;
224         }
225     }
226     else
227     {
228         msg_Err( p_playlist, "Insert mode not implemented" );
229     }
230
231     if( (i_mode & PLAYLIST_GO ) && p_view )
232     {
233         p_playlist->request.b_request = VLC_TRUE;
234         /* FIXME ... */
235         p_playlist->request.i_view = VIEW_CATEGORY;
236         p_playlist->request.p_node = p_view->p_root;
237         p_playlist->request.p_item = p_item;
238
239         if( p_playlist->p_input )
240         {
241             input_StopThread( p_playlist->p_input );
242         }
243         p_playlist->status.i_status = PLAYLIST_RUNNING;
244     }
245
246     if( i_mode & PLAYLIST_PREPARSE &&
247         var_CreateGetBool( p_playlist, "auto-preparse" ) )
248     {
249         playlist_PreparseEnqueue( p_playlist, &p_item->input );
250     }
251
252     vlc_mutex_unlock( &p_playlist->object_lock );
253
254     if( b_end == VLC_FALSE )
255     {
256         val.b_bool = VLC_TRUE;
257         var_Set( p_playlist, "intf-change", val );
258     }
259
260     free( p_add );
261
262     return p_item->input.i_id;
263 }
264
265
266 /**
267  * Add a playlist item to a given node (in the category view )
268  *
269  * \param p_playlist the playlist to insert into
270  * \param p_item the playlist item to insert
271  * \param i_view the view for which to add or TODO: ALL_VIEWS
272  * \param p_parent the parent node
273  * \param i_mode the mode used when adding
274  * \param i_pos the possition in the node where to add. If this is
275  *        PLAYLIST_END the item will be added at the end of the node
276  ** \return The id of the playlist item
277  */
278 int playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
279                           int i_view,playlist_item_t *p_parent,
280                           int i_mode, int i_pos)
281 {
282     vlc_value_t val;
283     int i_position;
284     playlist_view_t *p_view;
285
286     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
287
288     vlc_mutex_lock( &p_playlist->object_lock );
289
290     if ( i_pos == PLAYLIST_END ) i_pos = -1;
291
292     /* Sanity checks */
293     if( !p_parent || p_parent->i_children == -1 )
294     {
295         msg_Err( p_playlist, "invalid node" );
296     }
297
298     /*
299      * CHECK_INSERT : checks if the item is already enqued before
300      * enqueing it
301      */
302     if ( i_mode & PLAYLIST_CHECK_INSERT )
303     {
304          int j;
305
306         if ( p_playlist->pp_items )
307         {
308             for ( j = 0; j < p_playlist->i_size; j++ )
309             {
310                 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
311                               p_item->input.psz_uri ) )
312                 {
313                     playlist_ItemDelete( p_item );
314                     vlc_mutex_unlock( &p_playlist->object_lock );
315                     free( p_add );
316                     return -1;
317                 }
318             }
319         }
320         i_mode &= ~PLAYLIST_CHECK_INSERT;
321         i_mode |= PLAYLIST_APPEND;
322     }
323
324     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
325              p_item->input.psz_name, p_item->input.psz_uri );
326
327     p_item->input.i_id = ++p_playlist->i_last_id;
328
329     /* First, add the item at the right position in the item bank */
330     /* WHY THAT ? */
331      //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
332     i_position = p_playlist->i_size ;
333
334     INSERT_ELEM( p_playlist->pp_items,
335                  p_playlist->i_size,
336                  i_position,
337                  p_item );
338     INSERT_ELEM( p_playlist->pp_all_items,
339                  p_playlist->i_all_size,
340                  p_playlist->i_all_size,
341                  p_item );
342     p_playlist->i_enabled ++;
343
344     /* TODO: Handle modes */
345     playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, i_pos );
346
347     p_add->i_item = p_item->input.i_id;
348     p_add->i_node = p_parent->input.i_id;
349     p_add->i_view = i_view;
350     val.p_address = p_add;
351     var_Set( p_playlist, "item-append", val );
352
353     /* We update the ALL view directly */
354     p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
355     playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
356     playlist_ViewUpdate( p_playlist, VIEW_ALL );
357
358     /* TODO : Update sorted views*/
359
360     if( i_mode & PLAYLIST_GO )
361     {
362         p_playlist->request.b_request = VLC_TRUE;
363         p_playlist->request.i_view = VIEW_CATEGORY;
364         p_playlist->request.p_node = p_parent;
365         p_playlist->request.p_item = p_item;
366         if( p_playlist->p_input )
367         {
368             input_StopThread( p_playlist->p_input );
369         }
370         p_playlist->status.i_status = PLAYLIST_RUNNING;
371     }
372     if( i_mode & PLAYLIST_PREPARSE &&
373         var_CreateGetBool( p_playlist, "auto-preparse" ) )
374     {
375         playlist_PreparseEnqueue( p_playlist, &p_item->input );
376     }
377
378     vlc_mutex_unlock( &p_playlist->object_lock );
379
380     val.b_bool = VLC_TRUE;
381 //    var_Set( p_playlist, "intf-change", val );
382 //
383     free( p_add );
384
385     return p_item->input.i_id;
386 }
387
388 /***************************************************************************
389  * Item search functions
390  ***************************************************************************/
391
392 /**
393  * Search the position of an item by its id
394  * This function must be entered with the playlist lock
395  *
396  * \param p_playlist the playlist
397  * \param i_id the id to find
398  * \return the position, or VLC_EGENERIC on failure
399  */
400 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
401 {
402     int i;
403     for( i =  0 ; i < p_playlist->i_size ; i++ )
404     {
405         if( p_playlist->pp_items[i]->input.i_id == i_id )
406         {
407             return i;
408         }
409     }
410     return VLC_EGENERIC;
411 }
412
413
414 /**
415  * Search an item by its position
416  * This function must be entered with the playlist lock
417  *
418  * \param p_playlist the playlist
419  * \param i_pos the position of the item to find
420  * \return the item, or NULL on failure
421  */
422 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
423 {
424     if( i_pos >= 0 && i_pos < p_playlist->i_size)
425     {
426         return p_playlist->pp_items[i_pos];
427     }
428     else if( p_playlist->i_size > 0)
429     {
430         return p_playlist->pp_items[p_playlist->i_index];
431     }
432     else
433     {
434         return NULL;
435     }
436 }
437
438 playlist_item_t *playlist_LockItemGetByPos( playlist_t *p_playlist, int i_pos )
439 {
440     playlist_item_t *p_ret;
441     vlc_mutex_lock( &p_playlist->object_lock );
442     p_ret = playlist_ItemGetByPos( p_playlist, i_pos );
443     vlc_mutex_unlock( &p_playlist->object_lock );
444     return p_ret;
445 }
446
447 /**
448  * Search an item by its id
449  *
450  * \param p_playlist the playlist
451  * \param i_id the id to find
452  * \return the item, or NULL on failure
453  */
454 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
455 {
456     int i, i_top, i_bottom;
457     i_bottom = 0; i_top = p_playlist->i_all_size - 1;
458     i = i_top / 2;
459     while( p_playlist->pp_all_items[i]->input.i_id != i_id &&
460            i_top > i_bottom )
461     {
462         if( p_playlist->pp_all_items[i]->input.i_id < i_id )
463         {
464             i_bottom = i + 1;
465         }
466         else
467         {
468             i_top = i - 1;
469         }
470         i = i_bottom + ( i_top - i_bottom ) / 2;
471     }
472     if( p_playlist->pp_all_items[i]->input.i_id == i_id )
473     {
474         return p_playlist->pp_all_items[i];
475     }
476     return NULL;
477 }
478
479 playlist_item_t *playlist_LockItemGetById( playlist_t *p_playlist, int i_id)
480 {
481     playlist_item_t *p_ret;
482     vlc_mutex_lock( &p_playlist->object_lock );
483     p_ret = playlist_ItemGetById( p_playlist, i_id );
484     vlc_mutex_unlock( &p_playlist->object_lock );
485     return p_ret;
486 }
487
488 /**
489  * Search an item by its input_item_t
490  *
491  * \param p_playlist the playlist
492  * \param p_item the input_item_t to find
493  * \return the item, or NULL on failure
494  */
495 playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
496                                            input_item_t *p_item )
497 {
498     int i;
499     if( &p_playlist->status.p_item->input == p_item )
500     {
501         return p_playlist->status.p_item;
502     }
503
504     for( i =  0 ; i < p_playlist->i_size ; i++ )
505     {
506         if( &p_playlist->pp_items[i]->input == p_item )
507         {
508             return p_playlist->pp_items[i];
509         }
510     }
511     return NULL;
512 }
513
514 playlist_item_t *playlist_LockItemGetByInput( playlist_t *p_playlist,
515                                                input_item_t *p_item )
516 {
517     playlist_item_t *p_ret;
518     vlc_mutex_lock( &p_playlist->object_lock );
519     p_ret = playlist_ItemGetByInput( p_playlist, p_item );
520     vlc_mutex_unlock( &p_playlist->object_lock );
521     return p_ret;
522 }
523
524
525 /***********************************************************************
526  * Misc functions
527  ***********************************************************************/
528
529 /**
530  * Transform an item to a node
531  *
532  * This function must be entered without the playlist lock
533  *
534  * \param p_playlist the playlist object
535  * \param p_item the item to transform
536  * \return nothing
537  */
538 int playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
539 {
540     int i = 0;
541     if( p_item->i_children == -1 )
542     {
543         p_item->i_children = 0;
544     }
545
546     /* Remove it from the array of available items */
547     for( i = 0 ; i < p_playlist->i_size ; i++ )
548     {
549         if( p_item == p_playlist->pp_items[i] )
550         {
551             REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
552         }
553     }
554     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
555
556     return VLC_SUCCESS;
557 }
558
559 int playlist_LockItemToNode( playlist_t *p_playlist, playlist_item_t *p_item )
560 {
561     int i_ret;
562     vlc_mutex_lock( &p_playlist->object_lock );
563     i_ret = playlist_ItemToNode( p_playlist, p_item );
564     vlc_mutex_unlock( &p_playlist->object_lock );
565     return i_ret;
566 }
567
568 /**
569  * Replaces an item with another one
570  * This function must be entered without the playlist lock
571  *
572  * \see playlist_Replace
573  */
574 int playlist_LockReplace( playlist_t *p_playlist,
575                              playlist_item_t *p_olditem,
576                              input_item_t *p_new )
577 {
578     int i_ret;
579     vlc_mutex_lock( &p_playlist->object_lock );
580     i_ret = playlist_Replace( p_playlist, p_olditem, p_new );
581     vlc_mutex_unlock( &p_playlist->object_lock );
582     return i_ret;
583 }
584
585 /**
586  * Replaces an item with another one
587  * This function must be entered with the playlist lock:
588  *
589  * \param p_playlist the playlist
590  * \param p_olditem the item to replace
591  * \param p_new the new input_item
592  * \return VLC_SUCCESS or an error
593  */
594 int playlist_Replace( playlist_t *p_playlist, playlist_item_t *p_olditem,
595                        input_item_t *p_new )
596 {
597     int i;
598     int j;
599
600     if( p_olditem->i_children != -1 )
601     {
602         msg_Err( p_playlist, "playlist_Replace can only be used on leafs");
603         return VLC_EGENERIC;
604     }
605
606     p_olditem->i_nb_played = 0;
607     memcpy( &p_olditem->input, p_new, sizeof( input_item_t ) );
608
609     p_olditem->i_nb_played = 0;
610
611     for( i = 0 ; i< p_olditem->i_parents ; i++ )
612     {
613         playlist_item_t *p_parent = p_olditem->pp_parents[i]->p_parent;
614
615         for( j = 0 ; j< p_parent->i_children ; i++ )
616         {
617             if( p_parent->pp_children[j] == p_olditem )
618             {
619                 p_parent->i_serial++;
620             }
621         }
622     }
623     return VLC_SUCCESS;
624 }
625
626 /**
627  * Deletes an item from a playlist.
628  *
629  * This function must be entered without the playlist lock
630  *
631  * \param p_playlist the playlist to remove from.
632  * \param i_id the identifier of the item to delete
633  * \return returns VLC_SUCCESS or an error
634  */
635 int playlist_Delete( playlist_t * p_playlist, int i_id )
636 {
637     int i, i_top, i_bottom;
638     int i_pos;
639     vlc_bool_t b_flag = VLC_FALSE;
640
641     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
642
643     if( p_item == NULL )
644     {
645         return VLC_EGENERIC;
646     }
647     if( p_item->i_children > -1 )
648     {
649         return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
650     }
651
652     var_SetInteger( p_playlist, "item-deleted", i_id );
653
654     i_bottom = 0; i_top = p_playlist->i_all_size - 1;
655     i = i_top / 2;
656     while( p_playlist->pp_all_items[i]->input.i_id != i_id &&
657            i_top > i_bottom )
658     {
659         if( p_playlist->pp_all_items[i]->input.i_id < i_id )
660         {
661             i_bottom = i + 1;
662         }
663         else
664         {
665             i_top = i - 1;
666         }
667         i = i_bottom + ( i_top - i_bottom ) / 2;
668     }
669     if( p_playlist->pp_all_items[i]->input.i_id == i_id )
670     {
671         REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
672     }
673
674     /* Check if it is the current item */
675     if( p_playlist->status.p_item == p_item )
676     {
677         /* Hack we don't call playlist_Control for lock reasons */
678         p_playlist->status.i_status = PLAYLIST_STOPPED;
679         p_playlist->request.b_request = VLC_TRUE;
680         p_playlist->request.p_item = NULL;
681         msg_Info( p_playlist, "stopping playback" );
682         b_flag = VLC_TRUE;
683     }
684
685     /* Get position and update index if needed */
686     i_pos = playlist_GetPositionById( p_playlist, i_id );
687
688     if( i_pos >= 0 && i_pos <= p_playlist->i_index )
689     {
690         p_playlist->i_index--;
691     }
692
693     msg_Dbg( p_playlist, "deleting playlist item `%s'",
694                           p_item->input.psz_name );
695
696     /* Remove the item from all its parent nodes */
697     for ( i= 0 ; i < p_item->i_parents ; i++ )
698     {
699         playlist_NodeRemoveItem( p_playlist, p_item,
700                                  p_item->pp_parents[i]->p_parent );
701         if( p_item->pp_parents[i]->i_view == VIEW_ALL )
702         {
703             p_playlist->i_size--;
704         }
705     }
706
707     /* TODO : Update views */
708
709     if( b_flag == VLC_FALSE )
710         playlist_ItemDelete( p_item );
711     else
712         p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
713
714     return VLC_SUCCESS;
715 }
716
717 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
718 {
719     int i_ret;
720     vlc_mutex_lock( &p_playlist->object_lock );
721     i_ret = playlist_Delete( p_playlist, i_id );
722     vlc_mutex_unlock( &p_playlist->object_lock );
723     return i_ret;
724 }
725
726 /**
727  * Clear all playlist items
728  *
729  * \param p_playlist the playlist to be cleared.
730  * \return returns 0
731  */
732 int playlist_Clear( playlist_t * p_playlist )
733 {
734     int i;
735     for( i = p_playlist->i_size; i > 0 ; i-- )
736     {
737         playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
738     }
739     for( i = 0 ; i< p_playlist->i_views; i++ )
740     {
741         playlist_ViewEmpty( p_playlist, i, VLC_TRUE );
742     }
743     return VLC_SUCCESS;
744 }
745
746 int playlist_LockClear( playlist_t *p_playlist )
747 {
748     int i_ret;
749     vlc_mutex_lock( &p_playlist->object_lock );
750     i_ret = playlist_Clear( p_playlist );
751     vlc_mutex_unlock( &p_playlist->object_lock );
752     return i_ret;
753 }
754
755
756 /**
757  * Disables a playlist item
758  *
759  * \param p_playlist the playlist to disable from.
760  * \param i_pos the position of the item to disable
761  * \return returns 0
762  */
763 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
764 {
765     if( !p_item ) return VLC_EGENERIC;
766
767     msg_Dbg( p_playlist, "disabling playlist item `%s'",
768                    p_item->input.psz_name );
769
770     if( p_item->i_flags & PLAYLIST_ENA_FLAG )
771     {
772         p_playlist->i_enabled--;
773     }
774     p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
775
776     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
777     return VLC_SUCCESS;
778 }
779
780 /**
781  * Enables a playlist item
782  *
783  * \param p_playlist the playlist to enable from.
784  * \param i_pos the position of the item to enable
785  * \return returns 0
786  */
787 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
788 {
789     if( !p_item ) return VLC_EGENERIC;
790
791     msg_Dbg( p_playlist, "enabling playlist item `%s'",
792                    p_item->input.psz_name );
793
794     if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
795     {
796         p_playlist->i_enabled++;
797     }
798     p_item->i_flags |= PLAYLIST_ENA_FLAG;
799
800     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
801     return VLC_SUCCESS;
802 }
803
804 /**
805  * Move an item in a playlist
806  *
807  * This function must be entered without the playlist lock
808  *
809  * Move the item in the playlist with position i_pos before the current item
810  * at position i_newpos.
811  * \param p_playlist the playlist to move items in
812  * \param i_pos the position of the item to move
813  * \param i_newpos the position of the item that will be behind the moved item
814  *        after the move
815  * \return returns VLC_SUCCESS
816  */
817 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
818 {
819     vlc_value_t val;
820     vlc_mutex_lock( &p_playlist->object_lock );
821
822     /* take into account that our own row disappears. */
823     if( i_pos < i_newpos ) i_newpos--;
824
825     if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
826         i_newpos <= p_playlist->i_size )
827     {
828         playlist_item_t * temp;
829
830         msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
831                  p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
832
833         if( i_pos == p_playlist->i_index )
834         {
835             p_playlist->i_index = i_newpos;
836         }
837         else if( i_pos > p_playlist->i_index &&
838                  i_newpos <= p_playlist->i_index )
839         {
840             p_playlist->i_index++;
841         }
842         else if( i_pos < p_playlist->i_index &&
843                  i_newpos >= p_playlist->i_index )
844         {
845             p_playlist->i_index--;
846         }
847
848         if ( i_pos < i_newpos )
849         {
850             temp = p_playlist->pp_items[i_pos];
851             while ( i_pos < i_newpos )
852             {
853                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
854                 i_pos++;
855             }
856             p_playlist->pp_items[i_newpos] = temp;
857         }
858         else if ( i_pos > i_newpos )
859         {
860             temp = p_playlist->pp_items[i_pos];
861             while ( i_pos > i_newpos )
862             {
863                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
864                 i_pos--;
865             }
866             p_playlist->pp_items[i_newpos] = temp;
867         }
868     }
869
870     vlc_mutex_unlock( &p_playlist->object_lock );
871
872     val.b_bool = VLC_TRUE;
873     var_Set( p_playlist, "intf-change", val );
874
875     return VLC_SUCCESS;
876 }
877
878 /**
879  * Moves an item
880  *
881  * This function must be entered with the playlist lock
882  *
883  * \param p_playlist the playlist
884  * \param p_item the item to move
885  * \param p_node the new parent of the item
886  * \param i_newpos the new position under this new parent
887  * \param i_view the view in which the move must be done or ALL_VIEWS
888  * \return VLC_SUCCESS or an error
889  */
890 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
891                        playlist_item_t *p_node, int i_newpos, int i_view )
892 {
893     int i;
894     playlist_item_t *p_detach = NULL;
895     struct item_parent_t *p_parent;
896
897     if( p_node->i_children == -1 ) return VLC_EGENERIC;
898
899     /* Detach from the parent */
900     for( i = 0 ; i< p_item->i_parents; i++ )
901     {
902         if( p_item->pp_parents[i]->i_view == i_view )
903         {
904             int j;
905             p_detach = p_item->pp_parents[i]->p_parent;
906             for( j = 0; j < p_detach->i_children; j++ )
907             {
908                 if( p_detach->pp_children[j] == p_item ) break;
909             }
910             REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
911             p_detach->i_serial++;
912             free( p_item->pp_parents[i] );
913             REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, i );
914             i--;
915         }
916     }
917
918     /* Attach to new parent */
919     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
920
921     p_parent = malloc( sizeof( struct item_parent_t ) );
922     p_parent->p_parent = p_node;
923     p_parent->i_view = i_view;
924
925     INSERT_ELEM( p_item->pp_parents, p_item->i_parents, p_item->i_parents,
926                  p_parent );
927     p_node->i_serial++;
928     p_item->i_serial++;
929
930     return VLC_SUCCESS;
931 }