]> git.sesse.net Git - vlc/blob - src/input/item.c
input_item: Remove input_item_AddSubItem2 and send subitem_added event from input_ite...
[vlc] / src / input / item.c
1 /*****************************************************************************
2  * item.c: input_item management
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@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 along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include <vlc_url.h>
31 #include "vlc_playlist.h"
32 #include "vlc_interface.h"
33
34 #include "item.h"
35
36 static int GuessType( const input_item_t *p_item );
37
38 /** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
39  * anyway. */
40 static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
41 {
42     memset( p_i, 0, sizeof(input_item_t) );
43
44     p_i->psz_name = NULL;
45     p_i->psz_uri = NULL;
46     TAB_INIT( p_i->i_es, p_i->es );
47     TAB_INIT( p_i->i_options, p_i->ppsz_options );
48     p_i->optflagv = NULL, p_i->optflagc = 0;
49     TAB_INIT( p_i->i_categories, p_i->pp_categories );
50     TAB_INIT( p_i->i_epg, p_i->pp_epg );
51
52     p_i->i_type = ITEM_TYPE_UNKNOWN;
53     p_i->b_fixed_name = true;
54
55     p_i->p_stats = NULL;
56     p_i->p_meta = NULL;
57
58     vlc_mutex_init( &p_i->lock );
59     vlc_event_manager_t * p_em = &p_i->event_manager;
60     vlc_event_manager_init( p_em, p_i, p_o );
61     vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
62     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
63     vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemTreeAdded );
64     vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
65     vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
66     vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
67     vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
68     vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
69 }
70
71 static inline void input_item_Clean( input_item_t *p_i )
72 {
73     int i;
74
75     vlc_event_manager_fini( &p_i->event_manager );
76
77     free( p_i->psz_name );
78     free( p_i->psz_uri );
79     if( p_i->p_stats )
80     {
81         vlc_mutex_destroy( &p_i->p_stats->lock );
82         free( p_i->p_stats );
83     }
84
85     if( p_i->p_meta )
86         vlc_meta_Delete( p_i->p_meta );
87
88     for( i = 0; i < p_i->i_options; i++ )
89         free( p_i->ppsz_options[i] );
90     TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
91     free( p_i->optflagv);
92
93     for( i = 0; i < p_i->i_es; i++ )
94     {
95         es_format_Clean( p_i->es[i] );
96         free( p_i->es[i] );
97     }
98     TAB_CLEAN( p_i->i_es, p_i->es );
99
100     for( i = 0; i < p_i->i_epg; i++ )
101         vlc_epg_Delete( p_i->pp_epg[i] );
102     TAB_CLEAN( p_i->i_epg, p_i->pp_epg );
103
104     for( i = 0; i < p_i->i_categories; i++ )
105     {
106         info_category_t *p_category = p_i->pp_categories[i];
107         int j;
108
109         for( j = 0; j < p_category->i_infos; j++ )
110         {
111             struct info_t *p_info = p_category->pp_infos[j];
112
113             free( p_info->psz_name);
114             free( p_info->psz_value );
115             free( p_info );
116         }
117         TAB_CLEAN( p_category->i_infos, p_category->pp_infos );
118
119         free( p_category->psz_name );
120         free( p_category );
121     }
122     TAB_CLEAN( p_i->i_categories, p_i->pp_categories );
123
124     vlc_mutex_destroy( &p_i->lock );
125 }
126 void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
127 {
128     bool b_changed;
129
130     vlc_mutex_lock( &p_i->lock );
131
132     b_changed = p_i->b_error_when_reading != b_error;
133     p_i->b_error_when_reading = b_error;
134
135     vlc_mutex_unlock( &p_i->lock );
136
137     if( b_changed )
138     {
139         vlc_event_t event;
140
141         event.type = vlc_InputItemErrorWhenReadingChanged;
142         event.u.input_item_error_when_reading_changed.new_value = b_error;
143         vlc_event_send( &p_i->event_manager, &event );
144     }
145 }
146 void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
147 {
148     bool b_send_event = false;
149
150     vlc_mutex_lock( &p_i->lock );
151
152     if( !p_i->p_meta )
153         p_i->p_meta = vlc_meta_New();
154
155     int status = vlc_meta_GetStatus(p_i->p_meta);
156     int new_status;
157     if( b_preparsed )
158         new_status = status | ITEM_PREPARSED;
159     else
160         new_status = status & ~ITEM_PREPARSED;
161     if( status != new_status )
162     {
163         vlc_meta_SetStatus(p_i->p_meta, new_status);
164         b_send_event = true;
165     }
166
167     vlc_mutex_unlock( &p_i->lock );
168
169     if( b_send_event )
170     {
171         vlc_event_t event;
172         event.type = vlc_InputItemPreparsedChanged;
173         event.u.input_item_preparsed_changed.new_status = new_status;
174         vlc_event_send( &p_i->event_manager, &event );
175     }
176 }
177
178 void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
179 {
180     vlc_mutex_lock( &p_i->lock );
181
182     if( !p_i->p_meta )
183         p_i->p_meta = vlc_meta_New();
184
185     int status = vlc_meta_GetStatus(p_i->p_meta);
186
187     if( b_not_found )
188         status |= ITEM_ART_NOTFOUND;
189     else
190         status &= ~ITEM_ART_NOTFOUND;
191
192     vlc_meta_SetStatus(p_i->p_meta, status);
193
194     vlc_mutex_unlock( &p_i->lock );
195 }
196
197 void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
198 {
199     vlc_mutex_lock( &p_i->lock );
200
201     if( !p_i->p_meta )
202         p_i->p_meta = vlc_meta_New();
203
204     int status = vlc_meta_GetStatus(p_i->p_meta);
205
206     if( b_art_fetched )
207         status |= ITEM_ART_FETCHED;
208     else
209         status &= ~ITEM_ART_FETCHED;
210
211     vlc_meta_SetStatus(p_i->p_meta, status);
212
213     vlc_mutex_unlock( &p_i->lock );
214 }
215
216 void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
217 {
218     vlc_event_t event;
219
220     vlc_mutex_lock( &p_i->lock );
221     if( !p_i->p_meta )
222         p_i->p_meta = vlc_meta_New();
223     vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
224     vlc_mutex_unlock( &p_i->lock );
225
226     /* Notify interested third parties */
227     event.type = vlc_InputItemMetaChanged;
228     event.u.input_item_meta_changed.meta_type = meta_type;
229     vlc_event_send( &p_i->event_manager, &event );
230 }
231
232 /* FIXME GRRRRRRRRRR args should be in the reverse order to be
233  * consistant with (nearly?) all or copy funcs */
234 void input_item_CopyOptions( input_item_t *p_parent,
235                              input_item_t *p_child )
236 {
237     vlc_mutex_lock( &p_parent->lock );
238
239     for( int i = 0 ; i< p_parent->i_options; i++ )
240     {
241         if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
242             continue;
243
244         input_item_AddOption( p_child,
245                               p_parent->ppsz_options[i],
246                               p_parent->optflagv[i] );
247     }
248
249     vlc_mutex_unlock( &p_parent->lock );
250 }
251
252 static void notify_subitem_added(input_item_t *p_parent, input_item_t *p_child)
253 {
254     /* Notify interested third parties */
255     vlc_event_t event;
256     event.type = vlc_InputItemSubItemAdded;
257     event.u.input_item_subitem_added.p_new_child = p_child;
258     vlc_event_send( &p_parent->event_manager, &event );
259 }
260
261 /* This won't hold the item, but can tell to interested third parties
262  * Like the playlist, that there is a new sub item. With this design
263  * It is not the input item's responsability to keep all the ref of
264  * the input item children. */
265 void input_item_AddSubItem( input_item_t *p_parent, input_item_t *p_child )
266 {
267     vlc_mutex_lock( &p_parent->lock );
268     p_parent->i_type = ITEM_TYPE_PLAYLIST;
269     vlc_mutex_unlock( &p_parent->lock );
270
271     notify_subitem_added(p_parent, p_child);
272
273     input_item_node_t *p_node = input_item_node_Create( p_parent );
274     input_item_node_AppendItem( p_node, p_child );
275     input_item_AddSubItemTree( p_node );
276     input_item_node_Delete( p_node );
277 }
278
279 void input_item_AddSubItemTree ( input_item_node_t *p_root )
280 {
281     vlc_event_t event;
282     event.type = vlc_InputItemSubItemTreeAdded;
283     event.u.input_item_subitem_tree_added.p_root = p_root;
284     vlc_event_send( &p_root->p_item->event_manager, &event );
285 }
286
287 bool input_item_HasErrorWhenReading( input_item_t *p_item )
288 {
289     vlc_mutex_lock( &p_item->lock );
290
291     bool b_error = p_item->b_error_when_reading;
292
293     vlc_mutex_unlock( &p_item->lock );
294
295     return b_error;
296 }
297
298 bool input_item_MetaMatch( input_item_t *p_i,
299                            vlc_meta_type_t meta_type, const char *psz )
300 {
301     vlc_mutex_lock( &p_i->lock );
302
303     if( !p_i->p_meta )
304     {
305         vlc_mutex_unlock( &p_i->lock );
306         return false;
307     }
308     const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
309     bool b_ret = psz_meta && strcasestr( psz_meta, psz );
310
311     vlc_mutex_unlock( &p_i->lock );
312
313     return b_ret;
314 }
315
316 char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
317 {
318     vlc_mutex_lock( &p_i->lock );
319
320     if( !p_i->p_meta )
321     {
322         vlc_mutex_unlock( &p_i->lock );
323         return NULL;
324     }
325
326     char *psz = NULL;
327     if( vlc_meta_Get( p_i->p_meta, meta_type ) )
328         psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );
329
330     vlc_mutex_unlock( &p_i->lock );
331     return psz;
332 }
333
334 /* Get the title of a given item or fallback to the name if the title is empty */
335 char *input_item_GetTitleFbName( input_item_t *p_item )
336 {
337     char *psz_ret;
338     vlc_mutex_lock( &p_item->lock );
339
340     if( !p_item->p_meta )
341     {
342         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
343         vlc_mutex_unlock( &p_item->lock );
344         return psz_ret;
345     }
346
347     const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
348     if( !EMPTY_STR( psz_title ) )
349         psz_ret = strdup( psz_title );
350     else
351         psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
352
353     vlc_mutex_unlock( &p_item->lock );
354     return psz_ret;
355 }
356
357 char *input_item_GetName( input_item_t *p_item )
358 {
359     vlc_mutex_lock( &p_item->lock );
360
361     char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
362
363     vlc_mutex_unlock( &p_item->lock );
364     return psz_name;
365 }
366 void input_item_SetName( input_item_t *p_item, const char *psz_name )
367 {
368     vlc_mutex_lock( &p_item->lock );
369
370     free( p_item->psz_name );
371     p_item->psz_name = strdup( psz_name );
372
373     vlc_mutex_unlock( &p_item->lock );
374 }
375
376 char *input_item_GetURI( input_item_t *p_i )
377 {
378     vlc_mutex_lock( &p_i->lock );
379
380     char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
381
382     vlc_mutex_unlock( &p_i->lock );
383     return psz_s;
384 }
385 void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
386 {
387     vlc_mutex_lock( &p_i->lock );
388 #ifndef NDEBUG
389     if( !strstr( psz_uri, "://" ) || strstr( psz_uri, " " ) || strstr( psz_uri, "\"" ) )
390         fprintf( stderr, "input_item_SetURI() was likely called with a path. FIXME\n" );
391 #endif
392
393     free( p_i->psz_uri );
394     p_i->psz_uri = strdup( psz_uri );
395
396     p_i->i_type = GuessType( p_i );
397
398     if( p_i->psz_name )
399         ;
400     else
401     if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
402     {
403         const char *psz_filename = strrchr( p_i->psz_uri, '/' );
404
405         if( psz_filename && *psz_filename == '/' )
406             psz_filename++;
407         if( psz_filename && *psz_filename )
408             p_i->psz_name = strdup( psz_filename );
409
410         /* Make the name more readable */
411         if( p_i->psz_name )
412             decode_URI( p_i->psz_name );
413     }
414     else
415     {   /* Strip login and password from title */
416         int r;
417         vlc_url_t url;
418
419         vlc_UrlParse( &url, psz_uri, 0 );
420         if( url.psz_protocol )
421         {
422             if( url.i_port > 0 )
423                 r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
424                           url.psz_host, url.i_port,
425                           url.psz_path ? url.psz_path : "" );
426             else
427                 r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
428                           url.psz_host ? url.psz_host : "",
429                           url.psz_path ? url.psz_path : "" );
430         }
431         else
432         {
433             if( url.i_port > 0 )
434                 r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
435                           url.psz_path ? url.psz_path : "" );
436             else
437                 r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
438                           url.psz_path ? url.psz_path : "" );
439         }
440         vlc_UrlClean( &url );
441         if( -1==r )
442             p_i->psz_name=NULL; /* recover from undefined value */
443     }
444
445     vlc_mutex_unlock( &p_i->lock );
446 }
447
448 mtime_t input_item_GetDuration( input_item_t *p_i )
449 {
450     vlc_mutex_lock( &p_i->lock );
451
452     mtime_t i_duration = p_i->i_duration;
453
454     vlc_mutex_unlock( &p_i->lock );
455     return i_duration;
456 }
457
458 void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
459 {
460     bool b_send_event = false;
461
462     vlc_mutex_lock( &p_i->lock );
463     if( p_i->i_duration != i_duration )
464     {
465         p_i->i_duration = i_duration;
466         b_send_event = true;
467     }
468     vlc_mutex_unlock( &p_i->lock );
469
470     if( b_send_event )
471     {
472         vlc_event_t event;
473
474         event.type = vlc_InputItemDurationChanged;
475         event.u.input_item_duration_changed.new_duration = i_duration;
476         vlc_event_send( &p_i->event_manager, &event );
477     }
478 }
479
480
481 bool input_item_IsPreparsed( input_item_t *p_item )
482 {
483     vlc_mutex_lock( &p_item->lock );
484     bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
485     vlc_mutex_unlock( &p_item->lock );
486
487     return b_preparsed;
488 }
489
490 bool input_item_IsArtFetched( input_item_t *p_item )
491 {
492     vlc_mutex_lock( &p_item->lock );
493     bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
494     vlc_mutex_unlock( &p_item->lock );
495
496     return b_fetched;
497 }
498
499 static void input_item_Destroy ( gc_object_t *p_gc )
500 {
501     input_item_t *p_item = vlc_priv( p_gc, input_item_t );
502
503     input_item_Clean( p_item );
504     free( p_item );
505 }
506
507 int input_item_AddOption( input_item_t *p_input, const char *psz_option,
508                           unsigned flags )
509 {
510     int err = VLC_SUCCESS;
511
512     if( psz_option == NULL )
513         return VLC_EGENERIC;
514
515     vlc_mutex_lock( &p_input->lock );
516     if (flags & VLC_INPUT_OPTION_UNIQUE)
517     {
518         for (int i = 0 ; i < p_input->i_options; i++)
519             if( !strcmp( p_input->ppsz_options[i], psz_option ) )
520                 goto out;
521     }
522
523     uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
524     if (flagv == NULL)
525     {
526         err = VLC_ENOMEM;
527         goto out;
528     }
529     p_input->optflagv = flagv;
530     flagv[p_input->optflagc++] = flags;
531
532     INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
533                  p_input->i_options, strdup( psz_option ) );
534 out:
535     vlc_mutex_unlock( &p_input->lock );
536     return err;
537 }
538
539 /**
540  * Get a info item from a given category in a given input item.
541  *
542  * \param p_i The input item to get info from
543  * \param psz_cat String representing the category for the info
544  * \param psz_name String representing the name of the desired info
545  * \return A pointer to the string with the given info if found, or an
546  *         empty string otherwise. The caller should free the returned
547  *         pointer.
548  */
549 char *input_item_GetInfo( input_item_t *p_i,
550                           const char *psz_cat,
551                           const char *psz_name )
552 {
553     vlc_mutex_lock( &p_i->lock );
554
555     for( int i = 0; i< p_i->i_categories; i++ )
556     {
557         const info_category_t *p_cat = p_i->pp_categories[i];
558
559         if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
560             continue;
561
562         for( int j = 0; j < p_cat->i_infos; j++ )
563         {
564             if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
565             {
566                 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
567                 vlc_mutex_unlock( &p_i->lock );
568                 return psz_ret;
569             }
570         }
571     }
572     vlc_mutex_unlock( &p_i->lock );
573     return strdup( "" );
574 }
575
576 static int InputItemVaAddInfo( input_item_t *p_i,
577                                const char *psz_cat,
578                                const char *psz_name,
579                                const char *psz_format, va_list args )
580 {
581     int i;
582     info_t *p_info = NULL;
583     info_category_t *p_cat = NULL ;
584
585     vlc_assert_locked( &p_i->lock );
586
587     for( i = 0 ; i < p_i->i_categories ; i ++ )
588     {
589         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
590         {
591             p_cat = p_i->pp_categories[i];
592             break;
593         }
594     }
595     if( !p_cat )
596     {
597         if( !(p_cat = malloc( sizeof(*p_cat) )) )
598             return VLC_ENOMEM;
599
600         p_cat->psz_name = strdup( psz_cat );
601         p_cat->i_infos = 0;
602         p_cat->pp_infos = 0;
603         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
604                      p_cat );
605     }
606
607     for( i = 0; i< p_cat->i_infos; i++ )
608     {
609         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
610         {
611             p_info = p_cat->pp_infos[i];
612             break;
613         }
614     }
615
616     if( !p_info )
617     {
618         if( ( p_info = malloc( sizeof( *p_info ) ) ) == NULL )
619             return VLC_ENOMEM;
620
621         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
622         p_info->psz_name = strdup( psz_name );
623     }
624     else
625     {
626         free( p_info->psz_value );
627     }
628
629     if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
630         p_info->psz_value = NULL;
631
632     return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
633 }
634
635 static int InputItemAddInfo( input_item_t *p_i,
636                              const char *psz_cat,
637                              const char *psz_name,
638                              const char *psz_format, ... )
639 {
640     va_list args;
641
642     va_start( args, psz_format );
643     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
644     va_end( args );
645
646     return i_ret;
647 }
648
649 int input_item_AddInfo( input_item_t *p_i,
650                         const char *psz_cat,
651                         const char *psz_name,
652                         const char *psz_format, ... )
653 {
654     va_list args;
655
656     vlc_mutex_lock( &p_i->lock );
657
658     va_start( args, psz_format );
659     const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
660     va_end( args );
661
662     vlc_mutex_unlock( &p_i->lock );
663
664
665     if( !i_ret )
666     {
667         vlc_event_t event;
668
669         event.type = vlc_InputItemInfoChanged;
670         vlc_event_send( &p_i->event_manager, &event );
671     }
672     return i_ret;
673 }
674
675 int input_item_DelInfo( input_item_t *p_i,
676                         const char *psz_cat,
677                         const char *psz_name )
678 {
679     info_category_t *p_cat = NULL;
680     int i_cat;
681     int i;
682
683     vlc_mutex_lock( &p_i->lock );
684     for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
685     {
686         if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
687                      psz_cat ) )
688         {
689             p_cat = p_i->pp_categories[i_cat];
690             break;
691         }
692     }
693     if( p_cat == NULL )
694     {
695         vlc_mutex_unlock( &p_i->lock );
696         return VLC_EGENERIC;
697     }
698
699     if( psz_name )
700     {
701         /* Remove a specific info */
702         for( i = 0; i < p_cat->i_infos; i++ )
703         {
704             if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
705             {
706                 free( p_cat->pp_infos[i]->psz_name );
707                 free( p_cat->pp_infos[i]->psz_value );
708                 free( p_cat->pp_infos[i] );
709                 REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
710                 break;
711             }
712         }
713         if( i >= p_cat->i_infos )
714         {
715             vlc_mutex_unlock( &p_i->lock );
716             return VLC_EGENERIC;
717         }
718     }
719     else
720     {
721         /* Remove the complete categorie */
722         for( i = 0; i < p_cat->i_infos; i++ )
723         {
724             free( p_cat->pp_infos[i]->psz_name );
725             free( p_cat->pp_infos[i]->psz_value );
726             free( p_cat->pp_infos[i] );
727         }
728         free( p_cat->pp_infos );
729         REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
730     }
731
732     // Free the category
733     free( p_cat->psz_name );
734     free( p_cat );
735     vlc_mutex_unlock( &p_i->lock );
736
737
738     vlc_event_t event;
739     event.type = vlc_InputItemInfoChanged;
740     vlc_event_send( &p_i->event_manager, &event );
741
742     return VLC_SUCCESS;
743 }
744
745 #define EPG_DEBUG
746 void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update )
747 {
748     vlc_mutex_lock( &p_item->lock );
749
750     /* */
751     vlc_epg_t *p_epg = NULL;
752     for( int i = 0; i < p_item->i_epg; i++ )
753     {
754         vlc_epg_t *p_tmp = p_item->pp_epg[i];
755
756         if( (p_tmp->psz_name == NULL) != (p_update->psz_name == NULL) )
757             continue;
758         if( p_tmp->psz_name && p_update->psz_name && strcmp(p_tmp->psz_name, p_update->psz_name) )
759             continue;
760
761         p_epg = p_tmp;
762         break;
763     }
764
765     /* */
766     if( !p_epg )
767     {
768         p_epg = vlc_epg_New( p_update->psz_name );
769         if( p_epg )
770             TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
771     }
772     if( p_epg )
773         vlc_epg_Merge( p_epg, p_update );
774
775     vlc_mutex_unlock( &p_item->lock );
776
777 #ifdef EPG_DEBUG
778     char *psz_epg;
779     if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
780         goto signal;
781
782     input_item_DelInfo( p_item, psz_epg, NULL );
783
784     vlc_mutex_lock( &p_item->lock );
785     for( int i = 0; i < p_epg->i_event; i++ )
786     {
787         const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
788         time_t t_start = (time_t)p_evt->i_start;
789         struct tm tm_start;
790         char psz_start[128];
791
792         localtime_r( &t_start, &tm_start );
793
794         snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
795                   1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
796                   tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
797         if( p_evt->psz_short_description || p_evt->psz_description )
798             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
799                               p_evt->psz_name,
800                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
801                               p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
802                               p_evt->psz_description ? p_evt->psz_description : "" );
803         else
804             InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
805                               p_evt->psz_name,
806                               p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
807     }
808     vlc_mutex_unlock( &p_item->lock );
809     free( psz_epg );
810 signal:
811 #endif
812
813     if( p_epg->i_event > 0 )
814     {
815         vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
816         vlc_event_send( &p_item->event_manager, &event );
817     }
818 }
819
820 void input_item_SetEpgOffline( input_item_t *p_item )
821 {
822     vlc_mutex_lock( &p_item->lock );
823     for( int i = 0; i < p_item->i_epg; i++ )
824         vlc_epg_SetCurrent( p_item->pp_epg[i], -1 );
825     vlc_mutex_unlock( &p_item->lock );
826
827 #ifdef EPG_DEBUG
828     vlc_mutex_lock( &p_item->lock );
829     const int i_epg_info = p_item->i_epg;
830     char *ppsz_epg_info[i_epg_info];
831     for( int i = 0; i < p_item->i_epg; i++ )
832     {
833         const vlc_epg_t *p_epg = p_item->pp_epg[i];
834         if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
835             ppsz_epg_info[i] = NULL;
836     }
837     vlc_mutex_unlock( &p_item->lock );
838
839     for( int i = 0; i < i_epg_info; i++ )
840     {
841         if( !ppsz_epg_info[i] )
842             continue;
843         input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
844         free( ppsz_epg_info[i] );
845     }
846 #endif
847
848     vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
849     vlc_event_send( &p_item->event_manager, &event );
850 }
851
852
853 input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
854                                   const char *psz_name,
855                                   int i_options,
856                                   const char *const *ppsz_options,
857                                   unsigned i_option_flags,
858                                   mtime_t i_duration )
859 {
860     return input_item_NewWithType( p_obj, psz_uri, psz_name,
861                                   i_options, ppsz_options, i_option_flags,
862                                   i_duration, ITEM_TYPE_UNKNOWN );
863 }
864
865
866 input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
867                                 const char *psz_name,
868                                 int i_options,
869                                 const char *const *ppsz_options,
870                                 unsigned i_option_flags,
871                                 mtime_t i_duration,
872                                 int i_type )
873 {
874     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
875     static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
876
877     input_item_t* p_input = malloc( sizeof(input_item_t ) );
878     if( !p_input )
879         return NULL;
880
881     input_item_Init( p_obj, p_input );
882     vlc_gc_init( p_input, input_item_Destroy );
883
884     vlc_mutex_lock( &input_id_lock );
885     p_input->i_id = ++priv->i_last_input_id;
886     vlc_mutex_unlock( &input_id_lock );
887
888     p_input->b_fixed_name = false;
889
890     p_input->i_type = i_type;
891     p_input->b_prefers_tree = false;
892
893     if( psz_uri )
894         input_item_SetURI( p_input, psz_uri );
895
896     if( i_type != ITEM_TYPE_UNKNOWN )
897         p_input->i_type = i_type;
898
899     if( psz_name )
900         input_item_SetName( p_input, psz_name );
901
902     p_input->i_duration = i_duration;
903
904     for( int i = 0; i < i_options; i++ )
905         input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
906     return p_input;
907 }
908
909 struct item_type_entry
910 {
911     const char psz_scheme[7];
912     uint8_t    i_type;
913 };
914
915 static int typecmp( const void *key, const void *entry )
916 {
917     const struct item_type_entry *type = entry;
918     const char *uri = key, *scheme = type->psz_scheme;
919
920     return strncmp( uri, scheme, strlen( scheme ) );
921 }
922
923 /* Guess the type of the item using the beginning of the mrl */
924 static int GuessType( const input_item_t *p_item )
925 {
926     static const struct item_type_entry tab[] =
927     {   /* /!\ Alphabetical order /!\ */
928         /* Short match work, not just exact match */
929         { "alsa",   ITEM_TYPE_CARD },
930         { "atsc",   ITEM_TYPE_CARD },
931         { "bd",     ITEM_TYPE_DISC },
932         { "cable",  ITEM_TYPE_CARD },
933         { "cdda",   ITEM_TYPE_CDDA },
934         { "dc1394", ITEM_TYPE_CARD },
935         { "dccp",   ITEM_TYPE_NET },
936         { "dir",    ITEM_TYPE_DIRECTORY },
937         { "dshow",  ITEM_TYPE_CARD },
938         { "dv",     ITEM_TYPE_CARD },
939         { "dvb",    ITEM_TYPE_CARD },
940         { "dvd",    ITEM_TYPE_DISC },
941         { "ftp",    ITEM_TYPE_NET },
942         { "http",   ITEM_TYPE_NET },
943         { "icyx",   ITEM_TYPE_NET },
944         { "itpc",   ITEM_TYPE_NET },
945         { "jack",   ITEM_TYPE_CARD },
946         { "live",   ITEM_TYPE_NET }, /* livedotcom */
947         { "mms",    ITEM_TYPE_NET },
948         { "mtp",    ITEM_TYPE_DISC },
949         { "ofdm",   ITEM_TYPE_CARD },
950         { "oss",    ITEM_TYPE_CARD },
951         { "pnm",    ITEM_TYPE_NET },
952         { "pvr",    ITEM_TYPE_CARD },
953         { "qam",    ITEM_TYPE_CARD },
954         { "qpsk",   ITEM_TYPE_CARD },
955         { "qtcapt", ITEM_TYPE_CARD }, /* qtcapture */
956         { "raw139", ITEM_TYPE_CARD }, /* raw1394 */
957         { "rt",     ITEM_TYPE_NET }, /* rtp, rtsp, rtmp */
958         { "satell", ITEM_TYPE_CARD }, /* sattelite */
959         { "screen", ITEM_TYPE_CARD },
960         { "sdp",    ITEM_TYPE_NET },
961         { "smb",    ITEM_TYPE_NET },
962         { "svcd",   ITEM_TYPE_DISC },
963         { "tcp",    ITEM_TYPE_NET },
964         { "terres", ITEM_TYPE_CARD }, /* terrestrial */
965         { "udp",    ITEM_TYPE_NET },  /* udplite too */
966         { "unsv",   ITEM_TYPE_NET },
967         { "usdigi", ITEM_TYPE_CARD }, /* usdigital */
968         { "v4l",    ITEM_TYPE_CARD },
969         { "vcd",    ITEM_TYPE_DISC },
970         { "window", ITEM_TYPE_CARD },
971     };
972     const struct item_type_entry *e;
973
974     if( !strstr( p_item->psz_uri, "://" ) )
975         return ITEM_TYPE_FILE;
976
977     e = bsearch( p_item->psz_uri, tab, sizeof( tab ) / sizeof( tab[0] ),
978                  sizeof( tab[0] ), typecmp );
979     return e ? e->i_type : ITEM_TYPE_FILE;
980 }
981
982 input_item_node_t *input_item_node_Create( input_item_t *p_input )
983 {
984     input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
985     if( !p_node )
986         return NULL;
987
988     assert( p_input );
989
990     p_node->p_item = p_input;
991     vlc_gc_incref( p_input );
992
993     p_node->p_parent = NULL;
994     p_node->i_children = 0;
995     p_node->pp_children = NULL;
996
997     return p_node;
998 }
999
1000 static void RecursiveNodeDelete( input_item_node_t *p_node )
1001 {
1002   for( int i = 0; i < p_node->i_children; i++ )
1003       RecursiveNodeDelete( p_node->pp_children[i] );
1004
1005   vlc_gc_decref( p_node->p_item );
1006   free( p_node->pp_children );
1007   free( p_node );
1008 }
1009
1010 void input_item_node_Delete( input_item_node_t *p_node )
1011 {
1012   if(  p_node->p_parent )
1013   {
1014       for( int i = 0; i < p_node->p_parent->i_children; i++ )
1015           if( p_node->p_parent->pp_children[i] == p_node )
1016           {
1017               REMOVE_ELEM( p_node->p_parent->pp_children,
1018                            p_node->p_parent->i_children,
1019                            i );
1020               break;
1021           }
1022   }
1023
1024   RecursiveNodeDelete( p_node );
1025 }
1026
1027 input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
1028 {
1029     input_item_node_t *p_new_child = input_item_node_Create( p_item );
1030     if( !p_new_child ) return NULL;
1031     input_item_node_AppendNode( p_node, p_new_child );
1032     return p_new_child;
1033 }
1034
1035 void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
1036 {
1037     notify_subitem_added(p_parent->p_item, p_child->p_item);
1038
1039     assert( p_parent && p_child && p_child->p_parent == NULL );
1040     INSERT_ELEM( p_parent->pp_children,
1041                  p_parent->i_children,
1042                  p_parent->i_children,
1043                  p_child );
1044     p_child->p_parent = p_parent;
1045 }