item.c 27.3 KB
Newer Older
1
/*****************************************************************************
2
 * item.c : Playlist item creation/deletion/add/removal functions
3
 *****************************************************************************
4
 * Copyright (C) 1999-2007 the VideoLAN team
zorglub's avatar
zorglub committed
5
 * $Id$
6
7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Clément Stenac <zorglub@videolan.org>
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
 *****************************************************************************/
#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
25
#include <assert.h>
26
27
#include <vlc_playlist.h>
#include "playlist_internal.h"
28

29
30
31
32
33
34
35
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,
                           playlist_item_t *, playlist_item_t * );
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
                        vlc_bool_t b_stop );
zorglub's avatar
zorglub committed
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*****************************************************************************
 * An input item has gained a subitem (Event Callback)
 *****************************************************************************/
static void input_item_subitem_added( const vlc_event_t * p_event,
                                      void * user_data )
{
    playlist_t * p_playlist = user_data;
    input_item_t * p_parent, * p_child;
    vlc_bool_t b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
    playlist_item_t *p_item_in_category;
    playlist_item_t *p_current;

    p_parent = p_event->p_obj;
    p_child = p_event->u.input_item_subitem_added.p_new_child;

    p_current = playlist_ItemGetByInput( p_playlist, p_parent, VLC_FALSE );
53
    p_item_in_category = playlist_ItemToNode( p_playlist, p_current,
54
                                              VLC_FALSE );
55
    if(!p_item_in_category)
56
        p_item_in_category = p_current;
57

58
59
60
61
62
63
64
65
66
    b_play = b_play && p_current == p_playlist->status.p_item;

    playlist_NodeAddInput( p_playlist, p_child, p_item_in_category, 
        PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
        VLC_FALSE );

    if( b_play )
    {
        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
67
                          VLC_FALSE, p_item_in_category, NULL );
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        vlc_object_release( p_playlist );
    }
}

/*****************************************************************************
 * Listen to vlc_InputItemAddSubItem event
 *****************************************************************************/
static void install_input_item_observer( playlist_t * p_playlist,
                                         input_item_t * p_input )
{
    msg_Dbg( p_playlist, "Listening to %s with %p", p_input->psz_name, p_playlist);

    vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
                      input_item_subitem_added,
                      p_playlist );
}

static void uninstall_input_item_observer( playlist_t * p_playlist,
                                           input_item_t * p_input )
{
    msg_Dbg( p_playlist, "Not Listening to %s with %p", p_input->psz_name, p_playlist);

    vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
                      input_item_subitem_added,
                      p_playlist );
                      
}

96
97
98
/*****************************************************************************
 * Playlist item creation
 *****************************************************************************/
99
100
101
playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
                                            const char *psz_uri,
                                            const char *psz_name,
102
                                            int i_options,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
103
                                            const char *const *ppsz_options,
104
                                            int i_duration, int i_type )
105
{
106
    input_item_t *p_input;
107
    if( psz_uri == NULL ) return NULL;
108
    p_input = input_ItemNewWithType( p_obj, psz_uri,
109
110
                                     psz_name, i_options, ppsz_options,
                                     i_duration, i_type );
111
112
    return playlist_ItemNewFromInput( p_obj, p_input );
}
113

114
playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
115
                                              input_item_t *p_input )
116
{
117
    DECMALLOC_NULL( p_item, playlist_item_t );
118
    playlist_t *p_playlist = pl_Yield( p_obj );
zorglub's avatar
zorglub committed
119

120
121
    p_item->p_input = p_input;
    vlc_gc_incref( p_item->p_input );
122

123
    p_item->i_id = ++p_playlist->i_last_playlist_id;
zorglub's avatar
zorglub committed
124

125
    p_item->p_parent = NULL;
zorglub's avatar
zorglub committed
126
127
128
    p_item->i_children = -1;
    p_item->pp_children = NULL;
    p_item->i_flags = 0;
129
    p_item->p_playlist = p_playlist;
zorglub's avatar
zorglub committed
130

131
    install_input_item_observer( p_playlist, p_input );
132

133
134
    pl_Release( p_item->p_playlist );

135
136
137
    return p_item;
}

138
139
140
141
142
143
144
/***************************************************************************
 * Playlist item destruction
 ***************************************************************************/

