]> git.sesse.net Git - vlc/blob - src/control/media_list.c
control/media_list.c: Revert the tagging of the subitems. This is not our job. Also...
[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 /*
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     libvlc_media_descriptor_t * p_parent_md = p_event->p_obj;
112     int count;
113     char * psz_parent_tag = NULL;
114     char * psz_parent_name, *psz_tag;
115
116     p_new_md = p_event->u.media_descriptor_subitem_added.new_child;
117
118     /* Todo: Just forward that event to our event_manager */
119 }
120
121 /**************************************************************************
122  *       install_media_descriptor_observer (private)
123  *
124  * Do the appropriate action when an item is deleted.
125  **************************************************************************/
126 static void
127 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
128                                    libvlc_media_descriptor_t * p_md )
129 {
130     libvlc_event_attach( p_md->p_event_manager,
131                          libvlc_MediaDescriptorMetaChanged,
132                          media_descriptor_changed,
133                          p_mlist, NULL );
134     libvlc_event_attach( p_md->p_event_manager,
135                          libvlc_MediaDescriptorSubItemAdded,
136                          media_descriptor_subitem_added,
137                          p_mlist, NULL );
138 }
139
140 /**************************************************************************
141  *       uninstall_media_descriptor_observer (private)
142  *
143  * Do the appropriate action when an item is deleted.
144  **************************************************************************/
145 static void
146 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
147                                      libvlc_media_descriptor_t * p_md )
148 {
149     libvlc_event_detach( p_md->p_event_manager,
150                          libvlc_MediaDescriptorMetaChanged,
151                          media_descriptor_changed,
152                          p_mlist, NULL );
153     libvlc_event_detach( p_md->p_event_manager,
154                          libvlc_MediaDescriptorSubItemAdded,
155                          media_descriptor_subitem_added,
156                          p_mlist, NULL );
157 }
158
159 /*
160  * Public libvlc functions
161  */
162
163 /**************************************************************************
164  *       libvlc_media_list_new (Public)
165  *
166  * Init an object.
167  **************************************************************************/
168 libvlc_media_list_t *
169 libvlc_media_list_new( libvlc_instance_t * p_inst,
170                        libvlc_exception_t * p_e )
171 {
172     libvlc_media_list_t * p_mlist;
173
174     p_mlist = malloc(sizeof(libvlc_media_list_t));
175
176     if( !p_mlist )
177         return NULL;
178     
179     p_mlist->p_libvlc_instance = p_inst;
180     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
181
182     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
183             libvlc_MediaListItemAdded, p_e );
184     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
185             libvlc_MediaListItemChanged, p_e );
186     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
187             libvlc_MediaListItemDeleted, p_e );
188
189     if( libvlc_exception_raised( p_e ) )
190     {
191         libvlc_event_manager_release( p_mlist->p_event_manager );
192         free( p_mlist );
193         return NULL;
194     }
195
196     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
197     
198     ARRAY_INIT(p_mlist->items);
199     p_mlist->i_refcount = 1;
200     p_mlist->psz_name = NULL;
201
202     return p_mlist;
203 }
204
205 /**************************************************************************
206  *       libvlc_media_list_release (Public)
207  *
208  * Release an object.
209  **************************************************************************/
210 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
211 {
212     libvlc_media_descriptor_t * p_md;
213
214     vlc_mutex_lock( &p_mlist->object_lock );
215     p_mlist->i_refcount--;
216     if( p_mlist->i_refcount > 0 )
217     {
218         vlc_mutex_unlock( &p_mlist->object_lock );        
219         return;
220     }
221     vlc_mutex_unlock( &p_mlist->object_lock );        
222
223     /* Refcount null, time to free */
224
225     libvlc_event_manager_release( p_mlist->p_event_manager );
226
227     FOREACH_ARRAY( p_md, p_mlist->items )
228         uninstall_media_descriptor_observer( p_mlist, p_md );
229         libvlc_media_descriptor_release( p_md );
230     FOREACH_END()
231  
232     free( p_mlist );
233 }
234
235 /**************************************************************************
236  *       libvlc_media_list_retain (Public)
237  *
238  * Increase an object refcount.
239  **************************************************************************/
240 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
241 {
242     vlc_mutex_lock( &p_mlist->object_lock );
243     p_mlist->i_refcount++;
244     vlc_mutex_unlock( &p_mlist->object_lock );
245 }
246
247
248 /**************************************************************************
249  *       add_file_content (Public)
250  **************************************************************************/
251 void
252 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
253                                     const char * psz_uri,
254                                     libvlc_exception_t * p_e )
255 {
256     input_item_t * p_input_item;
257     libvlc_media_descriptor_t * p_md;
258
259     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
260                                 _("Media Library"), 0, NULL, -1 );
261
262     if( !p_input_item )
263     {
264         libvlc_exception_raise( p_e, "Can't create an input item" );
265         return;
266     }
267
268     p_md = libvlc_media_descriptor_new_from_input_item(
269             p_mlist->p_libvlc_instance,
270             p_input_item, p_e );
271
272     if( !p_md )
273     {
274         vlc_gc_decref( p_input_item );
275         return;
276     }
277
278     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
279     if( libvlc_exception_raised( p_e ) )
280         return;
281
282     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
283
284     return;
285 }
286
287 /**************************************************************************
288  *       set_name (Public)
289  **************************************************************************/
290 void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
291                                  const char * psz_name,
292                                  libvlc_exception_t * p_e)
293
294 {
295     (void)p_e;
296     vlc_mutex_lock( &p_mlist->object_lock );
297     free( p_mlist->psz_name );
298     p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
299     vlc_mutex_unlock( &p_mlist->object_lock );
300 }
301
302 /**************************************************************************
303  *       name (Public)
304  **************************************************************************/
305 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
306                                libvlc_exception_t * p_e)
307 {
308     char *ret;
309     (void)p_e;
310
311     vlc_mutex_lock( &p_mlist->object_lock );
312     ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
313     vlc_mutex_unlock( &p_mlist->object_lock );
314
315     return ret;
316 }
317
318 /**************************************************************************
319  *       libvlc_media_list_count (Public)
320  *
321  * Lock should be hold when entering.
322  **************************************************************************/
323 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
324                              libvlc_exception_t * p_e )
325 {
326     (void)p_e;
327     return p_mlist->items.i_size;
328 }
329
330 /**************************************************************************
331  *       libvlc_media_list_add_media_descriptor (Public)
332  *
333  * Lock should be hold when entering.
334  **************************************************************************/
335 void libvlc_media_list_add_media_descriptor( 
336                                    libvlc_media_list_t * p_mlist,
337                                    libvlc_media_descriptor_t * p_md,
338                                    libvlc_exception_t * p_e )
339 {
340     (void)p_e;
341     libvlc_media_descriptor_retain( p_md );
342     ARRAY_APPEND( p_mlist->items, p_md );
343     notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
344     install_media_descriptor_observer( p_mlist, p_md );
345 }
346
347 /**************************************************************************
348  *       libvlc_media_list_insert_media_descriptor (Public)
349  *
350  * Lock should be hold when entering.
351  **************************************************************************/
352 void libvlc_media_list_insert_media_descriptor( 
353                                    libvlc_media_list_t * p_mlist,
354                                    libvlc_media_descriptor_t * p_md,
355                                    int index,
356                                    libvlc_exception_t * p_e )
357 {
358     (void)p_e;
359     libvlc_media_descriptor_retain( p_md );
360
361     ARRAY_INSERT( p_mlist->items, p_md, index);
362     notify_item_addition( p_mlist, p_md, index );
363     install_media_descriptor_observer( p_mlist, p_md );
364 }
365
366 /**************************************************************************
367  *       libvlc_media_list_remove_index (Public)
368  *
369  * Lock should be hold when entering.
370  **************************************************************************/
371 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
372                                      int index,
373                                      libvlc_exception_t * p_e )
374 {
375     libvlc_media_descriptor_t * p_md;
376
377     p_md = ARRAY_VAL( p_mlist->items, index );
378
379     uninstall_media_descriptor_observer( p_mlist, p_md );
380
381     ARRAY_REMOVE( p_mlist->items, index )
382     notify_item_deletion( p_mlist, p_md, index );
383
384     libvlc_media_descriptor_release( p_md );
385 }
386
387 /**************************************************************************
388  *       libvlc_media_list_item_at_index (Public)
389  *
390  * Lock should be hold when entering.
391  **************************************************************************/
392 libvlc_media_descriptor_t *
393 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
394                                  int index,
395                                  libvlc_exception_t * p_e )
396 {
397     libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
398     libvlc_media_descriptor_retain( p_md );
399     return p_md;
400 }
401
402 /**************************************************************************
403  *       libvlc_media_list_index_of_item (Public)
404  *
405  * Lock should be hold when entering.
406  * Warning: this function would return the first matching item
407  **************************************************************************/
408 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
409                                      libvlc_media_descriptor_t * p_searched_md,
410                                      libvlc_exception_t * p_e )
411 {
412     libvlc_media_descriptor_t * p_md;
413     FOREACH_ARRAY( p_md, p_mlist->items )
414         if( p_searched_md == p_md )
415             return fe_idx; /* Once more, we hate macro for that */
416     FOREACH_END()
417     return -1;
418 }
419
420 /**************************************************************************
421  *       libvlc_media_list_lock (Public)
422  *
423  * The lock must be held in access operations. It is never used in the
424  * Public method.
425  **************************************************************************/
426 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
427 {
428     vlc_mutex_lock( &p_mlist->object_lock );
429 }
430
431
432 /**************************************************************************
433  *       libvlc_media_list_unlock (Public)
434  *
435  * The lock must be held in access operations
436  **************************************************************************/
437 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
438 {
439     vlc_mutex_unlock( &p_mlist->object_lock );
440 }
441
442
443
444 /**************************************************************************
445  *       libvlc_media_list_p_event_manager (Public)
446  *
447  * The p_event_manager is immutable, so you don't have to hold the lock
448  **************************************************************************/
449 libvlc_event_manager_t *
450 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
451                                     libvlc_exception_t * p_e )
452 {
453     (void)p_e;
454     return p_mlist->p_event_manager;
455 }