item.c 31.5 KB
Newer Older
1
/*****************************************************************************
2
 * item.c : Playlist item creation/deletion/add/removal functions
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2007 VLC authors and VideoLAN
Clément Stenac's avatar
Clément Stenac committed
5
 * $Id$
6 7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Clément Stenac <zorglub@videolan.org>
9
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
29
#include <assert.h>
30
#include <vlc_playlist.h>
31
#include <vlc_rand.h>
32
#include "playlist_internal.h"
33

34 35 36
static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
                     playlist_item_t *p_node, int i_mode, int i_pos );
static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
37
                           playlist_item_t * );
38
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
39

40 41 42 43
static int RecursiveAddIntoParent (
                playlist_t *p_playlist, playlist_item_t *p_parent,
                input_item_node_t *p_node, int i_pos, bool b_flat,
                playlist_item_t **pp_first_leaf );
44 45 46
static int RecursiveInsertCopy (
                playlist_t *p_playlist, playlist_item_t *p_item,
                playlist_item_t *p_parent, int i_pos, bool b_flat );
47

48
/*****************************************************************************
49
 * An input item has gained subitems (Event Callback)
50 51
 *****************************************************************************/

52 53 54 55 56 57
static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
                                          void * user_data )
{
    input_item_t *p_input = p_event->p_obj;
    playlist_t *p_playlist = (( playlist_item_t* ) user_data)->p_playlist;
    input_item_node_t *p_new_root = p_event->u.input_item_subitem_tree_added.p_root;
58

59 60
    PL_LOCK;

61 62 63 64
    playlist_item_t *p_item =
        playlist_ItemGetByInput( p_playlist, p_input );

    assert( p_item != NULL );
65

66
    bool b_current = get_current_status_item( p_playlist ) == p_item;
67
    bool b_autostart = var_GetBool( p_playlist, "playlist-autostart" );
68
    bool b_stop = p_item->i_flags & PLAYLIST_SUBITEM_STOP_FLAG;
69 70
    bool b_flat = false;

71
    p_item->i_flags &= ~PLAYLIST_SUBITEM_STOP_FLAG;
Pierre d'Herbemont's avatar
(oops).  
Pierre d'Herbemont committed
72

73 74 75 76 77 78
    /* We will have to flatten the tree out if we are in "the playlist" node and
    the user setting demands flat playlist */

    if( !pl_priv(p_playlist)->b_tree ) {
        playlist_item_t *p_up = p_item;
        while( p_up->p_parent )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
79
        {
80 81 82 83 84 85
            if( p_up->p_parent == p_playlist->p_playing )
            {
                b_flat = true;
                break;
            }
            p_up = p_up->p_parent;
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
86
        }
87
    }
88

89 90 91 92 93 94
    int pos = 0;

    /* If we have to flatten out, then take the item's position in the parent as
    insertion point and delete the item */

    if( b_flat )
95
    {
96 97 98 99 100
        playlist_item_t *p_parent = p_item->p_parent;
        assert( p_parent != NULL );

        int i;
        for( i = 0; i < p_parent->i_children; i++ )
101
        {
102 103 104 105 106
            if( p_parent->pp_children[i] == p_item )
            {
                pos = i;
                break;
            }
107
        }
108
        assert( i < p_parent->i_children );
Jakob Leben's avatar
Jakob Leben committed
109

110
        playlist_DeleteItem( p_playlist, p_item, true );
Jakob Leben's avatar
Jakob Leben committed
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        p_item = p_parent;
    }
    else
    {
        pos = p_item->i_children >= 0 ? p_item->i_children : 0;
    }

    /* At this point:
    "p_item" is the node where sub-items should be inserted,
    "pos" is the insertion position in that node */

    int last_pos = playlist_InsertInputItemTree( p_playlist,
                                                 p_item,
                                                 p_new_root,
                                                 pos,
                                                 b_flat );
128

129
    if( !b_flat ) var_SetInteger( p_playlist, "leaf-to-parent", p_item->i_id );
130

131
    //control playback only if it was the current playing item that got subitems
132
    if( b_current )
133
    {
134
        if( last_pos == pos || ( b_stop && !b_flat ) || !b_autostart )
135
        {
136 137
            /* We stop, either because no sub-item was actually created, or some
            flags/settings want us to do so at this point */
138 139 140 141 142 143
            PL_UNLOCK;
            playlist_Stop( p_playlist );
            return;
        }
        else
        {
144 145 146 147 148
            /* Continue to play, either random or the first new item */
            playlist_item_t *p_play_item;

            if( var_GetBool( p_playlist, "random" ) )
            {
149
                p_play_item = NULL;
150 151 152
            }
            else
            {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                /* Don't Play a directory if it can loop into a parent */
                if( p_new_root->b_can_loop )
                {
                    /* Play the first regular file */
                    for( ; pos < last_pos; pos++ )
                    {
                        if( p_item->pp_children[pos]->p_input->i_type != ITEM_TYPE_DIRECTORY )
                            break;
                    }
                    if( last_pos == pos )
                    {
                        PL_UNLOCK;
                        playlist_Stop( p_playlist );
                        return;
                    }
                }
169 170 171 172 173 174 175 176 177 178
                p_play_item = p_item->pp_children[pos];
                /* NOTE: this is a work around the general bug:
                if node-to-be-played contains sub-nodes, then second instead
                of first leaf starts playing (only in case the leafs have just
                been instered and playlist has not yet been rebuilt.)
                */
                while( p_play_item->i_children > 0 )
                    p_play_item = p_play_item->pp_children[0];
            }

179
            playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
180 181 182
                                  pl_Locked,
                                  get_current_status_node( p_playlist ),
                                  p_play_item );
183
        }
184
    }
