view.c 30.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * view.c : Playlist views functions
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * $Id: item.c 7997 2004-06-18 11:35:45Z sigmunau $
 *
 * Authors: Clment Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */

#include <vlc/vlc.h>
#include <vlc/input.h>

#include "vlc_playlist.h"

Clément Stenac's avatar
Clément Stenac committed
32 33
#undef PLAYLIST_DEBUG

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/************************************************************************
 * Local prototypes
 ************************************************************************/

/* TODO: inline */
playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
                                        playlist_item_t *, int );

playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
                int i_view,
                playlist_item_t *p_root,
                playlist_item_t *p_item,
                playlist_item_t *p_parent );

playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
                int i_view,
                playlist_item_t *p_root,
                playlist_item_t *p_item,
                playlist_item_t *p_parent );

void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
                        int i_level );

/**********************************************************************
 * Exported View management functions
 **********************************************************************/

/**
 * Create a new view
 *
 * \param p_playlist a playlist object
 * \param i_id the view identifier
 * \return the new view or NULL on failure
 */
playlist_view_t * playlist_ViewCreate( playlist_t *p_playlist, int i_id,
                                       char *psz_name )
{
    playlist_view_t * p_view;

    p_view = malloc( sizeof( playlist_view_t ) );

    memset( p_view, 0, sizeof( playlist_view_t ) );

    p_view->p_root = playlist_NodeCreate( p_playlist, i_id, NULL, NULL );
    p_view->i_id = i_id;
    p_view->psz_name = psz_name ? strdup( psz_name ) : strdup(_("Undefined") );

    return p_view;
}

/**
 * Creates a new view and add it to the list
 *
87 88
 * This function must be entered without the playlist lock
 *
89 90 91 92 93 94
 * \param p_playlist a playlist object
 * \param i_id the view identifier
 * \return VLC_SUCCESS or an error
 */
int playlist_ViewInsert( playlist_t *p_playlist, int i_id, char *psz_name )
{
95 96
    playlist_view_t *p_view =
        playlist_ViewCreate( p_playlist, i_id , psz_name );
97 98 99 100 101 102 103 104
    if( !p_view )
    {
        msg_Err( p_playlist, "Creation failed" );
        return VLC_EGENERIC;
    }

    vlc_mutex_lock( &p_playlist->object_lock );

105 106
    INSERT_ELEM( p_playlist->pp_views, p_playlist->i_views,
                 p_playlist->i_views, p_view );
107 108

    vlc_mutex_unlock( &p_playlist->object_lock );
109
    return VLC_SUCCESS;
110 111 112 113 114 115
}


/**
 * Deletes a view
 *
116 117
 * This function must be entered wit the playlist lock
 *
118 119 120
 * \param p_view the view to delete
 * \return nothing
 */
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 131 132 133 134 135
/**
 * Dumps the content of a view
 *
 * \param p_playlist the playlist
 * \param p_view the view to dump
 * \return nothing
 */
