]> git.sesse.net Git - vlc/blob - modules/demux/playlist/itml.c
input_item: Remove input_item_AddSubItem2 and send subitem_added event from input_ite...
[vlc] / modules / demux / playlist / itml.c
1 /*******************************************************************************
2  * itml.c : iTunes Music Library import functions
3  *******************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Yoann Peronneau <yoann@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  * \file modules/demux/playlist/itml.c
25  * \brief iTunes Music Library import functions
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
34 #include <vlc_xml.h>
35 #include <vlc_strings.h>
36 #include <vlc_url.h>
37
38 #include "itml.h"
39 #include "playlist.h"
40
41 struct demux_sys_t
42 {
43     int i_ntracks;
44 };
45
46 static int Control( demux_t *, int, va_list );
47 static int Demux( demux_t * );
48
49 /**
50  * \brief iTML submodule initialization function
51  */
52 int Import_iTML( vlc_object_t *p_this )
53 {
54     DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xml", "itml",
55                                       "using iTunes Media Library reader" );
56     return VLC_SUCCESS;
57 }
58
59 void Close_iTML( vlc_object_t *p_this )
60 {
61     demux_t *p_demux = (demux_t *)p_this;
62     free( p_demux->p_sys );
63 }
64
65 /**
66  * \brief demuxer function for iTML parsing
67  */
68 int Demux( demux_t *p_demux )
69 {
70     xml_t *p_xml;
71     xml_reader_t *p_xml_reader = NULL;
72     char *psz_name = NULL;
73
74     input_item_t *p_current_input = GetCurrentItem(p_demux);
75     p_demux->p_sys->i_ntracks = 0;
76
77     /* create new xml parser from stream */
78     p_xml = xml_Create( p_demux );
79     if( !p_xml )
80         goto end;
81
82     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
83     if( !p_xml_reader )
84         goto end;
85
86     /* locating the root node */
87     do
88     {
89         if( xml_ReaderRead( p_xml_reader ) != 1 )
90         {
91             msg_Err( p_demux, "can't read xml stream" );
92             goto end;
93         }
94     } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
95
96     /* checking root node name */
97     psz_name = xml_ReaderName( p_xml_reader );
98     if( !psz_name || strcmp( psz_name, "plist" ) )
99     {
100         msg_Err( p_demux, "invalid root node name: %s", psz_name );
101         goto end;
102     }
103
104     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
105     xml_elem_hnd_t pl_elements[] =
106         { {"dict",    COMPLEX_CONTENT, {.cmplx = parse_plist_dict} } };
107     parse_plist_node( p_demux, p_subitems, NULL, p_xml_reader, "plist",
108                       pl_elements );
109     input_item_AddSubItemTree( p_subitems );
110     input_item_node_Delete( p_subitems );
111
112     vlc_gc_decref(p_current_input);
113
114 end:
115     free( psz_name );
116     if( p_xml_reader )
117         xml_ReaderDelete( p_xml, p_xml_reader );
118     if( p_xml )
119         xml_Delete( p_xml );
120
121     /* Needed for correct operation of go back */
122     return 0;
123 }
124
125 /** \brief dummy function for demux callback interface */
126 static int Control( demux_t *p_demux, int i_query, va_list args )
127 {
128     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
129     return VLC_EGENERIC;
130 }
131
132 /**
133  * \brief parse the root node of the playlist
134  */
135 static bool parse_plist_node( demux_t *p_demux, input_item_node_t *p_input_node,
136                               track_elem_t *p_track, xml_reader_t *p_xml_reader,
137                               const char *psz_element,
138                               xml_elem_hnd_t *p_handlers )
139 {
140     VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
141     char *psz_name;
142     char *psz_value;
143     bool b_version_found = false;
144
145     /* read all playlist attributes */
146     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
147     {
148         psz_name = xml_ReaderName( p_xml_reader );
149         psz_value = xml_ReaderValue( p_xml_reader );
150         if( !psz_name || !psz_value )
151         {
152             msg_Err( p_demux, "invalid xml stream @ <plist>" );
153             free( psz_name );
154             free( psz_value );
155             return false;
156         }
157         /* attribute: version */
158         if( !strcmp( psz_name, "version" ) )
159         {
160             b_version_found = true;
161             if( strcmp( psz_value, "1.0" ) )
162                 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
163         }
164         /* unknown attribute */
165         else
166             msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", psz_name);
167
168         free( psz_name );
169         free( psz_value );
170     }
171
172     /* attribute version is mandatory !!! */
173     if( !b_version_found )
174         msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
175
176     return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
177                        "plist", p_handlers );
178 }
179
180 /**
181  * \brief parse a <dict>
182  * \param COMPLEX_INTERFACE
183  */
184 static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
185                         track_elem_t *p_track, xml_reader_t *p_xml_reader,
186                         const char *psz_element, xml_elem_hnd_t *p_handlers )
187 {
188     int i_node;
189     char *psz_name = NULL;
190     char *psz_value = NULL;
191     char *psz_key = NULL;
192     xml_elem_hnd_t *p_handler = NULL;
193     bool b_ret = false;
194
195     while( xml_ReaderRead( p_xml_reader ) == 1 )
196     {
197         i_node = xml_ReaderNodeType( p_xml_reader );
198         switch( i_node )
199         {
200         case XML_READER_NONE:
201             break;
202
203         case XML_READER_STARTELEM:
204             /*  element start tag  */
205             psz_name = xml_ReaderName( p_xml_reader );
206             if( !psz_name || !*psz_name )
207             {
208                 msg_Err( p_demux, "invalid xml stream" );
209                 goto end;
210             }
211             /* choose handler */
212             for( p_handler = p_handlers;
213                      p_handler->name && strcmp( psz_name, p_handler->name );
214                      p_handler++ );
215             if( !p_handler->name )
216             {
217                 msg_Err( p_demux, "unexpected element <%s>", psz_name );
218                 goto end;
219             }
220             FREE_NAME();
221             /* complex content is parsed in a separate function */
222             if( p_handler->type == COMPLEX_CONTENT )
223             {
224                 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
225                                                  p_xml_reader, p_handler->name,
226                                                  NULL ) )
227                 {
228                     p_handler = NULL;
229                     FREE_ATT_KEY();
230                 }
231                 else
232                     goto end;
233             }
234             break;
235
236         case XML_READER_TEXT:
237             /* simple element content */
238             free( psz_value );
239             psz_value = xml_ReaderValue( p_xml_reader );
240             if( !psz_value )
241             {
242                 msg_Err( p_demux, "invalid xml stream" );
243                 goto end;
244             }
245             break;
246
247         case XML_READER_ENDELEM:
248             /* element end tag */
249             psz_name = xml_ReaderName( p_xml_reader );
250             if( !psz_name )
251             {
252                 msg_Err( p_demux, "invalid xml stream" );
253                 goto end;
254             }
255             /* leave if the current parent node <track> is terminated */
256             if( !strcmp( psz_name, psz_element ) )
257             {
258                 b_ret = true;
259                 goto end;
260             }
261             /* there MUST have been a start tag for that element name */
262             if( !p_handler || !p_handler->name
263                 || strcmp( p_handler->name, psz_name ) )
264             {
265                 msg_Err( p_demux, "there's no open element left for <%s>",
266                          psz_name );
267                 goto end;
268             }
269             /* special case: key */
270             if( !strcmp( p_handler->name, "key" ) )
271             {
272                 free( psz_key );
273                 psz_key = strdup( psz_value );
274             }
275             /* call the simple handler */
276             else if( p_handler->pf_handler.smpl )
277             {
278                 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
279             }
280             FREE_ATT();
281             p_handler = NULL;
282             break;
283
284         default:
285             /* unknown/unexpected xml node */
286             msg_Err( p_demux, "unexpected xml node %i", i_node );
287             goto end;
288         }
289         FREE_NAME();
290     }
291     msg_Err( p_demux, "unexpected end of xml data" );
292
293 end:
294     free( psz_name );
295     free( psz_value );
296     free( psz_key );
297     return b_ret;
298 }
299
300 static bool parse_plist_dict( demux_t *p_demux, input_item_node_t *p_input_node,
301                               track_elem_t *p_track, xml_reader_t *p_xml_reader,
302                               const char *psz_element,
303                               xml_elem_hnd_t *p_handlers )
304 {
305     VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
306     xml_elem_hnd_t pl_elements[] =
307         { {"dict",    COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
308           {"array",   SIMPLE_CONTENT,  {NULL} },
309           {"key",     SIMPLE_CONTENT,  {NULL} },
310           {"integer", SIMPLE_CONTENT,  {NULL} },
311           {"string",  SIMPLE_CONTENT,  {NULL} },
312           {"date",    SIMPLE_CONTENT,  {NULL} },
313           {"true",    SIMPLE_CONTENT,  {NULL} },
314           {"false",   SIMPLE_CONTENT,  {NULL} },
315           {NULL,      UNKNOWN_CONTENT, {NULL} }
316         };
317
318     return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
319                        "dict", pl_elements );
320 }
321
322 static bool parse_tracks_dict( demux_t *p_demux, input_item_node_t *p_input_node,
323                                track_elem_t *p_track, xml_reader_t *p_xml_reader,
324                                const char *psz_element,
325                                xml_elem_hnd_t *p_handlers )
326 {
327     VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
328     xml_elem_hnd_t tracks_elements[] =
329         { {"dict",    COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
330           {"key",     SIMPLE_CONTENT,  {NULL} },
331           {NULL,      UNKNOWN_CONTENT, {NULL} }
332         };
333
334     parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
335                 "dict", tracks_elements );
336
337     msg_Info( p_demux, "added %i tracks successfully",
338               p_demux->p_sys->i_ntracks );
339
340     return true;
341 }
342
343 static bool parse_track_dict( demux_t *p_demux, input_item_node_t *p_input_node,
344                               track_elem_t *p_track, xml_reader_t *p_xml_reader,
345                               const char *psz_element,
346                               xml_elem_hnd_t *p_handlers )
347 {
348     VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
349     input_item_t *p_new_input = NULL;
350     int i_ret;
351     char *psz_uri = NULL;
352     p_track = new_track();
353
354     xml_elem_hnd_t track_elements[] =
355         { {"array",   COMPLEX_CONTENT, {.cmplx = skip_element} },
356           {"key",     SIMPLE_CONTENT,  {.smpl = save_data} },
357           {"integer", SIMPLE_CONTENT,  {.smpl = save_data} },
358           {"string",  SIMPLE_CONTENT,  {.smpl = save_data} },
359           {"date",    SIMPLE_CONTENT,  {.smpl = save_data} },
360           {"true",    SIMPLE_CONTENT,  {NULL} },
361           {"false",   SIMPLE_CONTENT,  {NULL} },
362           {NULL,      UNKNOWN_CONTENT, {NULL} }
363         };
364
365     i_ret = parse_dict( p_demux, p_input_node, p_track,
366                         p_xml_reader, "dict", track_elements );
367
368     msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
369              p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
370
371     if( !p_track->location )
372     {
373         msg_Err( p_demux, "Track needs Location" );
374         free_track( p_track );
375         return false;
376     }
377
378     psz_uri = decode_URI_duplicate( p_track->location );
379
380     if( psz_uri )
381     {
382         msg_Info( p_demux, "Adding '%s'", psz_uri );
383
384         p_new_input = input_item_New( p_demux, psz_uri, NULL );
385         input_item_node_AppendItem( p_input_node, p_new_input );
386
387         /* add meta info */
388         add_meta( p_new_input, p_track );
389         vlc_gc_decref( p_new_input );
390
391         p_demux->p_sys->i_ntracks++;
392         free( psz_uri );
393     }
394
395     free_track( p_track );
396     return i_ret;
397 }
398
399 static track_elem_t *new_track()
400 {
401     track_elem_t *p_track;
402     p_track = malloc( sizeof( track_elem_t ) );
403     if( p_track )
404     {
405         p_track->name = NULL;
406         p_track->artist = NULL;
407         p_track->album = NULL;
408         p_track->genre = NULL;
409         p_track->trackNum = NULL;
410         p_track->location = NULL;
411         p_track->duration = 0;
412     }
413     return p_track;
414 }
415
416 static void free_track( track_elem_t *p_track )
417 {
418     fprintf( stderr, "free track\n" );
419     if ( !p_track )
420         return;
421
422     FREENULL( p_track->name );
423     FREENULL( p_track->artist );
424     FREENULL( p_track->album );
425     FREENULL( p_track->genre );
426     FREENULL( p_track->trackNum );
427     FREENULL( p_track->location );
428     p_track->duration = 0;
429     free( p_track );
430 }
431
432 static bool save_data( track_elem_t *p_track, const char *psz_name,
433                        char *psz_value)
434 {
435     /* exit if setting is impossible */
436     if( !psz_name || !psz_value || !p_track )
437         return false;
438
439     /* re-convert xml special characters inside psz_value */
440     resolve_xml_special_chars( psz_value );
441
442 #define SAVE_INFO( name, value ) \
443     if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
444
445     SAVE_INFO( "Name", name )
446     else SAVE_INFO( "Artist", artist )
447     else SAVE_INFO( "Album", album )
448     else SAVE_INFO( "Genre", genre )
449     else SAVE_INFO( "Track Number", trackNum )
450     else SAVE_INFO( "Location", location )
451     else if( !strcmp( psz_name, "Total Time" ) )
452     {
453         long i_num = atol( psz_value );
454         p_track->duration = (mtime_t) i_num*1000;
455     }
456 #undef SAVE_INFO
457     return true;
458 }
459
460 /**
461  * \brief handles the supported <track> sub-elements
462  */
463 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
464 {
465     /* exit if setting is impossible */
466     if( !p_input_item || !p_track )
467         return false;
468
469 #define SET_INFO( type, prop ) \
470     if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
471     SET_INFO( Title, name )
472     SET_INFO( Artist, artist )
473     SET_INFO( Album, album )
474     SET_INFO( Genre, genre )
475     SET_INFO( TrackNum, trackNum )
476     SET_INFO( Duration, duration )
477 #undef SET_INFO
478     return true;
479 }
480
481 /**
482  * \brief skips complex element content that we can't manage
483  */
484 static bool skip_element( demux_t *p_demux, input_item_node_t *p_input_node,
485                           track_elem_t *p_track, xml_reader_t *p_xml_reader,
486                           const char *psz_element, xml_elem_hnd_t *p_handlers )
487 {
488     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
489     VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
490     char *psz_endname;
491
492     while( xml_ReaderRead( p_xml_reader ) == 1 )
493     {
494         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
495         {
496             psz_endname = xml_ReaderName( p_xml_reader );
497             if( !psz_endname )
498                 return false;
499             if( !strcmp( psz_element, psz_endname ) )
500             {
501                 free( psz_endname );
502                 return true;
503             }
504             else
505                 free( psz_endname );
506         }
507     }
508     return false;
509 }