]> git.sesse.net Git - vlc/blobdiff - src/playlist/item.c
Removes trailing spaces. Removes tabs.
[vlc] / src / playlist / item.c
index 86a26f8316d47d1d2e567d703b989cdde5da2d5b..28634fe85c32252daee5f4a6385b3cf314882d3e 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * item.c : Playlist item creation/deletion/add/removal functions
  *****************************************************************************
- * Copyright (C) 1999-2004 the VideoLAN team
+ * Copyright (C) 1999-2007 the VideoLAN team
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 #include <vlc/vlc.h>
-#include <vlc/input.h>
 #include <assert.h>
 #include <vlc_playlist.h>
 #include "playlist_internal.h"
 
-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,
-                    playlist_item_t *, playlist_item_t * );
-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 );
+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 );
+
+/*****************************************************************************
+ * 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_item_t *p_parent_playlist_item = user_data;
+    playlist_t * p_playlist = p_parent_playlist_item->p_playlist;
+    input_item_t * p_parent, * p_child;
+    playlist_item_t * p_child_in_category;
+    playlist_item_t * p_item_in_category;
+    vlc_bool_t b_play;
+
+    p_parent = p_event->p_obj;
+    p_child = p_event->u.input_item_subitem_added.p_new_child;
+
+    PL_LOCK;
+    b_play = var_CreateGetBool( p_playlist, "playlist-autostart" );
+
+    /* This part is really hakish, but this playlist system isn't simple */
+    /* First check if we haven't already added the item as we are
+     * listening using the onelevel and the category representent
+     * (Because of the playlist design) */
+    p_child_in_category = playlist_ItemFindFromInputAndRoot(
+                            p_playlist, p_child->i_id,
+                            p_playlist->p_root_category,
+                            VLC_FALSE /* Only non-node */ );
+
+    if( !p_child_in_category )
+    {
+        /* Then, transform to a node if needed */
+        p_item_in_category = playlist_ItemFindFromInputAndRoot(
+                                p_playlist, p_parent->i_id,
+                                p_playlist->p_root_category,
+                                VLC_FALSE /* Only non-node */ );
+        if( !p_item_in_category )
+        {
+            /* Item may have been removed */
+            PL_UNLOCK;
+            return;
+        }
+
+        b_play = b_play && p_item_in_category == p_playlist->status.p_item;
+
+        /* If this item is already a node don't transform it */
+        if( p_item_in_category->i_children == -1 )
+        {
+            p_item_in_category = playlist_ItemToNode( p_playlist,
+                    p_item_in_category, VLC_TRUE );
+            p_item_in_category->p_input->i_type = ITEM_TYPE_PLAYLIST;
+        }
+        playlist_BothAddInput( p_playlist, p_child, p_item_in_category,
+                PLAYLIST_APPEND | PLAYLIST_SPREPARSE , PLAYLIST_END,
+                NULL, NULL,  VLC_TRUE );
+
+        if( b_play )
+        {
+            playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
+                          VLC_TRUE, p_item_in_category, NULL );
+        }
+    }
+    PL_UNLOCK;
+
+}
+
+/*****************************************************************************
+ * Listen to vlc_InputItemAddSubItem event
+ *****************************************************************************/
+static void install_input_item_observer( playlist_item_t * p_item,
+                                         input_item_t * p_input )
+{
+    vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
+                      input_item_subitem_added,
+                      p_item );
+}
+
+static void uninstall_input_item_observer( playlist_item_t * p_item,
+                                           input_item_t * p_input )
+{
+    vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
+                      input_item_subitem_added,
+                      p_item );
+}
 
 /*****************************************************************************
  * Playlist item creation
@@ -57,8 +143,7 @@ playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
                                               input_item_t *p_input )
 {
     DECMALLOC_NULL( p_item, playlist_item_t );
-    playlist_t *p_playlist = p_obj->p_libvlc->p_playlist;
-    vlc_object_yield( p_playlist );
+    playlist_t *p_playlist = pl_Yield( p_obj );
 
     p_item->p_input = p_input;
     vlc_gc_incref( p_item->p_input );
@@ -69,8 +154,11 @@ playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
     p_item->i_children = -1;
     p_item->pp_children = NULL;
     p_item->i_flags = 0;
+    p_item->p_playlist = p_playlist;
+
+    install_input_item_observer( p_item, p_input );
 
-    vlc_object_release( p_playlist );
+    pl_Release( p_item->p_playlist );
 
     return p_item;
 }
@@ -82,6 +170,8 @@ playlist_item_t *__playlist_ItemNewFromInput( vlc_object_t *p_obj,
 /** Delete a playlist item and detach its input item */
 int playlist_ItemDelete( playlist_item_t *p_item )
 {
+    uninstall_input_item_observer( p_item, p_item->p_input );
+
     vlc_gc_decref( p_item->p_input );
     free( p_item );
     return VLC_SUCCESS;
@@ -110,17 +200,31 @@ static int DeleteFromInput( playlist_t *p_playlist, int i_input_id,
     return VLC_EGENERIC;
 }
 
+/** 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;
+}
+
 /** Remove an input item from ONELEVEL and CATEGORY */
 int playlist_DeleteFromInput( playlist_t *p_playlist, int i_input_id,
                               vlc_bool_t b_locked )
 {
+    int i_ret1, i_ret2;
     if( !b_locked ) PL_LOCK;
-    DeleteFromInput( p_playlist, i_input_id,
-                     p_playlist->p_root_category, VLC_TRUE );
-    DeleteFromInput( p_playlist, i_input_id,
+    i_ret1 = DeleteFromInput( p_playlist, i_input_id,
+                             p_playlist->p_root_category, VLC_TRUE );
+    i_ret2 = DeleteFromInput( p_playlist, i_input_id,
                      p_playlist->p_root_onelevel, VLC_TRUE );
     if( !b_locked ) PL_UNLOCK;
-    return VLC_SUCCESS;
+    return ( i_ret1 == VLC_SUCCESS || i_ret2 == VLC_SUCCESS ) ?
+                            VLC_SUCCESS : VLC_ENOITEM;
 }
 
 void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
@@ -135,7 +239,8 @@ void playlist_Clear( playlist_t * p_playlist, vlc_bool_t b_locked )
  * This function is to be used only by the playlist */
 int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
 {
-    playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id,  VLC_TRUE );
+    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 );
 }
