item.c 23.9 KB
Newer Older
1
/*****************************************************************************
2
 * item.c : Playlist item creation/deletion/add/removal functions
3
 *****************************************************************************
4
 * Copyright (C) 1999-2004 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>
Sam Hocevar's avatar
Sam Hocevar committed
25
#include <vlc/input.h>
zorglub's avatar
zorglub committed
26
#include <assert.h>
27
28
#include <vlc_playlist.h>
#include "playlist_internal.h"
29

30
31
32
void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
              playlist_item_t *p_node, int i_pos );
void GoAndPreparse( playlist_t *p_playlist, int i_mode,
zorglub's avatar
zorglub committed
33
                    playlist_item_t *, playlist_item_t * );
34
35
36
void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
                          vlc_bool_t b_stop );
zorglub's avatar
zorglub committed
37

38
39
40
/*****************************************************************************
 * Playlist item creation
 *****************************************************************************/
41
42
43
playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
                                            const char *psz_uri,
                                            const char *psz_name,
44
                                            int i_options,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
45
                                            const char *const *ppsz_options,
46
                                            int i_duration, int i_type )
47
{
48
    input_item_t *p_input;
49
    if( psz_uri == NULL ) return NULL;
50
    p_input = input_ItemNewWithType( p_obj, psz_uri,
51
52
                                     psz_name, i_options, ppsz_options,
                                     i_duration, i_type );
53
54
    return playlist_ItemNewFromInput( p_obj, p_input );
}
55

56
playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
57
                                              input_item_t *p_input )
58
{
59
    DECMALLOC_NULL( p_item, playlist_item_t );
60
61
    playlist_t *p_playlist = p_obj->p_libvlc->p_playlist;
    vlc_object_yield( p_playlist );
zorglub's avatar
zorglub committed
62

63
64
    p_item->p_input = p_input;
    vlc_gc_incref( p_item->p_input );
65

66
    p_item->i_id = ++p_playlist->i_last_playlist_id;
zorglub's avatar
zorglub committed
67

68
    p_item->p_parent = NULL;
zorglub's avatar
zorglub committed
69
70
71
72
    p_item->i_children = -1;
    p_item->pp_children = NULL;
    p_item->i_flags = 0;

73
    vlc_object_release( p_playlist );
74

75
76
77
    return p_item;
}

78
79
80
81
82
83
84
85
86
87
88
89
90
/***************************************************************************
 * Playlist item destruction
 ***************************************************************************/

/** Delete a playlist item and detach its input item */
int playlist_ItemDelete( playlist_item_t *p_item )
{
    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 */
91
92
static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
                            playlist_item_t *p_root, vlc_bool_t b_do_stop )
93
94
95
96
97
98
99
100
{
    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 );
101
            return VLC_SUCCESS;
102
103
104
        }
        else if( p_root->pp_children[i]->i_children >= 0 )
        {
105
106
            int i_ret = DeleteFromInput( p_playlist, i_input_id,
                                         p_root->pp_children[i], b_do_stop );
107
            if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
108
109
        }
    }
110
    return VLC_EGENERIC;
111
112
}

113
114
115
/** Remove an input item from ONELEVEL and CATEGORY */
int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
                              vlc_bool_t b_locked )
116
{
117
118
119
120
121
122
123
    if( !b_locked ) PL_LOCK;
    DeleteFromInput( p_playlist, i_input_id,
                     p_playlist->p_root_category, VLC_TRUE );
    DeleteFromInput( p_playlist, i_input_id,
                     p_playlist->p_root_onelevel, VLC_TRUE );
    if( !b_locked ) PL_UNLOCK;
    return VLC_SUCCESS;
124
125
126
}

/** Clear the playlist */
127
void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
128
{
129
    if( !b_locked ) PL_LOCK;
130
131
    playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
    playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
132
    if( !b_locked ) PL_UNLOCK;
133
}
134
135
136
137

/** 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 )
138
{
139
140
141
    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,  VLC_TRUE );
    if( !p_item ) return VLC_EGENERIC;
    return DeleteInner( p_playlist, p_item, VLC_TRUE );
142
143
144
145
146
}

/***************************************************************************
 * Playlist item addition
 ***************************************************************************/
147
/** Add an item to the playlist or the media library
148
149
150
151
152
153
 * \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
154
 *        regardless of its size
155
 * \param b_playlist TRUE for playlist, FALSE for media library
156
 * \return The id of the playlist item
157
 */
158
159
int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
                  const char *psz_name, int i_mode, int i_pos,
160
                  vlc_bool_t b_playlist )
161
{
162
163
    return playlist_AddExt( p_playlist, psz_uri, psz_name,
                            i_mode, i_pos, -1, NULL, 0, b_playlist );
164
}
165