185

186
    PL_UNLOCK;
187
}
188
/*****************************************************************************
189
 * An input item's meta or duration has changed (Event Callback)
190
 *****************************************************************************/
191 192
static void input_item_changed( const vlc_event_t * p_event,
                                void * user_data )
193
{
194 195
    playlist_item_t *p_item = user_data;
    VLC_UNUSED( p_event );
196
    var_SetAddress( p_item->p_playlist, "item-change", p_item->p_input );
197 198
}

199 200 201
/*****************************************************************************
 * Listen to vlc_InputItemAddSubItem event
 *****************************************************************************/
202
static void install_input_item_observer( playlist_item_t * p_item )
203
{
204
    vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
205 206
    vlc_event_attach( p_em, vlc_InputItemSubItemTreeAdded,
                      input_item_add_subitem_tree, p_item );
207 208 209 210
    vlc_event_attach( p_em, vlc_InputItemDurationChanged,
                      input_item_changed, p_item );
    vlc_event_attach( p_em, vlc_InputItemMetaChanged,
                      input_item_changed, p_item );
211 212 213 214
    vlc_event_attach( p_em, vlc_InputItemNameChanged,
                      input_item_changed, p_item );
    vlc_event_attach( p_em, vlc_InputItemInfoChanged,
                      input_item_changed, p_item );
215 216
    vlc_event_attach( p_em, vlc_InputItemErrorWhenReadingChanged,
                      input_item_changed, p_item );
217 218
}

219
static void uninstall_input_item_observer( playlist_item_t * p_item )
220
{
221
    vlc_event_manager_t * p_em = &p_item->p_input->event_manager;
222 223
    vlc_event_detach( p_em, vlc_InputItemSubItemTreeAdded,
                      input_item_add_subitem_tree, p_item );
224 225 226 227
    vlc_event_detach( p_em, vlc_InputItemMetaChanged,
                      input_item_changed, p_item );
    vlc_event_detach( p_em, vlc_InputItemDurationChanged,
                      input_item_changed, p_item );
228
    vlc_event_detach( p_em, vlc_InputItemNameChanged,
229
                      input_item_changed, p_item );
230
    vlc_event_detach( p_em, vlc_InputItemInfoChanged,
231
                      input_item_changed, p_item );
232 233
    vlc_event_detach( p_em, vlc_InputItemErrorWhenReadingChanged,
                      input_item_changed, p_item );
234 235
}

236 237 238
/*****************************************************************************
 * Playlist item creation
 *****************************************************************************/
