]> git.sesse.net Git - vlc/blobdiff - src/control/media_list.c
Replace vlc_bool_t by bool, VLC_TRUE by true and VLC_FALSE by false.
[vlc] / src / control / media_list.c
index 136d1529ceabbfd2f5804d77f57005487bafa7a8..3ae87239100a8c185d263b3e703635224ce4b35e 100644 (file)
 #include <assert.h>
 #include "vlc_arrays.h"
 
+typedef enum EventPlaceInTime {
+    EventWillHappen,
+    EventDidHappen
+} EventPlaceInTime;
+
+//#define DEBUG_MEDIA_LIST
+
+#ifdef DEBUG_MEDIA_LIST
+# define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
+#else
+# define trace( ... )
+#endif
+
 /*
  * Private functions
  */
  **************************************************************************/
 static void
 notify_item_addition( libvlc_media_list_t * p_mlist,
-                      libvlc_media_descriptor_t * p_md,
-                      int index )
+                      libvlc_media_t * p_md,
+                      int index,
+                      EventPlaceInTime event_status )
 {
     libvlc_event_t event;
 
     /* Construct the event */
-    event.type = libvlc_MediaListItemAdded;
-    event.u.media_list_item_added.item = p_md;
-    event.u.media_list_item_added.index = index;
+    if( event_status == EventDidHappen )
+    {
+        trace("item was added at index %d\n", index);
+        event.type = libvlc_MediaListItemAdded;
+        event.u.media_list_item_added.item = p_md;
+        event.u.media_list_item_added.index = index;
+    }
+    else /* if( event_status == EventWillHappen ) */
+    {
+        event.type = libvlc_MediaListWillAddItem;
+        event.u.media_list_will_add_item.item = p_md;
+        event.u.media_list_will_add_item.index = index;
+    }
 
     /* Send the event */
     libvlc_event_send( p_mlist->p_event_manager, &event );
@@ -60,93 +84,31 @@ notify_item_addition( libvlc_media_list_t * p_mlist,
  **************************************************************************/
 static void
 notify_item_deletion( libvlc_media_list_t * p_mlist,
-                      libvlc_media_descriptor_t * p_md,
-                      int index )
+                      libvlc_media_t * p_md,
+                      int index,
+                      EventPlaceInTime event_status )
 {
     libvlc_event_t event;
 
     /* Construct the event */
-    event.type = libvlc_MediaListItemDeleted;
-    event.u.media_list_item_deleted.item = p_md;
-    event.u.media_list_item_deleted.index = index;
-
-    /* Send the event */
-    libvlc_event_send( p_mlist->p_event_manager, &event );
-}
-
-/**************************************************************************
- *       media_descriptor_changed (private) (libvlc Event Callback )
- *
- * An item has changed.
- **************************************************************************/
-static void
-media_descriptor_changed( const libvlc_event_t * p_event, void * user_data )
-{
-    libvlc_media_list_t * p_mlist = user_data;
-    libvlc_media_descriptor_t * p_md = p_event->p_obj;
-    libvlc_event_t event;
-    
-    /* Construct the new media list event */
-    event.type = libvlc_MediaListItemChanged;
-    event.u.media_list_item_changed.item = p_md;
-
-    /* XXX: this is not good, but there is a solution in the pipeline */
-    event.u.media_list_item_changed.index =
-        libvlc_media_list_index_of_item( p_mlist, p_md, NULL );
+    if( event_status == EventDidHappen )
+    {
+        trace("item at index %d was deleted\n", index);
+        event.type = libvlc_MediaListItemDeleted;
+        event.u.media_list_item_deleted.item = p_md;
+        event.u.media_list_item_deleted.index = index;
+    }
+    else /* if( event_status == EventWillHappen ) */
+    {
+        event.type = libvlc_MediaListWillDeleteItem;
+        event.u.media_list_will_delete_item.item = p_md;
+        event.u.media_list_will_delete_item.index = index;
+    }
 
     /* Send the event */
     libvlc_event_send( p_mlist->p_event_manager, &event );
 }
 
-/**************************************************************************
- *       media_descriptor_subitem_added (private) (libvlc Event Callback )
- *
- * An item (which is a playlist) has gained sub child.
- **************************************************************************/
-static void
-media_descriptor_subitem_added( const libvlc_event_t * p_event, void * user_data )
-{
-    /* Todo: Just forward that event to our event_manager */
-}
-
-/**************************************************************************
- *       install_media_descriptor_observer (private)
- *
- * Do the appropriate action when an item is deleted.
- **************************************************************************/
-static void
-install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
-                                   libvlc_media_descriptor_t * p_md )
-{
-    libvlc_event_attach( p_md->p_event_manager,
-                         libvlc_MediaDescriptorMetaChanged,
-                         media_descriptor_changed,
-                         p_mlist, NULL );
-    libvlc_event_attach( p_md->p_event_manager,
-                         libvlc_MediaDescriptorSubItemAdded,
-                         media_descriptor_subitem_added,
-                         p_mlist, NULL );
-}
-
-/**************************************************************************
- *       uninstall_media_descriptor_observer (private)
- *
- * Do the appropriate action when an item is deleted.
- **************************************************************************/
-static void
-uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
-                                     libvlc_media_descriptor_t * p_md )
-{
-    libvlc_event_detach( p_md->p_event_manager,
-                         libvlc_MediaDescriptorMetaChanged,
-                         media_descriptor_changed,
-                         p_mlist, NULL );
-    libvlc_event_detach( p_md->p_event_manager,
-                         libvlc_MediaDescriptorSubItemAdded,
-                         media_descriptor_subitem_added,
-                         p_mlist, NULL );
-}
-
 /*
  * Public libvlc functions
  */