166
/**
167
 * Add a MRL into the playlist or the media library, duration and options given
168
169
170
171
172
173
174
 *
 * \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
175
 *        regardless of its size
176
177
178
 * \param i_duration length of the item in milliseconds.
 * \param ppsz_options an array of options
 * \param i_options the number of options
179
 * \param b_playlist TRUE for playlist, FALSE for media library
180
181
 * \return The id of the playlist item
*/
182
183
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
184
                     mtime_t i_duration, const char *const *ppsz_options,
185
                     int i_options, vlc_bool_t b_playlist )
186
187
188
189
{
    input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
                                              i_options, ppsz_options,
                                              i_duration );
190

191
    return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist );
192
}
193

194
/** Add an input item to the playlist node */
195
196
int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
                      int i_mode, int i_pos, vlc_bool_t b_playlist )
197
{
zorglub's avatar
zorglub committed
198
    playlist_item_t *p_item_cat, *p_item_one;
199

200
201
202
    if( !p_playlist->b_doing_ml )
        PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
                                             p_input->psz_uri );
203

204
    vlc_mutex_lock( &p_playlist->object_lock );
205

206
    /* Add to ONELEVEL */
zorglub's avatar
zorglub committed
207
208
    p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
    if( p_item_one == NULL ) return VLC_EGENERIC;
209
210
    AddItem( p_playlist, p_item_one,
             b_playlist ? p_playlist->p_local_onelevel :
211
                          p_playlist->p_ml_onelevel , i_pos );
212
213

    /* Add to CATEGORY */
zorglub's avatar
zorglub committed
214
215
    p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
    if( p_item_cat == NULL ) return VLC_EGENERIC;
216
    AddItem( p_playlist, p_item_cat,
217
             b_playlist ? p_playlist->p_local_category :
218
                          p_playlist->p_ml_category , i_pos );
219

zorglub's avatar
zorglub committed
220
    GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
221

222
223
224
225
226
227
228
229
230
    vlc_mutex_unlock( &p_playlist->object_lock );
    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,
231
232
                           int i_mode, int i_pos,
                           int *i_cat, int *i_one )
233
{
zorglub's avatar
zorglub committed
234
    playlist_item_t *p_item_cat, *p_item_one, *p_up;
235
    int i_top;
zorglub's avatar
zorglub committed
236
    assert( p_input );
237
238
239
    vlc_mutex_lock( & p_playlist->object_lock );

    /* Add to category */
zorglub's avatar
zorglub committed
240
241
242
    p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
    if( p_item_cat == NULL ) return VLC_EGENERIC;
    AddItem( p_playlist, p_item_cat, p_direct_parent, i_pos );
243
244

    /* Add to onelevel */
245
    /** \todo make a faster case for ml import */
zorglub's avatar
zorglub committed
246
247
    p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
    if( p_item_one == NULL ) return VLC_EGENERIC;
248
249
250

    p_up = p_direct_parent;
    while( p_up->p_parent != p_playlist->p_root_category )
251
    {
252
        p_up = p_up->p_parent;
253
    }
254
    for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
zorglub's avatar
zorglub committed
255
    {
256
257
        if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
                             p_up->p_input->i_id )
258
        {
zorglub's avatar
zorglub committed
259
            AddItem( p_playlist, p_item_one,
260
261
262
                     p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
            break;
        }
zorglub's avatar
zorglub committed
263
    }
zorglub's avatar
zorglub committed
264
    GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
zorglub's avatar
zorglub committed
265

266
267
268
    if( i_cat ) *i_cat = p_item_cat->i_id;
    if( i_one ) *i_one = p_item_one->i_id;

269
    vlc_mutex_unlock( &p_playlist->object_lock );
zorglub's avatar
zorglub committed
270
    return VLC_SUCCESS;
271
}
gbazin's avatar
   
gbazin committed
272

273
274
275
276
277
/** 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,
                                         int i_mode, int i_pos )
gbazin's avatar
   
gbazin committed
278
{
279
    playlist_item_t *p_item;
zorglub's avatar
zorglub committed
280
281
    assert( p_input );
    assert( p_parent && p_parent->i_children != -1 );
gbazin's avatar
   
gbazin committed
282

283
    vlc_mutex_lock( &p_playlist->object_lock );
gbazin's avatar
   
gbazin committed
284

285
286
287
288
289
290
291
    p_item = playlist_ItemNewFromInput( p_playlist, p_input );
    if( p_item == NULL ) return NULL;
    AddItem( p_playlist, p_item, p_parent, i_pos );

    vlc_mutex_unlock( &p_playlist->object_lock );

    return p_item;
gbazin's avatar
   
gbazin committed
292
}
zorglub's avatar
zorglub committed
293

294
295
296
297
/*****************************************************************************
 * Playlist item misc operations
 *****************************************************************************/

