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;
74 input_item_t *p_current_input = GetCurrentItem(p_demux);
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 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",
109 input_item_node_PostAndDelete( p_subitems );
111 vlc_gc_decref(p_current_input);
116 xml_ReaderDelete( p_xml, p_xml_reader );
120 /* Needed for correct operation of go back */
124 /** \brief dummy function for demux callback interface */
125 static int Control( demux_t *p_demux, int i_query, va_list args )
127 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
132 * \brief parse the root node of the playlist
134 static bool parse_plist_node( demux_t *p_demux, input_item_node_t *p_input_node,
135 track_elem_t *p_track, xml_reader_t *p_xml_reader,
136 const char *psz_element,
137 xml_elem_hnd_t *p_handlers )
139 VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
142 bool b_version_found = false;
144 /* read all playlist attributes */
145 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
147 psz_name = xml_ReaderName( p_xml_reader );
148 psz_value = xml_ReaderValue( p_xml_reader );
149 if( !psz_name || !psz_value )
151 msg_Err( p_demux, "invalid xml stream @ <plist>" );
156 /* attribute: version */
157 if( !strcmp( psz_name, "version" ) )
159 b_version_found = true;
160 if( strcmp( psz_value, "1.0" ) )
161 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
163 /* unknown attribute */
165 msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", psz_name);
171 /* attribute version is mandatory !!! */
172 if( !b_version_found )
173 msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
175 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
176 "plist", p_handlers );
180 * \brief parse a <dict>
181 * \param COMPLEX_INTERFACE
183 static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
184 track_elem_t *p_track, xml_reader_t *p_xml_reader,
185 const char *psz_element, xml_elem_hnd_t *p_handlers )
188 char *psz_name = NULL;
189 char *psz_value = NULL;
190 char *psz_key = NULL;
191 xml_elem_hnd_t *p_handler = NULL;
194 while( xml_ReaderRead( p_xml_reader ) == 1 )
196 i_node = xml_ReaderNodeType( p_xml_reader );
199 case XML_READER_NONE:
202 case XML_READER_STARTELEM:
203 /* element start tag */
204 psz_name = xml_ReaderName( p_xml_reader );
205 if( !psz_name || !*psz_name )
207 msg_Err( p_demux, "invalid xml stream" );
211 for( p_handler = p_handlers;
212 p_handler->name && strcmp( psz_name, p_handler->name );
214 if( !p_handler->name )
216 msg_Err( p_demux, "unexpected element <%s>", psz_name );
220 /* complex content is parsed in a separate function */
221 if( p_handler->type == COMPLEX_CONTENT )
223 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
224 p_xml_reader, p_handler->name,
235 case XML_READER_TEXT:
236 /* simple element content */
238 psz_value = xml_ReaderValue( p_xml_reader );
241 msg_Err( p_demux, "invalid xml stream" );
246 case XML_READER_ENDELEM:
247 /* element end tag */
248 psz_name = xml_ReaderName( p_xml_reader );
251 msg_Err( p_demux, "invalid xml stream" );
254 /* leave if the current parent node <track> is terminated */
255 if( !strcmp( psz_name, psz_element ) )
260 /* there MUST have been a start tag for that element name */
261 if( !p_handler || !p_handler->name
262 || strcmp( p_handler->name, psz_name ) )
264 msg_Err( p_demux, "there's no open element left for <%s>",
268 /* special case: key */
269 if( !strcmp( p_handler->name, "key" ) )
272 psz_key = strdup( psz_value );
274 /* call the simple handler */
275 else if( p_handler->pf_handler.smpl )
277 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
284 /* unknown/unexpected xml node */
285 msg_Err( p_demux, "unexpected xml node %i", i_node );
290 msg_Err( p_demux, "unexpected end of xml data" );
299 static bool parse_plist_dict( demux_t *p_demux, input_item_node_t *p_input_node,
300 track_elem_t *p_track, xml_reader_t *p_xml_reader,
301 const char *psz_element,
302 xml_elem_hnd_t *p_handlers )
304 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
305 xml_elem_hnd_t pl_elements[] =
306 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
307 {"array", SIMPLE_CONTENT, {NULL} },
308 {"key", SIMPLE_CONTENT, {NULL} },
309 {"integer", SIMPLE_CONTENT, {NULL} },
310 {"string", SIMPLE_CONTENT, {NULL} },
311 {"date", SIMPLE_CONTENT, {NULL} },
312 {"true", SIMPLE_CONTENT, {NULL} },
313 {"false", SIMPLE_CONTENT, {NULL} },
314 {NULL, UNKNOWN_CONTENT, {NULL} }
317 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
318 "dict", pl_elements );
321 static bool parse_tracks_dict( demux_t *p_demux, input_item_node_t *p_input_node,
322 track_elem_t *p_track, xml_reader_t *p_xml_reader,
323 const char *psz_element,
324 xml_elem_hnd_t *p_handlers )
326 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
327 xml_elem_hnd_t tracks_elements[] =
328 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
329 {"key", SIMPLE_CONTENT, {NULL} },
330 {NULL, UNKNOWN_CONTENT, {NULL} }
333 parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
334 "dict", tracks_elements );
336 msg_Info( p_demux, "added %i tracks successfully",
337 p_demux->p_sys->i_ntracks );
342 static bool parse_track_dict( demux_t *p_demux, input_item_node_t *p_input_node,
343 track_elem_t *p_track, xml_reader_t *p_xml_reader,
344 const char *psz_element,
345 xml_elem_hnd_t *p_handlers )
347 VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
348 input_item_t *p_new_input = NULL;
350 p_track = new_track();
352 xml_elem_hnd_t track_elements[] =
353 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
354 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
355 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
356 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
357 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
358 {"true", SIMPLE_CONTENT, {NULL} },
359 {"false", SIMPLE_CONTENT, {NULL} },
360 {NULL, UNKNOWN_CONTENT, {NULL} }
363 i_ret = parse_dict( p_demux, p_input_node, p_track,
364 p_xml_reader, "dict", track_elements );
366 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
367 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
369 if( !p_track->location )
371 msg_Err( p_demux, "Track needs Location" );
372 free_track( p_track );
376 msg_Info( p_demux, "Adding '%s'", p_track->location );
377 p_new_input = input_item_New( p_demux, p_track->location, NULL );
378 input_item_node_AppendItem( p_input_node, p_new_input );
381 add_meta( p_new_input, p_track );
382 vlc_gc_decref( p_new_input );
384 p_demux->p_sys->i_ntracks++;
386 free_track( p_track );
390 static track_elem_t *new_track()
392 track_elem_t *p_track;
393 p_track = malloc( sizeof( track_elem_t ) );
396 p_track->name = NULL;
397 p_track->artist = NULL;
398 p_track->album = NULL;
399 p_track->genre = NULL;
400 p_track->trackNum = NULL;
401 p_track->location = NULL;
402 p_track->duration = 0;
407 static void free_track( track_elem_t *p_track )
409 fprintf( stderr, "free track\n" );
413 FREENULL( p_track->name );
414 FREENULL( p_track->artist );
415 FREENULL( p_track->album );
416 FREENULL( p_track->genre );
417 FREENULL( p_track->trackNum );
418 FREENULL( p_track->location );
419 p_track->duration = 0;
423 static bool save_data( track_elem_t *p_track, const char *psz_name,
426 /* exit if setting is impossible */
427 if( !psz_name || !psz_value || !p_track )
430 /* re-convert xml special characters inside psz_value */
431 resolve_xml_special_chars( psz_value );
433 #define SAVE_INFO( name, value ) \
434 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
436 SAVE_INFO( "Name", name )
437 else SAVE_INFO( "Artist", artist )
438 else SAVE_INFO( "Album", album )
439 else SAVE_INFO( "Genre", genre )
440 else SAVE_INFO( "Track Number", trackNum )
441 else SAVE_INFO( "Location", location )
442 else if( !strcmp( psz_name, "Total Time" ) )
444 long i_num = atol( psz_value );
445 p_track->duration = (mtime_t) i_num*1000;
452 * \brief handles the supported <track> sub-elements
454 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
456 /* exit if setting is impossible */
457 if( !p_input_item || !p_track )
460 #define SET_INFO( type, prop ) \
461 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
462 SET_INFO( Title, name )
463 SET_INFO( Artist, artist )
464 SET_INFO( Album, album )
465 SET_INFO( Genre, genre )
466 SET_INFO( TrackNum, trackNum )
467 SET_INFO( Duration, duration )
473 * \brief skips complex element content that we can't manage
475 static bool skip_element( demux_t *p_demux, input_item_node_t *p_input_node,
476 track_elem_t *p_track, xml_reader_t *p_xml_reader,
477 const char *psz_element, xml_elem_hnd_t *p_handlers )
479 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
480 VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
483 while( xml_ReaderRead( p_xml_reader ) == 1 )
485 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
487 psz_endname = xml_ReaderName( p_xml_reader );
490 if( !strcmp( psz_element, psz_endname ) )