@@ -166,19 +128,22 @@ libvlc_media_list_new( libvlc_instance_t * p_inst,
 
     if( !p_mlist )
         return NULL;
-    
     p_mlist->p_libvlc_instance = p_inst;
     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
 
     /* Code for that one should be handled in flat_media_list.c */
     p_mlist->p_flat_mlist = NULL;
+    p_mlist->b_read_only = false;
 
     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
             libvlc_MediaListItemAdded, p_e );
     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
-            libvlc_MediaListItemChanged, p_e );
+            libvlc_MediaListWillAddItem, p_e );
     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
             libvlc_MediaListItemDeleted, p_e );
+    libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
+            libvlc_MediaListWillDeleteItem, p_e );
 
     if( libvlc_exception_raised( p_e ) )
     {
@@ -188,10 +153,10 @@ libvlc_media_list_new( libvlc_instance_t * p_inst,
     }
 
     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
-    
-    ARRAY_INIT(p_mlist->items);
+    vlc_array_init( &p_mlist->items );
     p_mlist->i_refcount = 1;
-    p_mlist->psz_name = NULL;
+    p_mlist->p_md = NULL;
 
     return p_mlist;
 }
@@ -203,29 +168,34 @@ libvlc_media_list_new( libvlc_instance_t * p_inst,
  **************************************************************************/
 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
 {
-    libvlc_media_descriptor_t * p_md;
+    libvlc_media_t * p_md;
+    int i;
 
     vlc_mutex_lock( &p_mlist->object_lock );
     p_mlist->i_refcount--;
     if( p_mlist->i_refcount > 0 )
     {
-        vlc_mutex_unlock( &p_mlist->object_lock );        
+        vlc_mutex_unlock( &p_mlist->object_lock );
         return;
     }
-    vlc_mutex_unlock( &p_mlist->object_lock );        
+    vlc_mutex_unlock( &p_mlist->object_lock );
 
     /* Refcount null, time to free */
 
-    /* Handled in flat_media_list.c */
-    libvlc_media_list_flat_media_list_release( p_mlist );
-
     libvlc_event_manager_release( p_mlist->p_event_manager );
 
-    FOREACH_ARRAY( p_md, p_mlist->items )
-        uninstall_media_descriptor_observer( p_mlist, p_md );
-        libvlc_media_descriptor_release( p_md );
-    FOREACH_END()
+    if( p_mlist->p_md )
+        libvlc_media_release( p_mlist->p_md );
+
+    for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
+    {
+        p_md = vlc_array_item_at_index( &p_mlist->items, i );
+        libvlc_media_release( p_md );
+    }
+
+    vlc_mutex_destroy( &p_mlist->object_lock );
+    vlc_array_clear( &p_mlist->items );
+
     free( p_mlist );
 }
 
@@ -251,7 +221,7 @@ libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
                                     libvlc_exception_t * p_e )
 {
     input_item_t * p_input_item;
-    libvlc_media_descriptor_t * p_md;
+    libvlc_media_t * p_md;
 
     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
                                 _("Media Library"), 0, NULL, -1 );