zorglub's avatar
zorglub committed
298
/**
299
300
301
 * 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
302
 */
303
playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
304
305
                                      playlist_item_t *p_item,
                                      vlc_bool_t b_locked )
zorglub's avatar
zorglub committed
306
{
307
308

    playlist_item_t *p_item_in_category;
309
310
311
312
313
    /* What we do
     * Find the input in CATEGORY.
     *  - If we find it
     *    - change it to node
     *    - we'll return it at the end
314
315
     *    - If we are a direct child of onelevel root, change to node, else
     *      delete the input from ONELEVEL
316
317
318
319
320
321
322
     *  - 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 )
     */

323
324
    if( !b_locked ) PL_LOCK;

325
    /* Fast track the media library, no time to loose */
326
327
    if( p_item == p_playlist->p_ml_category ) {
        if( !b_locked ) PL_UNLOCK;
328
        return p_item;
329
    }
330

331
    /** \todo First look if we don't already have it */
332
    p_item_in_category = playlist_ItemFindFromInputAndRoot(
333
                                            p_playlist, p_item->p_input->i_id,
zorglub's avatar
zorglub committed
334
335
                                            p_playlist->p_root_category,
                                            VLC_TRUE );
336
337

    if( p_item_in_category )
zorglub's avatar
zorglub committed
338
    {
339
340
        playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
                                            p_playlist, p_item->p_input->i_id,
zorglub's avatar
zorglub committed
341
342
                                            p_playlist->p_root_onelevel,
                                            VLC_TRUE );
343
        assert( p_item_in_one );
344
        ChangeToNode( p_playlist, p_item_in_category );
345
        /* Item in one is a root, change it to node */
346
347
348
349
        if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
            ChangeToNode( p_playlist, p_item_in_one );
        else
        {
350
351
            DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
                             p_playlist->p_root_onelevel, VLC_FALSE );
352
        }
zorglub's avatar
zorglub committed
353
        p_playlist->b_reset_currently_playing = VLC_TRUE;
354
        vlc_cond_signal( &p_playlist->object_wait );
355
356
        var_SetInteger( p_playlist, "item-change", p_item_in_category->
                                                        p_input->i_id );
357
        if( !b_locked ) PL_UNLOCK;
358
359
360
361
362
        return p_item_in_category;
    }
    else
    {
        ChangeToNode( p_playlist, p_item );
363
        if( !b_locked ) PL_UNLOCK;
364
        return NULL;
zorglub's avatar
zorglub committed
365
366
367
    }
}

368
369
370
371
372
/** 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
373
374
                                                    playlist_item_t *p_root,
                                                    vlc_bool_t b_items_only )
375
376
377
378
{
    int i;
    for( i = 0 ; i< p_root->i_children ; i++ )
    {
zorglub's avatar
zorglub committed
379
        if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
380
381
382
383
384
385
386
387
            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
388
389
                                                    p_root->pp_children[i],
                                                    b_items_only );
390
391
392
393
394
            if( p_search ) return p_search;
        }
    }
    return NULL;
}
zorglub's avatar
zorglub committed
395
396


zorglub's avatar
zorglub committed
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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;
    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
417
/**
418
419
420
 * Moves an item
 *
 * This function must be entered with the playlist lock
zorglub's avatar
zorglub committed
421
 *
422
423
424
425
426
 * \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
427
 */