136
int playlist_ViewDump( playlist_t *p_playlist, playlist_view_t *p_view )
137
{
138
#ifdef PLAYLIST_DEBUG
Clément Stenac's avatar
Clément Stenac committed
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

/**
 * Counts the items of a view
 *
 * \param p_playlist the playlist
 * \param p_view the view to count
 * \return the number of items
 */
int playlist_ViewItemCount( playlist_t *p_playlist,
                            playlist_view_t *p_view )
{
    return playlist_NodeChildrenCount( p_playlist, p_view->p_root );
}


/**
 * Updates a view. Only make sense for "sorted" and "ALL" views
 *
 * \param p_playlist the playlist
 * \param i_view the view to update
 * \return nothing
 */
int playlist_ViewUpdate( playlist_t *p_playlist, int i_view)
{
    playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );

    if( p_view == NULL )
    {
        return VLC_EGENERIC;
    }

    if( i_view == VIEW_ALL )
    {
        p_view->p_root->i_children = p_playlist->i_size;
        p_view->p_root->pp_children = p_playlist->pp_items;
    }

    /* Handle update of sorted views here */
182
    if( i_view >= VIEW_FIRST_SORTED )
183
    {
184
        int i_sort_type;
185 186
        playlist_ViewEmpty( p_playlist, i_view, VLC_FALSE );

187 188 189
        switch( i_view )
        {
            case VIEW_S_AUTHOR: i_sort_type = SORT_AUTHOR;break;
190
            case VIEW_S_ALBUM: i_sort_type = SORT_ALBUM;break;
191 192 193
            case VIEW_S_GENRE: i_sort_type = SORT_GENRE;break;
            default: i_sort_type = SORT_AUTHOR;
        }
194 195
        playlist_NodeGroup( p_playlist, i_view, p_view->p_root,
                            p_playlist->pp_items,p_playlist->i_size,
196
                            i_sort_type, ORDER_NORMAL );
197 198
    }

Clément Stenac's avatar
Clément Stenac committed
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    return VLC_SUCCESS;
}


/**
 * Find a view
 *
 * \param p_playlist the playlist
 * \param i_id the id to find
 * \return the found view or NULL if not found
 */
playlist_view_t *playlist_ViewFind( playlist_t *p_playlist, int i_id )
{
    int i;
    for( i=0 ; i< p_playlist->i_views ; i++ )
    {
        if( p_playlist->pp_views[i]->i_id == i_id )
        {
            return p_playlist->pp_views[i];
        }
    }
    return NULL;
}


int playlist_ViewEmpty( playlist_t *p_playlist, int i_view,
                        vlc_bool_t b_delete_items )
{
    playlist_view_t *p_view = playlist_ViewFind( p_playlist, i_view );

    if( p_view == NULL )
    {
        return VLC_EGENERIC;
    }

    return playlist_NodeEmpty( p_playlist, p_view->p_root, b_delete_items );
}

/**********************************************************************
 * Exported Nodes management functions
 **********************************************************************/



/**
 * Create a playlist node
 *
 * \param p_playlist the playlist
 * \paam psz_name the name of the node
 * \param p_parent the parent node to attach to or NULL if no attach
 * \return the new node
 */
playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, int i_view,
                                       char *psz_name,
                                       playlist_item_t *p_parent )
{
    /* Create the item */
    playlist_item_t *p_item = (playlist_item_t *)malloc(
                                        sizeof( playlist_item_t ) );
259
    vlc_value_t val;
260
    playlist_add_t *p_add;
261 262 263 264

    if( p_item == NULL )
    {
        return NULL;
265 266 267 268 269 270
    }
    p_add = (playlist_add_t*)malloc( sizeof(playlist_add_t) );
    if( p_add == NULL )
    {
        free( p_item );
        return NULL;
271
    }
272
    vlc_input_item_Init( VLC_OBJECT(p_playlist), &p_item->input );
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

    if( psz_name != NULL )
    {
        p_item->input.psz_name = strdup( psz_name );
    }
    else
    {
        p_item->input.psz_name = strdup( _("Undefined") );
    }

    p_item->input.psz_uri = NULL;

    p_item->b_enabled = VLC_TRUE;
    p_item->i_nb_played = 0;

    p_item->i_flags = 0;

    p_item->i_children = 0;
    p_item->pp_children = NULL;

    p_item->input.i_duration = -1;
    p_item->input.ppsz_options = NULL;
    p_item->input.i_options = 0;
    p_item->input.i_categories = 0;
    p_item->input.pp_categories = NULL;
298
    p_item->input.i_id = ++p_playlist->i_last_id;
299

Clément Stenac's avatar
Clément Stenac committed
300 301
    p_item->input.i_type = ITEM_TYPE_NODE;

302 303 304 305 306 307 308
    p_item->pp_parents = NULL;
    p_item->i_parents = 0;

    p_item->i_flags |= PLAYLIST_SKIP_FLAG; /* Default behaviour */

    vlc_mutex_init( p_playlist, &p_item->input.lock );

309 310 311 312 313
    INSERT_ELEM( p_playlist->pp_all_items,
                 p_playlist->i_all_size,
                 p_playlist->i_all_size,
                 p_item );

314 315 316 317 318
    if( p_parent != NULL )
    {
        playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
    }

Clément Stenac's avatar
Clément Stenac committed
319 320
    p_add->i_node = p_parent ? p_parent->input.i_id : -1;
    p_add->i_item = p_item->input.i_id;
321 322 323 324
    p_add->i_view = i_view;
    val.p_address = p_add;
    var_Set( p_playlist, "item-append", val);

Clément Stenac's avatar
Clément Stenac committed
325 326
    free( p_add );

327 328 329 330 331 332
    return p_item;
}

