]> git.sesse.net Git - vlc/blob - src/playlist/view.c
1521c3fa545414661c103486abd0430d2b05bd24
[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
623     }
624 #endif
625
626     if( !p_node  || p_node->i_children == -1 )
627     {
628         msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
629         return NULL;
630     }
631
632     /* Find the parent node of the item */
633     if( p_item != NULL )
634     {
635         p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
636         if( p_search == NULL )
637         {
638             msg_Err( p_playlist, "parent node not found" );
639             return NULL;
640         }
641     }
642     else
643     {
644         p_search = p_node;
645     }
646
647     /* Now, go up the tree until we find a suitable next item */
648     p_next = playlist_RecursiveFindNext( p_playlist,i_view,
649                                          p_node, p_item, p_search );
650
651     /* Not found, do we go past p_node ? */
652     if( p_next == NULL )
653     {
654         if( p_playlist->b_go_next )
655         {
656 #ifdef PLAYLIST_DEBUG
657             msg_Dbg( p_playlist, "Moving on to next node: search from %s",
658                             p_root->input.psz_name );
659 #endif
660             p_next = playlist_RecursiveFindNext( p_playlist, i_view,
661                                 p_root, p_item, p_search );
662             if( p_next == NULL )
663             {
664                 return NULL;
665             }
666             /* OK, we could continue, so set our current node to the root */
667             p_playlist->status.p_node = p_root;
668         }
669         else
670         {
671 #ifdef PLAYLIST_DEBUG
672             msg_Dbg( p_playlist, "Not moving on to next node: you loose" );
673 #endif
674             return NULL;
675         }
676     }
677     return p_next;
678 }
679
680 /**
681  * Finds the previous item to play
682  *
683  * \param p_playlist the playlist
684  * \param i_view the view
685  * \param p_root the root node
686  * \param p_node the node we are playing from
687  * \param p_item the item we were playing (NULL if none )
688  * \return the next item to play, or NULL if none found
689  */
690 playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
691                                         int i_view,
692                                         playlist_item_t *p_root,
693                                         playlist_item_t *p_node,
694                                         playlist_item_t *p_item )
695 {
696     playlist_item_t *p_search, *p_next;
697
698 #ifdef PLAYLIST_DEBUG
699     if( p_item != NULL )
700     {
701         msg_Dbg( p_playlist, "Finding prev of %s within %s",
702                         p_item->input.psz_name, p_node->input.psz_name );
703     }
704     else
705     {
706         msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
707     }
708 #endif
709
710     if( !p_node  || p_node->i_children == -1 )
711     {
712         msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
713         return NULL;
714     }
715
716     /* Find the parent node of the item */
717     if( p_item != NULL )
718     {
719         p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
720         if( p_search == NULL )
721         {
722             msg_Err( p_playlist, "parent node not found" );
723             return NULL;
724         }
725     }
726     else
727     {
728         p_search = p_node;
729     }
730
731     /* Now, go up the tree until we find a suitable next item */
732     p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
733                                          p_node, p_item, p_search );
734
735     if( p_next == NULL )
736     {
737         if( p_playlist->b_go_next )
738         {
739             p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
740                                 p_root, p_item, p_search );
741             if( p_next == NULL )
742             {
743                 return NULL;
744             }
745             /* OK, we could continue, so set our current node to the root */
746             p_playlist->status.p_node = p_root;
747         }
748         else
749         {
750             return NULL;
751         }
752     }
753     return p_next;
754 }
755
756 /************************************************************************
757  * Following functions are local
758  ***********************************************************************/
759
760
761 /* Recursively search the tree for next item */
762 playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
763                 int i_view,
764                 playlist_item_t *p_root,
765                 playlist_item_t *p_item,
766                 playlist_item_t *p_parent )
767 {
768     int i;
769     playlist_item_t *p_parent_parent;
770
771     for( i= 0 ; i < p_parent->i_children ; i++ )
772     {
773         if( p_parent->pp_children[i] == p_item || p_item == NULL )
774         {
775             if( p_item == NULL )
776             {
777                 i = -1;
778             }
779 #ifdef PLAYLIST_DEBUG
780             msg_Dbg( p_playlist,"Current item found, child %i of %s",
781                                 i , p_parent->input.psz_name );
782 #endif
783             /* We found our item */
784             if( i+1 >= p_parent->i_children )
785             {
786                 /* Too far... */
787 #ifdef PLAYLIST_DEBUG
788                 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
789                                 p_parent->input.psz_name );
790 #endif
791                 if( p_parent == p_root )
792                 {
793 #ifdef PLAYLIST_DEBUG
794                     msg_Dbg( p_playlist, "At root item (%s)",
795                                          p_root->input.psz_name );
796 #endif
797                     /* Hmm, seems it's the end for you, guy ! */
798                     return NULL;
799                 }
800
801                 /* Go up one level */
802                 p_parent_parent = playlist_FindDirectParent( p_playlist,
803                                                              p_parent, i_view );
804                 if( p_parent_parent == NULL )
805                 {
806                     msg_Warn( p_playlist, "Unable to find parent !");
807                     return NULL;
808                 }
809                 return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
810                                                    p_parent, p_parent_parent );
811             }
812             else
813             {
814                 if( p_parent->pp_children[i+1]->i_children == -1 )
815                 {
816                     /* Cool, we have found a real item to play */
817 #ifdef PLAYLIST_DEBUG
818                     msg_Dbg( p_playlist, "Playing child %i of %s",
819                                      i+1 , p_parent->input.psz_name );
820 #endif
821                     return p_parent->pp_children[i+1];
822                 }
823                 else if( p_parent->pp_children[i+1]->i_children > 0 )
824                 {
825                     /* Select the first child of this node */
826 #ifdef PLAYLIST_DEBUG
827                     msg_Dbg( p_playlist, "%s is a node with children, "
828                                  "playing the first",
829                                   p_parent->pp_children[i+1]->input.psz_name);
830 #endif
831                     if( p_parent->pp_children[i+1]->pp_children[0]
832                                     ->i_children >= 0 )
833                     {
834                         /* first child is a node ! */
835                         return playlist_RecursiveFindNext( p_playlist, i_view,
836                                    p_root, NULL ,
837                                    p_parent->pp_children[i+1]->pp_children[0]);
838                     }
839                     return p_parent->pp_children[i+1]->pp_children[0];
840                 }
841                 else
842                 {
843                     /* This node has no child... We must continue */
844 #ifdef PLAYLIST_DEBUG
845                     msg_Dbg( p_playlist, "%s is a node with no children",
846                                  p_parent->pp_children[i+1]->input.psz_name);
847 #endif
848                     p_item = p_parent->pp_children[i+1];
849                 }
850             }
851         }
852     }
853     /* Just in case :) */
854     return NULL;
855 }
856
857 /* Recursively search the tree for previous item */
858 playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
859                 int i_view,
860                 playlist_item_t *p_root,
861                 playlist_item_t *p_item,
862                 playlist_item_t *p_parent )
863 {
864     int i;
865     playlist_item_t *p_parent_parent;
866
867     for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
868     {
869         if( p_parent->pp_children[i] == p_item || p_item == NULL )
870         {
871             if( p_item == NULL )
872             {
873                 i = -1;
874             }
875 #ifdef PLAYLIST_DEBUG
876             msg_Dbg( p_playlist,"Current item found, child %i of %s",
877                              i , p_parent->input.psz_name );
878 #endif
879             /* We found our item */
880             if( i < 1 )
881             {
882                 /* Too far... */
883 #ifdef PLAYLIST_DEBUG
884                 msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
885                                      p_parent->input.psz_name );
886 #endif
887                 if( p_parent == p_root )
888                 {
889 #ifdef PLAYLIST_DEBUG
890                     msg_Dbg( p_playlist, "At root item (%s)",
891                                          p_root->input.psz_name );
892 #endif
893                     /* Hmm, seems it's the end for you, guy ! */
894                     return NULL;
895                 }
896                 /* Go up one level */
897                 p_parent_parent = playlist_FindDirectParent( p_playlist,
898                                             p_parent, i_view );
899                 if( p_parent_parent == NULL )
900                 {
901 #ifdef PLAYLIST_DEBUG
902                     msg_Dbg( p_playlist, "Mmmh, couldn't find parent" );
903 #endif
904                     return NULL;
905                 }
906                 return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
907                                             p_parent, p_parent_parent );
908             }
909             else
910             {
911                 if( p_parent->pp_children[i-1]->i_children == -1 )
912                 {
913                     /* Cool, we have found a real item to play */
914 #ifdef PLAYLIST_DEBUG
915                     msg_Dbg( p_playlist, "Playing child %i of %s",
916                                      i-1, p_parent->input.psz_name );
917 #endif
918                     return p_parent->pp_children[i-1];
919                 }
920                 else if( p_parent->pp_children[i-1]->i_children > 0 )
921                 {
922                     /* Select the last child of this node */
923 #ifdef PLAYLIST_DEBUG
924                     msg_Dbg( p_playlist, "%s is a node with children,"
925                                    " playing the last",
926                                    p_parent->pp_children[i-1]->input.psz_name);
927 #endif
928                     if( p_parent->pp_children[i-1]->pp_children[p_parent->
929                             pp_children[i-1]->i_children-1]->i_children >= 0 )
930                     {
931                         /* Last child is a node */
932                         return playlist_RecursiveFindPrev( p_playlist, i_view,
933                                     p_root,NULL,
934                                     p_parent->pp_children[i-1]->pp_children[
935                                     p_parent->pp_children[i-1]->i_children-1]);
936                     }
937                     return p_parent->pp_children[i-1]->pp_children[
938                                  p_parent->pp_children[i-1]->i_children-1];
939                 }
940                 else
941                 {
942                     /* This node has no child... We must continue */
943 #ifdef PLAYLIST_DEBUG
944                     msg_Dbg( p_playlist, "%s is a node with no children",
945                                 p_parent->pp_children[i-1]->input.psz_name);
946 #endif
947                     p_item = p_parent->pp_children[i-1];
948                 }
949             }
950         }
951     }
952     return NULL;
953 }
954
955 /* This function returns the parent of an item in a view */
956 playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
957                                          playlist_item_t *p_item,
958                                          int i_view )
959 {
960         int i = 0;
961         for( i= 0; i< p_item->i_parents ; i++ )
962         {
963             if( p_item->pp_parents[i]->i_view == i_view )
964             {
965                 return p_item->pp_parents[i]->p_parent;
966             }
967         }
968         return NULL;
969 }
970
971
972 #ifdef PLAYLIST_DEBUG
973 /* This function dumps a node : to be used only for debug*/
974 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
975                         int i_level )
976 {
977     char str[512];
978     int i;
979
980     if( i_level == 1 )
981     {
982         msg_Dbg( p_playlist, "%s (%i)",
983                         p_item->input.psz_name, p_item->i_children );
984     }
985
986     if( p_item->i_children == -1 )
987     {
988         return;
989     }
990
991     for( i = 0; i< p_item->i_children; i++ )
992     {
993         memset( str, 32, 512 );
994         sprintf( str + 2 * i_level , "%s (%i)",
995                                 p_item->pp_children[i]->input.psz_name,
996                                 p_item->pp_children[i]->i_children );
997         msg_Dbg( p_playlist, "%s",str );
998         if( p_item->pp_children[i]->i_children >= 0 )
999         {
1000             playlist_NodeDump( p_playlist, p_item->pp_children[i],
1001                               i_level + 1 );
1002         }
1003     }
1004     return;
1005 }
1006 #endif