/** Delete a playlist item and detach its input item */
int playlist_ItemDelete( playlist_item_t *p_item )
{
145
146
    uninstall_input_item_observer( p_item->p_playlist, p_item->p_input );

147
148
149
150
151
152
    vlc_gc_decref( p_item->p_input );
    free( p_item );
    return VLC_SUCCESS;
}

/** Remove an input item when it appears from a root playlist item */
153
154
static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
                            playlist_item_t *p_root, vlc_bool_t b_do_stop )
155
156
157
158
159
160
161
162
{
    int i;
    for( i = 0 ; i< p_root->i_children ; i++ )
    {
        if( p_root->pp_children[i]->i_children == -1 &&
            p_root->pp_children[i]->p_input->i_id == i_input_id )
        {
            DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
163
            return VLC_SUCCESS;
164
165
166
        }
        else if( p_root->pp_children[i]->i_children >= 0 )
        {
167
168
            int i_ret = DeleteFromInput( p_playlist, i_input_id,
                                         p_root->pp_children[i], b_do_stop );
169
            if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
170
171
        }
    }
172
    return VLC_EGENERIC;
173
174
}

175
176
177
178
179
180
181
182
183
184
185
186
/** Remove an input item when it appears from a root playlist item */
int playlist_DeleteInputInParent( playlist_t *p_playlist, int i_input_id,
                                  playlist_item_t *p_root, vlc_bool_t b_locked )
{
    int i_ret;
    if( !b_locked ) PL_LOCK;
    i_ret = DeleteFromInput( p_playlist, i_input_id,
                             p_root, VLC_TRUE );
    if( !b_locked ) PL_UNLOCK;
    return i_ret;
}

187
188
189
/** Remove an input item from ONELEVEL and CATEGORY */
int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
                              vlc_bool_t b_locked )
190
{
191
    int i_ret1, i_ret2;
192
    if( !b_locked ) PL_LOCK;
193
194
195
    i_ret1 = DeleteFromInput( p_playlist, i_input_id,
                             p_playlist->p_root_category, VLC_TRUE );
    i_ret2 = DeleteFromInput( p_playlist, i_input_id,
196
197
                     p_playlist->p_root_onelevel, VLC_TRUE );
    if( !b_locked ) PL_UNLOCK;
198
199
    return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
                            VLC_SUCCESS : VLC_ENOITEM;
200
201
}

202
void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
203
{
204
    if( !b_locked ) PL_LOCK;
205
206
    playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
    playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
207
    if( !b_locked ) PL_UNLOCK;
208
}
209
210
211
212

/** Remove a playlist item from the playlist, given its id
 * This function is to be used only by the playlist */
int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
213
{
214
215
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,
                                                    VLC_TRUE );
216
217
    if( !p_item ) return VLC_EGENERIC;
    return DeleteInner( p_playlist, p_item, VLC_TRUE );
218
219
220
221
222
}

/***************************************************************************
 * Playlist item addition
 ***************************************************************************/
223
/** Add an item to the playlist or the media library
224
225
226
227
228
229
 * \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
230
 *        regardless of its size
231
 * \param b_playlist TRUE for playlist, FALSE for media library
232
 * \return The id of the playlist item
233
 */
234
235
int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
                  const char *psz_name, int i_mode, int i_pos,
236
                  vlc_bool_t b_playlist, vlc_bool_t b_locked )
237
{
238
    return playlist_AddExt( p_playlist, psz_uri, psz_name,
239
                            i_mode, i_pos, -1, NULL, 0, b_playlist, b_locked );
240
}
241

242
/**
243
 * Add a MRL into the playlist or the media library, duration and options given
244
245
246
247
248
249
250
 *
 * \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
251
 *        regardless of its size
252
253
254
 * \param i_duration length of the item in milliseconds.
 * \param ppsz_options an array of options
 * \param i_options the number of options
255
 * \param b_playlist TRUE for playlist, FALSE for media library
256
257
 * \return The id of the playlist item
*/
258
259
int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
                     const char *psz_name, int i_mode, int i_pos,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
260
                     mtime_t i_duration, const char *const *ppsz_options,
261
                     int i_options, vlc_bool_t b_playlist, vlc_bool_t b_locked )
262
{
263
    int i_ret;
264
265
266
    input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
                                              i_options, ppsz_options,
                                              i_duration );
267

268
269
    i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
                               b_locked );
270
271
272
    if( i_ret == VLC_SUCCESS )
        return p_input->i_id;
    return -1;
273
}
274

275
/** Add an input item to the playlist node */
276
int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
277
278
                       int i_mode, int i_pos, vlc_bool_t b_playlist,
                       vlc_bool_t b_locked )
