hierarchical_media_list_view.c 7.79 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * hierarchical_media_list_view.c: libvlc hierarchical media list view functs.
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "libvlc_internal.h"
#include <vlc/libvlc.h>
#include <assert.h>
#include "vlc_arrays.h"

//#define DEBUG_HIERARCHICAL_VIEW

#ifdef DEBUG_HIERARCHICAL_VIEW
# define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
#else
# define trace( ... )
#endif

struct libvlc_media_list_view_private_t
{
    vlc_array_t array;
};

/*
 * Private functions
 */

/**************************************************************************
 *       flat_media_list_view_count  (private)
 * (called by media_list_view_count)
 **************************************************************************/
static int
hierarch_media_list_view_count( libvlc_media_list_view_t * p_mlv,
                                libvlc_exception_t * p_e )
{
    return libvlc_media_list_count( p_mlv->p_mlist, p_e );
}

/**************************************************************************
 *       flat_media_list_view_item_at_index  (private)
 * (called by flat_media_list_view_item_at_index)
 **************************************************************************/
static libvlc_media_descriptor_t *
hierarch_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
                                    int index,
                                    libvlc_exception_t * p_e )
{
    return libvlc_media_list_item_at_index( p_mlv->p_mlist, index, p_e );
}

/**************************************************************************
 *       flat_media_list_view_item_at_index  (private)
 * (called by flat_media_list_view_item_at_index)
 **************************************************************************/
static libvlc_media_list_view_t *
hierarch_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
                                        int index,
                                        libvlc_exception_t * p_e )
{
    libvlc_media_descriptor_t * p_md;
    libvlc_media_list_t * p_submlist;
80
    libvlc_media_list_view_t * p_ret;
81 82 83
    p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, index, p_e );
    if( !p_md ) return NULL;
    p_submlist = libvlc_media_descriptor_subitems( p_md, p_e );
84
    libvlc_media_descriptor_release( p_md );
85
    if( !p_submlist ) return NULL;
86 87 88 89
    p_ret = libvlc_media_list_hierarchical_view( p_submlist, p_e );
    libvlc_media_list_release( p_submlist );

    return p_ret;
90 91 92
}

/**************************************************************************
93
 *       media_list_(item|will)_* (private) (Event callback)
94 95
 **************************************************************************/
static void
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
media_list_item_added( const libvlc_event_t * p_event, void * user_data )
{
    libvlc_media_descriptor_t * p_md;
    libvlc_media_list_view_t * p_mlv = user_data;
    int index = p_event->u.media_list_item_added.index;
    p_md = p_event->u.media_list_item_added.item;
    libvlc_media_list_view_item_added( p_mlv, p_md, index );
}
static void
media_list_will_add_item( const libvlc_event_t * p_event, void * user_data )
{
    libvlc_media_descriptor_t * p_md;
    libvlc_media_list_view_t * p_mlv = user_data;
    int index = p_event->u.media_list_will_add_item.index;
    p_md = p_event->u.media_list_will_add_item.item;
    libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
}
static void
media_list_item_deleted( const libvlc_event_t * p_event, void * user_data )
{
    libvlc_media_descriptor_t * p_md;
    libvlc_media_list_view_t * p_mlv = user_data;
    int index = p_event->u.media_list_item_deleted.index;
    p_md = p_event->u.media_list_item_deleted.item;
    libvlc_media_list_view_item_deleted( p_mlv, p_md, index );
}
static void
media_list_will_delete_item( const libvlc_event_t * p_event, void * user_data )
124
{
125 126 127 128 129
    libvlc_media_descriptor_t * p_md;
    libvlc_media_list_view_t * p_mlv = user_data;
    int index = p_event->u.media_list_will_delete_item.index;
    p_md = p_event->u.media_list_will_delete_item.item;
    libvlc_media_list_view_will_delete_item( p_mlv, p_md, index );
130 131 132 133 134 135
}

/*
 * Public libvlc functions
 */

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

/**************************************************************************
 *       flat_media_list_view_release (private)
 * (called by media_list_view_release)
 **************************************************************************/
static void
hierarch_media_list_view_release( libvlc_media_list_view_t * p_mlv )
{
    libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListItemAdded,
                         media_list_item_added, p_mlv, NULL );
    libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListWillAddItem,
                         media_list_will_add_item, p_mlv, NULL );
    libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListItemDeleted,
                         media_list_item_deleted, p_mlv, NULL );
    libvlc_event_detach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListWillDeleteItem,
                         media_list_will_delete_item, p_mlv, NULL );
}

158 159 160 161 162 163 164 165 166 167 168 169 170
/**************************************************************************
 *       libvlc_media_list_flat_view (Public)
 **************************************************************************/
libvlc_media_list_view_t *
libvlc_media_list_hierarchical_view( libvlc_media_list_t * p_mlist,
                                     libvlc_exception_t * p_e )
{
    trace("\n");
    libvlc_media_list_view_t * p_mlv;
    p_mlv = libvlc_media_list_view_new( p_mlist,
                                        hierarch_media_list_view_count,
                                        hierarch_media_list_view_item_at_index,
                                        hierarch_media_list_view_children_at_index,
171
                                        libvlc_media_list_hierarchical_view,
172 173 174
                                        hierarch_media_list_view_release,
                                        NULL,
                                        p_e );
175
    libvlc_media_list_lock( p_mlist );
176 177 178 179 180 181 182 183 184 185 186 187
    libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListItemAdded,
                         media_list_item_added, p_mlv, NULL );
    libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListWillAddItem,
                         media_list_will_add_item, p_mlv, NULL );
    libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListItemDeleted,
                         media_list_item_deleted, p_mlv, NULL );
    libvlc_event_attach( p_mlv->p_mlist->p_event_manager,
                         libvlc_MediaListWillDeleteItem,
                         media_list_will_delete_item, p_mlv, NULL );
188 189 190
    libvlc_media_list_unlock( p_mlist );
    return p_mlv;
}