239
playlist_item_t *playlist_ItemNewFromInput( playlist_t *p_playlist,
240
                                              input_item_t *p_input )
241
{
242 243 244
    playlist_item_t* p_item = malloc( sizeof( playlist_item_t ) );
    if( !p_item )
        return NULL;
245

246 247
    assert( p_input );

248 249
    p_item->p_input = p_input;
    vlc_gc_incref( p_item->p_input );
250

251
    p_item->i_id = ++pl_priv(p_playlist)->i_last_playlist_id;
252

253
    p_item->p_parent = NULL;
254 255
    p_item->i_children = -1;
    p_item->pp_children = NULL;
256
    p_item->i_nb_played = 0;
257
    p_item->i_flags = 0;
258
    p_item->p_playlist = p_playlist;
259

260
    install_input_item_observer( p_item );
261

262 263 264
    return p_item;
}

265 266 267 268
/***************************************************************************
 * Playlist item destruction
 ***************************************************************************/

269
/**
270
 * Release an item
271 272 273 274
 *
 * \param p_item item to delete
 * \return VLC_SUCCESS
*/
275
int playlist_ItemRelease( playlist_item_t *p_item )
276
{
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
277 278
    /* For the assert */
    playlist_t *p_playlist = p_item->p_playlist;
279 280
    PL_ASSERT_LOCKED;

281 282 283 284 285 286 287
    /* Surprise, we can't actually do more because we
     * don't do refcounting, or eauivalent.
     * Because item are not only accessed by their id
     * using playlist_item outside the PL_LOCK isn't safe.
     * Most of the modules does that.
     *
     * Who wants to add proper memory management? */
288
    uninstall_input_item_observer( p_item );
289
    ARRAY_APPEND( pl_priv(p_playlist)->items_to_delete, p_item);
290 291 292
    return VLC_SUCCESS;
}

293 294 295 296 297
/**
 * Delete input item
 *
 * Remove an input item when it appears from a root playlist item
 * \param p_playlist playlist object
298
 * \param p_input the input to delete
299 300 301 302
 * \param p_root root playlist item
 * \param b_do_stop must stop or not the playlist
 * \return VLC_SUCCESS or VLC_EGENERIC
*/
303
static int DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
304
                            playlist_item_t *p_root, bool b_do_stop )
305
{
306
    PL_ASSERT_LOCKED;
307 308 309 310
    playlist_item_t *p_item = playlist_ItemFindFromInputAndRoot(
        p_playlist, p_input, p_root, false );
    if( !p_item ) return VLC_EGENERIC;
    return playlist_DeleteItem( p_playlist, p_item, b_do_stop );
311 312
}

313 314 315 316 317
/**
 * Delete input item
 *
 * Remove an input item when it appears from a root playlist item
 * \param p_playlist playlist object
318
 * \param p_input the input to delete
319 320 321 322
 * \param p_root root playlist item
 * \param b_locked TRUE if the playlist is locked
 * \return VLC_SUCCESS or VLC_EGENERIC
 */
323 324
int playlist_DeleteFromInputInParent( playlist_t *p_playlist,
                                      input_item_t *p_item,
325
                                      playlist_item_t *p_root, bool b_locked )
326 327
{
    int i_ret;
328
    PL_LOCK_IF( !b_locked );
329
    i_ret = DeleteFromInput( p_playlist, p_item, p_root, true );
330
    PL_UNLOCK_IF( !b_locked );
331 332 333
    return i_ret;
}

334 335 336
/**
 * Delete from input
 *
337
 * Search anywhere in playlist for an an input item and delete it
338
 * \param p_playlist playlist object
339
 * \param p_input the input to delete
340 341 342
 * \param b_locked TRUE if the playlist is locked
 * \return VLC_SUCCESS or VLC_ENOITEM
 */
343
int playlist_DeleteFromInput( playlist_t *p_playlist, input_item_t *p_input,
344
                              bool b_locked )
345
{
346
    int i_ret;
347
    PL_LOCK_IF( !b_locked );
348 349
    i_ret = DeleteFromInput( p_playlist, p_input,
                             p_playlist->p_root, true );
350
    PL_UNLOCK_IF( !b_locked );
351
    return ( i_ret == VLC_SUCCESS ? VLC_SUCCESS : VLC_ENOITEM );
352 353
}

