hierarchical_node_media_list_view.c 11.2 KB
Newer Older
1
2
3
4
5
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
/*****************************************************************************
 * hierarchical_node_media_list_view.c: libvlc hierarchical nodes media list
 * view functs.
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * 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"

/* FIXME: This version is probably a bit overheaded, and we may want to store
 * the items in a vlc_array_t to speed everything up */

//#define DEBUG_HIERARCHICAL_VIEW

#ifdef DEBUG_HIERARCHICAL_VIEW
# define trace( fmt, ... ) printf( "[HIERARCHICAL_NODE] %s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
#else
38
# define trace( ... ) {}
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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_node_media_list_view_count( libvlc_media_list_view_t * p_mlv,
                                libvlc_exception_t * p_e )
{
    /* FIXME: we may want to cache that */
    int i, ret, count = libvlc_media_list_count( p_mlv->p_mlist, p_e );
60
    libvlc_media_t * p_md;
61
62
63
64
65
66
67
    libvlc_media_list_t * p_submlist;
    ret = 0;
    trace("\n");
    for( i = 0; i < count; i++ )
    {
        p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, p_e );
        if( !p_md ) continue;
68
        p_submlist = libvlc_media_subitems( p_md, p_e );
69
        if( !p_submlist ) continue;
70
        libvlc_media_release( p_md );
71
72
73
74
75
76
77
78
79
80
        libvlc_media_list_release( p_submlist );
        ret++;
    }
    return ret;
}

/**************************************************************************
 *       flat_media_list_view_item_at_index  (private)
 * (called by flat_media_list_view_item_at_index)
 **************************************************************************/
81
static libvlc_media_t *
82
83
84
85
86
hierarch_node_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
                                        int index,
                                        libvlc_exception_t * p_e )
{
    /* FIXME: we may want to cache that */
87
    libvlc_media_t * p_md;
88
89
90
91
92
93
94
95
    libvlc_media_list_t * p_submlist;
    trace("%d\n", index);
    int i, current_index, count = libvlc_media_list_count( p_mlv->p_mlist, p_e );
    current_index = -1;
    for( i = 0; i < count; i++ )
    {
        p_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, p_e );
        if( !p_md ) continue;
96
        p_submlist = libvlc_media_subitems( p_md, p_e );
97
98
99
100
101
        if( !p_submlist ) continue;
        libvlc_media_list_release( p_submlist );
        current_index++;
        if( current_index == index )
            return p_md;
102
        libvlc_media_release( p_md );
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    }

    libvlc_exception_raise( p_e, "Index out of bound in Media List View" );
    return NULL;
}

/**************************************************************************
 *       flat_media_list_view_item_at_index  (private)
 * (called by flat_media_list_view_item_at_index)
 **************************************************************************/
static libvlc_media_list_view_t *
hierarch_node_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
                                            int index,
                                            libvlc_exception_t * p_e )
{
118
    libvlc_media_t * p_md;
119
120
121
122
    libvlc_media_list_t * p_submlist;
    libvlc_media_list_view_t * p_ret;
    p_md = hierarch_node_media_list_view_item_at_index( p_mlv, index, p_e );
    if( !p_md ) return NULL;
123
124
    p_submlist = libvlc_media_subitems( p_md, p_e );
    libvlc_media_release( p_md );
125
126
127
128
129
130
131
132
133
    if( !p_submlist ) return NULL;
    p_ret = libvlc_media_list_hierarchical_node_view( p_submlist, p_e );
    libvlc_media_list_release( p_submlist );

    return p_ret;
}

/* Helper */
static int
134
index_of_item( libvlc_media_list_view_t * p_mlv, libvlc_media_t * p_md )
135
{
136
    libvlc_media_t * p_iter_md;
137
138
139
140
141
142
143
144
    libvlc_media_list_t * p_submlist;

    int i, current_index, count = libvlc_media_list_count( p_mlv->p_mlist, NULL );
    current_index = -1;
    for( i = 0; i < count; i++ )
    {
        p_iter_md = libvlc_media_list_item_at_index( p_mlv->p_mlist, i, NULL );
        if( !p_iter_md ) continue;
145
        p_submlist = libvlc_media_subitems( p_iter_md, NULL );
146
147
        if( !p_submlist ) continue;
        libvlc_media_list_release( p_submlist );
148
        libvlc_media_release( p_iter_md );
149
150
151
152
153
154
155
156
        current_index++;
        if( p_md == p_iter_md )
            return current_index;
    }
    return -1;
}