/**
 * Remove all the children of a node
 *
333 334
 * This function must be entered with the playlist lock
 *
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
 * \param p_playlist the playlist
 * \param p_root the node
 * \param b_delete_items do we have to delete the children items ?
 * \return VLC_SUCCESS or an error
 */
int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
                        vlc_bool_t b_delete_items )
{
    int i;
    if( p_root->i_children == -1 )
    {
        return VLC_EGENERIC;
    }

    /* Delete the children */
    for( i =  p_root->i_children-1 ; i >= 0 ;i-- )
    {
        if( p_root->pp_children[i]->i_children > -1 )
        {
            playlist_NodeDelete( p_playlist, p_root->pp_children[i],
355
                                 b_delete_items , VLC_FALSE );
356 357 358 359
        }
        else if( b_delete_items )
        {
            /* Delete the item here */
Clément Stenac's avatar
Clément Stenac committed
360
            playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
361 362 363 364 365 366 367 368 369 370 371 372 373 374
        }
    }
    return VLC_SUCCESS;
}

/**
 * Remove all the children of a node and removes the node
 *
 * \param p_playlist the playlist
 * \param p_root the node
 * \param b_delete_items do we have to delete the children items ?
 * \return VLC_SUCCESS or an error
 */
int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
375
                         vlc_bool_t b_delete_items, vlc_bool_t b_force )
376
{
377
    int i, i_top, i_bottom;
378 379 380 381 382 383 384 385 386 387 388
    if( p_root->i_children == -1 )
    {
        return VLC_EGENERIC;
    }

    /* Delete the children */
    for( i =  p_root->i_children - 1 ; i >= 0; i-- )
    {
        if( p_root->pp_children[i]->i_children > -1 )
        {
            playlist_NodeDelete( p_playlist, p_root->pp_children[i],
389
                                b_delete_items , b_force );
390 391 392 393
        }
        else if( b_delete_items )
        {
            /* Delete the item here */
Clément Stenac's avatar
Clément Stenac committed
394
            playlist_Delete( p_playlist, p_root->pp_children[i]->input.i_id );
395 396 397
        }
    }
    /* Delete the node */
398
    if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
399 400
    {
    }
401 402 403 404 405 406 407 408
    else
    {
        for( i = 0 ; i< p_root->i_parents; i++ )
        {
            playlist_NodeRemoveItem( p_playlist, p_root,
                                     p_root->pp_parents[i]->p_parent );
        }
        var_SetInteger( p_playlist, "item-deleted", p_root->input.i_id );
409

410
        i_bottom = 0; i_top = p_playlist->i_all_size - 1;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        i = i_top / 2;
        while( p_playlist->pp_all_items[i]->input.i_id != p_root->input.i_id &&
               i_top > i_bottom )
        {
            if( p_playlist->pp_all_items[i]->input.i_id < p_root->input.i_id )
            {
                i_bottom = i + 1;
            }
            else
            {
                i_top = i - 1;
            }
            i = i_bottom + ( i_top - i_bottom ) / 2;
        }
        if( p_playlist->pp_all_items[i]->input.i_id == p_root->input.i_id )
        {
            REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
        }
429 430 431
        playlist_ItemDelete( p_root );
    }
    return VLC_SUCCESS;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
}


