]> git.sesse.net Git - vlc/blobdiff - src/playlist/item.c
Revert "playlist: refactor and fix #3932"
[vlc] / src / playlist / item.c
index 9b8a469037fa7833b475124e6dc4096d3d596e40..5f06fb3c01df7cb841456ba621a983f671b29e80 100644 (file)
@@ -40,6 +40,9 @@ static int RecursiveAddIntoParent (
                 playlist_t *p_playlist, playlist_item_t *p_parent,
                 input_item_node_t *p_node, int i_pos, bool b_flat,
                 playlist_item_t **pp_first_leaf );
+static int RecursiveInsertCopy (
+                playlist_t *p_playlist, playlist_item_t *p_item,
+                playlist_item_t *p_parent, int i_pos, bool b_flat );
 
 /*****************************************************************************
  * An input item has gained subitems (Event Callback)
@@ -87,21 +90,22 @@ static void input_item_add_subitem_tree ( const vlc_event_t * p_event,
         }
         p_up = p_up->p_parent;
     }
+
     if( b_flat )
-    {
         playlist_DeleteItem( p_playlist, p_item, true );
-        p_item = playlist_InsertInputItemTree( p_playlist, p_parent,
-                                               p_new_root, pos, true );
-    }
-    else
-        p_item = playlist_InsertInputItemTree( p_playlist, p_item,
-                                               p_new_root, PLAYLIST_END, false );
+
+    p_item = playlist_InsertInputItemTree( p_playlist,
+                                           b_flat ? p_parent : p_item,
+                                           p_new_root,
+                                           b_flat ? pos: PLAYLIST_END,
+                                           b_flat );
 
     if( !b_flat ) var_SetAddress( p_playlist, "leaf-to-parent", p_input );
 
+    //control playback only if it was the current playing item that got subitems
     if( b_current )
     {
-        if( ( b_stop && !b_flat ) || !b_autostart )
+        if( !p_item || ( b_stop && !b_flat ) || !b_autostart )
         {
             PL_UNLOCK;
             playlist_Stop( p_playlist );
@@ -452,6 +456,45 @@ playlist_item_t * playlist_NodeAddInput( playlist_t *p_playlist,
     return p_item;
 }
 
+/**
+ * Copy an item (and all its children, if any) into another node
+ *
+ * \param p_playlist the playlist to operate on
+ * \param p_item the playlist item to copy
+ * \param p_parent the parent item to copy into
+ * \param i_pos the position in the parent item for the new copy;
+ *              if this is PLAYLIST_END, the copy is appended after all
+ *              parent's children
+ * \return the position in parent item just behind the last new item inserted
+ */
+int playlist_NodeAddCopy (
+    playlist_t *p_playlist, playlist_item_t *p_item,
+    playlist_item_t *p_parent, int i_pos )
+{
+    PL_ASSERT_LOCKED;
+    assert( p_parent != NULL && p_item != NULL );
+    assert( p_parent->i_children > -1 );
+
+    if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
+
+    bool b_flat = false;
+
+    playlist_item_t *p_up = p_parent;
+    while( p_up )
+    {
+        if( p_up == p_playlist->p_playing )
+            if( !pl_priv(p_playlist)->b_tree ) b_flat = true;
+        if( p_up == p_item )
+            /* TODO: We don't support copying a node into itself (yet),
+            because we insert items as we copy. Instead, we should copy
+            all items first and then insert. */
+            return i_pos;
+        p_up = p_up->p_parent;
+    }
+
+    return RecursiveInsertCopy( p_playlist, p_item, p_parent, i_pos, b_flat );
+}
+
 /**
  * Insert a tree of input items into a given playlist node
  *
@@ -474,6 +517,7 @@ playlist_item_t *playlist_InsertInputItemTree (
   return p_first_leaf;
 }
 
+
 /*****************************************************************************
  * Playlist item misc operations
  *****************************************************************************/
