]> git.sesse.net Git - vlc/blob - src/playlist/view.c
Merge [11470],[11471],[11474] and [11475] - Closes: #205
[vlc] / src / playlist / view.c
1 /*****************************************************************************
2  * view.c : Playlist views functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id: item.c 7997 2004-06-18 11:35:45Z sigmunau $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23 #include <stdlib.h>                                      /* free(), strtol() */
24 #include <stdio.h>                                              /* sprintf() */
25 #include <string.h>                                            /* strerror() */
26
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include "vlc_playlist.h"
31
32 #undef PLAYLIST_DEBUG
33
34 /************************************************************************
35  * Local prototypes
36  ************************************************************************/
37
38 /* TODO: inline */
39 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
40                                         playlist_item_t *, int );
41
42 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
43                 int i_view,
44                 playlist_item_t *p_root,
45                 playlist_item_t *p_item,
46                 playlist_item_t *p_parent );
47
48 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
49                 int i_view,
50                 playlist_item_t *p_root,
51                 playlist_item_t *p_item,
52                 playlist_item_t *p_parent );
53
54 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
55                         int i_level );
56
57 /**********************************************************************
58  * Exported View management functions
59  **********************************************************************/
60
61 /**
62  * Create a new view
63  *
64  * \param p_playlist a playlist object
65  * \param i_id the view identifier
66  * \return the new view or NULL on failure
67  */
68 playlist_view_t * playlist_ViewCreate( playlist_t *p_playlist, int i_id,
69                                        char *psz_name )
70 {
71     playlist_view_t * p_view;
72
73     p_view = malloc( sizeof( playlist_view_t ) );
74
75     memset( p_view, 0, sizeof( playlist_view_t ) );
76
77     p_view->p_root = playlist_NodeCreate( p_playlist, i_id, NULL, NULL );
78     p_view->i_id = i_id;
79     p_view->psz_name = psz_name ? strdup( psz_name ) : strdup(_("Undefined") );
80
81     return p_view;
82 }
83
84 /**
85  * Creates a new view and add it to the list
86  *
87  * This function must be entered without the playlist lock
88  *
89  * \param p_playlist a playlist object
90  * \param i_id the view identifier
91  * \return VLC_SUCCESS or an error
92  */
93 int playlist_ViewInsert( playlist_t *p_playlist, int i_id, char *psz_name )
94 {
95     playlist_view_t *p_view =
96         playlist_ViewCreate( p_playlist, i_id , psz_name );
97     if( !p_view )
98     {
99         msg_Err( p_playlist, "Creation failed" );
100         return VLC_EGENERIC;
101     }
102
103     vlc_mutex_lock( &p_playlist->object_lock );
104
105     INSERT_ELEM( p_playlist->pp_views, p_playlist->i_views,
106                  p_playlist->i_views, p_view );
107
108     vlc_mutex_unlock( &p_playlist->object_lock );
109     return VLC_SUCCESS;
110 }
111
112
113 /**
114  * Deletes a view
115  *
116  * This function must be entered wit the playlist lock
117  *
118  * \param p_view the view to delete
119  * \return nothing
120  */
121 int playlist_ViewDelete( playlist_t *p_playlist,playlist_view_t *p_view )
122 {
123     playlist_NodeDelete( p_playlist, p_view->p_root, VLC_TRUE, VLC_TRUE );
124     REMOVE_ELEM( p_playlist->pp_views, p_playlist->i_views, 0 );
125     return VLC_SUCCESS;
126 }
127
128
129 /**
130  * Dumps the content of a view
131  *
132  * \param p_playlist the playlist
133  * \param p_view the view to dump
134  * \return nothing
135  */
136 int playlist_ViewDump( playlist_t *p_playlist, playlist_view_t *p_view )
137 {
138 #ifdef PLAYLIST_DEBUG
139     msg_Dbg( p_playlist, "dumping view %i",p_view->i_id );
140     playlist_NodeDump( p_playlist,p_view->p_root, 1 );
141 #endif
142     return VLC_SUCCESS;
143 }
144
145 /**
146  * Counts the items of a view
147  *
148  * \param p_playlist the playlist
149  * \param p_view the view to count
150  * \return the number of items
151  */
152 int playlist_ViewItemCount( playlist_t *p_playlist,
153                             playlist_view_t *p_view )
154 {
155     return playlist_NodeChildrenCount( p_playlist, p_view->p_root );
156 }
157
158
159 /**
160  * Updates a view. Only make sense for "sorted" and "ALL" views
161  *
162  * \param p_playlist the playlist
163  * \param i_view the view to update
164  * \return nothing
165  */
166 int playlist_ViewUpdate( playlist_t *p_playlist, int i_view)
167 {
168     playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
169
170     if( p_view == NULL )
171     {
172         return VLC_EGENERIC;
173     }
174
175     if( i_view == VIEW_ALL )
176     {
177         p_view->p_root->i_children = p_playlist->i_size;
178         p_view->p_root->pp_children = p_playlist->pp_items;
179     }
180
181     /* Handle update of sorted views here */
182     if( i_view >= VIEW_FIRST_SORTED )
183     {
184         int i_sort_type;
185         playlist_ViewEmpty( p_playlist, i_view, VLC_FALSE );
186
187         switch( i_view )
188         {
189             case VIEW_S_AUTHOR: i_sort_type = SORT_AUTHOR;break;
190             case VIEW_S_GENRE: i_sort_type = SORT_GENRE;break;
191             default: i_sort_type = SORT_AUTHOR;
192         }
193         playlist_NodeGroup( p_playlist, i_view, p_view->p_root,
194                             p_playlist->pp_items,p_playlist->i_size,
195                             i_sort_type, ORDER_NORMAL );
196     }
197
198
199     return VLC_SUCCESS;
200 }
201
202
203 /**
204  * Find a view
205  *
206  * \param p_playlist the playlist
207  * \param i_id the id to find
208  * \return the found view or NULL if not found
209  */
210 playlist_view_t *playlist_ViewFind( playlist_t *p_playlist, int i_id )
211 {
212     int i;
213     for( i=0 ; i< p_playlist->i_views ; i++ )
214     {
215         if( p_playlist->pp_views[i]->i_id == i_id )
216         {
217             return p_playlist->pp_views[i];
218         }
219     }
220     return NULL;
221 }
222
223
224 int playlist_ViewEmpty( playlist_t *p_playlist, int i_view,
225                         vlc_bool_t b_delete_items )
226 {
227     playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );
228
229     if( p_view == NULL )
230     {
231         return VLC_EGENERIC;
232     }
233
234     return playlist_NodeEmpty( p_playlist, p_view->p_root, b_delete_items );
235 }
236
237 /**********************************************************************
238  * Exported Nodes management functions
239  **********************************************************************/
240
241
242
243 /**
244  * Create a playlist node
245  *
246  * \param p_playlist the playlist
247  * \paam psz_name the name of the node
248  * \param p_parent the parent node to attach to or NULL if no attach
249  * \return the new node
250  */
251 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, int i_view,
252                                        char *psz_name,
253                                        playlist_item_t *p_parent )
254 {
255     /* Create the item */
256     playlist_item_t *p_item = (playlist_item_t *)malloc(
257                                         sizeof( playlist_item_t ) );
258     vlc_value_t val;
259     playlist_add_t *p_add;
260
261     if( p_item == NULL )
262     {
263         return NULL;
264     }
265     p_add = (playlist_add_t*)malloc( sizeof(playlist_add_t) );
266     if( p_add == NULL )
267     {
268         free( p_item );
269         return NULL;
270     }
271     vlc_input_item_Init( VLC_OBJECT(p_playlist), &p_item->input );
272
273     if( psz_name != NULL )
274     {
275         p_item->input.psz_name = strdup( psz_name );
276     }
277     else
278     {
279         p_item->input.psz_name = strdup( _("Undefined") );
280     }
281
282     p_item->input.psz_uri = NULL;
283
284     p_item->b_enabled = VLC_TRUE;
285     p_item->i_nb_played = 0;
286
287     p_item->i_flags = 0;
288
289     p_item->i_children = 0;
290     p_item->pp_children = NULL;
291
292     p_item->input.i_duration = -1;
293     p_item->input.ppsz_options = NULL;
294     p_item->input.i_options = 0;
295     p_item->input.i_categories = 0;
296     p_item->input.pp_categories = NULL;
297     p_item->input.i_id = ++p_playlist->i_last_id;
298
299     p_item->input.i_type = ITEM_TYPE_NODE;
300
301     p_item->pp_parents = NULL;
302     p_item->i_parents = 0;
303
304     p_item->i_flags |= PLAYLIST_SKIP_FLAG; /* Default behaviour */
305
306     vlc_mutex_init( p_playlist, &p_item->input.lock );
307
308     INSERT_ELEM( p_playlist->pp_all_items,
309                  p_playlist->i_all_size,
310                  p_playlist->i_all_size,
311                  p_item );
312
313     if( p_parent != NULL )
314     {
315         playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
316     }
317
318     p_add->i_node = p_parent ? p_parent->input.i_id : -1;
319     p_add->i_item = p_item->input.i_id;
320     p_add->i_view = i_view;
321     val.p_address = p_add;
322     var_Set( p_playlist, "item-append", val);
323
324     free( p_add );
325
326     return p_item;
327 }
328
329 /**
330  * Remove all the children of a node
331  *
332  * This function must be entered with the playlist lock
333  *
334  * \param p_playlist the playlist
335  * \param p_root the node
336  * \param b_delete_items do we have to delete the children items ?
337  * \return VLC_SUCCESS or an error
338  */
339 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
340                         vlc_bool_t b_delete_items )
341 {
342     int i;
343     if( p_root->i_children == -1 )
344     {
345         return VLC_EGENERIC;
346     }
347
348     /* Delete the children */
349     for( i =  p_root->i_children-1 ; i >= 0 ;i-- )
350     {
351         if( p_root->pp_children[i]->i_children > -1 )
352         {
353             playlist_NodeDelete( p_playlist, p_root->pp_children[i],
354                                  b_delete_items , VLC_FALSE );
355         }
356         else if( b_delete_items )
357         {
358             /* Delete the item here */
359             playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
360         }
361     }
362     return VLC_SUCCESS;
363 }
364
365 /**
366  * Remove all the children of a node and removes the node
367  *
368  * \param p_playlist the playlist
369  * \param p_root the node
370  * \param b_delete_items do we have to delete the children items ?
371  * \return VLC_SUCCESS or an error
372  */
373 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
374                          vlc_bool_t b_delete_items, vlc_bool_t b_force )
375 {
376     int i, i_top, i_bottom;
377     if( p_root->i_children == -1 )
378     {
379         return VLC_EGENERIC;
380     }
381
382     /* Delete the children */
383     for( i =  p_root->i_children - 1 ; i >= 0; i-- )
384     {
385         if( p_root->pp_children[i]->i_children > -1 )
386         {
387             playlist_NodeDelete( p_playlist, p_root->pp_children[i],
388                                 b_delete_items , b_force );
389         }
390         else if( b_delete_items )
391         {
392             /* Delete the item here */
393             playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
394         }
395     }
396     /* Delete the node */
397     if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
398     {
399     }
400     else
401     {
402         for( i = 0 ; i< p_root->i_parents; i++ )
403         {
404             playlist_NodeRemoveItem( p_playlist, p_root,
405                                      p_root->pp_parents[i]->p_parent );
406         }
407         var_SetInteger( p_playlist, "item-deleted", p_root->input.i_id );
408
409         i_bottom = 0; i_top = p_playlist->i_all_size - 1;
410         i = i_top / 2;
411         while( p_playlist->pp_all_items[i]->input.i_id != p_root->input.i_id &&
412                i_top > i_bottom )
413         {
414             if( p_playlist->pp_all_items[i]->input.i_id < p_root->input.i_id )
415             {
416                 i_bottom = i + 1;
417             }
418             else
419             {
420                 i_top = i - 1;
421             }
422             i = i_bottom + ( i_top - i_bottom ) / 2;
423         }
424         if( p_playlist->pp_all_items[i]->input.i_id == p_root->input.i_id )
425         {
426             REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
427         }
428         playlist_ItemDelete( p_root );
429     }
430     return VLC_SUCCESS;
431 }
432
433
434 /**
435  * Adds an item to the childs of a node
436  *
437  * \param p_playlist the playlist
438  * \param i_view the view of the node ( needed for parent search )
439  * \param p_item the item to append
440  * \param p_parent the parent node
441  * \return VLC_SUCCESS or an error
442  */
443 int playlist_NodeAppend( playlist_t *p_playlist,
444                          int i_view,
445                          playlist_item_t *p_item,
446                          playlist_item_t *p_parent )
447 {
448     return playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, -1 );
449 }
450
451 int playlist_NodeInsert( playlist_t *p_playlist,
452                          int i_view,
453                          playlist_item_t *p_item,
454                          playlist_item_t *p_parent,
455                          int i_position )
456 {
457    int i;
458    vlc_bool_t b_found = VLC_FALSE;
459    if( !p_parent || p_parent->i_children == -1 )
460    {
461         msg_Err( p_playlist, "invalid node" );
462         return VLC_EGENERIC;
463    }
464
465    if( i_position == -1 ) i_position = p_parent->i_children ;
466
467    INSERT_ELEM( p_parent->pp_children,
468                 p_parent->i_children,
469                 i_position,
470                 p_item );
471
472    /* Add the parent to the array */
473    for( i= 0; i< p_item->i_parents ; i++ )
474    {
475        if( p_item->pp_parents[i]->i_view == i_view )
476        {
477            b_found = VLC_TRUE;
478            break;
479        }
480    }
481    if( b_found == VLC_FALSE )
482    {
483         struct item_parent_t *p_ip = (struct item_parent_t *)
484                                      malloc(sizeof(struct item_parent_t) );
485         p_ip->i_view = i_view;
486         p_ip->p_parent = p_parent;
487
488         INSERT_ELEM( p_item->pp_parents,
489                      p_item->i_parents, p_item->i_parents,
490                      p_ip );
491    }
492
493    /* Let the interface know this has been updated */
494    p_parent->i_serial++;
495    return VLC_SUCCESS;
496 }
497
498 /**
499  * Deletes an item from the children of a node
500  *
501  * \param p_playlist the playlist
502  * \param p_item the item to remove
503  * \param p_parent the parent node
504  * \return VLC_SUCCESS or an error
505  */
506 int playlist_NodeRemoveItem( playlist_t *p_playlist,
507                         playlist_item_t *p_item,
508                         playlist_item_t *p_parent )
509 {
510    int i;
511    if( !p_parent || p_parent->i_children == -1 )
512    {
513         msg_Err( p_playlist, "invalid node" );
514    }
515
516    for( i= 0; i< p_parent->i_children ; i++ )
517    {
518        if( p_parent->pp_children[i] == p_item )
519        {
520            REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
521        }
522    }
523
524    /* Let the interface know this has been updated */
525    p_parent->i_serial++;
526
527    return VLC_SUCCESS;
528 }
529
530
531 /**
532  * Count the children of a node
533  *
534  * \param p_playlist the playlist
535  * \param p_node the node
536  * \return the number of children
537  */
538 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
539 {
540     int i;
541     int i_nb = 0;
542     if( p_node->i_children == -1 )
543     {
544         return 0;
545     }
546
547     for( i=0 ; i< p_node->i_children;i++ )
548     {
549         if( p_node->pp_children[i]->i_children == -1 )
550         {
551             i_nb++;
552         }
553         else
554         {
555             i_nb += playlist_NodeChildrenCount( p_playlist, 
556                                                 p_node->pp_children[i] );
557         }
558     }
559     return i_nb;
560 }
561
562 /**
563  * Search a child of a node by its name
564  *
565  * \param p_node the node
566  * \param psz_search the name of the child to search
567  * \return the child item or NULL if not found or error
568  */
569 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
570                                            const char *psz_search )
571 {
572     int i;
573
574     if( p_node->i_children < 0 )
575     {
576          return NULL;
577     }
578     for( i = 0 ; i< p_node->i_children; i++ )
579     {
580         if( !strcmp( p_node->pp_children[i]->input.psz_name, psz_search ) )
581         {
582             return p_node->pp_children[i];
583         }
584     }
585     return NULL;
586 }
587
588
589 /**********************************************************************
590  * Tree functions
591  **********************************************************************/
592
593 /**
594  * Finds the next item to play
595  *
596  * \param p_playlist the playlist
597  * \param i_view the view
598  * \param p_root the root node
599  * \param p_node the node we are playing from
600  * \param p_item the item we were playing (NULL if none )
601  * \return the next item to play, or NULL if none found
602  */
603 playlist_item_t *playlist_FindNextFromParent( playlist_t *p_playlist,
604                                         int i_view, /* FIXME: useless */
605                                         playlist_item_t *p_root,
606                                         playlist_item_t *p_node,
607                                         playlist_item_t *p_item )
608 {
609     playlist_item_t *p_search, *p_next;
610
611 #ifdef PLAYLIST_DEBUG
612     if( p_item != NULL )
613     {
614         msg_Dbg( p_playlist, "finding next of %s within %s - root %s",
615                         p_item->input.psz_name, p_node->input.psz_name,
616                         p_root->input.psz_name );
617     }
618     else
619     {
620         msg_Dbg( p_playlist, "finding something to play within %s -root %s",
621                             p_node->input.psz_name, p_root->input.psz_name );
622         msg_Dbg( p_playlist, "%s has %i children",
623                             p_node->input.psz_name, p_node->i_children );
624
625     }
626 #endif
627
628     if( !p_node  || p_node->i_children == -1 )
629     {
630         msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
631         return NULL;
632     }
633
634     /* Find the parent node of the item */
635     if( p_item != NULL )
636     {
637         p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
638         if( p_search == NULL )
639         {
640             msg_Err( p_playlist, "parent node not found" );
641             return NULL;
642         }
643     }
644     else
645     {
646         p_search = p_node;
647     }
648
649     /* Now, go up the tree until we find a suitable next item */
650     p_next = playlist_RecursiveFindNext( p_playlist,i_view,
651                                          p_node, p_item, p_search );
652
653     /* Not found, do we go past p_node ? */
654     if( p_next == NULL )
655     {
656         if( p_playlist->b_go_next )
657         {
658 #ifdef PLAYLIST_DEBUG
659             msg_Dbg( p_playlist, "Moving on to next node: search from %s",
660                             p_root->input.psz_name );
661 #endif
662             p_next = playlist_RecursiveFindNext( p_playlist, i_view,
663                                 p_root, p_item, p_search );
664             if( p_next == NULL )
665             {
666                 return NULL;
667             }
668             /* OK, we could continue, so set our current node to the root */
669             p_playlist->status.p_node = p_root;
670         }
671         else
672         {
673 #ifdef PLAYLIST_DEBUG
674             msg_Dbg( p_playlist, "Not moving on to next node: you loose" );
675 #endif
676             return NULL;
677         }
678     }
679     return p_next;
680 }
681
682 /**
683  * Finds the previous item to play
684  *
685  * \param p_playlist the playlist
686  * \param i_view the view
687  * \param p_root the root node
688  * \param p_node the node we are playing from
689  * \param p_item the item we were playing (NULL if none )
690  * \return the next item to play, or NULL if none found
691  */
692 playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
693                                         int i_view,
694                                         playlist_item_t *p_root,
695                                         playlist_item_t *p_node,
696                                         playlist_item_t *p_item )
697 {
698     playlist_item_t *p_search, *p_next;
699
700 #ifdef PLAYLIST_DEBUG
701     if( p_item != NULL )
702     {
703         msg_Dbg( p_playlist, "Finding prev of %s within %s",
704                         p_item->input.psz_name, p_node->input.psz_name );
705     }
706     else
707     {
708         msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
709     }
710 #endif
711
712     if( !p_node  || p_node->i_children == -1 )
713     {
714         msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
715         return NULL;
716     }
717
718     /* Find the parent node of the item */
719     if( p_item != NULL )
720     {
721         p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
722         if( p_search == NULL )
723         {
724             msg_Err( p_playlist, "parent node not found" );
725             return NULL;
726         }
727     }
728     else
729     {
730         p_search = p_node;
731     }
732
733     /* Now, go up the tree until we find a suitable next item */
734     p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
735                                          p_node, p_item, p_search );
736
737     if( p_next == NULL )
738     {
739         if( p_playlist->b_go_next )
740         {
741             p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
742                                 p_root, p_item, p_search );
743             if( p_next == NULL )
744             {
745                 return NULL;
746             }
747             /* OK, we could continue, so set our current node to the root */
748             p_playlist->status.p_node = p_root;
749         }
750         else
751         {
752             return NULL;
753         }
754     }
755     return p_next;
756 }
757
758 /************************************************************************
759  * Following functions are local
760  ***********************************************************************/
761
762
763 /* Recursively search the tree for next item */
764 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
765                 int i_view,
766                 playlist_item_t *p_root,
767                 playlist_item_t *p_item,
768                 playlist_item_t *p_parent )
769 {
770     int i;
771     playlist_item_t *p_parent_parent;
772
773     for( i= 0 ; i < p_parent->i_children ; i++ )
774     {
775         if( p_parent->pp_children[i] == p_item || p_item == NULL )
776         {
777             if( p_item == NULL )
778             {
779                 i = -1;
780             }
781 #ifdef PLAYLIST_DEBUG
782             msg_Dbg( p_playlist,"Current item found, child %i of %s",
783                                 i , p_parent->input.psz_name );
784 #endif
785             /* We found our item */
786             if( i+1 >= p_parent->i_children )
787             {
788                 /* Too far... */
789 #ifdef PLAYLIST_DEBUG
790                 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
791                                 p_parent->input.psz_name );
792 #endif
793                 if( p_parent == p_root )
794                 {
795 #ifdef PLAYLIST_DEBUG
796                     msg_Dbg( p_playlist, "At root item (%s)",
797                                          p_root->input.psz_name );
798 #endif
799                     /* Hmm, seems it's the end for you, guy ! */
800                     return NULL;
801                 }
802
803                 /* Go up one level */
804                 p_parent_parent = playlist_FindDirectParent( p_playlist,
805                                                              p_parent, i_view );
806                 if( p_parent_parent == NULL )
807                 {
808                     msg_Warn( p_playlist, "Unable to find parent !");
809                     return NULL;
810                 }
811                 return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
812                                                    p_parent, p_parent_parent );
813             }
814             else
815             {
816                 if( p_parent->pp_children[i+1]->i_children == -1 )
817                 {
818                     /* Cool, we have found a real item to play */
819 #ifdef PLAYLIST_DEBUG
820                     msg_Dbg( p_playlist, "Playing child %i of %s",
821                                      i+1 , p_parent->input.psz_name );
822 #endif
823                     return p_parent->pp_children[i+1];
824                 }
825                 else if( p_parent->pp_children[i+1]->i_children > 0 )
826                 {
827                     /* Select the first child of this node */
828 #ifdef PLAYLIST_DEBUG
829                     msg_Dbg( p_playlist, "%s is a node with children, "
830                                  "playing the first",
831                                   p_parent->pp_children[i+1]->input.psz_name);
832 #endif
833                     if( p_parent->pp_children[i+1]->pp_children[0]
834                                     ->i_children >= 0 )
835                     {
836                         /* first child is a node ! */
837                         return playlist_RecursiveFindNext( p_playlist, i_view,
838                                    p_root, NULL ,
839                                    p_parent->pp_children[i+1]->pp_children[0]);
840                     }
841                     return p_parent->pp_children[i+1]->pp_children[0];
842                 }
843                 else
844                 {
845                     /* This node has no child... We must continue */
846 #ifdef PLAYLIST_DEBUG
847                     msg_Dbg( p_playlist, "%s is a node with no children",
848                                  p_parent->pp_children[i+1]->input.psz_name);
849 #endif
850                     p_item = p_parent->pp_children[i+1];
851                 }
852             }
853         }
854     }
855     /* Just in case :) */
856     return NULL;
857 }
858
859 /* Recursively search the tree for previous item */
860 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
861                 int i_view,
862                 playlist_item_t *p_root,
863                 playlist_item_t *p_item,
864                 playlist_item_t *p_parent )
865 {
866     int i;
867     playlist_item_t *p_parent_parent;
868
869     for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
870     {
871         if( p_parent->pp_children[i] == p_item || p_item == NULL )
872         {
873             if( p_item == NULL )
874             {
875                 i = -1;
876             }
877 #ifdef PLAYLIST_DEBUG
878             msg_Dbg( p_playlist,"Current item found, child %i of %s",
879                              i , p_parent->input.psz_name );
880 #endif
881             /* We found our item */
882             if( i < 1 )
883             {
884                 /* Too far... */
885 #ifdef PLAYLIST_DEBUG
886                 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
887                                      p_parent->input.psz_name );
888 #endif
889                 if( p_parent == p_root )
890                 {
891 #ifdef PLAYLIST_DEBUG
892                     msg_Dbg( p_playlist, "At root item (%s)",
893                                          p_root->input.psz_name );
894 #endif
895                     /* Hmm, seems it's the end for you, guy ! */
896                     return NULL;
897                 }
898                 /* Go up one level */
899                 p_parent_parent = playlist_FindDirectParent( p_playlist,
900                                             p_parent, i_view );
901                 if( p_parent_parent == NULL )
902                 {
903 #ifdef PLAYLIST_DEBUG
904                     msg_Dbg( p_playlist, "Mmmh, couldn't find parent" );
905 #endif
906                     return NULL;
907                 }
908                 return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
909                                             p_parent, p_parent_parent );
910             }
911             else
912             {
913                 if( p_parent->pp_children[i-1]->i_children == -1 )
914                 {
915                     /* Cool, we have found a real item to play */
916 #ifdef PLAYLIST_DEBUG
917                     msg_Dbg( p_playlist, "Playing child %i of %s",
918                                      i-1, p_parent->input.psz_name );
919 #endif
920                     return p_parent->pp_children[i-1];
921                 }
922                 else if( p_parent->pp_children[i-1]->i_children > 0 )
923                 {
924                     /* Select the last child of this node */
925 #ifdef PLAYLIST_DEBUG
926                     msg_Dbg( p_playlist, "%s is a node with children,"
927                                    " playing the last",
928                                    p_parent->pp_children[i-1]->input.psz_name);
929 #endif
930                     if( p_parent->pp_children[i-1]->pp_children[p_parent->
931                             pp_children[i-1]->i_children-1]->i_children >= 0 )
932                     {
933                         /* Last child is a node */
934                         return playlist_RecursiveFindPrev( p_playlist, i_view,
935                                     p_root,NULL,
936                                     p_parent->pp_children[i-1]->pp_children[
937                                     p_parent->pp_children[i-1]->i_children-1]);
938                     }
939                     return p_parent->pp_children[i-1]->pp_children[
940                                  p_parent->pp_children[i-1]->i_children-1];
941                 }
942                 else
943                 {
944                     /* This node has no child... We must continue */
945 #ifdef PLAYLIST_DEBUG
946                     msg_Dbg( p_playlist, "%s is a node with no children",
947                                 p_parent->pp_children[i-1]->input.psz_name);
948 #endif
949                     p_item = p_parent->pp_children[i-1];
950                 }
951             }
952         }
953     }
954     return NULL;
955 }
956
957 /* This function returns the parent of an item in a view */
958 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
959                                          playlist_item_t *p_item,
960                                          int i_view )
961 {
962         int i = 0;
963         for( i= 0; i< p_item->i_parents ; i++ )
964         {
965             if( p_item->pp_parents[i]->i_view == i_view )
966             {
967                 return p_item->pp_parents[i]->p_parent;
968             }
969         }
970         return NULL;
971 }
972
973
974 #ifdef PLAYLIST_DEBUG
975 /* This function dumps a node : to be used only for debug*/
976 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
977                         int i_level )
978 {
979     char str[512];
980     int i;
981
982     if( i_level == 1 )
983     {
984         msg_Dbg( p_playlist, "%s (%i)",
985                         p_item->input.psz_name, p_item->i_children );
986     }
987
988     if( p_item->i_children == -1 )
989     {
990         return;
991     }
992
993     for( i = 0; i< p_item->i_children; i++ )
994     {
995         memset( str, 32, 512 );
996         sprintf( str + 2 * i_level , "%s (%i)",
997                                 p_item->pp_children[i]->input.psz_name,
998                                 p_item->pp_children[i]->i_children );
999         msg_Dbg( p_playlist, "%s",str );
1000         if( p_item->pp_children[i]->i_children >= 0 )
1001         {
1002             playlist_NodeDump( p_playlist, p_item->pp_children[i],
1003                               i_level + 1 );
1004         }
1005     }
1006     return;
1007 }
1008 #endif