]> git.sesse.net Git - vlc/blob - src/playlist/tree.c
libvlc: use vlc_common.h (libvlccore) instead of vlc/vlc.h
[vlc] / src / playlist / tree.c
1 /*****************************************************************************
2  * tree.c : Playlist tree walking functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <assert.h>
29 #include "vlc_playlist.h"
30 #include "playlist_internal.h"
31
32 /************************************************************************
33  * Local prototypes
34  ************************************************************************/
35 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
36                                playlist_item_t *p_root );
37 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
38                                playlist_item_t *p_root );
39
40 playlist_item_t *GetNextItem( playlist_t *p_playlist,
41                               playlist_item_t *p_root,
42                               playlist_item_t *p_item );
43 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
44                               playlist_item_t *p_item,
45                               playlist_item_t *p_root );
46
47 /**
48  * Create a playlist node
49  *
50  * \param p_playlist the playlist
51  * \param psz_name the name of the node
52  * \param p_parent the parent node to attach to or NULL if no attach
53  * \param p_flags miscellaneous flags
54  * \param p_input the input_item to attach to or NULL if it has to be created
55  * \return the new node
56  */
57 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
58                                        const char *psz_name,
59                                        playlist_item_t *p_parent, int i_flags,
60                                        input_item_t *p_input )
61 {
62     input_item_t *p_new_input;
63     playlist_item_t *p_item;
64
65     if( !psz_name ) psz_name = _("Undefined");
66
67     if( !p_input )
68         p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL,
69                                         psz_name, 0, NULL, -1, ITEM_TYPE_NODE );
70     p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist),
71                                         p_input ? p_input : p_new_input );
72     if( !p_input )
73         vlc_gc_decref( p_new_input );
74
75     if( p_item == NULL )  return NULL;
76     p_item->i_children = 0;
77
78     ARRAY_APPEND(p_playlist->all_items, p_item);
79
80     if( p_parent != NULL )
81         playlist_NodeAppend( p_playlist, p_item, p_parent );
82     playlist_SendAddNotify( p_playlist, p_item->i_id,
83                             p_parent ? p_parent->i_id : -1,
84                             !( i_flags & PLAYLIST_NO_REBUILD ));
85     return p_item;
86 }
87
88 /**
89  * Remove all the children of a node
90  *
91  * This function must be entered with the playlist lock
92  *
93  * \param p_playlist the playlist
94  * \param p_root the node
95  * \param b_delete_items do we have to delete the children items ?
96  * \return VLC_SUCCESS or an error
97  */
98 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
99                         bool b_delete_items )
100 {
101     int i;
102     if( p_root->i_children == -1 )
103     {
104         return VLC_EGENERIC;
105     }
106
107     /* Delete the children */
108     for( i =  p_root->i_children-1 ; i >= 0 ;i-- )
109     {
110         if( p_root->pp_children[i]->i_children > -1 )
111         {
112             playlist_NodeDelete( p_playlist, p_root->pp_children[i],
113                                  b_delete_items , false );
114         }
115         else if( b_delete_items )
116         {
117             /* Delete the item here */
118             playlist_DeleteFromItemId( p_playlist,
119                                        p_root->pp_children[i]->i_id );
120         }
121     }
122     return VLC_SUCCESS;
123 }
124
125 /**
126  * Remove all the children of a node and removes the node
127  *
128  * \param p_playlist the playlist
129  * \param p_root the node
130  * \param b_delete_items do we have to delete the children items ?
131  * \return VLC_SUCCESS or an error
132  */
133 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
134                          bool b_delete_items, bool b_force )
135 {
136     int i;
137
138     if( p_root->i_children == -1 )
139     {
140         return VLC_EGENERIC;
141     }
142
143     /* Delete the children */
144     for( i =  p_root->i_children - 1 ; i >= 0; i-- )
145     {
146         if( p_root->pp_children[i]->i_children > -1 )
147         {
148             playlist_NodeDelete( p_playlist, p_root->pp_children[i],
149                                  b_delete_items , b_force );
150         }
151         else if( b_delete_items )
152         {
153             playlist_DeleteFromItemId( p_playlist,
154                                        p_root->pp_children[i]->i_id );
155         }
156     }
157     /* Delete the node */
158     if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
159     {
160     }
161     else
162     {
163         int i;
164         var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
165         ARRAY_BSEARCH( p_playlist->all_items, ->i_id, int,
166                        p_root->i_id, i );
167         if( i != -1 )
168             ARRAY_REMOVE( p_playlist->all_items, i );
169
170         /* Remove the item from its parent */
171         if( p_root->p_parent )
172             playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
173
174         playlist_ItemDelete( p_root );
175     }
176     return VLC_SUCCESS;
177 }
178
179
180 /**
181  * Adds an item to the children of a node
182  *
183  * \param p_playlist the playlist
184  * \param p_item the item to append
185  * \param p_parent the parent node
186  * \return VLC_SUCCESS or an error
187  */
188 int playlist_NodeAppend( playlist_t *p_playlist,
189                          playlist_item_t *p_item,
190                          playlist_item_t *p_parent )
191 {
192     return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
193 }
194
195 int playlist_NodeInsert( playlist_t *p_playlist,
196                          playlist_item_t *p_item,
197                          playlist_item_t *p_parent,
198                          int i_position )
199 {
200    (void)p_playlist;
201    assert( p_parent && p_parent->i_children != -1 );
202    if( i_position == -1 ) i_position = p_parent->i_children ;
203
204    INSERT_ELEM( p_parent->pp_children,
205                 p_parent->i_children,
206                 i_position,
207                 p_item );
208    p_item->p_parent = p_parent;
209    return VLC_SUCCESS;
210 }
211
212 /**
213  * Deletes an item from the children of a node
214  *
215  * \param p_playlist the playlist
216  * \param p_item the item to remove
217  * \param p_parent the parent node
218  * \return VLC_SUCCESS or an error
219  */
220 int playlist_NodeRemoveItem( playlist_t *p_playlist,
221                         playlist_item_t *p_item,
222                         playlist_item_t *p_parent )
223 {
224    (void)p_playlist;
225
226    for(int i= 0; i< p_parent->i_children ; i++ )
227    {
228        if( p_parent->pp_children[i] == p_item )
229        {
230            REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
231        }
232    }
233
234    return VLC_SUCCESS;
235 }
236
237
238 /**
239  * Count the children of a node
240  *
241  * \param p_playlist the playlist
242  * \param p_node the node
243  * \return the number of children
244  */
245 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
246 {
247     int i;
248     int i_nb = 0;
249
250     if( p_node->i_children == -1 )
251         return 0;
252
253     i_nb = p_node->i_children;
254     for( i=0 ; i< p_node->i_children;i++ )
255     {
256         if( p_node->pp_children[i]->i_children == -1 )
257             break;
258         else
259             i_nb += playlist_NodeChildrenCount( p_playlist,
260                                                 p_node->pp_children[i] );
261     }
262     return i_nb;
263 }
264
265 /**
266  * Search a child of a node by its name
267  *
268  * \param p_node the node
269  * \param psz_search the name of the child to search
270  * \return the child item or NULL if not found or error
271  */
272 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
273                                            const char *psz_search )
274 {
275     int i;
276
277     if( p_node->i_children < 0 )
278     {
279          return NULL;
280     }
281     for( i = 0 ; i< p_node->i_children; i++ )
282     {
283         if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
284         {
285             return p_node->pp_children[i];
286         }
287     }
288     return NULL;
289 }
290
291 /**
292  * Create a pair of nodes in the category and onelevel trees.
293  * They share the same input item.
294  * \param p_playlist the playlist
295  * \param psz_name the name of the nodes
296  * \param pp_node_cat pointer to return the node in category tree
297  * \param pp_node_one pointer to return the node in onelevel tree
298  * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
299  */
300 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
301                                playlist_item_t **pp_node_cat,
302                                playlist_item_t **pp_node_one,
303                                bool b_for_sd )
304 {
305     *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
306                                         p_playlist->p_root_category, 0, NULL );
307     *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
308                                         p_playlist->p_root_onelevel, 0,
309                                         (*pp_node_cat)->p_input );
310     if( b_for_sd )
311     {
312         (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
313         (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
314         (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
315         (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
316     }
317 }
318
319 /**
320  * Get the node in the preferred tree from a node in one of category
321  * or onelevel tree.
322  */
323 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
324                                              playlist_item_t *p_node )
325 {
326     int i;
327     if( p_node->p_parent == p_playlist->p_root_category )
328     {
329         if( p_playlist->b_tree )
330             return p_node;
331         for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
332         {
333             if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
334                     p_node->p_input->i_id )
335                 return p_playlist->p_root_onelevel->pp_children[i];
336         }
337     }
338     else if( p_node->p_parent == p_playlist->p_root_onelevel )
339     {
340         if( !p_playlist->b_tree )
341             return p_node;
342         for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
343         {
344             if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
345                     p_node->p_input->i_id )
346                 return p_playlist->p_root_category->pp_children[i];
347         }
348     }
349     return NULL;
350 }
351
352 /**********************************************************************
353  * Tree walking functions
354  **********************************************************************/
355
356 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
357                                       playlist_item_t *p_root )
358 {
359     int i;
360     playlist_item_t *p_item;
361     for ( i = p_root->i_children - 1; i >= 0; i-- )
362     {
363         if( p_root->pp_children[i]->i_children == -1 )
364             return p_root->pp_children[i];
365         else if( p_root->pp_children[i]->i_children > 0)
366         {
367              p_item = playlist_GetLastLeaf( p_playlist,
368                                             p_root->pp_children[i] );
369             if ( p_item != NULL )
370                 return p_item;
371         }
372         else if( i == 0 )
373             return NULL;
374     }
375     return NULL;
376 }
377
378 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
379                                     playlist_item_t *p_node,
380                                     playlist_item_t ***ppp_items )
381 {
382     int i_count = 0;
383     playlist_item_t *p_next = NULL;
384     while( 1 )
385     {
386         p_next = playlist_GetNextLeaf( p_playlist, p_node,
387                                        p_next, true, false );
388         if( p_next )
389             INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
390         else
391             break;
392     }
393     return i_count;
394 }
395
396 /**
397  * Finds the next item to play
398  *
399  * \param p_playlist the playlist
400  * \param p_root the root node
401  * \param p_item the previous item  (NULL if none )
402  * \return the next item to play, or NULL if none found
403  */
404 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
405                                        playlist_item_t *p_root,
406                                        playlist_item_t *p_item,
407                                        bool b_ena, bool b_unplayed )
408 {
409     playlist_item_t *p_next;
410
411     assert( p_root && p_root->i_children != -1 );
412
413     PL_DEBUG2( "finding next of %s within %s",
414                PLI_NAME( p_item ), PLI_NAME( p_root ) );
415
416     /* Now, walk the tree until we find a suitable next item */
417     p_next = p_item;
418     while( 1 )
419     {
420         bool b_ena_ok = true, b_unplayed_ok = true;
421         p_next = GetNextItem( p_playlist, p_root, p_next );
422         if( !p_next || p_next == p_root )
423             break;
424         if( p_next->i_children == -1 )
425         {
426             if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
427                 b_ena_ok = false;
428             if( b_unplayed && p_next->p_input->i_nb_played != 0 )
429                 b_unplayed_ok = false;
430             if( b_ena_ok && b_unplayed_ok ) break;
431         }
432     }
433     if( p_next == NULL ) PL_DEBUG2( "at end of node" );
434     return p_next;
435 }
436
437 /**
438  * Finds the previous item to play
439  *
440  * \param p_playlist the playlist
441  * \param p_root the root node
442  * \param p_item the previous item  (NULL if none )
443  * \return the next item to play, or NULL if none found
444  */
445 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
446                                        playlist_item_t *p_root,
447                                        playlist_item_t *p_item,
448                                        bool b_ena, bool b_unplayed )
449 {
450     playlist_item_t *p_prev;
451
452     PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
453                                                    PLI_NAME( p_root ) );
454     assert( p_root && p_root->i_children != -1 );
455
456     /* Now, walk the tree until we find a suitable previous item */
457     p_prev = p_item;
458     while( 1 )
459     {
460         bool b_ena_ok = true, b_unplayed_ok = true;
461         p_prev = GetPrevItem( p_playlist, p_root, p_prev );
462         if( !p_prev || p_prev == p_root )
463             break;
464         if( p_prev->i_children == -1 )
465         {
466             if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
467                 b_ena_ok = false;
468             if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
469                 b_unplayed_ok = false;
470             if( b_ena_ok && b_unplayed_ok ) break;
471         }
472     }
473     if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
474     return p_prev;
475 }
476
477 /************************************************************************
478  * Following functions are local
479  ***********************************************************************/
480
481 /**
482  * Get the next item in the tree
483  * If p_item is NULL, return the first child of root
484  **/
485 playlist_item_t *GetNextItem( playlist_t *p_playlist,
486                               playlist_item_t *p_root,
487                               playlist_item_t *p_item )
488 {
489     playlist_item_t *p_parent;
490     int i;
491
492     /* Node with children, get the first one */
493     if( p_item && p_item->i_children > 0 )
494         return p_item->pp_children[0];
495
496     if( p_item != NULL )
497         p_parent = p_item->p_parent;
498     else
499         p_parent = p_root;
500     for( i= 0 ; i < p_parent->i_children ; i++ )
501     {
502         if( p_item == NULL || p_parent->pp_children[i] == p_item )
503         {
504             if( p_item == NULL )
505                 i = -1;
506
507             if( i+1 >= p_parent->i_children )
508             {
509                 /* Was already the last sibling. Look for uncles */
510                 PL_DEBUG2( "Current item is the last of the node,"
511                            "looking for uncle from %s",
512                             p_parent->p_input->psz_name );
513
514                 if( p_parent == p_root )
515                 {
516                     PL_DEBUG2( "already at root" );
517                     return NULL;
518                 }
519                 return GetNextUncle( p_playlist, p_item, p_root );
520             }
521             else
522             {
523                 return  p_parent->pp_children[i+1];
524             }
525         }
526     }
527     return NULL;
528 }
529
530 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
531                                playlist_item_t *p_root )
532 {
533     playlist_item_t *p_parent = p_item->p_parent;
534     playlist_item_t *p_grandparent;
535     bool b_found = false;
536
537     (void)p_playlist;
538
539     if( p_parent != NULL )
540     {
541         p_grandparent = p_parent->p_parent;
542         while( p_grandparent )
543         {
544             int i;
545             for( i = 0 ; i< p_grandparent->i_children ; i++ )
546             {
547                 if( p_parent == p_grandparent->pp_children[i] )
548                 {
549                     PL_DEBUG2( "parent %s found as child %i of grandparent %s",
550                                p_parent->p_input->psz_name, i,
551                                p_grandparent->p_input->psz_name );
552                     b_found = true;
553                     break;
554                 }
555             }
556             if( b_found && i + 1 < p_grandparent->i_children )
557             {
558                     return p_grandparent->pp_children[i+1];
559             }
560             /* Not found at root */
561             if( p_grandparent == p_root )
562             {
563                 return NULL;
564             }
565             else
566             {
567                 p_parent = p_grandparent;
568                 p_grandparent = p_parent->p_parent;
569             }
570         }
571     }
572     /* We reached root */
573     return NULL;
574 }
575
576 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
577                                playlist_item_t *p_root )
578 {
579     playlist_item_t *p_parent = p_item->p_parent;
580     playlist_item_t *p_grandparent;
581     bool b_found = false;
582
583     (void)p_playlist;
584
585     if( p_parent != NULL )
586     {
587         p_grandparent = p_parent->p_parent;
588         while( 1 )
589         {
590             int i;
591             for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
592             {
593                 if( p_parent == p_grandparent->pp_children[i] )
594                 {
595                     b_found = true;
596                     break;
597                 }
598             }
599             if( b_found && i - 1 > 0 )
600             {
601                 return p_grandparent->pp_children[i-1];
602             }
603             /* Not found at root */
604             if( p_grandparent == p_root )
605             {
606                 return NULL;
607             }
608             else
609             {
610                 p_parent = p_grandparent;
611                 p_grandparent = p_parent->p_parent;
612             }
613         }
614     }
615     /* We reached root */
616     return NULL;
617 }
618
619
620 /* Recursively search the tree for previous item */
621 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
622                               playlist_item_t *p_root,
623                               playlist_item_t *p_item )
624 {
625     playlist_item_t *p_parent;
626     int i;
627
628     /* Node with children, get the last one */
629     if( p_item && p_item->i_children > 0 )
630         return p_item->pp_children[p_item->i_children - 1];
631
632     /* Last child of its parent ? */
633     if( p_item != NULL )
634         p_parent = p_item->p_parent;
635     else
636     {
637         msg_Err( p_playlist, "Get the last one" );
638         abort();
639     };
640
641     for( i = p_parent->i_children -1 ; i >= 0 ;  i-- )
642     {
643         if( p_parent->pp_children[i] == p_item )
644         {
645             if( i-1 < 0 )
646             {
647                /* Was already the first sibling. Look for uncles */
648                 PL_DEBUG2( "current item is the first of its node,"
649                            "looking for uncle from %s",
650                            p_parent->p_input->psz_name );
651                 if( p_parent == p_root )
652                 {
653                     PL_DEBUG2( "already at root" );
654                     return NULL;
655                 }
656                 return GetPrevUncle( p_playlist, p_item, p_root );
657             }
658             else
659             {
660                 return p_parent->pp_children[i-1];
661             }
662         }
663     }
664     return NULL;
665 }