@@ -262,7 +232,7 @@ libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
         return;
     }
 
-    p_md = libvlc_media_descriptor_new_from_input_item(
+    p_md = libvlc_media_new_from_input_item(
             p_mlist->p_libvlc_instance,
             p_input_item, p_e );
 
@@ -272,44 +242,55 @@ libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
         return;
     }
 
-    libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
+    libvlc_media_list_add_media( p_mlist, p_md, p_e );
     if( libvlc_exception_raised( p_e ) )
         return;
 
-    input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
+    input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, true );
 
     return;
 }
 
 /**************************************************************************
- *       set_name (Public)
+ *       set_media (Public)
  **************************************************************************/
-void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
-                                 const char * psz_name,
-                                 libvlc_exception_t * p_e)
+void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
+                                             libvlc_media_t * p_md,
+                                             libvlc_exception_t * p_e)
 
 {
     (void)p_e;
     vlc_mutex_lock( &p_mlist->object_lock );
-    free( p_mlist->psz_name );
-    p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
+    if( p_mlist->p_md )
+        libvlc_media_release( p_mlist->p_md );
+    libvlc_media_retain( p_md );
+    p_mlist->p_md = p_md;
     vlc_mutex_unlock( &p_mlist->object_lock );
 }
 
 /**************************************************************************
- *       name (Public)
+ *       media (Public)
+ *
+ * If this media_list comes is a media's subitems,
+ * This holds the corresponding media.
+ * This md is also seen as the information holder for the media_list.
+ * Indeed a media_list can have meta information through this
+ * media.
  **************************************************************************/
-char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
-                               libvlc_exception_t * p_e)
+libvlc_media_t *
+libvlc_media_list_media( libvlc_media_list_t * p_mlist,
+                                    libvlc_exception_t * p_e)
 {
-    char *ret;
+    libvlc_media_t *p_md;
     (void)p_e;
 
     vlc_mutex_lock( &p_mlist->object_lock );
-    ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
+    p_md = p_mlist->p_md;
+    if( p_md )
+        libvlc_media_retain( p_md );
     vlc_mutex_unlock( &p_mlist->object_lock );
 
-    return ret;
+    return p_md;
 }
 
 /**************************************************************************
@@ -321,43 +302,76 @@ int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
                              libvlc_exception_t * p_e )
 {
     (void)p_e;
-    return p_mlist->items.i_size;
+    return vlc_array_count( &p_mlist->items );
 }
 
 /**************************************************************************
- *       libvlc_media_list_add_media_descriptor (Public)
+ *       libvlc_media_list_add_media (Public)
  *
  * Lock should be hold when entering.
  **************************************************************************/
-void libvlc_media_list_add_media_descriptor( 
+void libvlc_media_list_add_media(
+                                   libvlc_media_list_t * p_mlist,
+                                   libvlc_media_t * p_md,
+                                   libvlc_exception_t * p_e )
+{
+    if( p_mlist->b_read_only )
+    {
+        /* We are read only from user side */
+        libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
+        return;
+    }
+
+    _libvlc_media_list_add_media( p_mlist, p_md, p_e );
+}
+
+/* LibVLC internal version */
+void _libvlc_media_list_add_media(
                                    libvlc_media_list_t * p_mlist,
-                                   libvlc_media_descriptor_t * p_md,
+                                   libvlc_media_t * p_md,
                                    libvlc_exception_t * p_e )
 {
     (void)p_e;
-    libvlc_media_descriptor_retain( p_md );
-    ARRAY_APPEND( p_mlist->items, p_md );
-    notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
-    install_media_descriptor_observer( p_mlist, p_md );
+    libvlc_media_retain( p_md );
+
+    notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
+    vlc_array_append( &p_mlist->items, p_md );
+    notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
 }
 
 /**************************************************************************
- *       libvlc_media_list_insert_media_descriptor (Public)
+ *       libvlc_media_list_insert_media (Public)
  *
  * Lock should be hold when entering.
  **************************************************************************/
-void libvlc_media_list_insert_media_descriptor( 
+void libvlc_media_list_insert_media(
+                                   libvlc_media_list_t * p_mlist,
+                                   libvlc_media_t * p_md,
+                                   int index,
+                                   libvlc_exception_t * p_e )
+{
+    if( p_mlist->b_read_only )
+    {
+        /* We are read only from user side */
+        libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
+        return;
+    }
+    _libvlc_media_list_insert_media( p_mlist, p_md, index, p_e );
+}
+
+/* LibVLC internal version */
+void _libvlc_media_list_insert_media(
                                    libvlc_media_list_t * p_mlist,
-                                   libvlc_media_descriptor_t * p_md,
+                                   libvlc_media_t * p_md,
                                    int index,
                                    libvlc_exception_t * p_e )
 {
     (void)p_e;
-    libvlc_media_descriptor_retain( p_md );
+    libvlc_media_retain( p_md );
 
-    ARRAY_INSERT( p_mlist->items, p_md, index);
-    notify_item_addition( p_mlist, p_md, index );
-    install_media_descriptor_observer( p_mlist, p_md );
+    notify_item_addition( p_mlist, p_md, index, EventWillHappen );
+    vlc_array_insert( &p_mlist->items, p_md, index );
+    notify_item_addition( p_mlist, p_md, index, EventDidHappen );
 }
 
 /**************************************************************************
@@ -369,16 +383,36 @@ void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
                                      int index,
                                      libvlc_exception_t * p_e )
 {
-    libvlc_media_descriptor_t * p_md;
+    if( p_mlist->b_read_only )
+    {
+        /* We are read only from user side */
+        libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
+        return;
+    }
+    _libvlc_media_list_remove_index( p_mlist, index, p_e );
+}
 