/**
 * Adds an item to the childs of a node
 *
 * \param p_playlist the playlist
 * \param i_view the view of the node ( needed for parent search )
 * \param p_item the item to append
 * \param p_parent the parent node
 * \return VLC_SUCCESS or an error
 */
int playlist_NodeAppend( playlist_t *p_playlist,
                         int i_view,
                         playlist_item_t *p_item,
                         playlist_item_t *p_parent )
{
    return playlist_NodeInsert( p_playlist, i_view, p_item, p_parent, -1 );
}

int playlist_NodeInsert( playlist_t *p_playlist,
                         int i_view,
                         playlist_item_t *p_item,
                         playlist_item_t *p_parent,
                         int i_position )
{
   int i;
   vlc_bool_t b_found = VLC_FALSE;
   if( !p_parent || p_parent->i_children == -1 )
   {
        msg_Err( p_playlist, "invalid node" );
Clément Stenac's avatar
Clément Stenac committed
463
        return VLC_EGENERIC;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
   }

   if( i_position == -1 ) i_position = p_parent->i_children ;

   INSERT_ELEM( p_parent->pp_children,
                p_parent->i_children,
                i_position,
                p_item );

   /* Add the parent to the array */
   for( i= 0; i< p_item->i_parents ; i++ )
   {
       if( p_item->pp_parents[i]->i_view == i_view )
       {
           b_found = VLC_TRUE;
           break;
       }
   }
   if( b_found == VLC_FALSE )
   {
        struct item_parent_t *p_ip = (struct item_parent_t *)
Clément Stenac's avatar
Clément Stenac committed
485
                                     malloc(sizeof(struct item_parent_t) );
486 487 488 489 490 491 492 493 494 495 496 497 498 499
        p_ip->i_view = i_view;
        p_ip->p_parent = p_parent;

        INSERT_ELEM( p_item->pp_parents,
                     p_item->i_parents, p_item->i_parents,
                     p_ip );
   }

   /* Let the interface know this has been updated */
   p_parent->i_serial++;
   return VLC_SUCCESS;
}

/**
500
 * Deletes a parent from the parent list of a node
501 502 503 504 505 506
 *
 * \param p_playlist the playlist
 * \param p_item the item to remove
 * \param p_parent the parent node
 * \return VLC_SUCCESS or an error
 */
507
int playlist_NodeRemoveParent( playlist_t *p_playlist,
508 509 510 511 512 513 514 515 516
                        playlist_item_t *p_item,
                        playlist_item_t *p_parent )
{
   int i;
   if( !p_parent || p_parent->i_children == -1 )
   {
        msg_Err( p_playlist, "invalid node" );
   }

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
   for( i = 0; i < p_item->i_parents; i++ )
   {
       if( p_item->pp_parents[i]->p_parent == p_parent )
       {
           if( p_item->pp_parents[i] )
           {
               free( p_item->pp_parents[i] );
           }
           REMOVE_ELEM( p_item->pp_parents, p_item->i_parents, i );
       }
   }
   p_item->i_serial++;
   return VLC_SUCCESS;
}

/**
 * Deletes an item from the children of a node
 *
 * \param p_playlist the playlist
 * \param p_item the item to remove
 * \param p_parent the parent node
 * \return VLC_SUCCESS or an error
 */
int playlist_NodeRemoveItem( playlist_t *p_playlist,
                        playlist_item_t *p_item,
                        playlist_item_t *p_parent )
{
   int i;
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
   for( i= 0; i< p_parent->i_children ; i++ )
   {
       if( p_parent->pp_children[i] == p_item )
       {
           REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
       }
   }

   /* Let the interface know this has been updated */
   p_parent->i_serial++;

   return VLC_SUCCESS;
}


/**
 * Count the children of a node
 *
 * \param p_playlist the playlist
 * \param p_node the node
 * \return the number of children
 */
