control/media_list.c: Fix a typo about subitems.
[vlc.git] / 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 /*
30  * Private functions
31  */
32
33
34
35 /**************************************************************************
36  *       notify_item_addition (private)
37  *
38  * Do the appropriate action when an item is deleted.
39  **************************************************************************/
40 static void
41 notify_item_addition( libvlc_media_list_t * p_mlist,
42                       libvlc_media_descriptor_t * p_md,
43                       int index )
44 {
45     libvlc_event_t event;
46
47     /* Construct the event */
48     event.type = libvlc_MediaListItemAdded;
49     event.u.media_list_item_added.item = p_md;
50     event.u.media_list_item_added.index = index;
51
52     /* Send the event */
53     libvlc_event_send( p_mlist->p_event_manager, &event );
54 }
55
56 /**************************************************************************
57  *       notify_item_deletion (private)
58  *
59  * Do the appropriate action when an item is added.
60  **************************************************************************/
61 static void
62 notify_item_deletion( libvlc_media_list_t * p_mlist,
63                       libvlc_media_descriptor_t * p_md,
64                       int index )
65 {
66     libvlc_event_t event;
67
68     /* Construct the event */
69     event.type = libvlc_MediaListItemDeleted;
70     event.u.media_list_item_deleted.item = p_md;
71     event.u.media_list_item_deleted.index = index;
72
73     /* Send the event */
74     libvlc_event_send( p_mlist->p_event_manager, &event );
75 }
76
77 /**************************************************************************
78  *       media_descriptor_changed (private) (libvlc Event Callback )
79  *
80  * An item has changed.
81  **************************************************************************/
82 static void
83 media_descriptor_changed( const libvlc_event_t * p_event, void * user_data )
84 {
85     libvlc_media_list_t * p_mlist = user_data;
86     libvlc_media_descriptor_t * p_md = p_event->p_obj;
87     libvlc_event_t event;
88     
89     /* Construct the new media list event */
90     event.type = libvlc_MediaListItemChanged;
91     event.u.media_list_item_changed.item = p_md;
92
93     /* XXX: this is not good, but there is a solution in the pipeline */
94     event.u.media_list_item_changed.index =
95         libvlc_media_list_index_of_item( p_mlist, p_md, NULL );
96
97     /* Send the event */
98     libvlc_event_send( p_mlist->p_event_manager, &event );
99 }
100
101 /**************************************************************************
102  *       media_descriptor_subitem_added (private) (libvlc Event Callback )
103  *
104  * An item (which is a playlist) has gained sub child.
105  **************************************************************************/
106 static void
107 media_descriptor_subitem_added( const libvlc_event_t * p_event, void * user_data )
108 {
109     libvlc_media_list_t * p_mlist = user_data;
110     libvlc_media_descriptor_t * p_new_md;
111
112     p_new_md = p_event->u.media_descriptor_subitem_added.new_child;
113
114     /* For now, just add the new item to this media list */
115     libvlc_media_list_add_media_descriptor( p_mlist, p_new_md, NULL );
116 }
117
118 /**************************************************************************
119  *       install_media_descriptor_observer (private)
120  *
121  * Do the appropriate action when an item is deleted.
122  **************************************************************************/
123 static void
124 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
125                                    libvlc_media_descriptor_t * p_md )
126 {
127     libvlc_event_attach( p_md->p_event_manager,
128                          libvlc_MediaDescriptorMetaChanged,
129                          media_descriptor_changed,
130                          p_mlist, NULL );
131     libvlc_event_attach( p_md->p_event_manager,
132                          libvlc_MediaDescriptorSubItemAdded,
133                          media_descriptor_subitem_added,
134                          p_mlist, NULL );
135 }
136
137 /**************************************************************************
138  *       uninstall_media_descriptor_observer (private)
139  *
140  * Do the appropriate action when an item is deleted.
141  **************************************************************************/
142 static void
143 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
144                                      libvlc_media_descriptor_t * p_md )
145 {
146     libvlc_event_detach( p_md->p_event_manager,
147                          libvlc_MediaDescriptorMetaChanged,
148                          media_descriptor_changed,
149                          p_mlist, NULL );
150     libvlc_event_detach( p_md->p_event_manager,
151                          libvlc_MediaDescriptorSubItemAdded,
152                          media_descriptor_subitem_added,
153                          p_mlist, NULL );
154 }
155
156 /**************************************************************************
157  *       dynamic_list_propose_item (private) (Event Callback)
158  *
159  * This is called if the dynamic sublist's data provider adds a new item.
160  **************************************************************************/
161 static void
162 dynamic_list_propose_item( const libvlc_event_t * p_event, void * p_user_data )
163 {
164     libvlc_media_list_t * p_submlist = p_user_data;
165     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
166
167     //libvlc_media_descriptor_lock( p_md );
168     if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
169     {
170         libvlc_media_list_lock( p_submlist );
171         libvlc_media_list_add_media_descriptor( p_submlist, p_md, NULL );
172         libvlc_media_list_unlock( p_submlist );
173     }
174     //libvlc_media_descriptor_unlock( p_md );
175 }
176
177 /**************************************************************************
178  *       dynamic_list_remove_item (private) (Event Callback)
179  *
180  * This is called if the dynamic sublist's data provider adds a new item.
181  **************************************************************************/
182 static void
183 dynamic_list_remove_item( const libvlc_event_t * p_event, void * p_user_data )
184 {
185     libvlc_media_list_t * p_submlist = p_user_data;
186     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_deleted.item;
187
188     //libvlc_media_descriptor_lock( p_md );
189     if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
190     {
191         int i;
192         libvlc_media_list_lock( p_submlist );        
193         i = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
194         if ( i < 0 )
195         {
196             /* We've missed one item addition, that could happen especially
197              * if we add item in a threaded maner, so we just ignore */
198             libvlc_media_list_unlock( p_submlist );
199             //libvlc_media_descriptor_unlock( p_md );           
200             return;
201         }
202         libvlc_media_list_remove_index( p_submlist, i, NULL );
203         libvlc_media_list_unlock( p_submlist );
204     }
205     //libvlc_media_descriptor_unlock( p_md );
206 }
207
208 /**************************************************************************
209  *       dynamic_list_change_item (private) (Event Callback)
210  *
211  * This is called if the dynamic sublist's data provider adds a new item.
212  **************************************************************************/
213 static void
214 dynamic_list_change_item( const libvlc_event_t * p_event , void * p_user_data)
215 {
216     libvlc_media_list_t * p_submlist = p_user_data;
217     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_changed.item;
218     int index;
219
220     libvlc_media_list_lock( p_submlist );        
221     
222     index = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
223     if( index < 0 )
224     {
225         libvlc_media_list_unlock( p_submlist );     
226         return; /* Not found, no prob, just ignore */
227     }
228
229     //libvlc_media_descriptor_lock( p_md );
230     if( !libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
231         libvlc_media_list_remove_index( p_submlist, index, NULL );
232     //libvlc_media_descriptor_unlock( p_md );
233
234     libvlc_media_list_unlock( p_submlist );        
235 }
236
237 /*
238  * Public libvlc functions
239  */
240
241 /**************************************************************************
242  *       libvlc_media_list_new (Public)
243  *
244  * Init an object.
245  **************************************************************************/
246 libvlc_media_list_t *
247 libvlc_media_list_new( libvlc_instance_t * p_inst,
248                        libvlc_exception_t * p_e )
249 {
250     libvlc_media_list_t * p_mlist;
251
252     p_mlist = malloc(sizeof(libvlc_media_list_t));
253
254     if( !p_mlist )
255         return NULL;
256     
257     p_mlist->p_libvlc_instance = p_inst;
258     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
259
260     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
261             libvlc_MediaListItemAdded, p_e );
262     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
263             libvlc_MediaListItemChanged, p_e );
264     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
265             libvlc_MediaListItemDeleted, p_e );
266
267     if( libvlc_exception_raised( p_e ) )
268     {
269         libvlc_event_manager_release( p_mlist->p_event_manager );
270         free( p_mlist );
271         return NULL;
272     }
273
274     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
275     
276     ARRAY_INIT(p_mlist->items);
277     p_mlist->i_refcount = 1;
278     p_mlist->p_media_provider = NULL;
279
280     return p_mlist;
281 }
282
283 /**************************************************************************
284  *       libvlc_media_list_release (Public)
285  *
286  * Release an object.
287  **************************************************************************/
288 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
289 {
290     libvlc_media_descriptor_t * p_md;
291
292     vlc_mutex_lock( &p_mlist->object_lock );
293     p_mlist->i_refcount--;
294     if( p_mlist->i_refcount > 0 )
295     {
296         vlc_mutex_unlock( &p_mlist->object_lock );        
297         return;
298     }
299     vlc_mutex_unlock( &p_mlist->object_lock );        
300
301     /* Refcount null, time to free */
302     if( p_mlist->p_media_provider )
303         libvlc_media_list_release( p_mlist->p_media_provider );
304
305     if( p_mlist->p_query )
306         libvlc_tag_query_release( p_mlist->p_query );
307
308     libvlc_event_manager_release( p_mlist->p_event_manager );
309
310     FOREACH_ARRAY( p_md, p_mlist->items )
311         uninstall_media_descriptor_observer( p_mlist, p_md );
312         libvlc_media_descriptor_release( p_md );
313     FOREACH_END()
314  
315     free( p_mlist );
316 }
317 /**************************************************************************
318  *       libvlc_media_list_retain (Public)
319  *
320  * Increase an object refcount.
321  **************************************************************************/
322 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
323 {
324     vlc_mutex_lock( &p_mlist->object_lock );
325     p_mlist->i_refcount++;
326     vlc_mutex_unlock( &p_mlist->object_lock );
327 }
328
329 /**************************************************************************
330  *       libvlc_media_list_count (Public)
331  *
332  * Lock should be hold when entering.
333  **************************************************************************/
334 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
335                              libvlc_exception_t * p_e )
336 {
337     (void)p_e;
338     return p_mlist->items.i_size;
339 }
340
341 /**************************************************************************
342  *       libvlc_media_list_add_media_descriptor (Public)
343  *
344  * Lock should be hold when entering.
345  **************************************************************************/
346 void libvlc_media_list_add_media_descriptor( 
347                                    libvlc_media_list_t * p_mlist,
348                                    libvlc_media_descriptor_t * p_md,
349                                    libvlc_exception_t * p_e )
350 {
351     (void)p_e;
352     libvlc_media_descriptor_retain( p_md );
353     ARRAY_APPEND( p_mlist->items, p_md );
354     notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
355     install_media_descriptor_observer( p_mlist, p_md );
356 }
357
358 /**************************************************************************
359  *       libvlc_media_list_insert_media_descriptor (Public)
360  *
361  * Lock should be hold when entering.
362  **************************************************************************/
363 void libvlc_media_list_insert_media_descriptor( 
364                                    libvlc_media_list_t * p_mlist,
365                                    libvlc_media_descriptor_t * p_md,
366                                    int index,
367                                    libvlc_exception_t * p_e )
368 {
369     (void)p_e;
370     libvlc_media_descriptor_retain( p_md );
371
372     ARRAY_INSERT( p_mlist->items, p_md, index);
373     notify_item_addition( p_mlist, p_md, index );
374     install_media_descriptor_observer( p_mlist, p_md );
375 }
376
377 /**************************************************************************
378  *       libvlc_media_list_remove_index (Public)
379  *
380  * Lock should be hold when entering.
381  **************************************************************************/
382 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
383                                      int index,
384                                      libvlc_exception_t * p_e )
385 {
386     libvlc_media_descriptor_t * p_md;
387
388     p_md = ARRAY_VAL( p_mlist->items, index );
389
390     uninstall_media_descriptor_observer( p_mlist, p_md );
391
392     ARRAY_REMOVE( p_mlist->items, index )
393     notify_item_deletion( p_mlist, p_md, index );
394
395     libvlc_media_descriptor_release( p_md );
396 }
397
398 /**************************************************************************
399  *       libvlc_media_list_item_at_index (Public)
400  *
401  * Lock should be hold when entering.
402  **************************************************************************/
403 libvlc_media_descriptor_t *
404 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
405                                  int index,
406                                  libvlc_exception_t * p_e )
407 {
408     libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
409     libvlc_media_descriptor_retain( p_md );
410     return p_md;
411 }
412
413 /**************************************************************************
414  *       libvlc_media_list_index_of_item (Public)
415  *
416  * Lock should be hold when entering.
417  * Warning: this function would return the first matching item
418  **************************************************************************/
419 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
420                                      libvlc_media_descriptor_t * p_searched_md,
421                                      libvlc_exception_t * p_e )
422 {
423     libvlc_media_descriptor_t * p_md;
424     FOREACH_ARRAY( p_md, p_mlist->items )
425         if( p_searched_md == p_md )
426             return fe_idx; /* Once more, we hate macro for that */
427     FOREACH_END()
428     return -1;
429 }
430
431 /**************************************************************************
432  *       libvlc_media_list_lock (Public)
433  *
434  * The lock must be held in access operations. It is never used in the
435  * Public method.
436  **************************************************************************/
437 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
438 {
439     vlc_mutex_lock( &p_mlist->object_lock );
440 }
441
442
443 /**************************************************************************
444  *       libvlc_media_list_unlock (Public)
445  *
446  * The lock must be held in access operations
447  **************************************************************************/
448 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
449 {
450     vlc_mutex_unlock( &p_mlist->object_lock );
451 }
452
453
454
455 /**************************************************************************
456  *       libvlc_media_list_p_event_manager (Public)
457  *
458  * The p_event_manager is immutable, so you don't have to hold the lock
459  **************************************************************************/
460 libvlc_event_manager_t *
461 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
462                                     libvlc_exception_t * p_e )
463 {
464     (void)p_e;
465     return p_mlist->p_event_manager;
466 }
467
468 /**************************************************************************
469  *       libvlc_media_list_dynamic_sublist (Public)
470  *
471  * Lock should be hold when entering.
472  **************************************************************************/
473 libvlc_media_list_t *
474 libvlc_media_list_dynamic_sublist( libvlc_media_list_t * p_mlist,
475                                    libvlc_tag_query_t * p_query,
476                                    libvlc_exception_t * p_e )
477 {
478     libvlc_media_list_t * p_submlist;
479     libvlc_event_manager_t * p_em;
480     int count, i;
481
482     (void)p_e;
483
484     p_submlist = libvlc_media_list_new( p_mlist->p_libvlc_instance, p_e );
485     if( !p_submlist )
486     {
487         if( !libvlc_exception_raised( p_e ) )
488             libvlc_exception_raise( p_e, "Can't get the new media_list" );
489         return NULL;
490     }
491
492     /* We have a query */
493     libvlc_tag_query_retain( p_query );
494     p_submlist->p_query = p_query;
495
496     /* We have a media provider */
497     libvlc_media_list_retain( p_mlist );
498     p_submlist->p_media_provider = p_mlist;
499
500
501     libvlc_media_list_lock( p_submlist );        
502     
503     count = libvlc_media_list_count( p_mlist, p_e );
504
505     /* This should be running in a thread, a good plan to achieve that
506      * move all the dynamic code to libvlc_tag_query. */
507     for( i = 0; i < count; i++ )
508     {
509         libvlc_media_descriptor_t * p_md;
510         p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
511         if( libvlc_tag_query_match( p_query, p_md, NULL ) )
512             libvlc_media_list_add_media_descriptor( p_submlist, p_md, p_e );
513     }
514
515     /* And we will listen to its event, so we can update p_submlist
516      * accordingly */
517     p_em = libvlc_media_list_event_manager( p_mlist, p_e );
518     libvlc_event_attach( p_em, libvlc_MediaListItemAdded,
519                          dynamic_list_propose_item, p_submlist, p_e );
520     libvlc_event_attach( p_em, libvlc_MediaListItemDeleted,
521                          dynamic_list_remove_item, p_submlist, p_e );
522     libvlc_event_attach( p_em, libvlc_MediaListItemChanged,
523                          dynamic_list_change_item, p_submlist, p_e );
524
525     libvlc_media_list_unlock( p_submlist );        
526
527     return p_submlist;
528 }