@@ -156,10 +261,10 @@ int playlist_DeleteFromItemId( playlist_t *p_playlist, int i_id )
  */
 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
                   const char *psz_name, int i_mode, int i_pos,
-                  vlc_bool_t b_playlist )
+                  vlc_bool_t b_playlist, vlc_bool_t b_locked )
 {
     return playlist_AddExt( p_playlist, psz_uri, psz_name,
-                            i_mode, i_pos, -1, NULL, 0, b_playlist );
+                            i_mode, i_pos, -1, NULL, 0, b_playlist, b_locked );
 }
 
 /**
@@ -181,18 +286,24 @@ int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
                      const char *psz_name, int i_mode, int i_pos,
                      mtime_t i_duration, const char *const *ppsz_options,
-                     int i_options, vlc_bool_t b_playlist )
+                     int i_options, vlc_bool_t b_playlist, vlc_bool_t b_locked )
 {
+    int i_ret;
     input_item_t *p_input = input_ItemNewExt( p_playlist, psz_uri, psz_name,
                                               i_options, ppsz_options,
                                               i_duration );
 
-    return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist );
+    i_ret = playlist_AddInput( p_playlist, p_input, i_mode, i_pos, b_playlist,
+                               b_locked );
+    if( i_ret == VLC_SUCCESS )
+        return p_input->i_id;
+    return -1;
 }
 
 /** Add an input item to the playlist node */
 int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
-                      int i_mode, int i_pos, vlc_bool_t b_playlist )
+                       int i_mode, int i_pos, vlc_bool_t b_playlist,
+                       vlc_bool_t b_locked )
 {
     playlist_item_t *p_item_cat, *p_item_one;
 
@@ -200,25 +311,25 @@ int playlist_AddInput( playlist_t* p_playlist, input_item_t *p_input,
         PL_DEBUG( "adding item `%s' ( %s )", p_input->psz_name,
                                              p_input->psz_uri );
 
-    vlc_mutex_lock( &p_playlist->object_lock );
+    if( !b_locked ) PL_LOCK;
 
     /* Add to ONELEVEL */
     p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
-    if( p_item_one == NULL ) return VLC_EGENERIC;
+    if( p_item_one == NULL ) return VLC_ENOMEM;
     AddItem( p_playlist, p_item_one,
              b_playlist ? p_playlist->p_local_onelevel :
-                          p_playlist->p_ml_onelevel , i_pos );
+                          p_playlist->p_ml_onelevel , i_mode, i_pos );
 
     /* Add to CATEGORY */
     p_item_cat = playlist_ItemNewFromInput( p_playlist, p_input );
-    if( p_item_cat == NULL ) return VLC_EGENERIC;
+    if( p_item_cat == NULL ) return VLC_ENOMEM;
     AddItem( p_playlist, p_item_cat,
              b_playlist ? p_playlist->p_local_category :
-                          p_playlist->p_ml_category , i_pos );
+                          p_playlist->p_ml_category , i_mode, i_pos );
 
     GoAndPreparse( p_playlist, i_mode, p_item_cat, p_item_one );
 
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    if( !b_locked ) PL_UNLOCK;
     return VLC_SUCCESS;
 }
 