@@ -700,6 +744,9 @@ static void ChangeToNode( playlist_t *p_playlist, playlist_item_t *p_item )
 int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
                         bool b_stop )
 {
+    assert( b_stop );
+    return playlist_NodeDelete( p_playlist, p_item, true, false );
+#if 0
     int i;
     int i_id = p_item->i_id;
     PL_ASSERT_LOCKED;
@@ -746,6 +793,7 @@ int playlist_DeleteItem( playlist_t * p_playlist, playlist_item_t *p_item,
     playlist_ItemRelease( p_item );
 
     return VLC_SUCCESS;
+#endif
 }
 
 static int RecursiveAddIntoParent (
@@ -753,40 +801,96 @@ static int RecursiveAddIntoParent (
     input_item_node_t *p_node, int i_pos, bool b_flat,
     playlist_item_t **pp_first_leaf )
 {
+  *pp_first_leaf = NULL;
+
   if( p_parent->i_children == -1 ) ChangeToNode( p_playlist, p_parent );
 
   if( i_pos == PLAYLIST_END ) i_pos = p_parent->i_children;
 
   for( int i = 0; i < p_node->i_children; i++ )
   {
-      playlist_item_t *p_child = NULL;
-      if( b_flat ? p_node->pp_children[i]->i_children == 0 : 1 )
+      input_item_node_t *p_child_node = p_node->pp_children[i];
+
+      playlist_item_t *p_new_item = NULL;
+      bool b_children = p_child_node->i_children > 0;
+
+      //Create the playlist item represented by input node, if allowed.
+      if( !(b_flat && b_children) )
       {
-          p_child = playlist_NodeAddInput( p_playlist,
-                                 p_node->pp_children[i]->p_item,
-                                 p_parent,
-                                 PLAYLIST_INSERT, i_pos,
-                                 pl_Locked );
+          p_new_item = playlist_NodeAddInput( p_playlist,
+                                              p_child_node->p_item,
+                                              p_parent,
+                                              PLAYLIST_INSERT, i_pos,
+                                              pl_Locked );
+          if( !p_new_item ) return i_pos;
+
           i_pos++;
       }
-      if( p_node->pp_children[i]->i_children > 0 )
+      //Recurse if any children
+      if( b_children )
       {
-          if( b_flat )
-          {
-              i_pos = RecursiveAddIntoParent(
-                                      p_playlist, p_parent,
-                                      p_node->pp_children[i], i_pos, true,
-                                      &p_child );
-          }
-          else
-          {
-              RecursiveAddIntoParent( p_playlist, p_child,
-                                      p_node->pp_children[i], 0, false,
-                                      &p_child );
-          }
+          //Substitute p_new_item for first child leaf
+          //(If flat, continue counting from current position)
+          int i_last_pos = RecursiveAddIntoParent(
+                                      p_playlist,
+                                      p_new_item ? p_new_item : p_parent,
+                                      p_child_node,
+                                      ( b_flat ? i_pos : 0 ),
+                                      b_flat,
+                                      &p_new_item );
+          //If flat, take position after recursion as current position
+          if( b_flat ) i_pos = i_last_pos;
       }
-      assert( p_child != NULL );
-      if( i == 0 ) *pp_first_leaf = p_child;
+
+      assert( p_new_item != NULL );
+      if( i == 0 ) *pp_first_leaf = p_new_item;
   }
   return i_pos;
 }
+
+static int RecursiveInsertCopy (
+    playlist_t *p_playlist, playlist_item_t *p_item,
+    playlist_item_t *p_parent, int i_pos, bool b_flat )
+{
+    PL_ASSERT_LOCKED;
+    assert( p_parent != NULL && p_item != NULL );
+
+    if( p_item == p_parent ) return i_pos;
+
+    input_item_t *p_input = p_item->p_input;
+
+    if( !(p_item->i_children != -1 && b_flat) )
+    {
+        input_item_t *p_new_input = input_item_Copy( VLC_OBJECT(p_playlist),
+                                                     p_input );
+        if( !p_new_input ) return i_pos;
+
+        playlist_item_t *p_new_item = NULL;
+        if( p_item->i_children == -1 )
+            p_new_item = playlist_NodeAddInput( p_playlist, p_new_input,
+                                   p_parent, PLAYLIST_INSERT, i_pos,
+                                   pl_Locked );
+        else
+            p_new_item = playlist_NodeCreate( p_playlist, NULL,
+                                 p_parent, i_pos, 0, p_new_input );
+        vlc_gc_decref( p_new_input );
+        if( !p_new_item ) return i_pos;
+
+        i_pos++;
+
+        if( p_new_item->i_children != -1 )
+            p_parent = p_new_item;
+    }
+
+    for( int i = 0; i < p_item->i_children; i++ )
+    {
+        if( b_flat )
+            i_pos = RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
+                                         p_parent, i_pos, true );
+        else
+            RecursiveInsertCopy( p_playlist, p_item->pp_children[i],
+                                 p_parent, p_parent->i_children, false );
+    }
+
+    return i_pos;
+}