]> git.sesse.net Git - vlc/blob - src/playlist/item-ext.c
Reduce the global verbosity
[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     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_Info( 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         p_playlist->i_enabled ++;
192
193         /* We update the ALL view directly */
194         playlist_ViewUpdate( p_playlist, VIEW_ALL );
195
196         /* Add the item to the General category */
197         if( b_end == VLC_TRUE )
198         {
199             playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
200                                  p_playlist->p_general );
201             p_add->p_item = p_item;
202             p_add->p_node = p_playlist->p_general;
203             p_add->i_view = VIEW_CATEGORY;
204             val.p_address = p_add;
205             var_Set( p_playlist, "item-append", val );
206         }
207         else
208         {
209             playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
210                                  p_playlist->p_general, i_pos );
211         }
212
213
214         p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
215         playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
216
217         /* Also add the item to the "simple" view */
218         p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
219
220         if( b_end == VLC_TRUE )
221         {
222             playlist_NodeAppend( p_playlist, VIEW_SIMPLE,p_item,
223                                   p_view->p_root );
224             p_add->p_item = p_item;
225             p_add->p_node = p_view->p_root;
226             p_add->i_view = VIEW_SIMPLE;
227             val.p_address = p_add;
228             var_Set( p_playlist, "item-append", val );
229         }
230         else
231         {
232             playlist_NodeInsert( p_playlist, VIEW_SIMPLE,p_item,
233                                   p_view->p_root, i_pos );
234         }
235
236
237         /* FIXME : Update sorted views */
238
239         if( p_playlist->i_index >= i_pos )
240         {
241             p_playlist->i_index++;
242         }
243     }
244     else
245     {
246         msg_Err( p_playlist, "Insert mode not implemented" );
247     }
248
249     if( (i_mode & PLAYLIST_GO ) && p_view )
250     {
251         p_playlist->request.b_request = VLC_TRUE;
252         /* FIXME ... */
253         p_playlist->request.i_view = VIEW_SIMPLE;
254         p_playlist->request.p_node = p_view->p_root;
255         p_playlist->request.p_item = p_item;
256
257         if( p_playlist->p_input )
258         {
259             input_StopThread( p_playlist->p_input );
260         }
261         p_playlist->status.i_status = PLAYLIST_RUNNING;
262     }
263
264     vlc_mutex_unlock( &p_playlist->object_lock );
265
266     if( b_end == VLC_FALSE )
267     {
268         val.b_bool = VLC_TRUE;
269         var_Set( p_playlist, "intf-change", val );
270     }
271
272     free( p_add );
273
274     return p_item->input.i_id;
275 }
276
277
278 /**
279  * Add a playlist item to a given node (in the category view )
280  *
281  * \param p_playlist the playlist to insert into
282  * \param p_item the playlist item to insert
283  * \param i_view the view for which to add or TODO: ALL_VIEWS
284  * \param p_parent the parent node
285  * \param i_mode the mode used when adding
286  * \param i_pos the possition in the node where to add. If this is
287  *        PLAYLIST_END the item will be added at the end of the node
288  ** \return The id of the playlist item
289  */
290 int playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
291                           int i_view,playlist_item_t *p_parent,
292                           int i_mode, int i_pos)
293 {
294     vlc_value_t val;
295     int i_position;
296     playlist_view_t *p_view;
297
298     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
299
300     vlc_mutex_lock( &p_playlist->object_lock );
301
302     /* Sanity checks */
303     if( !p_parent || p_parent->i_children == -1 )
304     {
305         msg_Err( p_playlist, "invalid node" );
306     }
307
308     /*
309      * CHECK_INSERT : checks if the item is already enqued before
310      * enqueing it
311      */
312     if ( i_mode & PLAYLIST_CHECK_INSERT )
313     {
314          int j;
315
316         if ( p_playlist->pp_items )
317         {
318             for ( j = 0; j < p_playlist->i_size; j++ )
319             {
320                 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
321                               p_item->input.psz_uri ) )
322                 {
323                     playlist_ItemDelete( p_item );
324                     vlc_mutex_unlock( &p_playlist->object_lock );
325                     free( p_add );
326                     return -1;
327                 }
328             }
329         }
330         i_mode &= ~PLAYLIST_CHECK_INSERT;
331         i_mode |= PLAYLIST_APPEND;
332     }
333
334     msg_Info( p_playlist, "adding playlist item `%s' ( %s )",
335              p_item->input.psz_name, p_item->input.psz_uri );
336
337     p_item->input.i_id = ++p_playlist->i_last_id;
338
339     /* First, add the item at the right position in the item bank */
340     /* WHY THAT ? */
341      //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
342     i_position = p_playlist->i_size ;
343
344     INSERT_ELEM( p_playlist->pp_items,
345                  p_playlist->i_size,
346                  i_position,
347                  p_item );
348     p_playlist->i_enabled ++;
349
350     /* TODO: Handle modes */
351     playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
352
353     p_add->p_item = p_item;
354     p_add->p_node = p_parent;
355     p_add->i_view = i_view;
356     val.p_address = p_add;
357     var_Set( p_playlist, "item-append", val );
358
359     /* We update the ALL view directly */
360     p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
361     playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
362     playlist_ViewUpdate( p_playlist, VIEW_ALL );
363
364     /* TODO : Update sorted views*/
365
366     if( i_mode & PLAYLIST_GO )
367     {
368         p_playlist->request.b_request = VLC_TRUE;
369         p_playlist->request.i_view = VIEW_CATEGORY;
370         p_playlist->request.p_node = p_parent;
371         p_playlist->request.p_item = p_item;
372         if( p_playlist->p_input )
373         {
374             input_StopThread( p_playlist->p_input );
375         }
376         p_playlist->status.i_status = PLAYLIST_RUNNING;
377     }
378
379     vlc_mutex_unlock( &p_playlist->object_lock );
380
381     val.b_bool = VLC_TRUE;
382 //    var_Set( p_playlist, "intf-change", val );
383 //
384     free( p_add );
385
386     return p_item->input.i_id;
387 }
388
389 /***************************************************************************
390  * Item search functions
391  ***************************************************************************/
392
393 /**
394  * Search the position of an item by its id
395  * This function must be entered with the playlist lock
396  *
397  * \param p_playlist the playlist
398  * \param i_id the id to find
399  * \return the position, or VLC_EGENERIC on failure
400  */
401 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
402 {
403     int i;
404     for( i =  0 ; i < p_playlist->i_size ; i++ )
405     {
406         if( p_playlist->pp_items[i]->input.i_id == i_id )
407         {
408             return i;
409         }
410     }
411     return VLC_EGENERIC;
412 }
413
414
415 /**
416  * Search an item by its position
417  * This function must be entered with the playlist lock
418  *
419  * \param p_playlist the playlist
420  * \param i_pos the position of the item to find
421  * \return the item, or NULL on failure
422  */
423 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
424 {
425     if( i_pos >= 0 && i_pos < p_playlist->i_size)
426     {
427         return p_playlist->pp_items[i_pos];
428     }
429     else if( p_playlist->i_size > 0)
430     {
431         return p_playlist->pp_items[p_playlist->i_index];
432     }
433     else
434     {
435         return NULL;
436     }
437 }
438
439 playlist_item_t *playlist_LockItemGetByPos( playlist_t *p_playlist, int i_pos )
440 {
441     playlist_item_t *p_ret;
442     vlc_mutex_lock( &p_playlist->object_lock );
443     p_ret = playlist_ItemGetByPos( p_playlist, i_pos );
444     vlc_mutex_unlock( &p_playlist->object_lock );
445     return p_ret;
446 }
447
448 /**
449  * Search an item by its id
450  *
451  * \param p_playlist the playlist
452  * \param i_id the id to find
453  * \return the item, or NULL on failure
454  */
455 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
456 {
457     int i;
458     for( i =  0 ; i < p_playlist->i_size ; i++ )
459     {
460         if( p_playlist->pp_items[i]->input.i_id == i_id )
461         {
462             return p_playlist->pp_items[i];
463         }
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;
627
628     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
629
630     if( p_item == NULL )
631     {
632         return VLC_EGENERIC;
633     }
634     var_SetInteger( p_playlist, "item-deleted", i_id );
635
636     /* Check if it is the current item */
637     if( p_playlist->status.p_item == p_item )
638     {
639         /* Hack we don't call playlist_Control for lock reasons */
640         p_playlist->status.i_status = PLAYLIST_STOPPED;
641         p_playlist->request.b_request = VLC_TRUE;
642         p_playlist->status.p_item = NULL;
643     }
644
645     msg_Info( p_playlist, "deleting playlist item `%s'",
646                           p_item->input.psz_name );
647
648     /* Remove the item from all its parent nodes */
649     for ( i= 0 ; i < p_item->i_parents ; i++ )
650     {
651         playlist_NodeRemoveItem( p_playlist, p_item,
652                                  p_item->pp_parents[i]->p_parent );
653         if( p_item->pp_parents[i]->i_view == VIEW_ALL )
654         {
655             p_playlist->i_size--;
656         }
657     }
658
659     /* TODO : Update views */
660
661     playlist_ItemDelete( p_item );
662
663     return VLC_SUCCESS;
664 }
665
666 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
667 {
668     int i_ret;
669     vlc_mutex_lock( &p_playlist->object_lock );
670     i_ret = playlist_Delete( p_playlist, i_id );
671     vlc_mutex_unlock( &p_playlist->object_lock );
672     return i_ret;
673 }
674
675 /**
676  * Clear all playlist items
677  *
678  * \param p_playlist the playlist to be cleared.
679  * \return returns 0
680  */
681 int playlist_Clear( playlist_t * p_playlist )
682 {
683     int i;
684     for( i = p_playlist->i_size; i > 0 ; i-- )
685     {
686         playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
687     }
688     for( i = 0 ; i< p_playlist->i_views; i++ )
689     {
690         playlist_ViewEmpty( p_playlist, i, VLC_TRUE );
691     }
692     return VLC_SUCCESS;
693 }
694
695 int playlist_LockClear( playlist_t *p_playlist )
696 {
697     int i_ret;
698     vlc_mutex_lock( &p_playlist->object_lock );
699     playlist_Clear( p_playlist );
700     vlc_mutex_unlock( &p_playlist->object_lock );
701     return i_ret;
702 }
703
704
705 /**
706  * Disables a playlist item
707  *
708  * \param p_playlist the playlist to disable from.
709  * \param i_pos the position of the item to disable
710  * \return returns 0
711  */
712 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
713 {
714     if( !p_item ) return VLC_EGENERIC;
715
716     msg_Dbg( p_playlist, "disabling playlist item `%s'",
717                    p_item->input.psz_name );
718
719     if( p_item->i_flags & PLAYLIST_ENA_FLAG )
720     {
721         p_playlist->i_enabled--;
722     }
723     p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
724
725     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
726     return VLC_SUCCESS;
727 }
728
729 /**
730  * Enables a playlist item
731  *
732  * \param p_playlist the playlist to enable from.
733  * \param i_pos the position of the item to enable
734  * \return returns 0
735  */
736 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
737 {
738     if( !p_item ) return VLC_EGENERIC;
739
740     msg_Dbg( p_playlist, "enabling playlist item `%s'",
741                    p_item->input.psz_name );
742
743     if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
744     {
745         p_playlist->i_enabled++;
746     }
747     p_item->i_flags |= PLAYLIST_ENA_FLAG;
748
749     var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
750     return VLC_SUCCESS;
751 }
752
753 /**
754  * Move an item in a playlist
755  *
756  * This function must be entered without the playlist lock
757  *
758  * Move the item in the playlist with position i_pos before the current item
759  * at position i_newpos.
760  * \param p_playlist the playlist to move items in
761  * \param i_pos the position of the item to move
762  * \param i_newpos the position of the item that will be behind the moved item
763  *        after the move
764  * \return returns VLC_SUCCESS
765  */
766 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
767 {
768     vlc_value_t val;
769     vlc_mutex_lock( &p_playlist->object_lock );
770
771     /* take into account that our own row disappears. */
772     if( i_pos < i_newpos ) i_newpos--;
773
774     if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
775         i_newpos <= p_playlist->i_size )
776     {
777         playlist_item_t * temp;
778
779         msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
780                  p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
781
782         if( i_pos == p_playlist->i_index )
783         {
784             p_playlist->i_index = i_newpos;
785         }
786         else if( i_pos > p_playlist->i_index &&
787                  i_newpos <= p_playlist->i_index )
788         {
789             p_playlist->i_index++;
790         }
791         else if( i_pos < p_playlist->i_index &&
792                  i_newpos >= p_playlist->i_index )
793         {
794             p_playlist->i_index--;
795         }
796
797         if ( i_pos < i_newpos )
798         {
799             temp = p_playlist->pp_items[i_pos];
800             while ( i_pos < i_newpos )
801             {
802                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
803                 i_pos++;
804             }
805             p_playlist->pp_items[i_newpos] = temp;
806         }
807         else if ( i_pos > i_newpos )
808         {
809             temp = p_playlist->pp_items[i_pos];
810             while ( i_pos > i_newpos )
811             {
812                 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
813                 i_pos--;
814             }
815             p_playlist->pp_items[i_newpos] = temp;
816         }
817     }
818
819     vlc_mutex_unlock( &p_playlist->object_lock );
820
821     val.b_bool = VLC_TRUE;
822     var_Set( p_playlist, "intf-change", val );
823
824     return VLC_SUCCESS;
825 }
826
827 /**
828  * Moves an item
829  *
830  * \param p_playlist the playlist
831  * \param p_item the item to move
832  * \param p_node the new parent of the item
833  * \param i_newpos the new position under this new parent
834  * \param i_view the view in which the move must be done or ALL_VIEWS
835  * \return VLC_SUCCESS or an error
836  */
837 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
838                        playlist_item_t *p_node, int i_newpos, int i_view )
839 {
840     int i;
841     playlist_item_t *p_detach = NULL;
842 #if 0
843     if( i_view == ALL_VIEWS )
844     {
845         for( i = 0 ; i < p_playlist->i_views; i++ )
846         {
847             playlist_TreeMove( p_playlist, p_item, p_node, i_newpos,
848                                p_playlist->pp_views[i] );
849         }
850     }
851 #endif
852
853     /* Find the parent */
854     for( i = 0 ; i< p_item->i_parents; i++ )
855     {
856         if( p_item->pp_parents[i]->i_view == i_view )
857         {
858             p_detach = p_item->pp_parents[i]->p_parent;
859             break;
860         }
861     }
862     if( p_detach == NULL )
863     {
864         msg_Err( p_playlist, "item not found in view %i", i_view );
865         return VLC_EGENERIC;
866     }
867
868     /* Detach from the parent */
869 //    playlist_NodeDetach( p_detach, p_item );
870
871     /* Attach to new parent */
872
873     return VLC_SUCCESS;
874 }