@@ -228,22 +339,22 @@ int playlist_BothAddInput( playlist_t *p_playlist,
                            input_item_t *p_input,
                            playlist_item_t *p_direct_parent,
                            int i_mode, int i_pos,
-                           int *i_cat, int *i_one )
+                           int *i_cat, int *i_one, vlc_bool_t b_locked )
 {
     playlist_item_t *p_item_cat, *p_item_one, *p_up;
     int i_top;
     assert( p_input );
-    vlc_mutex_lock( & p_playlist->object_lock );
+    if( !b_locked ) PL_LOCK;
 
     /* Add to category */
     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 );
+    if( p_item_cat == NULL ) return VLC_ENOMEM;
+    AddItem( p_playlist, p_item_cat, p_direct_parent, i_mode, i_pos );
 
     /* Add to onelevel */
     /** \todo make a faster case for ml import */
     p_item_one = playlist_ItemNewFromInput( p_playlist, p_input );
-    if( p_item_one == NULL ) return VLC_EGENERIC;
+    if( p_item_one == NULL ) return VLC_ENOMEM;
 
     p_up = p_direct_parent;
     while( p_up->p_parent != p_playlist->p_root_category )
@@ -256,7 +367,8 @@ int playlist_BothAddInput( playlist_t *p_playlist,
                              p_up->p_input->i_id )
         {
             AddItem( p_playlist, p_item_one,
-                     p_playlist->p_root_onelevel->pp_children[i_top], i_pos );
+                     p_playlist->p_root_onelevel->pp_children[i_top],
+                     i_mode, i_pos );
             break;
         }
     }
@@ -265,7 +377,7 @@ int playlist_BothAddInput( playlist_t *p_playlist,
     if( i_cat ) *i_cat = p_item_cat->i_id;
     if( i_one ) *i_one = p_item_one->i_id;
 
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    if( !b_locked ) PL_UNLOCK;
     return VLC_SUCCESS;
 }
 
@@ -273,19 +385,20 @@ int playlist_BothAddInput( playlist_t *p_playlist,
 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 )
+                                         int i_mode, int i_pos,
+                                         vlc_bool_t b_locked )
 {
     playlist_item_t *p_item;
     assert( p_input );
     assert( p_parent && p_parent->i_children != -1 );
 
-    vlc_mutex_lock( &p_playlist->object_lock );
+    if( !b_locked ) PL_LOCK;
 
     p_item = playlist_ItemNewFromInput( p_playlist, p_input );
     if( p_item == NULL ) return NULL;
-    AddItem( p_playlist, p_item, p_parent, i_pos );
+    AddItem( p_playlist, p_item, p_parent, i_mode, i_pos );
 
-    vlc_mutex_unlock( &p_playlist->object_lock );
+    if( !b_locked ) PL_UNLOCK;
 
     return p_item;
 }
@@ -340,7 +453,10 @@ playlist_item_t *playlist_ItemToNode( playlist_t *p_playlist,
                                             p_playlist->p_root_onelevel,
                                             VLC_TRUE );
         assert( p_item_in_one );
+
+        /* We already have it, and there is nothing more to do */
         ChangeToNode( p_playlist, p_item_in_category );
+
         /* Item in one is a root, change it to node */
         if( p_item_in_one->p_parent == p_playlist->p_root_onelevel )
             ChangeToNode( p_playlist, p_item_in_one );