279
{
zorglub's avatar
zorglub committed
280
    playlist_item_t *p_item_cat, *p_item_one;
281

282
283
284
    if( !p_playlist->b_doing_ml )
        PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
                                             p_input->psz_uri );
285

286
    if( !b_locked ) PL_LOCK;
287

288
    /* Add to ONELEVEL */
zorglub's avatar
zorglub committed
289
    p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
290
    if( p_item_one == NULL ) return VLC_ENOMEM;
291
292
    AddItem( p_playlist, p_item_one,
             b_playlist ? p_playlist->p_local_onelevel :
293
                          p_playlist->p_ml_onelevel , i_mode, i_pos );
294
295

    /* Add to CATEGORY */
zorglub's avatar
zorglub committed
296
    p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
297
    if( p_item_cat == NULL ) return VLC_ENOMEM;
298
    AddItem( p_playlist, p_item_cat,
299
             b_playlist ? p_playlist->p_local_category :
300
                          p_playlist->p_ml_category , i_mode, i_pos );
301

zorglub's avatar
zorglub committed
302
    GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
303

304
    if( !b_locked ) PL_UNLOCK;
305
306
307
308
309
310
311
312
    return VLC_SUCCESS;
}

/** Add an input item to p_direct_parent in the category tree, and to the
 *  matching top category in onelevel **/
int playlist_BothAddInput( playlist_t *p_playlist,
                           input_item_t *p_input,
                           playlist_item_t *p_direct_parent,
313
                           int i_mode, int i_pos,
314
                           int *i_cat, int *i_one, vlc_bool_t b_locked )
315
{
zorglub's avatar
zorglub committed
316
    playlist_item_t *p_item_cat, *p_item_one, *p_up;
317
    int i_top;
zorglub's avatar
zorglub committed
318
    assert( p_input );
319
    if( !b_locked ) PL_LOCK;
320
321

    /* Add to category */
zorglub's avatar
zorglub committed
322
    p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
323
    if( p_item_cat == NULL ) return VLC_ENOMEM;
324
    AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
325
326

    /* Add to onelevel */
327
    /** \todo make a faster case for ml import */
zorglub's avatar
zorglub committed
328
    p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
329
    if( p_item_one == NULL ) return VLC_ENOMEM;
330
331
332

    p_up = p_direct_parent;
    while( p_up->p_parent != p_playlist->p_root_category )
333
    {
334
        p_up = p_up->p_parent;
335
    }
336
    for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
zorglub's avatar
zorglub committed
337
    {
338
339
        if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
                             p_up->p_input->i_id )
340
        {
zorglub's avatar
zorglub committed
341
            AddItem( p_playlist, p_item_one,
342
343
                     p_playlist->p_root_onelevel->pp_children[i_top],
                     i_mode, i_pos );
344
345
            break;
        }
zorglub's avatar
zorglub committed
346
    }
zorglub's avatar
zorglub committed
347
    GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
zorglub's avatar
zorglub committed
348

349
350
351
    if( i_cat ) *i_cat = p_item_cat->i_id;
    if( i_one ) *i_one = p_item_one->i_id;

352
    if( !b_locked ) PL_UNLOCK;
zorglub's avatar
zorglub committed
353
    return VLC_SUCCESS;
354
}
gbazin's avatar
   
gbazin committed
355

356
357
358
359
/** Add an input item to a given node */
playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
                                         input_item_t *p_input,
                                         playlist_item_t *p_parent,
360
361
                                         int i_mode, int i_pos,
                                         vlc_bool_t b_locked )
gbazin's avatar
   
gbazin committed
362
{
363
    playlist_item_t *p_item;
zorglub's avatar
zorglub committed
364
365
    assert( p_input );
    assert( p_parent && p_parent->i_children != -1 );
gbazin's avatar
   
gbazin committed
366

367
    if( !b_locked ) PL_LOCK;
gbazin's avatar
   
gbazin committed
368

369
370
    p_item = playlist_ItemNewFromInput( p_playlist, p_input );
    if( p_item == NULL ) return NULL;
371
    AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
372

373
    if( !b_locked ) PL_UNLOCK;
374
375

    return p_item;
gbazin's avatar
   
gbazin committed
376
}
zorglub's avatar
zorglub committed
377

378
379
380
381
/*****************************************************************************
 * Playlist item misc operations
 *****************************************************************************/