354 355 356 357 358 359 360
/**
 * Clear the playlist
 *
 * \param p_playlist playlist object
 * \param b_locked TRUE if the playlist is locked
 * \return nothing
 */
361
void playlist_Clear( playlist_t * p_playlist, bool b_locked )
362
{
363
    PL_LOCK_IF( !b_locked );
364
    playlist_NodeEmpty( p_playlist, p_playlist->p_playing, true );
365
    PL_UNLOCK_IF( !b_locked );
366
}
367

368 369 370 371 372 373 374 375 376
/**
 * Delete playlist item
 *
 * Remove a playlist item from the playlist, given its id
 * This function is to be used only by the playlist
 * \param p_playlist playlist object
 * \param i_id id of the item do delete
 * \return VLC_SUCCESS or an error
 */
377
int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
378
{
379
    PL_ASSERT_LOCKED;
380
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
381
    if( !p_item ) return VLC_EGENERIC;
382
    return playlist_DeleteItem( p_playlist, p_item, true );
383 384 385 386 387
}

/***************************************************************************
 * Playlist item addition
 ***************************************************************************/
388 389 390 391
/**
 * Playlist add
 *
 * Add an item to the playlist or the media library
392 393 394 395 396 397
 * \param p_playlist the playlist to add into
 * \param psz_uri the mrl to add to the playlist
 * \param psz_name a text giving a name or description of this item
 * \param i_mode the mode used when adding
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
398
 *        regardless of its size
399
 * \param b_playlist TRUE for playlist, FALSE for media library
400
 * \param b_locked TRUE if the playlist is locked
401
 * \return VLC_SUCCESS or a VLC error code
402
 */
403 404
int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
                  const char *psz_name, int i_mode, int i_pos,
405
                  bool b_playlist, bool b_locked )
406
{
407
    return playlist_AddExt( p_playlist, psz_uri, psz_name,
408
                            i_mode, i_pos, -1, 0, NULL, 0, b_playlist, b_locked );
409
}
410

411
/**
412
 * Add a MRL into the playlist or the media library, duration and options given
413 414 415 416 417 418 419
 *
 * \param p_playlist the playlist to add into
 * \param psz_uri the mrl to add to the playlist
 * \param psz_name a text giving a name or description of this item
 * \param i_mode the mode used when adding
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
420
 *        regardless of its size
421 422
 * \param i_duration length of the item in milliseconds.
 * \param i_options the number of options
423 424
 * \param ppsz_options an array of options
 * \param i_option_flags options flags
425
 * \param b_playlist TRUE for playlist, FALSE for media library
426
 * \param b_locked TRUE if the playlist is locked
427
 * \return VLC_SUCCESS or a VLC error code
428
*/
429 430
int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
                     const char *psz_name, int i_mode, int i_pos,
431
                     mtime_t i_duration,
432 433
                     int i_options, const char *const *ppsz_options,
                     unsigned i_option_flags,
434
                     bool b_playlist, bool b_locked )
435
{
436
    int i_ret;
437
    input_item_t *p_input;
438

439
    p_input = input_item_NewExt( psz_uri, psz_name,
440 441 442 443
                                 i_options, ppsz_options, i_option_flags,
                                 i_duration );
    if( p_input == NULL )
        return VLC_ENOMEM;
444 445
    i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
                               b_locked );
446
    vlc_gc_decref( p_input );
447
    return i_ret;
448
}
449

450 451 452 453 454 455 456 457 458 459 460
/**
 * Add an input item to the playlist node
 *
 * \param p_playlist the playlist to add into
 * \param p_input the input item to add
 * \param i_mode the mode used when adding
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
 *        regardless of its size
 * \param b_playlist TRUE for playlist, FALSE for media library
 * \param b_locked TRUE if the playlist is locked
461
 * \return VLC_SUCCESS or VLC_ENOMEM or VLC_EGENERIC
462
*/
463
int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
464 465
                       int i_mode, int i_pos, bool b_playlist,
                       bool b_locked )
