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