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