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