int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
{
    int i;
    int i_nb = 0;
    if( p_node->i_children == -1 )
    {
        return 0;
    }

    for( i=0 ; i< p_node->i_children;i++ )
    {
        if( p_node->pp_children[i]->i_children == -1 )
        {
            i_nb++;
        }
        else
        {
            i_nb += playlist_NodeChildrenCount( p_playlist, 
                                                p_node->pp_children[i] );
        }
    }
    return i_nb;
}

/**
 * Search a child of a node by its name
 *
 * \param p_node the node
 * \param psz_search the name of the child to search
 * \return the child item or NULL if not found or error
 */
playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
                                           const char *psz_search )
{
Clément Stenac's avatar
Clément Stenac committed
601
    int i;
602

Clément Stenac's avatar
Clément Stenac committed
603 604 605 606 607 608 609
    if( p_node->i_children < 0 )
    {
         return NULL;
    }
    for( i = 0 ; i< p_node->i_children; i++ )
    {
        if( !strcmp( p_node->pp_children[i]->input.psz_name, psz_search ) )
610 611 612
        {
            return p_node->pp_children[i];
        }
Clément Stenac's avatar
Clément Stenac committed
613 614
    }
    return NULL;
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
}


/**********************************************************************
 * Tree functions
 **********************************************************************/

/**
 * Finds the next item to play
 *
 * \param p_playlist the playlist
 * \param i_view the view
 * \param p_root the root node
 * \param p_node the node we are playing from
 * \param p_item the item we were playing (NULL if none )
 * \return the next item to play, or NULL if none found
 */
playlist_item_t *playlist_FindNextFromParent( playlist_t *p_playlist,
                                        int i_view, /* FIXME: useless */
                                        playlist_item_t *p_root,
                                        playlist_item_t *p_node,
                                        playlist_item_t *p_item )
{
    playlist_item_t *p_search, *p_next;

Clément Stenac's avatar
Clément Stenac committed
640
#ifdef PLAYLIST_DEBUG
641 642
    if( p_item != NULL )
    {
643 644 645
        msg_Dbg( p_playlist, "finding next of %s within %s - root %s",
                        p_item->input.psz_name, p_node->input.psz_name,
                        p_root->input.psz_name );
646 647 648
    }
    else
    {
649 650
        msg_Dbg( p_playlist, "finding something to play within %s -root %s",
                            p_node->input.psz_name, p_root->input.psz_name );
651 652

    }
Clément Stenac's avatar
Clément Stenac committed
653
#endif
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

    if( !p_node  || p_node->i_children == -1 )
    {
        msg_Err( p_playlist,"invalid arguments for FindNextFromParent" );
        return NULL;
    }

    /* Find the parent node of the item */
    if( p_item != NULL )
    {
        p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
        if( p_search == NULL )
        {
            msg_Err( p_playlist, "parent node not found" );
            return NULL;
        }
    }
    else
    {
        p_search = p_node;
    }

    /* Now, go up the tree until we find a suitable next item */
    p_next = playlist_RecursiveFindNext( p_playlist,i_view,
                                         p_node, p_item, p_search );

    /* Not found, do we go past p_node ? */
    if( p_next == NULL )
    {
        if( p_playlist->b_go_next )
        {
685 686 687 688
#ifdef PLAYLIST_DEBUG
            msg_Dbg( p_playlist, "Moving on to next node: search from %s",
                            p_root->input.psz_name );
#endif
689 690 691 692 693 694 695 696 697 698 699
            p_next = playlist_RecursiveFindNext( p_playlist, i_view,
                                p_root, p_item, p_search );
            if( p_next == NULL )
            {
                return NULL;
            }
            /* OK, we could continue, so set our current node to the root */
            p_playlist->status.p_node = p_root;
        }
        else
        {
700 701 702
#ifdef PLAYLIST_DEBUG
            msg_Dbg( p_playlist, "Not moving on to next node: you loose" );
#endif
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
            return NULL;
        }
    }
    return p_next;
}