static vlc_bool_t
157
item_is_already_added( libvlc_media_t * p_md )
158
159
160
{
    libvlc_media_list_t * p_submlist;

161
    p_submlist = libvlc_media_subitems( p_md, NULL );
162
163
164
165
166
167
168
169
170
171
172
173
174
    if( !p_submlist ) return VLC_FALSE;
    int count = libvlc_media_list_count( p_submlist, NULL );
    libvlc_media_list_release( p_submlist );
    return count > 1;
}


/**************************************************************************
 *       media_list_(item|will)_* (private) (Event callback)
 **************************************************************************/
static void
items_subitems_added( const libvlc_event_t * p_event, void * user_data )
{
175
    libvlc_media_t * p_md;
176
177
178
    libvlc_media_list_view_t * p_mlv = user_data;
    int index;
    p_md = p_event->p_obj;
Rafaël Carré's avatar
Rafaël Carré committed
179
    if( !item_is_already_added( p_md ) )
180
181
182
183
184
185
186
187
188
189
    {
        index = index_of_item( p_mlv, p_md );
        trace("%d\n", index);
        if( index >= 0 )
        {
            libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
            libvlc_media_list_view_item_added( p_mlv, p_md, index );
        }
    }
    else
190
    {
191
        trace("item already added\n");
192
    }
193
194
195
196
197
}

static void
media_list_item_added( const libvlc_event_t * p_event, void * user_data )
{
198
    libvlc_media_t * p_md;
199
200
201
202
203
204
205
    libvlc_media_list_view_t * p_mlv = user_data;
    int index;
    p_md = p_event->u.media_list_item_added.item;
    index = index_of_item( p_mlv, p_md );
    trace("%d\n", index);
    if( index >= 0)
        libvlc_media_list_view_item_added( p_mlv, p_md, index );
206
    libvlc_event_attach( p_md->p_event_manager, libvlc_MediaSubItemAdded,
207
208
209
210
211
212
                         items_subitems_added, p_mlv, NULL );
                         
}
static void
media_list_will_add_item( const libvlc_event_t * p_event, void * user_data )
{
213
    libvlc_media_t * p_md;
214
215
216
217
218
219
220
221
222
223
224
    libvlc_media_list_view_t * p_mlv = user_data;
    int index;
    p_md = p_event->u.media_list_will_add_item.item;
    index = index_of_item( p_mlv, p_md );
    trace("%d\n", index);
    if( index >= 0)
        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 )
{
225
    libvlc_media_t * p_md;
226
227
228
229
230
231
232
    libvlc_media_list_view_t * p_mlv = user_data;
    int index;
    p_md = p_event->u.media_list_item_deleted.item;
    index = index_of_item( p_mlv, p_md );
    trace("%d\n", index);
    if( index >= 0)
        libvlc_media_list_view_item_deleted( p_mlv, p_md, index );
233
    libvlc_event_detach( p_md->p_event_manager, libvlc_MediaSubItemAdded,
234
235
236
237
238
                         items_subitems_added, p_mlv, NULL );
}
static void
media_list_will_delete_item( const libvlc_event_t * p_event, void * user_data )
{
239
    libvlc_media_t * p_md;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    libvlc_media_list_view_t * p_mlv = user_data;
    int index;
    p_md = p_event->u.media_list_will_delete_item.item;
    index = index_of_item( p_mlv, p_md );
    trace("%d\n", index);
    if( index >= 0)
        libvlc_media_list_view_will_delete_item( p_mlv, p_md, index );
}


/*
 * Public libvlc functions
 */


/**************************************************************************
 *       flat_media_list_view_release (private)
 * (called by media_list_view_release)
 **************************************************************************/
static void
hierarch_node_media_list_view_release( libvlc_media_list_view_t * p_mlv )
{
    trace("\n");
    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 );
}

/**************************************************************************
 *       libvlc_media_list_flat_view (Public)
 **************************************************************************/
libvlc_media_list_view_t *
libvlc_media_list_hierarchical_node_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_node_media_list_view_count,
                                        hierarch_node_media_list_view_item_at_index,
                                        hierarch_node_media_list_view_children_at_index,
290
                                        libvlc_media_list_hierarchical_node_view,
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
                                        hierarch_node_media_list_view_release,
                                        NULL,
                                        p_e );
    libvlc_media_list_lock( p_mlist );
    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 );
    libvlc_media_list_unlock( p_mlist );
    return p_mlv;
}