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