zorglub's avatar
zorglub committed
382
/**
383
384
385
 * Transform an item to a node. Return the node in the category tree, or NULL
 * if not found there
 * This function must be entered without the playlist lock
zorglub's avatar
zorglub committed
386
 */
387
playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
388
389
                                      playlist_item_t *p_item,
                                      vlc_bool_t b_locked )
zorglub's avatar
zorglub committed
390
{
391
392

    playlist_item_t *p_item_in_category;
393
394
395
396
397
    /* What we do
     * Find the input in CATEGORY.
     *  - If we find it
     *    - change it to node
     *    - we'll return it at the end
398
399
     *    - If we are a direct child of onelevel root, change to node, else
     *      delete the input from ONELEVEL
400
401
402
403
404
405
406
     *  - If we don't find it, just change to node (we are probably in VLM)
     *    and return NULL
     *
     * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
     * useful for later BothAddInput )
     */

407
408
    if( !b_locked ) PL_LOCK;

409
    /* Fast track the media library, no time to loose */
410
411
    if( p_item == p_playlist->p_ml_category ) {
        if( !b_locked ) PL_UNLOCK;
412
        return p_item;
413
    }
414

415
    /** \todo First look if we don't already have it */
416
    p_item_in_category = playlist_ItemFindFromInputAndRoot(
417
                                            p_playlist, p_item->p_input->i_id,
zorglub's avatar
zorglub committed
418
419
                                            p_playlist->p_root_category,
                                            VLC_TRUE );
420
421

    if( p_item_in_category )
zorglub's avatar
zorglub committed
422
    {
423
424
        playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
                                            p_playlist, p_item->p_input->i_id,
zorglub's avatar
zorglub committed
425
426
                                            p_playlist->p_root_onelevel,
                                            VLC_TRUE );
427
        /* We already have it, and there is nothing more to do */
428
        ChangeToNode( p_playlist, p_item_in_category );
429
430

        if( !p_item_in_one )
431
432
        {
            if( !b_locked ) PL_UNLOCK;
433
            return p_item_in_category;
434
        }
435

436
        /* Item in one is a root, change it to node */
437
438
439
440
        if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
            ChangeToNode( p_playlist, p_item_in_one );
        else
        {
441
442
            DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
                             p_playlist->p_root_onelevel, VLC_FALSE );
443
        }
zorglub's avatar
zorglub committed
444
        p_playlist->b_reset_currently_playing = VLC_TRUE;
445
        vlc_cond_signal( &p_playlist->object_wait );
446
447
        var_SetInteger( p_playlist, "item-change", p_item_in_category->
                                                        p_input->i_id );
448
        if( !b_locked ) PL_UNLOCK;
449
450
451
452
453
        return p_item_in_category;
    }
    else
    {
        ChangeToNode( p_playlist, p_item );
454
        if( !b_locked ) PL_UNLOCK;
455
        return NULL;
zorglub's avatar
zorglub committed
456
457
458
    }
}

459
460
461
462
463
/** Find an item within a root, given its input id.
 * \return the first found item, or NULL if not found
 */
playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
                                                    int i_input_id,
zorglub's avatar
zorglub committed
464
465
                                                    playlist_item_t *p_root,
                                                    vlc_bool_t b_items_only )
466
467
468
469
{
    int i;
    for( i = 0 ; i< p_root->i_children ; i++ )
    {
zorglub's avatar
zorglub committed
470
        if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
471
472
473
474
475
476
477
478
            p_root->pp_children[i]->p_input->i_id == i_input_id )
        {
            return p_root->pp_children[i];
        }
        else if( p_root->pp_children[i]->i_children >= 0 )
        {
            playlist_item_t *p_search =
                 playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
zorglub's avatar
zorglub committed
479
480
                                                    p_root->pp_children[i],
                                                    b_items_only );
481
482
483
484
485
            if( p_search ) return p_search;
        }
    }
    return NULL;
}
zorglub's avatar
zorglub committed
486
487


zorglub's avatar
zorglub committed
488
489
490
491
492
static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
                     playlist_item_t *p_node, int i_newpos )
{
    int j;
    playlist_item_t *p_detach = p_item->p_parent;
493
494
    (void)p_playlist;

zorglub's avatar
zorglub committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
    if( p_node->i_children == -1 ) return VLC_EGENERIC;

    for( j = 0; j < p_detach->i_children; j++ )
    {
         if( p_detach->pp_children[j] == p_item ) break;
    }
    REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );

    /* Attach to new parent */
    INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
    p_item->p_parent = p_node;

    return VLC_SUCCESS;
}