/**
 * Finds the previous item to play
 *
 * \param p_playlist the playlist
 * \param i_view the view
 * \param p_root the root node
 * \param p_node the node we are playing from
 * \param p_item the item we were playing (NULL if none )
 * \return the next item to play, or NULL if none found
 */
playlist_item_t *playlist_FindPrevFromParent( playlist_t *p_playlist,
                                        int i_view,
                                        playlist_item_t *p_root,
                                        playlist_item_t *p_node,
                                        playlist_item_t *p_item )
{
    playlist_item_t *p_search, *p_next;

Clément Stenac's avatar
Clément Stenac committed
727
#ifdef PLAYLIST_DEBUG
728 729 730 731 732 733 734 735 736
    if( p_item != NULL )
    {
        msg_Dbg( p_playlist, "Finding prev of %s within %s",
                        p_item->input.psz_name, p_node->input.psz_name );
    }
    else
    {
        msg_Dbg( p_playlist, "Finding prev from %s",p_node->input.psz_name );
    }
Clément Stenac's avatar
Clément Stenac committed
737
#endif
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

    if( !p_node  || p_node->i_children == -1 )
    {
        msg_Err( p_playlist,"invalid arguments for FindPrevFromParent" );
        return NULL;
    }

    /* Find the parent node of the item */
    if( p_item != NULL )
    {
        p_search = playlist_FindDirectParent( p_playlist, p_item, i_view );
        if( p_search == NULL )
        {
            msg_Err( p_playlist, "parent node not found" );
            return NULL;
        }
    }
    else
    {
        p_search = p_node;
    }

    /* Now, go up the tree until we find a suitable next item */
    p_next = playlist_RecursiveFindPrev( p_playlist,i_view,
                                         p_node, p_item, p_search );

    if( p_next == NULL )
    {
        if( p_playlist->b_go_next )
        {
            p_next = playlist_RecursiveFindPrev( p_playlist, i_view,
                                p_root, p_item, p_search );
            if( p_next == NULL )
            {
                return NULL;
            }
            /* OK, we could continue, so set our current node to the root */
            p_playlist->status.p_node = p_root;
        }
        else
        {
            return NULL;
        }
    }
    return p_next;
}

/************************************************************************
 * Following functions are local
 ***********************************************************************/