428
429
430
int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
                       playlist_item_t *p_node, int i_newpos )
{
431
    int i_ret;
zorglub's avatar
zorglub committed
432
433
434
    /* 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 )
435
    {
zorglub's avatar
zorglub committed
436
437
        /* Fixme: avoid useless lookups but we need some clean helpers */
        {
zorglub's avatar
zorglub committed
438
439
440
441
            /* 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
442
443
444
445
446
447
448
449
450
451
            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
452
453
            if( p_node_onelevel && p_item_onelevel )
                TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
zorglub's avatar
zorglub committed
454
455
456
457
458
459
460
461
462
463
464
465
        }
        {
            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
466
467
            if( p_node_category && p_item_category )
                TreeMove( p_playlist, p_item_category, p_node_category, 0 );
zorglub's avatar
zorglub committed
468
        }
469
        i_ret = VLC_SUCCESS;
470
    }
zorglub's avatar
zorglub committed
471
    else
472
473
474
475
        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;
476
477
478
479
480
481
482
483
484
485
486
}

/** Send a notification that an item has been added to a node */
void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
                             int i_node_id )
{
    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
487
    p_playlist->b_reset_currently_playing = VLC_TRUE;
488
    vlc_cond_signal( &p_playlist->object_wait );
489
490
491
492
493
494
495
496
497
    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
498
int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
zorglub's avatar
zorglub committed
499
500
501
{
    if( psz_name && p_item )
    {
502
503
        if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
        p_item->p_input->psz_name = strdup( psz_name );
zorglub's avatar
zorglub committed
504
505
506
507
508
        return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}

509
510
511
512
513
514
/***************************************************************************
 * The following functions are local
 ***************************************************************************/

/* Enqueue an item for preparsing, and play it, if needed */
void GoAndPreparse( playlist_t *p_playlist, int i_mode,
zorglub's avatar
zorglub committed
515
                    playlist_item_t *p_item_cat, playlist_item_t *p_item_one )
516
{
zorglub's avatar
zorglub committed
517
    if( (i_mode & PLAYLIST_GO ) )
zorglub's avatar
zorglub committed
518
    {
zorglub's avatar
zorglub committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
        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 );
534
        p_playlist->request.b_request = VLC_TRUE;
535
        p_playlist->request.i_skip = 0;
zorglub's avatar
zorglub committed
536
        p_playlist->request.p_item = p_toplay;
537
538
        if( p_playlist->p_input )
            input_StopThread( p_playlist->p_input );
zorglub's avatar
zorglub committed
539
        p_playlist->request.i_status = PLAYLIST_RUNNING;
540
        vlc_cond_signal( &p_playlist->object_wait );
541
    }
542
543
544
545
546
547
548
549
550
551
    /* Preparse if PREPARSE or SPREPARSE & not enough meta */
    if( p_playlist->b_auto_preparse &&
          (i_mode & PLAYLIST_PREPARSE ||
          ( i_mode & PLAYLIST_SPREPARSE &&
            ( !p_item_cat->p_input->p_meta || (p_item_cat->p_input->p_meta &&
              ( EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) ||
                EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
              )
            )
          ) ) )
zorglub's avatar
zorglub committed
552
        playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
553
554
555
556
557
    /* If we already have it, signal it */
    else if( p_item_cat->p_input->p_meta &&
             !EMPTY_STR( p_item_cat->p_input->p_meta->psz_artist ) &&
             !EMPTY_STR( p_item_cat->p_input->p_meta->psz_album ) )
        p_item_cat->p_input->p_meta->i_status = ITEM_PREPARSED;
558
559
560
561
562
563
}

/* Add the playlist item to the requested node and fire a notification */
void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
              playlist_item_t *p_node, int i_pos )
{
zorglub's avatar
zorglub committed
564
565
    ARRAY_APPEND(p_playlist->items, p_item);
    ARRAY_APPEND(p_playlist->all_items, p_item);
566
567
568
569
570
571
    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
572

573
574
    if( !p_playlist->b_doing_ml )
        playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
575
576
577
578
579
580
581
582
583
584
}

/* Actually convert an item to a node */
void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
{
    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
585
586
587
    ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
    if( i != -1 )
        ARRAY_REMOVE( p_playlist->items, i );
588
589
590
591
592
593
}

/* Do the actual removal */
int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
                vlc_bool_t b_stop )
{
zorglub's avatar
zorglub committed
594
    int i;
595
596
597
598
599
600
601
    int i_id = p_item->i_id;
    vlc_bool_t b_flag = VLC_FALSE;

    if( p_item->i_children > -1 )
    {
        return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
    }
zorglub's avatar
zorglub committed
602
    p_playlist->b_reset_currently_playing = VLC_TRUE;
603
604
605
    var_SetInteger( p_playlist, "item-deleted", i_id );

    /* Remove the item from the bank */
zorglub's avatar
zorglub committed
606
607
608
    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
609

610
611
    /* Check if it is the current item */
    if( p_playlist->status.p_item == p_item )
zorglub's avatar
zorglub committed
612
    {
613
614
        /* Hack we don't call playlist_Control for lock reasons */
        if( b_stop )
zorglub's avatar
zorglub committed
615
        {
zorglub's avatar
zorglub committed
616
            p_playlist->request.i_status = PLAYLIST_STOPPED;
617
618
619
            p_playlist->request.b_request = VLC_TRUE;
            p_playlist->request.p_item = NULL;
            msg_Info( p_playlist, "stopping playback" );
620
            vlc_cond_signal( &p_playlist->object_wait );
zorglub's avatar
zorglub committed
621
        }
622
        b_flag = VLC_TRUE;
zorglub's avatar
zorglub committed
623
    }
624

625
    PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
626
627
628
629
630
631
632

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

    if( b_flag == VLC_FALSE )
        playlist_ItemDelete( p_item );
    else
zorglub's avatar
zorglub committed
633
634
    {
        PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
635
        p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
zorglub's avatar
zorglub committed
636
    }
637
638

    return VLC_SUCCESS;
zorglub's avatar
zorglub committed
639
}