@@ -398,6 +514,8 @@ static int TreeMove( playlist_t *p_playlist, playlist_item_t *p_item,
 {
     int j;
     playlist_item_t *p_detach = p_item->p_parent;
+    (void)p_playlist;
+
     if( p_node->i_children == -1 ) return VLC_EGENERIC;
 
     for( j = 0; j < p_detach->i_children; j++ )
@@ -476,7 +594,7 @@ int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
 
 /** 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 )
+                             int i_node_id, vlc_bool_t b_signal )
 {
     vlc_value_t val;
     playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
@@ -484,7 +602,8 @@ void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id,
     p_add->i_node = i_node_id;
     val.p_address = p_add;
     p_playlist->b_reset_currently_playing = VLC_TRUE;
-    vlc_cond_signal( &p_playlist->object_wait );
+    if( b_signal )
+        vlc_cond_signal( &p_playlist->object_wait );
     var_Set( p_playlist, "item-append", val );
     free( p_add );
 }
@@ -498,8 +617,7 @@ int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
 {
     if( psz_name && p_item )
     {
-        if( p_item->p_input->psz_name ) free( p_item->p_input->psz_name );
-        p_item->p_input->psz_name = strdup( psz_name );
+        input_item_SetName( p_item->p_input, psz_name );
         return VLC_SUCCESS;
     }
     return VLC_EGENERIC;
@@ -510,8 +628,9 @@ int playlist_ItemSetName( playlist_item_t *p_item, const char *psz_name )
  ***************************************************************************/
 
 /* Enqueue an item for preparsing, and play it, if needed */
-void GoAndPreparse( playlist_t *p_playlist, int i_mode,
-                    playlist_item_t *p_item_cat, playlist_item_t *p_item_one )
+static void GoAndPreparse( playlist_t *p_playlist, int i_mode,
+                           playlist_item_t *p_item_cat,
+                           playlist_item_t *p_item_one )
 {
     if( (i_mode & PLAYLIST_GO ) )
     {
@@ -539,26 +658,24 @@ void GoAndPreparse( playlist_t *p_playlist, int i_mode,
         vlc_cond_signal( &p_playlist->object_wait );
     }
     /* Preparse if PREPARSE or SPREPARSE & not enough meta */
+    char *psz_artist = input_item_GetArtist( p_item_cat->p_input );
+    char *psz_album = input_item_GetAlbum( p_item_cat->p_input );
     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 ) )
-              )
-            )
+            ( EMPTY_STR( psz_artist ) || ( EMPTY_STR( psz_album ) ) )
           ) ) )
         playlist_PreparseEnqueue( p_playlist, p_item_cat->p_input );
     /* 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;
+    else if( !EMPTY_STR( psz_artist ) && !EMPTY_STR( psz_album ) )
+        input_item_SetPreparsed( p_item_cat->p_input, VLC_TRUE );
+    free( psz_artist );
+    free( psz_album );
 }
 
 /* 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 )
+static void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
+                     playlist_item_t *p_node, int i_mode, int i_pos )
 {
     ARRAY_APPEND(p_playlist->items, p_item);
     ARRAY_APPEND(p_playlist->all_items, p_item);
@@ -570,11 +687,12 @@ void AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
         playlist_NodeInsert( p_playlist, p_item, p_node, i_pos );
 
     if( !p_playlist->b_doing_ml )
-        playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id );
+        playlist_SendAddNotify( p_playlist, p_item->i_id, p_node->i_id,
+                                 !( i_mode & PLAYLIST_NO_REBUILD ) );
 }
 
 /* Actually convert an item to a node */
-void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
+static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
 {
     int i;
     if( p_item->i_children == -1 )
@@ -587,12 +705,12 @@ void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
 }
 
 /* Do the actual removal */
-int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
-                vlc_bool_t b_stop )
+static int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
+                        vlc_bool_t b_stop )
 {
     int i;
     int i_id = p_item->i_id;
-    vlc_bool_t b_flag = VLC_FALSE;
+    vlc_bool_t b_delay_deletion = VLC_FALSE;
 
     if( p_item->i_children > -1 )
     {
@@ -618,7 +736,7 @@ int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
             msg_Info( p_playlist, "stopping playback" );
             vlc_cond_signal( &p_playlist->object_wait );
         }
-        b_flag = VLC_TRUE;
+        b_delay_deletion = VLC_TRUE;
     }
 
     PL_DEBUG( "deleting item `%s'", p_item->p_input->psz_name );
@@ -626,7 +744,7 @@ int DeleteInner( playlist_t * p_playlist, playlist_item_t *p_item,
     /* Remove the item from its parent */
     playlist_NodeRemoveItem( p_playlist, p_item, p_item->p_parent );
 
-    if( b_flag == VLC_FALSE )
+    if( !b_delay_deletion )
         playlist_ItemDelete( p_item );
     else
     {