466
{
467
    playlist_item_t *p_item;
468

469
    PL_LOCK_IF( !b_locked );
470

471
    p_item = playlist_ItemNewFromInput( p_playlist, p_input );
472 473 474 475 476
    if( p_item == NULL )
    {
        PL_UNLOCK_IF( !b_locked );
        return VLC_ENOMEM;
    }
477 478 479
    AddItem( p_playlist, p_item,
             b_playlist ? p_playlist->p_playing :
                          p_playlist->p_media_library , i_mode, i_pos );
480

481
    GoAndPreparse( p_playlist, i_mode, p_item );
482

483
    PL_UNLOCK_IF( !b_locked );
484
    return VLC_SUCCESS;
485
}
Gildas Bazin's avatar
 
Gildas Bazin committed
486

487 488 489 490 491 492 493 494 495 496 497 498 499
/**
 * Add an input item to a given node
 *
 * \param p_playlist the playlist to add into
 * \param p_input the input item to add
 * \param p_parent the parent item to add into
 * \param i_mode the mode used when addin
 * \param i_pos the position in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
 *        regardless of its size
 * \param b_locked TRUE if the playlist is locked
 * \return the new playlist item
 */
500 501 502
playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
                                         input_item_t *p_input,
                                         playlist_item_t *p_parent,
503
                                         int i_mode, int i_pos,
504
                                         bool b_locked )
Gildas Bazin's avatar
 
Gildas Bazin committed
505
{
506
    playlist_item_t *p_item;
Clément Stenac's avatar
Clément Stenac committed
507 508
    assert( p_input );
    assert( p_parent && p_parent->i_children != -1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
509

510
    PL_LOCK_IF( !b_locked );
Gildas Bazin's avatar
 
Gildas Bazin committed
511

512
    p_item = playlist_ItemNewFromInput( p_playlist, p_input );
513 514
    if( p_item == NULL )
        goto end;
515
    AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
516

517 518
    GoAndPreparse( p_playlist, i_mode, p_item );

519
end:
520
    PL_UNLOCK_IF( !b_locked );
521 522

    return p_item;
Gildas Bazin's avatar
 
Gildas Bazin committed
523
}
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
/**
 * Copy an item (and all its children, if any) into another node
 *
 * \param p_playlist the playlist to operate on
 * \param p_item the playlist item to copy
 * \param p_parent the parent item to copy into
 * \param i_pos the position in the parent item for the new copy;
 *              if this is PLAYLIST_END, the copy is appended after all
 *              parent's children
 * \return the position in parent item just behind the last new item inserted
 */
int playlist_NodeAddCopy (
    playlist_t *p_playlist, playlist_item_t *p_item,
    playlist_item_t *p_parent, int i_pos )
{
    PL_ASSERT_LOCKED;
    assert( p_parent != NULL && p_item != NULL );
    assert( p_parent->i_children > -1 );

    if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;

    bool b_flat = false;

    playlist_item_t *p_up = p_parent;
    while( p_up )
    {
        if( p_up == p_playlist->p_playing )
            if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
        if( p_up == p_item )
            /* TODO: We don't support copying a node into itself (yet),
            because we insert items as we copy. Instead, we should copy
            all items first and then insert. */
            return i_pos;
        p_up = p_up->p_parent;
    }

    return RecursiveInsertCopy( p_playlist, p_item, p_parent, i_pos, b_flat );
}

564 565 566 567 568 569 570 571 572 573 574 575 576
/**
 * Insert a tree of input items into a given playlist node
 *
 * \param p_playlist the playlist to insert into
 * \param p_parent the receiving playlist node (can be an item)
 * \param p_node the root of input item tree,
          only it's contents will be inserted
 * \param i_pos the position in the playlist where to insert. If this is
 *        PLAYLIST_END the items will be added at the end of the playlist
 *        regardless of its size
 * \param b_flat TRUE if the new tree contents should be flattened into a list
 * \return the first new leaf inserted (in playing order)
 */
577
int playlist_InsertInputItemTree (
578 579 580
    playlist_t *p_playlist, playlist_item_t *p_parent,
    input_item_node_t *p_node, int i_pos, bool b_flat )
{
581 582
    playlist_item_t *p_first_leaf = NULL;
    return RecursiveAddIntoParent ( p_playlist, p_parent, p_node, i_pos, b_flat, &p_first_leaf );
583 584
}

585

586 587 588 589
/*****************************************************************************
 * Playlist item misc operations
 *****************************************************************************/

590 591 592 593
/**
 * Find an item within a root, given its input id.
 *
 * \param p_playlist the playlist object
594
 * \param p_item the input item
595 596
 * \param p_root root playlist item
 * \param b_items_only TRUE if we want the item himself
597 598 599
 * \return the first found item, or NULL if not found
 */
playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
600
                                                    input_item_t *p_item,
Clément Stenac's avatar
Clément Stenac committed
601
                                                    playlist_item_t *p_root,
602
                                                    bool b_items_only )
