]> git.sesse.net Git - vlc/blob - src/playlist/item.c
playlist/item.c: Rework item addition to make it work better with the current playlis...
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item creation/deletion/add/removal functions
3  *****************************************************************************
4  * Copyright (C) 1999-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #include <vlc/vlc.h>
25 #include <assert.h>
26 #include <vlc_playlist.h>
27 #include "playlist_internal.h"
28
29 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
30                      playlist_item_t *p_node, int i_mode, int i_pos );
31 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
32                            playlist_item_t *, playlist_item_t * );
33 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item );
34 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
35                         vlc_bool_t b_stop );
36
37 /*****************************************************************************
38  * An input item has gained a subitem (Event Callback)
39  *****************************************************************************/
40 static void input_item_subitem_added( const vlc_event_t * p_event,
41                                       void * user_data )
42 {
43     playlist_item_t *p_parent_playlist_item = user_data;
44     playlist_t * p_playlist = p_parent_playlist_item->p_playlist;
45     input_item_t * p_parent, * p_child;
46     playlist_item_t * p_child_in_category;
47     playlist_item_t * p_item_in_category;
48     vlc_bool_t b_play;
49
50     p_parent = p_event->p_obj;
51     p_child = p_event->u.input_item_subitem_added.p_new_child;
52
53     PL_LOCK;
54     b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
55
56     /* This part is really hakish, but this playlist system isn't simple */
57     /* First check if we haven't already added the item as we are 
58      * listening using the onelevel and the category representent 
59      * (Because of the playlist design) */
60     p_child_in_category = playlist_ItemFindFromInputAndRoot(
61                             p_playlist, p_child->i_id,
62                             p_playlist->p_root_category,
63                             VLC_FALSE /* Only non-node */ );
64
65     if( !p_child_in_category )
66     {
67         b_play = b_play && p_item_in_category == p_playlist->status.p_item;
68
69         /* Then, transform to a node if needed */
70         p_item_in_category = playlist_ItemFindFromInputAndRoot(
71                                 p_playlist, p_parent->i_id,
72                                 p_playlist->p_root_category,
73                                 VLC_FALSE /* Only non-node */ );
74         if( !p_item_in_category )
75         {
76             /* Item may have been removed */
77             PL_UNLOCK;
78             return
79         }
80
81         /* If this item is already a node don't transform it */
82         if( p_item_in_category->i_children == -1 )
83         {
84             p_item_in_category = playlist_ItemToNode( p_playlist,
85                     p_item_in_category, VLC_TRUE );
86         }
87         
88         playlist_BothAddInput( p_playlist, p_child, p_item_in_category, 
89                 PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
90                 NULL, NULL,  VLC_TRUE );
91
92         if( b_play )
93         {
94             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
95                           VLC_TRUE, p_item_in_category, NULL );
96         }
97     }
98     
99     PL_UNLOCK;
100
101 }
102
103 /*****************************************************************************
104  * Listen to vlc_InputItemAddSubItem event
105  *****************************************************************************/
106 static void install_input_item_observer( playlist_item_t * p_item,
107                                          input_item_t * p_input )
108 {
109     vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
110                       input_item_subitem_added,
111                       p_item );
112 }
113
114 static void uninstall_input_item_observer( playlist_item_t * p_item,
115                                            input_item_t * p_input )
116 {
117     vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
118                       input_item_subitem_added,
119                       p_item );
120                       
121 }
122
123 /*****************************************************************************
124  * Playlist item creation
125  *****************************************************************************/
126 playlist_item_t * playlist_ItemNewWithType( vlc_object_t *p_obj,
127                                             const char *psz_uri,
128                                             const char *psz_name,
129                                             int i_options,
130                                             const char *const *ppsz_options,
131                                             int i_duration, int i_type )
132 {
133     input_item_t *p_input;
134     if( psz_uri == NULL ) return NULL;
135     p_input = input_ItemNewWithType( p_obj, psz_uri,
136                                      psz_name, i_options, ppsz_options,
137                                      i_duration, i_type );
138     return playlist_ItemNewFromInput( p_obj, p_input );
139 }
140
141 playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
142                                               input_item_t *p_input )
143 {
144     DECMALLOC_NULL( p_item, playlist_item_t );
145     playlist_t *p_playlist = pl_Yield( p_obj );
146
147     p_item->p_input = p_input;
148     vlc_gc_incref( p_item->p_input );
149
150     p_item->i_id = ++p_playlist->i_last_playlist_id;
151
152     p_item->p_parent = NULL;
153     p_item->i_children = -1;
154     p_item->pp_children = NULL;
155     p_item->i_flags = 0;
156     p_item->p_playlist = p_playlist;
157
158     install_input_item_observer( p_item, p_input );
159
160     pl_Release( p_item->p_playlist );
161
162     return p_item;
163 }
164
165 /***************************************************************************
166  * Playlist item destruction
167  ***************************************************************************/
168
169 /** Delete a playlist item and detach its input item */
170 int playlist_ItemDelete( playlist_item_t *p_item )
171 {
172     uninstall_input_item_observer( p_item->p_playlist, p_item->p_input );
173
174     vlc_gc_decref( p_item->p_input );
175     free( p_item );
176     return VLC_SUCCESS;
177 }
178
179 /** Remove an input item when it appears from a root playlist item */
180 static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
181                             playlist_item_t *p_root, vlc_bool_t b_do_stop )
182 {
183     int i;
184     for( i = 0 ; i< p_root->i_children ; i++ )
185     {
186         if( p_root->pp_children[i]->i_children == -1 &&
187             p_root->pp_children[i]->p_input->i_id == i_input_id )
188         {
189             DeleteInner( p_playlist, p_root->pp_children[i], b_do_stop );
190             return VLC_SUCCESS;
191         }
192         else if( p_root->pp_children[i]->i_children >= 0 )
193         {
194             int i_ret = DeleteFromInput( p_playlist, i_input_id,
195                                          p_root->pp_children[i], b_do_stop );
196             if( i_ret == VLC_SUCCESS ) return VLC_SUCCESS;
197         }
198     }
199     return VLC_EGENERIC;
200 }
201
202 /** Remove an input item when it appears from a root playlist item */
203 int playlist_DeleteInputInParent( playlist_t *p_playlist, int i_input_id,
204                                   playlist_item_t *p_root, vlc_bool_t b_locked )
205 {
206     int i_ret;
207     if( !b_locked ) PL_LOCK;
208     i_ret = DeleteFromInput( p_playlist, i_input_id,
209                              p_root, VLC_TRUE );
210     if( !b_locked ) PL_UNLOCK;
211     return i_ret;
212 }
213
214 /** Remove an input item from ONELEVEL and CATEGORY */
215 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
216                               vlc_bool_t b_locked )
217 {
218     int i_ret1, i_ret2;
219     if( !b_locked ) PL_LOCK;
220     i_ret1 = DeleteFromInput( p_playlist, i_input_id,
221                              p_playlist->p_root_category, VLC_TRUE );
222     i_ret2 = DeleteFromInput( p_playlist, i_input_id,
223                      p_playlist->p_root_onelevel, VLC_TRUE );
224     if( !b_locked ) PL_UNLOCK;
225     return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
226                             VLC_SUCCESS : VLC_ENOITEM;
227 }
228
229 void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
230 {
231     if( !b_locked ) PL_LOCK;
232     playlist_NodeEmpty( p_playlist, p_playlist->p_root_category, VLC_TRUE );
233     playlist_NodeEmpty( p_playlist, p_playlist->p_root_onelevel, VLC_TRUE );
234     if( !b_locked ) PL_UNLOCK;
235 }
236
237 /** Remove a playlist item from the playlist, given its id
238  * This function is to be used only by the playlist */
239 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
240 {
241     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,
242                                                     VLC_TRUE );
243     if( !p_item ) return VLC_EGENERIC;
244     return DeleteInner( p_playlist, p_item, VLC_TRUE );
245 }
246
247 /***************************************************************************
248  * Playlist item addition
249  ***************************************************************************/
250 /** Add an item to the playlist or the media library
251  * \param p_playlist the playlist to add into
252  * \param psz_uri the mrl to add to the playlist
253  * \param psz_name a text giving a name or description of this item
254  * \param i_mode the mode used when adding
255  * \param i_pos the position in the playlist where to add. If this is
256  *        PLAYLIST_END the item will be added at the end of the playlist
257  *        regardless of its size
258  * \param b_playlist TRUE for playlist, FALSE for media library
259  * \return The id of the playlist item
260  */
261 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
262                   const char *psz_name, int i_mode, int i_pos,
263                   vlc_bool_t b_playlist, vlc_bool_t b_locked )
264 {
265     return playlist_AddExt( p_playlist, psz_uri, psz_name,
266                             i_mode, i_pos, -1, NULL, 0, b_playlist, b_locked );
267 }
268
269 /**
270  * Add a MRL into the playlist or the media library, duration and options given
271  *
272  * \param p_playlist the playlist to add into
273  * \param psz_uri the mrl to add to the playlist
274  * \param psz_name a text giving a name or description of this item
275  * \param i_mode the mode used when adding
276  * \param i_pos the position in the playlist where to add. If this is
277  *        PLAYLIST_END the item will be added at the end of the playlist
278  *        regardless of its size
279  * \param i_duration length of the item in milliseconds.
280  * \param ppsz_options an array of options
281  * \param i_options the number of options
282  * \param b_playlist TRUE for playlist, FALSE for media library
283  * \return The id of the playlist item
284 */
285 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
286                      const char *psz_name, int i_mode, int i_pos,
287                      mtime_t i_duration, const char *const *ppsz_options,
288                      int i_options, vlc_bool_t b_playlist, vlc_bool_t b_locked )
289 {
290     int i_ret;
291     input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
292                                               i_options, ppsz_options,
293                                               i_duration );
294
295     i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
296                                b_locked );
297     if( i_ret == VLC_SUCCESS )
298         return p_input->i_id;
299     return -1;
300 }
301
302 /** Add an input item to the playlist node */
303 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
304                        int i_mode, int i_pos, vlc_bool_t b_playlist,
305                        vlc_bool_t b_locked )
306 {
307     playlist_item_t *p_item_cat, *p_item_one;
308
309     if( !p_playlist->b_doing_ml )
310         PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
311                                              p_input->psz_uri );
312
313     if( !b_locked ) PL_LOCK;
314
315     /* Add to ONELEVEL */
316     p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
317     if( p_item_one == NULL ) return VLC_ENOMEM;
318     AddItem( p_playlist, p_item_one,
319              b_playlist ? p_playlist->p_local_onelevel :
320                           p_playlist->p_ml_onelevel , i_mode, i_pos );
321
322     /* Add to CATEGORY */
323     p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
324     if( p_item_cat == NULL ) return VLC_ENOMEM;
325     AddItem( p_playlist, p_item_cat,
326              b_playlist ? p_playlist->p_local_category :
327                           p_playlist->p_ml_category , i_mode, i_pos );
328
329     GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
330
331     if( !b_locked ) PL_UNLOCK;
332     return VLC_SUCCESS;
333 }
334
335 /** Add an input item to p_direct_parent in the category tree, and to the
336  *  matching top category in onelevel **/
337 int playlist_BothAddInput( playlist_t *p_playlist,
338                            input_item_t *p_input,
339                            playlist_item_t *p_direct_parent,
340                            int i_mode, int i_pos,
341                            int *i_cat, int *i_one, vlc_bool_t b_locked )
342 {
343     playlist_item_t *p_item_cat, *p_item_one, *p_up;
344     int i_top;
345     assert( p_input );
346     if( !b_locked ) PL_LOCK;
347
348     /* Add to category */
349     p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
350     if( p_item_cat == NULL ) return VLC_ENOMEM;
351     AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
352
353     /* Add to onelevel */
354     /** \todo make a faster case for ml import */
355     p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
356     if( p_item_one == NULL ) return VLC_ENOMEM;
357
358     p_up = p_direct_parent;
359     while( p_up->p_parent != p_playlist->p_root_category )
360     {
361         p_up = p_up->p_parent;
362     }
363     for( i_top = 0 ; i_top < p_playlist->p_root_onelevel->i_children; i_top++ )
364     {
365         if( p_playlist->p_root_onelevel->pp_children[i_top]->p_input->i_id ==
366                              p_up->p_input->i_id )
367         {
368             AddItem( p_playlist, p_item_one,
369                      p_playlist->p_root_onelevel->pp_children[i_top],
370                      i_mode, i_pos );
371             break;
372         }
373     }
374     GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
375
376     if( i_cat ) *i_cat = p_item_cat->i_id;
377     if( i_one ) *i_one = p_item_one->i_id;
378
379     if( !b_locked ) PL_UNLOCK;
380     return VLC_SUCCESS;
381 }
382
383 /** Add an input item to a given node */
384 playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
385                                          input_item_t *p_input,
386                                          playlist_item_t *p_parent,
387                                          int i_mode, int i_pos,
388                                          vlc_bool_t b_locked )
389 {
390     playlist_item_t *p_item;
391     assert( p_input );
392     assert( p_parent && p_parent->i_children != -1 );
393
394     if( !b_locked ) PL_LOCK;
395
396     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
397     if( p_item == NULL ) return NULL;
398     AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
399
400     if( !b_locked ) PL_UNLOCK;
401
402     return p_item;
403 }
404
405 /*****************************************************************************
406  * Playlist item misc operations
407  *****************************************************************************/
408
409 /**
410  * Transform an item to a node. Return the node in the category tree, or NULL
411  * if not found there
412  * This function must be entered without the playlist lock
413  */
414 playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
415                                       playlist_item_t *p_item,
416                                       vlc_bool_t b_locked )
417 {
418
419     playlist_item_t *p_item_in_category;
420     /* What we do
421      * Find the input in CATEGORY.
422      *  - If we find it
423      *    - change it to node
424      *    - we'll return it at the end
425      *    - If we are a direct child of onelevel root, change to node, else
426      *      delete the input from ONELEVEL
427      *  - If we don't find it, just change to node (we are probably in VLM)
428      *    and return NULL
429      *
430      * If we were in ONELEVEL, we thus retrieve the node in CATEGORY (will be
431      * useful for later BothAddInput )
432      */
433
434     if( !b_locked ) PL_LOCK;
435
436     /* Fast track the media library, no time to loose */
437     if( p_item == p_playlist->p_ml_category ) {
438         if( !b_locked ) PL_UNLOCK;
439         return p_item;
440     }
441
442     /** \todo First look if we don't already have it */
443     p_item_in_category = playlist_ItemFindFromInputAndRoot(
444                                             p_playlist, p_item->p_input->i_id,
445                                             p_playlist->p_root_category,
446                                             VLC_TRUE );
447
448     if( p_item_in_category )
449     {
450         playlist_item_t *p_item_in_one = playlist_ItemFindFromInputAndRoot(
451                                             p_playlist, p_item->p_input->i_id,
452                                             p_playlist->p_root_onelevel,
453                                             VLC_TRUE );
454         assert( p_item_in_one );
455
456         /* We already have it, and there is nothing more to do */
457         ChangeToNode( p_playlist, p_item_in_category );
458
459         /* Item in one is a root, change it to node */
460         if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
461             ChangeToNode( p_playlist, p_item_in_one );
462         else
463         {
464             DeleteFromInput( p_playlist, p_item_in_one->p_input->i_id,
465                              p_playlist->p_root_onelevel, VLC_FALSE );
466         }
467         p_playlist->b_reset_currently_playing = VLC_TRUE;
468         vlc_cond_signal( &p_playlist->object_wait );
469         var_SetInteger( p_playlist, "item-change", p_item_in_category->
470                                                         p_input->i_id );
471         if( !b_locked ) PL_UNLOCK;
472         return p_item_in_category;
473     }
474     else
475     {
476         ChangeToNode( p_playlist, p_item );
477         if( !b_locked ) PL_UNLOCK;
478         return NULL;
479     }
480 }
481
482 /** Find an item within a root, given its input id.
483  * \return the first found item, or NULL if not found
484  */
485 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
486                                                     int i_input_id,
487                                                     playlist_item_t *p_root,
488                                                     vlc_bool_t b_items_only )
489 {
490     int i;
491     for( i = 0 ; i< p_root->i_children ; i++ )
492     {
493         if( ( b_items_only ? p_root->pp_children[i]->i_children == -1 : 1 ) &&
494             p_root->pp_children[i]->p_input->i_id == i_input_id )
495         {
496             return p_root->pp_children[i];
497         }
498         else if( p_root->pp_children[i]->i_children >= 0 )
499         {
500             playlist_item_t *p_search =
501                  playlist_ItemFindFromInputAndRoot( p_playlist, i_input_id,
502                                                     p_root->pp_children[i],
503                                                     b_items_only );
504             if( p_search ) return p_search;
505         }
506     }
507     return NULL;
508 }
509
510
511 static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
512                      playlist_item_t *p_node, int i_newpos )
513 {
514     int j;
515     playlist_item_t *p_detach = p_item->p_parent;
516     (void)p_playlist;
517
518     if( p_node->i_children == -1 ) return VLC_EGENERIC;
519
520     for( j = 0; j < p_detach->i_children; j++ )
521     {
522          if( p_detach->pp_children[j] == p_item ) break;
523     }
524     REMOVE_ELEM( p_detach->pp_children, p_detach->i_children, j );
525
526     /* Attach to new parent */
527     INSERT_ELEM( p_node->pp_children, p_node->i_children, i_newpos, p_item );
528     p_item->p_parent = p_node;
529
530     return VLC_SUCCESS;
531 }
532
533 /**
534  * Moves an item
535  *
536  * This function must be entered with the playlist lock
537  *
538  * \param p_playlist the playlist
539  * \param p_item the item to move
540  * \param p_node the new parent of the item
541  * \param i_newpos the new position under this new parent
542  * \return VLC_SUCCESS or an error
543  */
544 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
545                        playlist_item_t *p_node, int i_newpos )
546 {
547     int i_ret;
548     /* Drop on a top level node. Move in the two trees */
549     if( p_node->p_parent == p_playlist->p_root_category ||
550         p_node->p_parent == p_playlist->p_root_onelevel )
551     {
552         /* Fixme: avoid useless lookups but we need some clean helpers */
553         {
554             /* Fixme: if we try to move a node on a top-level node, it will
555              * fail because the node doesn't exist in onelevel and we will
556              * do some shit in onelevel. We should recursively move all items
557              * within the node */
558             playlist_item_t *p_node_onelevel;
559             playlist_item_t *p_item_onelevel;
560             p_node_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
561                                                 p_node->p_input->i_id,
562                                                 p_playlist->p_root_onelevel,
563                                                 VLC_FALSE );
564             p_item_onelevel = playlist_ItemFindFromInputAndRoot( p_playlist,
565                                                 p_item->p_input->i_id,
566                                                 p_playlist->p_root_onelevel,
567                                                 VLC_FALSE );
568             if( p_node_onelevel && p_item_onelevel )
569                 TreeMove( p_playlist, p_item_onelevel, p_node_onelevel, 0 );
570         }
571         {
572             playlist_item_t *p_node_category;
573             playlist_item_t *p_item_category;
574             p_node_category = playlist_ItemFindFromInputAndRoot( p_playlist,
575                                                 p_node->p_input->i_id,
576                                                 p_playlist->p_root_category,
577                                                 VLC_FALSE );
578             p_item_category = playlist_ItemFindFromInputAndRoot( p_playlist,
579                                                 p_item->p_input->i_id,
580                                                 p_playlist->p_root_category,
581                                                 VLC_FALSE );
582             if( p_node_category && p_item_category )
583                 TreeMove( p_playlist, p_item_category, p_node_category, 0 );
584         }
585         i_ret = VLC_SUCCESS;
586     }
587     else
588         i_ret = TreeMove( p_playlist, p_item, p_node, i_newpos );
589     p_playlist->b_reset_currently_playing = VLC_TRUE;
590     vlc_cond_signal( &p_playlist->object_wait );
591     return i_ret;
592 }
593
594 /** Send a notification that an item has been added to a node */
595 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
596                              int i_node_id, vlc_bool_t b_signal )
597 {
598     vlc_value_t val;
599     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
600     p_add->i_item = i_item_id;
601     p_add->i_node = i_node_id;
602     val.p_address = p_add;
603     p_playlist->b_reset_currently_playing = VLC_TRUE;
604     if( b_signal )
605         vlc_cond_signal( &p_playlist->object_wait );
606     var_Set( p_playlist, "item-append", val );
607     free( p_add );
608 }
609
610 /*****************************************************************************
611  * Playlist item accessors
612  *****************************************************************************/
613
614 /** Set the name of a playlist item */
615 int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
616 {
617     if( psz_name && p_item )
618     {
619         input_ItemSetName( p_item->p_input, psz_name );
620         return VLC_SUCCESS;
621     }
622     return VLC_EGENERIC;
623 }
624
625 /***************************************************************************
626  * The following functions are local
627  ***************************************************************************/
628
629 /* Enqueue an item for preparsing, and play it, if needed */
630 static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
631                            playlist_item_t *p_item_cat,
632                            playlist_item_t *p_item_one )
633 {
634     if( (i_mode & PLAYLIST_GO ) )
635     {
636         playlist_item_t *p_parent = p_item_one;
637         playlist_item_t *p_toplay = NULL;
638         while( p_parent )
639         {
640             if( p_parent == p_playlist->p_root_category )
641             {
642                 p_toplay = p_item_cat; break;
643             }
644             else if( p_parent == p_playlist->p_root_onelevel )
645             {
646                 p_toplay = p_item_one; break;
647             }
648             p_parent = p_parent->p_parent;
649         }
650         assert( p_toplay );
651         p_playlist->request.b_request = VLC_TRUE;
652         p_playlist->request.i_skip = 0;
653         p_playlist->request.p_item = p_toplay;
654         if( p_playlist->p_input )
655             input_StopThread( p_playlist->p_input );
656         p_playlist->request.i_status = PLAYLIST_RUNNING;
657         vlc_cond_signal( &p_playlist->object_wait );
658     }
659     /* Preparse if PREPARSE or SPREPARSE & not enough meta */
660     if( p_playlist->b_auto_preparse &&
661           (i_mode & PLAYLIST_PREPARSE ||
662           ( i_mode & PLAYLIST_SPREPARSE &&
663             ( EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) ||
664             ( EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) ) )
665           ) ) )
666         playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
667     /* If we already have it, signal it */
668     else if( !EMPTY_STR( input_item_GetArtist( p_item_cat->p_input ) ) &&
669              !EMPTY_STR( input_item_GetAlbum( p_item_cat->p_input ) ) )
670         input_item_SetPreparsed( p_item_cat->p_input, VLC_TRUE );
671 }
672
673 /* Add the playlist item to the requested node and fire a notification */
674 static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
675                      playlist_item_t *p_node, int i_mode, int i_pos )
676 {
677     ARRAY_APPEND(p_playlist->items, p_item);
678     ARRAY_APPEND(p_playlist->all_items, p_item);
679     p_playlist->i_enabled ++;
680
681     if( i_pos == PLAYLIST_END )
682         playlist_NodeAppend( p_playlist, p_item, p_node );
683     else
684         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
685
686     if( !p_playlist->b_doing_ml )
687         playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
688                                  !( i_mode & PLAYLIST_NO_REBUILD ) );
689 }
690
691 /* Actually convert an item to a node */
692 static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
693 {
694     int i;
695     if( p_item->i_children == -1 )
696         p_item->i_children = 0;
697
698     /* Remove it from the array of available items */
699     ARRAY_BSEARCH( p_playlist->items,->i_id, int, p_item->i_id, i );
700     if( i != -1 )
701         ARRAY_REMOVE( p_playlist->items, i );
702 }
703
704 /* Do the actual removal */
705 static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
706                         vlc_bool_t b_stop )
707 {
708     int i;
709     int i_id = p_item->i_id;
710     vlc_bool_t b_delay_deletion = VLC_FALSE;
711
712     if( p_item->i_children > -1 )
713     {
714         return playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_FALSE );
715     }
716     p_playlist->b_reset_currently_playing = VLC_TRUE;
717     var_SetInteger( p_playlist, "item-deleted", i_id );
718
719     /* Remove the item from the bank */
720     ARRAY_BSEARCH( p_playlist->all_items,->i_id, int, i_id, i );
721     if( i != -1 )
722         ARRAY_REMOVE( p_playlist->all_items, i );
723
724     /* Check if it is the current item */
725     if( p_playlist->status.p_item == p_item )
726     {
727         /* Hack we don't call playlist_Control for lock reasons */
728         if( b_stop )
729         {
730             p_playlist->request.i_status = PLAYLIST_STOPPED;
731             p_playlist->request.b_request = VLC_TRUE;
732             p_playlist->request.p_item = NULL;
733             msg_Info( p_playlist, "stopping playback" );
734             vlc_cond_signal( &p_playlist->object_wait );
735         }
736         b_delay_deletion = VLC_TRUE;
737     }
738
739     PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
740
741     /* Remove the item from its parent */
742     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
743
744     if( !b_delay_deletion )
745         playlist_ItemDelete( p_item );
746     else
747     {
748         PL_DEBUG( "marking %s for further deletion", PLI_NAME( p_item ) );
749         p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
750     }
751
752     return VLC_SUCCESS;
753 }