From 9a224d8dfc78a22bce6c0781dd5bbff1fe2e72d2 Mon Sep 17 00:00:00 2001
From: Jakob Leben <jleben@videolan.org>
Date: Tue, 9 Feb 2010 01:14:31 +0100
Subject: [PATCH] input_item: post individual subitems later when posting the whole tree

This way the _node_Append functions can be used for general input_item_node_t
manipulation without side effects.
---
 include/vlc_input_item.h |   27 +++++++++++++--------------
 src/input/item.c         |   20 ++++++++++++--------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/include/vlc_input_item.h b/include/vlc_input_item.h
index 81616fc..d4858c9 100644
--- a/include/vlc_input_item.h
+++ b/include/vlc_input_item.h
@@ -124,10 +124,12 @@ VLC_EXPORT( void, input_item_SetName, ( input_item_t *p_item, const char *psz_na
  * This won't hold the item, but can tell to interested third parties
  * Like the playlist, that there is a new sub item. With this design
  * It is not the input item's responsability to keep all the ref of
- * the input item children. */
+ * the input item children.
+ *
+ * Sends a vlc_InputItemSubItemTreeAdded and a vlc_InputItemSubItemAdded event
+ */
 VLC_EXPORT( void, input_item_PostSubItem, ( input_item_t *p_parent, input_item_t *p_child ) );
 
-
 /**
  * Start adding multiple subitems.
  *
@@ -136,19 +138,14 @@ VLC_EXPORT( void, input_item_PostSubItem, ( input_item_t *p_parent, input_item_t
 VLC_EXPORT( input_item_node_t *, input_item_node_Create, ( input_item_t *p_input ) );
 
 /**
- * Add a subitem to this input_item and to this input_item_node.
- *
- * A vlc_InputItemSubItemAdded event will be sent right away.
+ * Add a new child node to this parent node that will point to this subitem.
  */
 VLC_EXPORT( input_item_node_t *, input_item_node_AppendItem, ( input_item_node_t *p_node, input_item_t *p_item ) );
 
 /**
- * Add a subitem to this input_item and to this input_item_node.
- *
- * A vlc_InputItemSubItemAdded event will be sent right away for the subitem
- * pointed by input_item_node_t.
+ * Add an already created node to children of this parent node.
  */
-VLC_EXPORT( void, input_item_node_AppendNode, ( input_item_node_t *p_node, input_item_node_t *p_item ) );
+VLC_EXPORT( void, input_item_node_AppendNode, ( input_item_node_t *p_parent, input_item_node_t *p_child ) );
 
 /**
  * Delete a node created with input_item_node_Create() and all its children.
@@ -158,11 +155,13 @@ VLC_EXPORT( void, input_item_node_Delete, ( input_item_node_t *p_node ) );
 /**
  * End adding multiple subitems.
  *
- * Send a notification that the item pointed to by the given root node
- * has created new subitems that are pointed to by all the children of the node.
- * Then delete the node and all its children.
+ * Sends a vlc_InputItemSubItemTreeAdded event to notify that the item pointed to
+ * by the given root node has created new subitems that are pointed to by all the
+ * children of the node.
+ *
+ * Also sends vlc_InputItemSubItemAdded event for every child under the given root node;
  *
- * A vlc_InputItemSubItemTreeAdded event will be sent.
+ * In the end deletes the node and all its children nodes.
  */
 VLC_EXPORT( void, input_item_node_PostAndDelete, ( input_item_node_t *p_node ) );
 
diff --git a/src/input/item.c b/src/input/item.c
index 616f15d..29446ae 100644
--- a/src/input/item.c
+++ b/src/input/item.c
@@ -234,13 +234,17 @@ void input_item_CopyOptions( input_item_t *p_parent,
     vlc_mutex_unlock( &p_parent->lock );
 }
 
-static void notify_subitem_added(input_item_t *p_parent, input_item_t *p_child)
+static void post_subitems( input_item_node_t *p_node )
 {
-    /* Notify interested third parties */
-    vlc_event_t event;
-    event.type = vlc_InputItemSubItemAdded;
-    event.u.input_item_subitem_added.p_new_child = p_child;
-    vlc_event_send( &p_parent->event_manager, &event );
+    for( int i = 0; i < p_node->i_children; i++ )
+    {
+        vlc_event_t event;
+        event.type = vlc_InputItemSubItemAdded;
+        event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
+        vlc_event_send( &p_node->p_item->event_manager, &event );
+
+        post_subitems( p_node->pp_children[i] );
+    }
 }
 
 /* This won't hold the item, but can tell to interested third parties
@@ -997,8 +1001,6 @@ input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_
 
 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
 {
-    notify_subitem_added(p_parent->p_item, p_child->p_item);
-
     assert( p_parent && p_child && p_child->p_parent == NULL );
     INSERT_ELEM( p_parent->pp_children,
                  p_parent->i_children,
@@ -1009,6 +1011,8 @@ void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t
 
 void input_item_node_PostAndDelete( input_item_node_t *p_root )
 {
+  post_subitems( p_root );
+
   vlc_event_t event;
   event.type = vlc_InputItemSubItemTreeAdded;
   event.u.input_item_subitem_tree_added.p_root = p_root;
-- 
1.6.3.3