zorglub's avatar
zorglub committed
510
/**
511
512
513
 * Moves an item
 *
 * This function must be entered with the playlist lock
zorglub's avatar
zorglub committed
514
 *
515
516
517
518
519
 * \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
zorglub's avatar
zorglub committed
520
 */
521
522
523
int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
                       playlist_item_t *p_node, int i_newpos )
{
524
    int i_ret;
zorglub's avatar
zorglub committed
525
526
527
    /* Drop on a top level node. Move in the two trees */
    if( p_node->p_parent == p_playlist->p_root_category ||
        p_node->p_parent == p_playlist->p_root_onelevel )
528
    {
zorglub's avatar
zorglub committed
529
530
        /* Fixme: avoid useless lookups but we need some clean helpers */
        {
zorglub's avatar
zorglub committed
531
532
533
534
            /* Fixme: if we try to move a node on a top-level node, it will
             * fail because the node doesn't exist in onelevel and we will
             * do some shit in onelevel. We should recursively move all items
             * within the node */
zorglub's avatar
zorglub committed
535
536
537
538
539
540
541
542
543
544
            playlist_item_t *p_node_onelevel;
            playlist_item_t *p_item_onelevel;
            p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
                                                p_node->p_input->i_id,
                                                p_playlist->p_root_onelevel,
                                                VLC_FALSE );
            p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
                                                p_item->p_input->i_id,
                                                p_playlist->p_root_onelevel,
                                                VLC_FALSE );
zorglub's avatar
zorglub committed
545
546
            if( p_node_onelevel && p_item_onelevel )
                TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
zorglub's avatar
zorglub committed
547
548
549
550
551
552
553
554
555
556
557
558
        }
        {
            playlist_item_t *p_node_category;
            playlist_item_t *p_item_category;
            p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
                                                p_node->p_input->i_id,
                                                p_playlist->p_root_category,
                                                VLC_FALSE );
            p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
                                                p_item->p_input->i_id,
                                                p_playlist->p_root_category,
                                                VLC_FALSE );
zorglub's avatar
zorglub committed
559
560
            if( p_node_category && p_item_category )
                TreeMove( p_playlist, p_item_category, p_node_category, 0 );
zorglub's avatar
zorglub committed
561
        }
562
        i_ret = VLC_SUCCESS;
563
    }
zorglub's avatar
zorglub committed
564
    else
565
566
567
568
        i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
    p_playlist->b_reset_currently_playing = VLC_TRUE;
    vlc_cond_signal( &p_playlist->object_wait );
    return i_ret;
569
570
571
572
}

/** Send a notification that an item has been added to a node */
void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
573
                             int i_node_id, vlc_bool_t b_signal )
574
575
576
577
578
579
{
    vlc_value_t val;
    playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
    p_add->i_item = i_item_id;
    p_add->i_node = i_node_id;
    val.p_address = p_add;
zorglub's avatar
zorglub committed
580
    p_playlist->b_reset_currently_playing = VLC_TRUE;
581
582
    if( b_signal )
        vlc_cond_signal( &p_playlist->object_wait );
583
584
585
586
587
588
589
590
591
    var_Set( p_playlist, "item-append", val );
    free( p_add );
}

/*****************************************************************************
 * Playlist item accessors
 *****************************************************************************/

/** Set the name of a playlist item */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
592
int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
zorglub's avatar
zorglub committed
593
594
595
{
    if( psz_name && p_item )
    {
596
        input_ItemSetName( p_item->p_input, psz_name );
zorglub's avatar
zorglub committed
597
598
599
600
601
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}

602
603
604
605
606
/***************************************************************************
 * The following functions are local
 ***************************************************************************/

/* Enqueue an item for preparsing, and play it, if needed */
607
608
609
static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
                           playlist_item_t *p_item_cat,
                           playlist_item_t *p_item_one )