-    p_md = ARRAY_VAL( p_mlist->items, index );
+/* LibVLC internal version */
+void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
+                                     int index,
+                                     libvlc_exception_t * p_e )
+{
 
-    uninstall_media_descriptor_observer( p_mlist, p_md );
+    libvlc_media_t * p_md;
 
-    ARRAY_REMOVE( p_mlist->items, index )
-    notify_item_deletion( p_mlist, p_md, index );
+    if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
+    {
+        libvlc_exception_raise( p_e, "Index out of bounds exception");
+        return;
+    }
+            
+    p_md = vlc_array_item_at_index( &p_mlist->items, index );
 
-    libvlc_media_descriptor_release( p_md );
+    notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
+    vlc_array_remove( &p_mlist->items, index );
+    notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
+
+    libvlc_media_release( p_md );
 }
 
 /**************************************************************************
@@ -386,13 +420,22 @@ void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
  *
  * Lock should be hold when entering.
  **************************************************************************/
-libvlc_media_descriptor_t *
+libvlc_media_t *
 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
                                  int index,
                                  libvlc_exception_t * p_e )
 {
-    libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
-    libvlc_media_descriptor_retain( p_md );
+    VLC_UNUSED(p_e);
+
+    if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
+    {
+        libvlc_exception_raise( p_e, "Index out of bounds exception");
+        return NULL;
+    }
+
+    libvlc_media_t * p_md;
+    p_md = vlc_array_item_at_index( &p_mlist->items, index );
+    libvlc_media_retain( p_md );
     return p_md;
 }
 
@@ -403,17 +446,32 @@ libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
  * Warning: this function would return the first matching item
  **************************************************************************/
 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
-                                     libvlc_media_descriptor_t * p_searched_md,
+                                     libvlc_media_t * p_searched_md,
                                      libvlc_exception_t * p_e )
 {
-    libvlc_media_descriptor_t * p_md;
-    FOREACH_ARRAY( p_md, p_mlist->items )
+    VLC_UNUSED(p_e);
+
+    libvlc_media_t * p_md;
+    int i;
+    for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
+    {
+        p_md = vlc_array_item_at_index( &p_mlist->items, i );
         if( p_searched_md == p_md )
-            return fe_idx; /* Once more, we hate macro for that */
-    FOREACH_END()
+            return i;
+    }
     return -1;
 }
 
+/**************************************************************************
+ *       libvlc_media_list_is_readonly (Public)
+ *
+ * This indicates if this media list is read-only from a user point of view
+ **************************************************************************/
+int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
+{
+    return p_mlist->b_read_only;
+}
+
 /**************************************************************************
  *       libvlc_media_list_lock (Public)
  *
@@ -437,7 +495,6 @@ void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
 }
 
 
-
 /**************************************************************************
  *       libvlc_media_list_p_event_manager (Public)
  *