603 604 605 606
{
    int i;
    for( i = 0 ; i< p_root->i_children ; i++ )
    {
Clément Stenac's avatar
Clément Stenac committed
607
        if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
608
            p_root->pp_children[i]->p_input == p_item )
609 610 611 612 613 614
        {
            return p_root->pp_children[i];
        }
        else if( p_root->pp_children[i]->i_children >= 0 )
        {
            playlist_item_t *p_search =
615
                 playlist_ItemFindFromInputAndRoot( p_playlist, p_item,
Clément Stenac's avatar
Clément Stenac committed
616 617
                                                    p_root->pp_children[i],
                                                    b_items_only );
618 619 620 621 622
            if( p_search ) return p_search;
        }
    }
    return NULL;
}
623 624


625
static int ItemIndex ( playlist_item_t *p_item )
Clément Stenac's avatar
Clément Stenac committed
626
{
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    for( int i = 0; i < p_item->p_parent->i_children; i++ )
        if( p_item->p_parent->pp_children[i] == p_item ) return i;
    return -1;
}

/**
 * Moves an item
 *
 * This function must be entered with the playlist lock
 *
 * \param p_playlist the playlist
 * \param p_item the item to move
 * \param p_node the new parent of the item
 * \param i_newpos the new position under this new parent
 * \return VLC_SUCCESS or an error
 */
int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
                       playlist_item_t *p_node, int i_newpos )
{
    PL_ASSERT_LOCKED;
647

Clément Stenac's avatar
Clément Stenac committed
648 649
    if( p_node->i_children == -1 ) return VLC_EGENERIC;

650 651
    playlist_item_t *p_detach = p_item->p_parent;
    int i_index = ItemIndex( p_item );
Clément Stenac's avatar
Clément Stenac committed
652

653 654 655
    REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, i_index );

    if( p_detach == p_node && i_index < i_newpos )
Rémi Duraffort's avatar
Rémi Duraffort committed
656 657
        i_newpos--;

Clément Stenac's avatar
Clément Stenac committed
658 659 660
    INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
    p_item->p_parent = p_node;

661 662
    pl_priv( p_playlist )->b_reset_currently_playing = true;
    vlc_cond_signal( &pl_priv( p_playlist )->signal );
Clément Stenac's avatar
Clément Stenac committed
663 664 665
    return VLC_SUCCESS;
}

666
/**
667
 * Moves an array of items
668 669
 *
 * This function must be entered with the playlist lock
670
 *
671
 * \param p_playlist the playlist
672 673 674 675
 * \param i_items the number of indexes to move
 * \param pp_items the array of indexes to move
 * \param p_node the target node
 * \param i_newpos the target position under this node
676
 * \return VLC_SUCCESS or an error
677
 */
678 679 680
int playlist_TreeMoveMany( playlist_t *p_playlist,
                            int i_items, playlist_item_t **pp_items,
                            playlist_item_t *p_node, int i_newpos )