/* Recursively search the tree for next item */
playlist_item_t *playlist_RecursiveFindNext( playlist_t *p_playlist,
                int i_view,
                playlist_item_t *p_root,
                playlist_item_t *p_item,
                playlist_item_t *p_parent )
{
    int i;
    playlist_item_t *p_parent_parent;

    for( i= 0 ; i < p_parent->i_children ; i++ )
    {
        if( p_parent->pp_children[i] == p_item || p_item == NULL )
        {
            if( p_item == NULL )
            {
                i = -1;
            }
Clément Stenac's avatar
Clément Stenac committed
808
#ifdef PLAYLIST_DEBUG
809 810
            msg_Dbg( p_playlist,"Current item found, child %i of %s",
                                i , p_parent->input.psz_name );
Clément Stenac's avatar
Clément Stenac committed
811
#endif
812 813 814 815
            /* We found our item */
            if( i+1 >= p_parent->i_children )
            {
                /* Too far... */
Clément Stenac's avatar
Clément Stenac committed
816
#ifdef PLAYLIST_DEBUG
817 818
                msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
                                p_parent->input.psz_name );
Clément Stenac's avatar
Clément Stenac committed
819
#endif
820 821
                if( p_parent == p_root )
                {
822 823 824 825
#ifdef PLAYLIST_DEBUG
                    msg_Dbg( p_playlist, "At root item (%s)",
                                         p_root->input.psz_name );
#endif
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
                    /* Hmm, seems it's the end for you, guy ! */
                    return NULL;
                }

                /* Go up one level */
                p_parent_parent = playlist_FindDirectParent( p_playlist,
                                                             p_parent, i_view );
                if( p_parent_parent == NULL )
                {
                    msg_Warn( p_playlist, "Unable to find parent !");
                    return NULL;
                }
                return playlist_RecursiveFindNext( p_playlist, i_view,p_root,
                                                   p_parent, p_parent_parent );
            }
            else
            {
                if( p_parent->pp_children[i+1]->i_children == -1 )
                {
                    /* Cool, we have found a real item to play */
Clément Stenac's avatar
Clément Stenac committed
846
#ifdef PLAYLIST_DEBUG
847 848
                    msg_Dbg( p_playlist, "Playing child %i of %s",
                                     i+1 , p_parent->input.psz_name );
Clément Stenac's avatar
Clément Stenac committed
849
#endif
850 851 852 853 854
                    return p_parent->pp_children[i+1];
                }
                else if( p_parent->pp_children[i+1]->i_children > 0 )
                {
                    /* Select the first child of this node */
Clément Stenac's avatar
Clément Stenac committed
855
#ifdef PLAYLIST_DEBUG
856 857 858
                    msg_Dbg( p_playlist, "%s is a node with children, "
                                 "playing the first",
                                  p_parent->pp_children[i+1]->input.psz_name);
Clément Stenac's avatar
Clément Stenac committed
859
#endif
860 861 862 863 864 865 866 867 868 869 870 871 872
                    if( p_parent->pp_children[i+1]->pp_children[0]
                                    ->i_children >= 0 )
                    {
                        /* first child is a node ! */
                        return playlist_RecursiveFindNext( p_playlist, i_view,
                                   p_root, NULL ,
                                   p_parent->pp_children[i+1]->pp_children[0]);
                    }
                    return p_parent->pp_children[i+1]->pp_children[0];
                }
                else
                {
                    /* This node has no child... We must continue */
Clément Stenac's avatar
Clément Stenac committed
873
#ifdef PLAYLIST_DEBUG
874 875
                    msg_Dbg( p_playlist, "%s is a node with no children",
                                 p_parent->pp_children[i+1]->input.psz_name);
Clément Stenac's avatar
Clément Stenac committed
876
#endif
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
                    p_item = p_parent->pp_children[i+1];
                }
            }
        }
    }
    /* Just in case :) */
    return NULL;
}

