]> git.sesse.net Git - vlc/blob - src/control/media_list.c
control/media_list.c: Send WillAddItem and WillDeleteItem events.
[vlc] / src / control / media_list.c
1 /*****************************************************************************
2  * media_list.c: libvlc new API media list 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 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_arrays.h"
28
29 typedef enum EventPlaceInTime {
30     EventWillHappen,
31     EventDidHappen
32 } EventPlaceInTime;
33
34 /*
35  * Private functions
36  */
37
38
39
40 /**************************************************************************
41  *       notify_item_addition (private)
42  *
43  * Do the appropriate action when an item is deleted.
44  **************************************************************************/
45 static void
46 notify_item_addition( libvlc_media_list_t * p_mlist,
47                       libvlc_media_descriptor_t * p_md,
48                       int index,
49                       EventPlaceInTime event_status )
50 {
51     libvlc_event_t event;
52
53     /* Construct the event */
54     if( event_status == EventDidHappen )
55     {
56         event.type = libvlc_MediaListItemAdded;
57         event.u.media_list_item_added.item = p_md;
58         event.u.media_list_item_added.index = index;
59     }
60     else /* if( event_status == EventWillHappen ) */
61     {
62         event.type = libvlc_MediaListWillAddItem;
63         event.u.media_list_will_add_item.item = p_md;
64         event.u.media_list_will_add_item.index = index;
65     }
66
67     /* Send the event */
68     libvlc_event_send( p_mlist->p_event_manager, &event );
69 }
70
71 /**************************************************************************
72  *       notify_item_deletion (private)
73  *
74  * Do the appropriate action when an item is added.
75  **************************************************************************/
76 static void
77 notify_item_deletion( libvlc_media_list_t * p_mlist,
78                       libvlc_media_descriptor_t * p_md,
79                       int index,
80                       EventPlaceInTime event_status )
81 {
82     libvlc_event_t event;
83
84     /* Construct the event */
85     if( event_status == EventDidHappen )
86     {
87         event.type = libvlc_MediaListItemDeleted;
88         event.u.media_list_item_deleted.item = p_md;
89         event.u.media_list_item_deleted.index = index;
90     }
91     else /* if( event_status == EventWillHappen ) */
92     {
93         event.type = libvlc_MediaListWillDeleteItem;
94         event.u.media_list_will_delete_item.item = p_md;
95         event.u.media_list_will_delete_item.index = index;
96     }
97
98     /* Send the event */
99     libvlc_event_send( p_mlist->p_event_manager, &event );
100 }
101
102 /*
103  * Public libvlc functions
104  */
105
106 /**************************************************************************
107  *       libvlc_media_list_new (Public)
108  *
109  * Init an object.
110  **************************************************************************/
111 libvlc_media_list_t *
112 libvlc_media_list_new( libvlc_instance_t * p_inst,
113                        libvlc_exception_t * p_e )
114 {
115     libvlc_media_list_t * p_mlist;
116
117     p_mlist = malloc(sizeof(libvlc_media_list_t));
118
119     if( !p_mlist )
120         return NULL;
121  
122     p_mlist->p_libvlc_instance = p_inst;
123     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
124
125     /* Code for that one should be handled in flat_media_list.c */
126     p_mlist->p_flat_mlist = NULL;
127
128     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
129             libvlc_MediaListItemAdded, p_e );
130     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
131             libvlc_MediaListItemDeleted, p_e );
132
133     if( libvlc_exception_raised( p_e ) )
134     {
135         libvlc_event_manager_release( p_mlist->p_event_manager );
136         free( p_mlist );
137         return NULL;
138     }
139
140     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
141  
142     vlc_array_init( &p_mlist->items );
143     p_mlist->i_refcount = 1;
144     p_mlist->p_md = NULL;
145
146     return p_mlist;
147 }
148
149 /**************************************************************************
150  *       libvlc_media_list_release (Public)
151  *
152  * Release an object.
153  **************************************************************************/
154 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
155 {
156     libvlc_media_descriptor_t * p_md;
157     int i;
158
159     vlc_mutex_lock( &p_mlist->object_lock );
160     p_mlist->i_refcount--;
161     if( p_mlist->i_refcount > 0 )
162     {
163         vlc_mutex_unlock( &p_mlist->object_lock );
164         return;
165     }
166     vlc_mutex_unlock( &p_mlist->object_lock );
167
168     /* Refcount null, time to free */
169
170     libvlc_event_manager_release( p_mlist->p_event_manager );
171
172     if( p_mlist->p_md )
173         libvlc_media_descriptor_release( p_mlist->p_md );
174
175     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
176     {
177         p_md = vlc_array_item_at_index( &p_mlist->items, i );
178         libvlc_media_descriptor_release( p_md );
179     }
180
181     vlc_mutex_destroy( &p_mlist->object_lock );
182     vlc_array_clear( &p_mlist->items );
183
184     free( p_mlist );
185 }
186
187 /**************************************************************************
188  *       libvlc_media_list_retain (Public)
189  *
190  * Increase an object refcount.
191  **************************************************************************/
192 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
193 {
194     vlc_mutex_lock( &p_mlist->object_lock );
195     p_mlist->i_refcount++;
196     vlc_mutex_unlock( &p_mlist->object_lock );
197 }
198
199
200 /**************************************************************************
201  *       add_file_content (Public)
202  **************************************************************************/
203 void
204 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
205                                     const char * psz_uri,
206                                     libvlc_exception_t * p_e )
207 {
208     input_item_t * p_input_item;
209     libvlc_media_descriptor_t * p_md;
210
211     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
212                                 _("Media Library"), 0, NULL, -1 );
213
214     if( !p_input_item )
215     {
216         libvlc_exception_raise( p_e, "Can't create an input item" );
217         return;
218     }
219
220     p_md = libvlc_media_descriptor_new_from_input_item(
221             p_mlist->p_libvlc_instance,
222             p_input_item, p_e );
223
224     if( !p_md )
225     {
226         vlc_gc_decref( p_input_item );
227         return;
228     }
229
230     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
231     if( libvlc_exception_raised( p_e ) )
232         return;
233
234     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
235
236     return;
237 }
238
239 /**************************************************************************
240  *       set_media_descriptor (Public)
241  **************************************************************************/
242 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
243                                              libvlc_media_descriptor_t * p_md,
244                                              libvlc_exception_t * p_e)
245
246 {
247     (void)p_e;
248     vlc_mutex_lock( &p_mlist->object_lock );
249     if( p_mlist->p_md )
250         libvlc_media_descriptor_release( p_mlist->p_md );
251     libvlc_media_descriptor_retain( p_md );
252     p_mlist->p_md = p_md;
253     vlc_mutex_unlock( &p_mlist->object_lock );
254 }
255
256 /**************************************************************************
257  *       media_descriptor (Public)
258  *
259  * If this media_list comes is a media_descriptor's subitems,
260  * This holds the corresponding media_descriptor.
261  * This md is also seen as the information holder for the media_list.
262  * Indeed a media_list can have meta information through this
263  * media_descriptor.
264  **************************************************************************/
265 libvlc_media_descriptor_t *
266 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
267                                     libvlc_exception_t * p_e)
268 {
269     libvlc_media_descriptor_t *p_md;
270     (void)p_e;
271
272     vlc_mutex_lock( &p_mlist->object_lock );
273     p_md = p_mlist->p_md;
274     if( p_md )
275         libvlc_media_descriptor_retain( p_md );
276     vlc_mutex_unlock( &p_mlist->object_lock );
277
278     return p_md;
279 }
280
281 /**************************************************************************
282  *       libvlc_media_list_count (Public)
283  *
284  * Lock should be hold when entering.
285  **************************************************************************/
286 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
287                              libvlc_exception_t * p_e )
288 {
289     (void)p_e;
290     return vlc_array_count( &p_mlist->items );
291 }
292
293 /**************************************************************************
294  *       libvlc_media_list_add_media_descriptor (Public)
295  *
296  * Lock should be hold when entering.
297  **************************************************************************/
298 void libvlc_media_list_add_media_descriptor(
299                                    libvlc_media_list_t * p_mlist,
300                                    libvlc_media_descriptor_t * p_md,
301                                    libvlc_exception_t * p_e )
302 {
303     (void)p_e;
304     libvlc_media_descriptor_retain( p_md );
305
306     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
307     vlc_array_append( &p_mlist->items, p_md );
308     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
309 }
310
311 /**************************************************************************
312  *       libvlc_media_list_insert_media_descriptor (Public)
313  *
314  * Lock should be hold when entering.
315  **************************************************************************/
316 void libvlc_media_list_insert_media_descriptor(
317                                    libvlc_media_list_t * p_mlist,
318                                    libvlc_media_descriptor_t * p_md,
319                                    int index,
320                                    libvlc_exception_t * p_e )
321 {
322     (void)p_e;
323     libvlc_media_descriptor_retain( p_md );
324
325     notify_item_addition( p_mlist, p_md, index, EventWillHappen );
326     vlc_array_insert( &p_mlist->items, p_md, index );
327     notify_item_addition( p_mlist, p_md, index, EventDidHappen );
328 }
329
330 /**************************************************************************
331  *       libvlc_media_list_remove_index (Public)
332  *
333  * Lock should be hold when entering.
334  **************************************************************************/
335 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
336                                      int index,
337                                      libvlc_exception_t * p_e )
338 {
339     libvlc_media_descriptor_t * p_md;
340
341     p_md = vlc_array_item_at_index( &p_mlist->items, index );
342
343     notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
344     vlc_array_remove( &p_mlist->items, index );
345     notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
346
347     libvlc_media_descriptor_release( p_md );
348 }
349
350 /**************************************************************************
351  *       libvlc_media_list_item_at_index (Public)
352  *
353  * Lock should be hold when entering.
354  **************************************************************************/
355 libvlc_media_descriptor_t *
356 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
357                                  int index,
358                                  libvlc_exception_t * p_e )
359 {
360     libvlc_media_descriptor_t * p_md;
361     p_md = vlc_array_item_at_index( &p_mlist->items, index );
362     libvlc_media_descriptor_retain( p_md );
363     return p_md;
364 }
365
366 /**************************************************************************
367  *       libvlc_media_list_index_of_item (Public)
368  *
369  * Lock should be hold when entering.
370  * Warning: this function would return the first matching item
371  **************************************************************************/
372 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
373                                      libvlc_media_descriptor_t * p_searched_md,
374                                      libvlc_exception_t * p_e )
375 {
376     libvlc_media_descriptor_t * p_md;
377     int i;
378     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
379     {
380         p_md = vlc_array_item_at_index( &p_mlist->items, i );
381         if( p_searched_md == p_md )
382             return i;
383     }
384     return -1;
385 }
386
387 /**************************************************************************
388  *       libvlc_media_list_lock (Public)
389  *
390  * The lock must be held in access operations. It is never used in the
391  * Public method.
392  **************************************************************************/
393 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
394 {
395     vlc_mutex_lock( &p_mlist->object_lock );
396 }
397
398
399 /**************************************************************************
400  *       libvlc_media_list_unlock (Public)
401  *
402  * The lock must be held in access operations
403  **************************************************************************/
404 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
405 {
406     vlc_mutex_unlock( &p_mlist->object_lock );
407 }
408
409
410
411 /**************************************************************************
412  *       libvlc_media_list_p_event_manager (Public)
413  *
414  * The p_event_manager is immutable, so you don't have to hold the lock
415  **************************************************************************/
416 libvlc_event_manager_t *
417 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
418                                     libvlc_exception_t * p_e )
419 {
420     (void)p_e;
421     return p_mlist->p_event_manager;
422 }