681
{
682 683
    PL_ASSERT_LOCKED;

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    if ( p_node->i_children == -1 ) return VLC_EGENERIC;

    int i;
    for( i = 0; i < i_items; i++ )
    {
        playlist_item_t *p_item = pp_items[i];
        int i_index = ItemIndex( p_item );
        playlist_item_t *p_parent = p_item->p_parent;
        REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i_index );
        if ( p_parent == p_node && i_index < i_newpos ) i_newpos--;
    }
    for( i = i_items - 1; i >= 0; i-- )
    {
        playlist_item_t *p_item = pp_items[i];
        INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
        p_item->p_parent = p_node;
    }

    pl_priv( p_playlist )->b_reset_currently_playing = true;
    vlc_cond_signal( &pl_priv( p_playlist )->signal );
    return VLC_SUCCESS;
705 706
}

707 708
/**
 * Send a notification that an item has been added to a node
Rémi Duraffort's avatar
Rémi Duraffort committed
709
 *
710 711
 * \param p_playlist the playlist object
 * \param i_item_id id of the item added
712
 * \param i_node_id id of the node in which the item was added
713 714 715
 * \param b_signal TRUE if the function must send a signal
 * \return nothing
 */
716
void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
717
                             int i_node_id, bool b_signal )
718
{
719
    playlist_private_t *p_sys = pl_priv(p_playlist);
720 721
    PL_ASSERT_LOCKED;

722
    p_sys->b_reset_currently_playing = true;
723
    if( b_signal )
724 725 726 727 728 729
        vlc_cond_signal( &p_sys->signal );

    playlist_add_t add;
    add.i_item = i_item_id;
    add.i_node = i_node_id;

730
    var_SetAddress( p_playlist, "playlist-item-append", &add );
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
}

/**
 * Get the duration of all items in a node.
 */
mtime_t playlist_GetNodeDuration( playlist_item_t* node )
{
    /* For the assert */
    playlist_t *p_playlist = node->p_playlist;
    PL_ASSERT_LOCKED;

    mtime_t mt_duration = 0;

    if( node->i_children != -1 )
        for( int i = 0; i < node->i_children; i++ )
746 747 748 749 750 751 752
        {
            input_item_t* p_input = node->pp_children[i]->p_input;
            if ( p_input->i_type == ITEM_TYPE_NODE )
                mt_duration += playlist_GetNodeDuration( node->pp_children[i] );
            else
                mt_duration += input_item_GetDuration( p_input );
        }
753 754

    return mt_duration;
755 756 757 758 759 760 761
}

/***************************************************************************
 * The following functions are local
 ***************************************************************************/

/* Enqueue an item for preparsing, and play it, if needed */
762
static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
763
                           playlist_item_t *p_item )
764
{
765 766
    playlist_private_t *sys = pl_priv(p_playlist);

767
    PL_ASSERT_LOCKED;
Clément Stenac's avatar
Clément Stenac committed
768
    if( (i_mode & PLAYLIST_GO ) )
769
    {
770 771 772 773
        sys->request.b_request = true;
        sys->request.i_skip = 0;
        sys->request.p_item = p_item;
        if( sys->p_input != NULL )
774
            input_Stop( sys->p_input );
775
        vlc_cond_signal( &sys->signal );
776
    }
777
    /* Preparse if no artist/album info, and hasn't been preparsed already
778 779
       and if user has some preparsing option (auto-preparse variable)
       enabled*/
780 781
    char *psz_artist = input_item_GetArtist( p_item->p_input );
    char *psz_album = input_item_GetAlbum( p_item->p_input );
782
    if( sys->p_preparser != NULL && !input_item_IsPreparsed( p_item->p_input )
783
     && (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album)) )
784
        playlist_preparser_Push( sys->p_preparser, p_item->p_input, 0 );
Rafaël Carré's avatar
Rafaël Carré committed
785 786
    free( psz_artist );
    free( psz_album );
787 788 789
}

/* Add the playlist item to the requested node and fire a notification */
790 791
static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
                     playlist_item_t *p_node, int i_mode, int i_pos )
792
{
793
    PL_ASSERT_LOCKED;
Clément Stenac's avatar
Clément Stenac committed
794 795
    ARRAY_APPEND(p_playlist->items, p_item);
    ARRAY_APPEND(p_playlist->all_items, p_item);
796 797 798 799 800

    if( i_pos == PLAYLIST_END )
        playlist_NodeAppend( p_playlist, p_item, p_node );
    else
        playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
Clément Stenac's avatar
Clément Stenac committed
801

802 803
    playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
                            !( i_mode & PLAYLIST_NO_REBUILD ) );
