]> git.sesse.net Git - vlc/blob - src/control/flat_media_list_view.c
control/flat_media_list_view.c: Import pre existing md in the view.
[vlc] / src / control / flat_media_list_view.c
1 /*****************************************************************************
2  * flat_media_list_view.c: libvlc flat media list view functions.
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id: flat_media_list.c 21287 2007-08-20 01:28:12Z pdherbemont $
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
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_arrays.h"
28
29 //#define DEBUG_FLAT_VIEW
30
31 #ifdef DEBUG_FLAT_VIEW
32 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
33 #else
34 # define trace( ... )
35 #endif
36
37 struct libvlc_media_list_view_private_t
38 {
39     vlc_array_t array;
40 };
41
42 /*
43  * Private functions
44  */
45
46 /**************************************************************************
47  *       ml_item_added  (private) (Callback from media_list_view item_added)
48  **************************************************************************/
49 static void
50 ml_item_added( const libvlc_event_t * p_event, libvlc_media_list_view_t * p_mlv )
51 {
52     int index = vlc_array_count( &p_mlv->p_this_view_data->array );
53     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
54     libvlc_media_descriptor_retain( p_md );
55     libvlc_media_list_view_will_add_item( p_mlv, p_md, index );
56     vlc_array_append( &p_mlv->p_this_view_data->array, p_md );
57     libvlc_media_list_view_item_added( p_mlv, p_md, index );
58 }
59
60 /**************************************************************************
61  *       ml_item_removed  (private) (Callback from media_list_view)
62  **************************************************************************/
63 static void
64 ml_item_removed( const libvlc_event_t * p_event, libvlc_media_list_view_t * p_mlv )
65 {
66     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_deleted.item;
67     int i = vlc_array_index_of_item( &p_mlv->p_this_view_data->array, p_md );
68     if( i >= 0 )
69     {
70         libvlc_media_list_view_will_delete_item( p_mlv, p_md, i );
71         vlc_array_remove( &p_mlv->p_this_view_data->array, i );
72         libvlc_media_list_view_item_deleted( p_mlv, p_md, i );
73         libvlc_media_descriptor_release( p_md );
74     }
75 }
76
77 /**************************************************************************
78  *       flat_media_list_view_count  (private)
79  * (called by media_list_view_count)
80  **************************************************************************/
81 static int
82 flat_media_list_view_count( libvlc_media_list_view_t * p_mlv,
83                             libvlc_exception_t * p_e )
84 {
85     (void)p_e;
86     return vlc_array_count( &p_mlv->p_this_view_data->array );
87 }
88
89 /**************************************************************************
90  *       flat_media_list_view_item_at_index  (private)
91  * (called by flat_media_list_view_item_at_index)
92  **************************************************************************/
93 static libvlc_media_descriptor_t *
94 flat_media_list_view_item_at_index( libvlc_media_list_view_t * p_mlv,
95                                     int index,
96                                     libvlc_exception_t * p_e )
97 {
98     libvlc_media_descriptor_t * p_md;
99     (void)p_e;
100     p_md = vlc_array_item_at_index( &p_mlv->p_this_view_data->array, index );
101     libvlc_media_descriptor_retain( p_md );
102     return p_md;
103 }
104
105 /**************************************************************************
106  *       flat_media_list_view_item_at_index  (private)
107  * (called by flat_media_list_view_item_at_index)
108  **************************************************************************/
109 static libvlc_media_list_view_t *
110 flat_media_list_view_children_at_index( libvlc_media_list_view_t * p_mlv,
111                                         int index,
112                                         libvlc_exception_t * p_e )
113 {
114     return NULL;
115 }
116
117 /**************************************************************************
118  *       flat_media_list_view_release (private)
119  * (called by media_list_view_release)
120  **************************************************************************/
121 static void
122 flat_media_list_view_release( libvlc_media_list_view_t * p_mlv )
123 {
124     vlc_array_clear( &p_mlv->p_this_view_data->array );
125     free( p_mlv->p_this_view_data );
126 }
127
128 /*
129  * Public libvlc functions
130  */
131
132 /* Little helper */
133 static void
134 import_mlist_rec( libvlc_media_list_view_t * p_mlv,
135                   libvlc_media_list_t * p_mlist,
136                   libvlc_exception_t * p_e )
137 {
138     int i, count;
139     count = libvlc_media_list_count( p_mlist, p_e );
140     for( i = 0; i < count; i++ )
141     {
142         libvlc_media_descriptor_t * p_md;
143         libvlc_media_list_t * p_submlist;
144         p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
145         vlc_array_append( &p_mlv->p_this_view_data->array, p_md );
146         p_submlist = libvlc_media_descriptor_subitems( p_md, p_e );
147         if( p_submlist )
148         {
149             libvlc_media_list_lock( p_submlist );
150             import_mlist_rec( p_mlv, p_submlist, p_e );
151             libvlc_media_list_unlock( p_submlist );
152             libvlc_media_list_release( p_submlist );
153         }
154         /* No need to release the md, as we want to retain it, as it is
155          * stored in our array */
156     }
157 }
158                         
159 /**************************************************************************
160  *       libvlc_media_list_flat_view (Public)
161  **************************************************************************/
162 libvlc_media_list_view_t *
163 libvlc_media_list_flat_view( libvlc_media_list_t * p_mlist,
164                              libvlc_exception_t * p_e )
165 {
166     trace("\n");
167     libvlc_media_list_view_t * p_mlv;
168     struct libvlc_media_list_view_private_t * p_this_view_data;
169     p_this_view_data = malloc(sizeof(struct libvlc_media_list_view_private_t));
170     vlc_array_init( &p_this_view_data->array );
171     p_mlv = libvlc_media_list_view_new( p_mlist,
172                                         flat_media_list_view_count,
173                                         flat_media_list_view_item_at_index,
174                                         flat_media_list_view_children_at_index,
175                                         flat_media_list_view_release,
176                                         p_this_view_data,
177                                         p_e );
178     libvlc_media_list_lock( p_mlist );
179     import_mlist_rec( p_mlv, p_mlist, p_e );
180     libvlc_media_list_view_set_ml_notification_callback( p_mlv,
181         ml_item_added,
182         ml_item_removed );
183     libvlc_media_list_unlock( p_mlist );
184
185     return p_mlv;
186 }