flat_media_list_view.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:7k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * flat_media_list_view.c: libvlc flat media list view functions.
  3.  *****************************************************************************
  4.  * Copyright (C) 2007 the VideoLAN team
  5.  * $Id: e5f51401d62cb2f3e00b4977a1793ed208674524 $
  6.  *
  7.  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. #include <assert.h>
  24. #include <vlc/libvlc.h>
  25. #include <vlc/libvlc_media.h>
  26. #include <vlc/libvlc_media_list.h>
  27. #include <vlc/libvlc_media_list_view.h>
  28. #include "media_list_view_internal.h"
  29. //#define DEBUG_FLAT_VIEW
  30. #ifdef DEBUG_FLAT_VIEW
  31. # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
  32. #else
  33. # define trace( ... )
  34. #endif
  35. struct libvlc_media_list_view_private_t
  36. {
  37.     vlc_array_t array;
  38. };
  39. /*
  40.  * Private functions
  41.  */
  42. /**************************************************************************
  43.  *       ml_item_added  (private) (Callback from media_list_view item_added)
  44.  **************************************************************************/
  45. static void
  46. ml_item_added( const libvlc_event_t * p_event, libvlc_media_list_view_t * p_mlv )
  47. {
  48.     int index = vlc_array_count( &p_mlv->p_this_view_data->array );
  49.     libvlc_media_t * p_md = p_event->u.media_list_item_added.item;
  50.     libvlc_media_retain( p_md );
  51.     trace("appending item at index %dn", index);
  52.     libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
  53.     vlc_array_append( &p_mlv->p_this_view_data->array, p_md );
  54.     libvlc_media_list_view_item_added( p_mlv, p_md, index );
  55. }
  56. /**************************************************************************
  57.  *       ml_item_removed  (private) (Callback from media_list_view)
  58.  **************************************************************************/
  59. static void
  60. ml_item_removed( const libvlc_event_t * p_event, libvlc_media_list_view_t * p_mlv )
  61. {
  62.     libvlc_media_t * p_md = p_event->u.media_list_item_deleted.item;
  63.     int i = vlc_array_index_of_item( &p_mlv->p_this_view_data->array, p_md );
  64.     if( i >= 0 )
  65.     {
  66.         libvlc_media_list_view_will_delete_item( p_mlv, p_md, i );
  67.         vlc_array_remove( &p_mlv->p_this_view_data->array, i );
  68.         libvlc_media_list_view_item_deleted( p_mlv, p_md, i );
  69.         libvlc_media_release( p_md );
  70.     }
  71. }
  72. /**************************************************************************
  73.  *       flat_media_list_view_count  (private)
  74.  * (called by media_list_view_count)
  75.  **************************************************************************/
  76. static int
  77. flat_media_list_view_count( libvlc_media_list_view_t * p_mlv,
  78.                             libvlc_exception_t * p_e )
  79. {
  80.     (void)p_e;
  81.     return vlc_array_count( &p_mlv->p_this_view_data->array );
  82. }
  83. /**************************************************************************
  84.  *       flat_media_list_view_item_at_index  (private)
  85.  * (called by flat_media_list_view_item_at_index)
  86.  **************************************************************************/
  87. static libvlc_media_t *
  88. flat_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
  89.                                     int index,
  90.                                     libvlc_exception_t * p_e )
  91. {
  92.     libvlc_media_t * p_md;
  93.     (void)p_e;
  94.     p_md = vlc_array_item_at_index( &p_mlv->p_this_view_data->array, index );
  95.     libvlc_media_retain( p_md );
  96.     return p_md;
  97. }
  98. /**************************************************************************
  99.  *       flat_media_list_view_item_at_index  (private)
  100.  * (called by flat_media_list_view_item_at_index)
  101.  **************************************************************************/
  102. static libvlc_media_list_view_t *
  103. flat_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
  104.                                         int index,
  105.                                         libvlc_exception_t * p_e )
  106. {
  107.     (void)p_mlv; (void)index; (void)p_e;
  108.     return NULL;
  109. }
  110. /**************************************************************************
  111.  *       flat_media_list_view_release (private)
  112.  * (called by media_list_view_release)
  113.  **************************************************************************/
  114. static void
  115. flat_media_list_view_release( libvlc_media_list_view_t * p_mlv )
  116. {
  117.     vlc_array_clear( &p_mlv->p_this_view_data->array );
  118.     free( p_mlv->p_this_view_data );
  119. }
  120. /*
  121.  * Public libvlc functions
  122.  */
  123. /* Little helper */
  124. static void
  125. import_mlist_rec( libvlc_media_list_view_t * p_mlv,
  126.                   libvlc_media_list_t * p_mlist,
  127.                   libvlc_exception_t * p_e )
  128. {
  129.     int i, count;
  130.     count = libvlc_media_list_count( p_mlist, p_e );
  131.     for( i = 0; i < count; i++ )
  132.     {
  133.         libvlc_media_t * p_md;
  134.         libvlc_media_list_t * p_submlist;
  135.         p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
  136.         vlc_array_append( &p_mlv->p_this_view_data->array, p_md );
  137.         p_submlist = libvlc_media_subitems( p_md, p_e );
  138.         if( p_submlist )
  139.         {
  140.             libvlc_media_list_lock( p_submlist );
  141.             import_mlist_rec( p_mlv, p_submlist, p_e );
  142.             libvlc_media_list_unlock( p_submlist );
  143.             libvlc_media_list_release( p_submlist );
  144.         }
  145.         /* No need to release the md, as we want to retain it, as it is
  146.          * stored in our array */
  147.     }
  148. }
  149.                         
  150. /**************************************************************************
  151.  *       libvlc_media_list_flat_view (Public)
  152.  **************************************************************************/
  153. libvlc_media_list_view_t *
  154. libvlc_media_list_flat_view( libvlc_media_list_t * p_mlist,
  155.                              libvlc_exception_t * p_e )
  156. {
  157.     trace("n");
  158.     libvlc_media_list_view_t * p_mlv;
  159.     struct libvlc_media_list_view_private_t * p_this_view_data;
  160.     p_this_view_data = malloc(sizeof(struct libvlc_media_list_view_private_t));
  161.     vlc_array_init( &p_this_view_data->array );
  162.     p_mlv = libvlc_media_list_view_new( p_mlist,
  163.                                         flat_media_list_view_count,
  164.                                         flat_media_list_view_item_at_index,
  165.                                         flat_media_list_view_children_at_index,
  166.                                         libvlc_media_list_flat_view,
  167.                                         flat_media_list_view_release,
  168.                                         p_this_view_data,
  169.                                         p_e );
  170.     libvlc_media_list_lock( p_mlist );
  171.     import_mlist_rec( p_mlv, p_mlist, p_e );
  172.     libvlc_media_list_view_set_ml_notification_callback( p_mlv,
  173.         ml_item_added,
  174.         ml_item_removed );
  175.     libvlc_media_list_unlock( p_mlist );
  176.     return p_mlv;
  177. }