/* Recursively search the tree for previous item */
playlist_item_t *playlist_RecursiveFindPrev( playlist_t *p_playlist,
                int i_view,
                playlist_item_t *p_root,
                playlist_item_t *p_item,
                playlist_item_t *p_parent )
{
    int i;
    playlist_item_t *p_parent_parent;

    for( i= p_parent->i_children - 1 ; i >= 0 ; i-- )
    {
        if( p_parent->pp_children[i] == p_item || p_item == NULL )
        {
            if( p_item == NULL )
            {
                i = -1;
            }
Clément Stenac's avatar
Clément Stenac committed
904
#ifdef PLAYLIST_DEBUG
905 906
            msg_Dbg( p_playlist,"Current item found, child %i of %s",
                             i , p_parent->input.psz_name );
Clément Stenac's avatar
Clément Stenac committed
907
#endif
908 909 910 911
            /* We found our item */
            if( i < 1 )
            {
                /* Too far... */
Clément Stenac's avatar
Clément Stenac committed
912
#ifdef PLAYLIST_DEBUG
913 914
                msg_Dbg( p_playlist, "Going up the tree,at parent of %s",
                                     p_parent->input.psz_name );
Clément Stenac's avatar
Clément Stenac committed
915
#endif
916 917
                if( p_parent == p_root )
                {
918 919 920 921
#ifdef PLAYLIST_DEBUG
                    msg_Dbg( p_playlist, "At root item (%s)",
                                         p_root->input.psz_name );
#endif
922 923 924 925 926 927
                    /* Hmm, seems it's the end for you, guy ! */
                    return NULL;
                }
                /* Go up one level */
                p_parent_parent = playlist_FindDirectParent( p_playlist,
                                            p_parent, i_view );
928 929 930 931 932 933 934
                if( p_parent_parent == NULL )
                {
#ifdef PLAYLIST_DEBUG
                    msg_Dbg( p_playlist, "Mmmh, couldn't find parent" );
#endif
                    return NULL;
                }
935 936 937 938 939 940 941 942
                return playlist_RecursiveFindPrev( p_playlist, i_view,p_root,
                                            p_parent, p_parent_parent );
            }
            else
            {
                if( p_parent->pp_children[i-1]->i_children == -1 )
                {
                    /* Cool, we have found a real item to play */
Clément Stenac's avatar
Clément Stenac committed
943
#ifdef PLAYLIST_DEBUG
944 945
                    msg_Dbg( p_playlist, "Playing child %i of %s",
                                     i-1, p_parent->input.psz_name );
Clément Stenac's avatar
Clément Stenac committed
946
#endif
947 948 949 950 951
                    return p_parent->pp_children[i-1];
                }
                else if( p_parent->pp_children[i-1]->i_children > 0 )
                {
                    /* Select the last child of this node */
Clément Stenac's avatar
Clément Stenac committed
952
#ifdef PLAYLIST_DEBUG
953 954 955
                    msg_Dbg( p_playlist, "%s is a node with children,"
                                   " playing the last",
                                   p_parent->pp_children[i-1]->input.psz_name);
Clément Stenac's avatar
Clément Stenac committed
956
#endif
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
                    if( p_parent->pp_children[i-1]->pp_children[p_parent->
                            pp_children[i-1]->i_children-1]->i_children >= 0 )
                    {
                        /* Last child is a node */
                        return playlist_RecursiveFindPrev( p_playlist, i_view,
                                    p_root,NULL,
                                    p_parent->pp_children[i-1]->pp_children[
                                    p_parent->pp_children[i-1]->i_children-1]);
                    }
                    return p_parent->pp_children[i-1]->pp_children[
                                 p_parent->pp_children[i-1]->i_children-1];
                }
                else
                {
                    /* This node has no child... We must continue */
Clément Stenac's avatar
Clément Stenac committed
972
#ifdef PLAYLIST_DEBUG
973 974
                    msg_Dbg( p_playlist, "%s is a node with no children",
                                p_parent->pp_children[i-1]->input.psz_name);
Clément Stenac's avatar
Clément Stenac committed
975
#endif
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
                    p_item = p_parent->pp_children[i-1];
                }
            }
        }
    }
    return NULL;
}

/* This function returns the parent of an item in a view */
playlist_item_t *playlist_FindDirectParent( playlist_t *p_playlist,
                                         playlist_item_t *p_item,
                                         int i_view )
{
        int i = 0;
        for( i= 0; i< p_item->i_parents ; i++ )
        {
            if( p_item->pp_parents[i]->i_view == i_view )
            {
                return p_item->pp_parents[i]->p_parent;
            }
        }
        return NULL;
}


1001 1002
#ifdef PLAYLIST_DEBUG
/* This function dumps a node : to be used only for debug*/
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
                        int i_level )
{
    char str[512];
    int i;

    if( i_level == 1 )
    {
        msg_Dbg( p_playlist, "%s (%i)",
                        p_item->input.psz_name, p_item->i_children );
    }

    if( p_item->i_children == -1 )
    {
        return;
    }

    for( i = 0; i< p_item->i_children; i++ )
    {
        memset( str, 32, 512 );
        sprintf( str + 2 * i_level , "%s (%i)",
                                p_item->pp_children[i]->input.psz_name,
                                p_item->pp_children[i]->i_children );
        msg_Dbg( p_playlist, "%s",str );
        if( p_item->pp_children[i]->i_children >= 0 )
        {
            playlist_NodeDump( p_playlist, p_item->pp_children[i],
                              i_level + 1 );
        }
    }
    return;
}
1035
#endif