610
{
zorglub's avatar
zorglub committed
611
    if( (i_mode & PLAYLIST_GO ) )
zorglub's avatar
zorglub committed
612
    {
zorglub's avatar
zorglub committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
        playlist_item_t *p_parent = p_item_one;
        playlist_item_t *p_toplay = NULL;
        while( p_parent )
        {
            if( p_parent == p_playlist->p_root_category )
            {
                p_toplay = p_item_cat; break;
            }
            else if( p_parent == p_playlist->p_root_onelevel )
            {
                p_toplay = p_item_one; break;
            }
            p_parent = p_parent->p_parent;
        }
        assert( p_toplay );
628
        p_playlist->request.b_request = VLC_TRUE;
629
        p_playlist->request.i_skip = 0;
zorglub's avatar
zorglub committed
630
        p_playlist->request.p_item = p_toplay;
631
632
        if( p_playlist->p_input )
            input_StopThread( p_playlist->p_input );
zorglub's avatar
zorglub committed
633
        p_playlist->request.i_status = PLAYLIST_RUNNING;
634
        vlc_cond_signal( &p_playlist->object_wait );
635
    }
636
637
638
639
    /* Preparse if PREPARSE or SPREPARSE & not enough meta */
    if( p_playlist->b_auto_preparse &&
          (i_mode & PLAYLIST_PREPARSE ||
          ( i_mode & PLAYLIST_SPREPARSE &&
640
641
            ( EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) ||
            ( EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) ) )
642
          ) ) )
zorglub's avatar
zorglub committed
643
        playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
644
    /* If we already have it, signal it */
645
646
647
    else if( !EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) &&
             !EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) )
        input_item_SetPreparsed( p_item_cat->p_input, VLC_TRUE );
648
649
650
}

/* Add the playlist item to the requested node and fire a notification */
651
652
static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
                     playlist_item_t *p_node, int i_mode, int i_pos )
653
{
zorglub's avatar
zorglub committed
654
655
    ARRAY_APPEND(p_playlist->items, p_item);
    ARRAY_APPEND(p_playlist->all_items, p_item);
656
657
658
659
660
661
    p_playlist->i_enabled ++;

    if( i_pos == PLAYLIST_END )
        playlist_NodeAppend( p_playlist, p_item, p_node );
    else
        playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
zorglub's avatar
zorglub committed
662

663
    if( !p_playlist->b_doing_ml )
664
665
        playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
                                 !( i_mode & PLAYLIST_NO_REBUILD ) );
666
667
668
}

/* Actually convert an item to a node */
669
static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
670
671
672
673
674
675
{
    int i;
    if( p_item->i_children == -1 )
        p_item->i_children = 0;

    /* Remove it from the array of available items */
zorglub's avatar
zorglub committed
676
677
678
    ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
    if( i != -1 )
        ARRAY_REMOVE( p_playlist->items, i );
679
680
681
}

/* Do the actual removal */
682
683
static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
                        vlc_bool_t b_stop )
684
{
zorglub's avatar
zorglub committed
685
    int i;
686
    int i_id = p_item->i_id;
687
    vlc_bool_t b_delay_deletion = VLC_FALSE;
688
689
690
691
692

    if( p_item->i_children > -1 )
    {
        return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
    }
zorglub's avatar
zorglub committed
693
    p_playlist->b_reset_currently_playing = VLC_TRUE;
694
695
696
    var_SetInteger( p_playlist, "item-deleted", i_id );

    /* Remove the item from the bank */
zorglub's avatar
zorglub committed
697
698
699
    ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
    if( i != -1 )
        ARRAY_REMOVE( p_playlist->all_items, i );
zorglub's avatar
zorglub committed
700

701
702
    /* Check if it is the current item */
    if( p_playlist->status.p_item == p_item )
zorglub's avatar
zorglub committed
703
    {
704
705
        /* Hack we don't call playlist_Control for lock reasons */
        if( b_stop )
zorglub's avatar
zorglub committed
706
        {
zorglub's avatar
zorglub committed
707
            p_playlist->request.i_status = PLAYLIST_STOPPED;
708
709
710
            p_playlist->request.b_request = VLC_TRUE;
            p_playlist->request.p_item = NULL;
            msg_Info( p_playlist, "stopping playback" );
711
            vlc_cond_signal( &p_playlist->object_wait );
zorglub's avatar
zorglub committed
712
        }
713
        b_delay_deletion = VLC_TRUE;
zorglub's avatar
zorglub committed
714
    }
715

716
    PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
717
718
719
720

    /* Remove the item from its parent */
    playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );

721
    if( !b_delay_deletion )
722
723
        playlist_ItemDelete( p_item );
    else
zorglub's avatar
zorglub committed
724
725
    {
        PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
726
        p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
zorglub's avatar
zorglub committed
727
    }
728
729

    return VLC_SUCCESS;
zorglub's avatar
zorglub committed
730
}