804 805 806
}

/* Actually convert an item to a node */
807
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
808 809
{
    int i;
810 811 812 813 814 815 816 817 818 819
    if( p_item->i_children != -1 ) return;

    p_item->i_children = 0;

    input_item_t *p_input = p_item->p_input;
    vlc_mutex_lock( &p_input->lock );
    p_input->i_type = ITEM_TYPE_NODE;
    vlc_mutex_unlock( &p_input->lock );

    var_SetAddress( p_playlist, "item-change", p_item->p_input );
820 821

    /* Remove it from the array of available items */
Clément Stenac's avatar
Clément Stenac committed
822 823 824
    ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
    if( i != -1 )
        ARRAY_REMOVE( p_playlist->items, i );
825 826 827
}

/* Do the actual removal */
828
int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
829
                        bool b_stop )
830
{
831 832
    assert( b_stop );
    return playlist_NodeDelete( p_playlist, p_item, true, false );
833
}
834 835 836 837 838 839

static int RecursiveAddIntoParent (
    playlist_t *p_playlist, playlist_item_t *p_parent,
    input_item_node_t *p_node, int i_pos, bool b_flat,
    playlist_item_t **pp_first_leaf )
{
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
    *pp_first_leaf = NULL;

    if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );

    if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;

    for( int i = 0; i < p_node->i_children; i++ )
    {
        input_item_node_t *p_child_node = p_node->pp_children[i];

        playlist_item_t *p_new_item = NULL;
        bool b_children = p_child_node->i_children > 0;

        //Create the playlist item represented by input node, if allowed.
        if( !(b_flat && b_children) )
        {
            p_new_item = playlist_NodeAddInput( p_playlist,
                    p_child_node->p_item,
                    p_parent,
                    PLAYLIST_INSERT, i_pos,
                    pl_Locked );
            if( !p_new_item ) return i_pos;

            i_pos++;
        }
        //Recurse if any children
        if( b_children )
        {
            //Substitute p_new_item for first child leaf
            //(If flat, continue counting from current position)
            int i_last_pos = RecursiveAddIntoParent(
                    p_playlist,
                    p_new_item ? p_new_item : p_parent,
                    p_child_node,
                    ( b_flat ? i_pos : 0 ),
                    b_flat,
                    &p_new_item );
            //If flat, take position after recursion as current position
            if( b_flat ) i_pos = i_last_pos;
        }

        assert( p_new_item != NULL );
        if( i == 0 ) *pp_first_leaf = p_new_item;
    }
    return i_pos;
885
}
886 887 888 889 890 891 892 893 894 895 896 897 898 899

static int RecursiveInsertCopy (
    playlist_t *p_playlist, playlist_item_t *p_item,
    playlist_item_t *p_parent, int i_pos, bool b_flat )
{
    PL_ASSERT_LOCKED;
    assert( p_parent != NULL && p_item != NULL );

    if( p_item == p_parent ) return i_pos;

    input_item_t *p_input = p_item->p_input;

    if( !(p_item->i_children != -1 && b_flat) )
    {
900
        input_item_t *p_new_input = input_item_Copy( p_input );
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
        if( !p_new_input ) return i_pos;

        playlist_item_t *p_new_item = NULL;
        if( p_item->i_children == -1 )
            p_new_item = playlist_NodeAddInput( p_playlist, p_new_input,
                                   p_parent, PLAYLIST_INSERT, i_pos,
                                   pl_Locked );
        else
            p_new_item = playlist_NodeCreate( p_playlist, NULL,
                                 p_parent, i_pos, 0, p_new_input );
        vlc_gc_decref( p_new_input );
        if( !p_new_item ) return i_pos;

        i_pos++;

        if( p_new_item->i_children != -1 )
            p_parent = p_new_item;
    }

    for( int i = 0; i < p_item->i_children; i++ )
    {
        if( b_flat )
            i_pos = RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
                                         p_parent, i_pos, true );
        else
            RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
                                 p_parent, p_parent->i_children, false );
    }

    return i_pos;
}