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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * hierarchical_node_media_list_view.c: libvlc hierarchical nodes media list
  3.  * view functs.
  4.  *****************************************************************************
  5.  * Copyright (C) 2007 the VideoLAN team
  6.  * $Id: fed48995bca17c45ce350cbe467f88d7a0d1ea1d $
  7.  *
  8.  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23.  *****************************************************************************/
  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_internal.h" // Abuse, could ans should be removed
  29. #include "media_list_internal.h" // Abuse, could ans should be removed
  30. #include "media_list_view_internal.h"
  31. /* FIXME: This version is probably a bit overheaded, and we may want to store
  32.  * the items in a vlc_array_t to speed everything up */
  33. //#define DEBUG_HIERARCHICAL_VIEW
  34. #ifdef DEBUG_HIERARCHICAL_VIEW
  35. # define trace( fmt, ... ) printf( "[HIERARCHICAL_NODE] %s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
  36. #else
  37. # define trace( ... ) {}
  38. #endif
  39. /*
  40.  * Private functions
  41.  */
  42. /**************************************************************************
  43.  *       flat_media_list_view_count  (private)
  44.  * (called by media_list_view_count)
  45.  **************************************************************************/
  46. static int
  47. hierarch_node_media_list_view_count( libvlc_media_list_view_t * p_mlv,
  48.                                 libvlc_exception_t * p_e )
  49. {
  50.     /* FIXME: we may want to cache that */
  51.     int i, ret, count = libvlc_media_list_count( p_mlv->p_mlist, p_e );
  52.     libvlc_media_t * p_md;
  53.     libvlc_media_list_t * p_submlist;
  54.     ret = 0;
  55.     trace("n");
  56.     for( i = 0; i < count; i++ )
  57.     {
  58.         p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, p_e );
  59.         if( !p_md ) continue;
  60.         p_submlist = libvlc_media_subitems( p_md, p_e );
  61.         if( !p_submlist ) continue;
  62.         libvlc_media_release( p_md );
  63.         libvlc_media_list_release( p_submlist );
  64.         ret++;
  65.     }
  66.     return ret;
  67. }
  68. /**************************************************************************
  69.  *       flat_media_list_view_item_at_index  (private)
  70.  * (called by flat_media_list_view_item_at_index)
  71.  **************************************************************************/
  72. static libvlc_media_t *
  73. hierarch_node_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
  74.                                         int index,
  75.                                         libvlc_exception_t * p_e )
  76. {
  77.     /* FIXME: we may want to cache that */
  78.     libvlc_media_t * p_md;
  79.     libvlc_media_list_t * p_submlist;
  80.     trace("%dn", index);
  81.     int i, current_index, count = libvlc_media_list_count( p_mlv->p_mlist, p_e );
  82.     current_index = -1;
  83.     for( i = 0; i < count; i++ )
  84.     {
  85.         p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, p_e );
  86.         if( !p_md ) continue;
  87.         p_submlist = libvlc_media_subitems( p_md, p_e );
  88.         if( !p_submlist ) continue;
  89.         libvlc_media_list_release( p_submlist );
  90.         current_index++;
  91.         if( current_index == index )
  92.             return p_md;
  93.         libvlc_media_release( p_md );
  94.     }
  95.     libvlc_exception_raise( p_e, "Index out of bound in Media List View" );
  96.     return NULL;
  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. hierarch_node_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
  104.                                             int index,
  105.                                             libvlc_exception_t * p_e )
  106. {
  107.     libvlc_media_t * p_md;
  108.     libvlc_media_list_t * p_submlist;
  109.     libvlc_media_list_view_t * p_ret;
  110.     p_md = hierarch_node_media_list_view_item_at_index( p_mlv, index, p_e );
  111.     if( !p_md ) return NULL;
  112.     p_submlist = libvlc_media_subitems( p_md, p_e );
  113.     libvlc_media_release( p_md );
  114.     if( !p_submlist ) return NULL;
  115.     p_ret = libvlc_media_list_hierarchical_node_view( p_submlist, p_e );
  116.     libvlc_media_list_release( p_submlist );
  117.     return p_ret;
  118. }
  119. /* Helper */
  120. static int
  121. index_of_item( libvlc_media_list_view_t * p_mlv, libvlc_media_t * p_md )
  122. {
  123.     libvlc_media_t * p_iter_md;
  124.     libvlc_media_list_t * p_submlist;
  125.     int i, current_index, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
  126.     current_index = -1;
  127.     for( i = 0; i < count; i++ )
  128.     {
  129.         p_iter_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
  130.         if( !p_iter_md ) continue;
  131.         p_submlist = libvlc_media_subitems( p_iter_md, NULL );
  132.         if( !p_submlist ) continue;
  133.         libvlc_media_list_release( p_submlist );
  134.         libvlc_media_release( p_iter_md );
  135.         current_index++;
  136.         if( p_md == p_iter_md )
  137.             return current_index;
  138.     }
  139.     return -1;
  140. }
  141. static bool
  142. item_is_already_added( libvlc_media_t * p_md )
  143. {
  144.     libvlc_media_list_t * p_submlist;
  145.     p_submlist = libvlc_media_subitems( p_md, NULL );
  146.     if( !p_submlist ) return false;
  147.     int count = libvlc_media_list_count( p_submlist, NULL );
  148.     libvlc_media_list_release( p_submlist );
  149.     return count > 1;
  150. }
  151. /**************************************************************************
  152.  *       media_list_(item|will)_* (private) (Event callback)
  153.  **************************************************************************/
  154. static void
  155. items_subitems_added( const libvlc_event_t * p_event, void * user_data )
  156. {
  157.     libvlc_media_t * p_md;
  158.     libvlc_media_list_view_t * p_mlv = user_data;
  159.     int index;
  160.     p_md = p_event->p_obj;
  161.     if( !item_is_already_added( p_md ) )
  162.     {
  163.         index = index_of_item( p_mlv, p_md );
  164.         trace("%dn", index);
  165.         if( index >= 0 )
  166.         {
  167.             libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
  168.             libvlc_media_list_view_item_added( p_mlv, p_md, index );
  169.         }
  170.     }
  171.     else
  172.     {
  173.         trace("item already addedn");
  174.     }
  175. }
  176. static void
  177. media_list_item_added( const libvlc_event_t * p_event, void * user_data )
  178. {
  179.     libvlc_media_t * p_md;
  180.     libvlc_media_list_view_t * p_mlv = user_data;
  181.     int index;
  182.     p_md = p_event->u.media_list_item_added.item;
  183.     index = index_of_item( p_mlv, p_md );
  184.     trace("%dn", index);
  185.     if( index >= 0)
  186.         libvlc_media_list_view_item_added( p_mlv, p_md, index );
  187.     libvlc_event_attach( p_md->p_event_manager, libvlc_MediaSubItemAdded,
  188.                          items_subitems_added, p_mlv, NULL );
  189.                          
  190. }
  191. static void
  192. media_list_will_add_item( const libvlc_event_t * p_event, void * user_data )
  193. {
  194.     libvlc_media_t * p_md;
  195.     libvlc_media_list_view_t * p_mlv = user_data;
  196.     int index;
  197.     p_md = p_event->u.media_list_will_add_item.item;
  198.     index = index_of_item( p_mlv, p_md );
  199.     trace("%dn", index);
  200.     if( index >= 0)
  201.         libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
  202. }
  203. static void
  204. media_list_item_deleted( const libvlc_event_t * p_event, void * user_data )
  205. {
  206.     libvlc_media_t * p_md;
  207.     libvlc_media_list_view_t * p_mlv = user_data;
  208.     int index;
  209.     p_md = p_event->u.media_list_item_deleted.item;
  210.     index = index_of_item( p_mlv, p_md );
  211.     trace("%dn", index);
  212.     if( index >= 0)
  213.         libvlc_media_list_view_item_deleted( p_mlv, p_md, index );
  214.     libvlc_event_detach( p_md->p_event_manager, libvlc_MediaSubItemAdded,
  215.                          items_subitems_added, p_mlv, NULL );
  216. }
  217. static void
  218. media_list_will_delete_item( const libvlc_event_t * p_event, void * user_data )
  219. {
  220.     libvlc_media_t * p_md;
  221.     libvlc_media_list_view_t * p_mlv = user_data;
  222.     int index;
  223.     p_md = p_event->u.media_list_will_delete_item.item;
  224.     index = index_of_item( p_mlv, p_md );
  225.     trace("%dn", index);
  226.     if( index >= 0)
  227.         libvlc_media_list_view_will_delete_item( p_mlv, p_md, index );
  228. }
  229. /*
  230.  * Public libvlc functions
  231.  */
  232. /**************************************************************************
  233.  *       flat_media_list_view_release (private)
  234.  * (called by media_list_view_release)
  235.  **************************************************************************/
  236. static void
  237. hierarch_node_media_list_view_release( libvlc_media_list_view_t * p_mlv )
  238. {
  239.     trace("n");
  240.     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
  241.                          libvlc_MediaListItemAdded,
  242.                          media_list_item_added, p_mlv, NULL );
  243.     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
  244.                          libvlc_MediaListWillAddItem,
  245.                          media_list_will_add_item, p_mlv, NULL );
  246.     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
  247.                          libvlc_MediaListItemDeleted,
  248.                          media_list_item_deleted, p_mlv, NULL );
  249.     libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
  250.                          libvlc_MediaListWillDeleteItem,
  251.                          media_list_will_delete_item, p_mlv, NULL );
  252. }
  253. /**************************************************************************
  254.  *       libvlc_media_list_flat_view (Public)
  255.  **************************************************************************/
  256. libvlc_media_list_view_t *
  257. libvlc_media_list_hierarchical_node_view( libvlc_media_list_t * p_mlist,
  258.                                      libvlc_exception_t * p_e )
  259. {
  260.     trace("n");
  261.     libvlc_media_list_view_t * p_mlv;
  262.     p_mlv = libvlc_media_list_view_new( p_mlist,
  263.                                         hierarch_node_media_list_view_count,
  264.                                         hierarch_node_media_list_view_item_at_index,
  265.                                         hierarch_node_media_list_view_children_at_index,
  266.                                         libvlc_media_list_hierarchical_node_view,
  267.                                         hierarch_node_media_list_view_release,
  268.                                         NULL,
  269.                                         p_e );
  270.     libvlc_media_list_lock( p_mlist );
  271.     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
  272.                          libvlc_MediaListItemAdded,
  273.                          media_list_item_added, p_mlv, NULL );
  274.     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
  275.                          libvlc_MediaListWillAddItem,
  276.                          media_list_will_add_item, p_mlv, NULL );
  277.     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
  278.                          libvlc_MediaListItemDeleted,
  279.                          media_list_item_deleted, p_mlv, NULL );
  280.     libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
  281.                          libvlc_MediaListWillDeleteItem,
  282.                          media_list_will_delete_item, p_mlv, NULL );
  283.     libvlc_media_list_unlock( p_mlist );
  284.     return p_mlv;
  285. }