1 /*******************************************************************************
2 * itml.c : iTunes Music Library import functions
3 *******************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Yoann Peronneau <yoann@videolan.org>
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.
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.
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 *******************************************************************************/
24 * \file modules/demux/playlist/itml.c
25 * \brief iTunes Music Library import functions
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
35 #include <vlc_strings.h>
46 static int Control( demux_t *, int, va_list );
47 static int Demux( demux_t * );
50 * \brief iTML submodule initialization function
52 int Import_iTML( vlc_object_t *p_this )
54 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xml", "itml",
55 "using iTunes Media Library reader" );
59 void Close_iTML( vlc_object_t *p_this )
61 demux_t *p_demux = (demux_t *)p_this;
62 free( p_demux->p_sys );
66 * \brief demuxer function for iTML parsing
68 int Demux( demux_t *p_demux )
71 xml_reader_t *p_xml_reader = NULL;
72 char *psz_name = NULL;
75 p_demux->p_sys->i_ntracks = 0;
77 /* create new xml parser from stream */
78 p_xml = xml_Create( p_demux );
82 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
86 /* locating the root node */
89 if( xml_ReaderRead( p_xml_reader ) != 1 )
91 msg_Err( p_demux, "can't read xml stream" );
94 } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
96 /* checking root node name */
97 psz_name = xml_ReaderName( p_xml_reader );
98 if( !psz_name || strcmp( psz_name, "plist" ) )
100 msg_Err( p_demux, "invalid root node name: %s", psz_name );
104 xml_elem_hnd_t pl_elements[] =
105 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_plist_dict} } };
106 parse_plist_node( p_demux, p_current_input, NULL, p_xml_reader, "plist",
109 HANDLE_PLAY_AND_RELEASE;
114 xml_ReaderDelete( p_xml, p_xml_reader );
118 /* Needed for correct operation of go back */
122 /** \brief dummy function for demux callback interface */
123 static int Control( demux_t *p_demux, int i_query, va_list args )
125 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
130 * \brief parse the root node of the playlist
132 static bool parse_plist_node( demux_t *p_demux, input_item_t *p_input_item,
133 track_elem_t *p_track, xml_reader_t *p_xml_reader,
134 const char *psz_element,
135 xml_elem_hnd_t *p_handlers )
137 VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
140 bool b_version_found = false;
142 /* read all playlist attributes */
143 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
145 psz_name = xml_ReaderName( p_xml_reader );
146 psz_value = xml_ReaderValue( p_xml_reader );
147 if( !psz_name || !psz_value )
149 msg_Err( p_demux, "invalid xml stream @ <plist>" );
154 /* attribute: version */
155 if( !strcmp( psz_name, "version" ) )
157 b_version_found = true;
158 if( strcmp( psz_value, "1.0" ) )
159 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
161 /* unknown attribute */
163 msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", psz_name);
169 /* attribute version is mandatory !!! */
170 if( !b_version_found )
171 msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
173 return parse_dict( p_demux, p_input_item, NULL, p_xml_reader,
174 "plist", p_handlers );
178 * \brief parse a <dict>
179 * \param COMPLEX_INTERFACE
181 static bool parse_dict( demux_t *p_demux, input_item_t *p_input_item,
182 track_elem_t *p_track, xml_reader_t *p_xml_reader,
183 const char *psz_element, xml_elem_hnd_t *p_handlers )
186 char *psz_name = NULL;
187 char *psz_value = NULL;
188 char *psz_key = NULL;
189 xml_elem_hnd_t *p_handler = NULL;
192 while( xml_ReaderRead( p_xml_reader ) == 1 )
194 i_node = xml_ReaderNodeType( p_xml_reader );
197 case XML_READER_NONE:
200 case XML_READER_STARTELEM:
201 /* element start tag */
202 psz_name = xml_ReaderName( p_xml_reader );
203 if( !psz_name || !*psz_name )
205 msg_Err( p_demux, "invalid xml stream" );
209 for( p_handler = p_handlers;
210 p_handler->name && strcmp( psz_name, p_handler->name );
212 if( !p_handler->name )
214 msg_Err( p_demux, "unexpected element <%s>", psz_name );
218 /* complex content is parsed in a separate function */
219 if( p_handler->type == COMPLEX_CONTENT )
221 if( p_handler->pf_handler.cmplx( p_demux, p_input_item, NULL,
222 p_xml_reader, p_handler->name,
233 case XML_READER_TEXT:
234 /* simple element content */
236 psz_value = xml_ReaderValue( p_xml_reader );
239 msg_Err( p_demux, "invalid xml stream" );
244 case XML_READER_ENDELEM:
245 /* element end tag */
246 psz_name = xml_ReaderName( p_xml_reader );
249 msg_Err( p_demux, "invalid xml stream" );
252 /* leave if the current parent node <track> is terminated */
253 if( !strcmp( psz_name, psz_element ) )
258 /* there MUST have been a start tag for that element name */
259 if( !p_handler || !p_handler->name
260 || strcmp( p_handler->name, psz_name ) )
262 msg_Err( p_demux, "there's no open element left for <%s>",
266 /* special case: key */
267 if( !strcmp( p_handler->name, "key" ) )
270 psz_key = strdup( psz_value );
272 /* call the simple handler */
273 else if( p_handler->pf_handler.smpl )
275 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
282 /* unknown/unexpected xml node */
283 msg_Err( p_demux, "unexpected xml node %i", i_node );
288 msg_Err( p_demux, "unexpected end of xml data" );
297 static bool parse_plist_dict( demux_t *p_demux, input_item_t *p_input_item,
298 track_elem_t *p_track, xml_reader_t *p_xml_reader,
299 const char *psz_element,
300 xml_elem_hnd_t *p_handlers )
302 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
303 xml_elem_hnd_t pl_elements[] =
304 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
305 {"array", SIMPLE_CONTENT, {NULL} },
306 {"key", SIMPLE_CONTENT, {NULL} },
307 {"integer", SIMPLE_CONTENT, {NULL} },
308 {"string", SIMPLE_CONTENT, {NULL} },
309 {"date", SIMPLE_CONTENT, {NULL} },
310 {"true", SIMPLE_CONTENT, {NULL} },
311 {"false", SIMPLE_CONTENT, {NULL} },
312 {NULL, UNKNOWN_CONTENT, {NULL} }
315 return parse_dict( p_demux, p_input_item, NULL, p_xml_reader,
316 "dict", pl_elements );
319 static bool parse_tracks_dict( demux_t *p_demux, input_item_t *p_input_item,
320 track_elem_t *p_track, xml_reader_t *p_xml_reader,
321 const char *psz_element,
322 xml_elem_hnd_t *p_handlers )
324 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
325 xml_elem_hnd_t tracks_elements[] =
326 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
327 {"key", SIMPLE_CONTENT, {NULL} },
328 {NULL, UNKNOWN_CONTENT, {NULL} }
331 parse_dict( p_demux, p_input_item, NULL, p_xml_reader,
332 "dict", tracks_elements );
334 msg_Info( p_demux, "added %i tracks successfully",
335 p_demux->p_sys->i_ntracks );
340 static bool parse_track_dict( demux_t *p_demux, input_item_t *p_input_item,
341 track_elem_t *p_track, xml_reader_t *p_xml_reader,
342 const char *psz_element,
343 xml_elem_hnd_t *p_handlers )
345 VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
346 input_item_t *p_new_input = NULL;
348 char *psz_uri = NULL;
349 p_track = new_track();
351 xml_elem_hnd_t track_elements[] =
352 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
353 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
354 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
355 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
356 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
357 {"true", SIMPLE_CONTENT, {NULL} },
358 {"false", SIMPLE_CONTENT, {NULL} },
359 {NULL, UNKNOWN_CONTENT, {NULL} }
362 i_ret = parse_dict( p_demux, p_input_item, p_track,
363 p_xml_reader, "dict", track_elements );
365 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
366 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
368 if( !p_track->location )
370 msg_Err( p_demux, "Track needs Location" );
371 free_track( p_track );
375 psz_uri = decode_URI_duplicate( p_track->location );
379 msg_Info( p_demux, "Adding '%s'", psz_uri );
381 p_new_input = input_item_New( p_demux, psz_uri, NULL );
382 input_item_AddSubItem( p_input_item, p_new_input );
385 add_meta( p_new_input, p_track );
386 vlc_gc_decref( p_new_input );
388 p_demux->p_sys->i_ntracks++;
392 free_track( p_track );
396 static track_elem_t *new_track()
398 track_elem_t *p_track;
399 p_track = malloc( sizeof( track_elem_t ) );
402 p_track->name = NULL;
403 p_track->artist = NULL;
404 p_track->album = NULL;
405 p_track->genre = NULL;
406 p_track->trackNum = NULL;
407 p_track->location = NULL;
408 p_track->duration = 0;
413 static void free_track( track_elem_t *p_track )
415 fprintf( stderr, "free track\n" );
419 FREENULL( p_track->name );
420 FREENULL( p_track->artist );
421 FREENULL( p_track->album );
422 FREENULL( p_track->genre );
423 FREENULL( p_track->trackNum );
424 FREENULL( p_track->location );
425 p_track->duration = 0;
429 static bool save_data( track_elem_t *p_track, const char *psz_name,
432 /* exit if setting is impossible */
433 if( !psz_name || !psz_value || !p_track )
436 /* re-convert xml special characters inside psz_value */
437 resolve_xml_special_chars( psz_value );
439 #define SAVE_INFO( name, value ) \
440 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
442 SAVE_INFO( "Name", name )
443 else SAVE_INFO( "Artist", artist )
444 else SAVE_INFO( "Album", album )
445 else SAVE_INFO( "Genre", genre )
446 else SAVE_INFO( "Track Number", trackNum )
447 else SAVE_INFO( "Location", location )
448 else if( !strcmp( psz_name, "Total Time" ) )
450 long i_num = atol( psz_value );
451 p_track->duration = (mtime_t) i_num*1000;
458 * \brief handles the supported <track> sub-elements
460 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
462 /* exit if setting is impossible */
463 if( !p_input_item || !p_track )
466 #define SET_INFO( type, prop ) \
467 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
468 SET_INFO( Title, name )
469 SET_INFO( Artist, artist )
470 SET_INFO( Album, album )
471 SET_INFO( Genre, genre )
472 SET_INFO( TrackNum, trackNum )
473 SET_INFO( Duration, duration )
479 * \brief skips complex element content that we can't manage
481 static bool skip_element( demux_t *p_demux, input_item_t *p_input_item,
482 track_elem_t *p_track, xml_reader_t *p_xml_reader,
483 const char *psz_element, xml_elem_hnd_t *p_handlers )
485 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
486 VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
489 while( xml_ReaderRead( p_xml_reader ) == 1 )
491 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
493 psz_endname = xml_ReaderName( p_xml_reader );
496 if( !strcmp( psz_element, psz_endname ) )