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