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
33 #include <vlc_demux.h>
37 #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 E_(Import_iTML)( vlc_object_t *p_this )
54 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xml", "itml",
55 "using iTunes Media Library reader" );
59 void E_(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 )
70 int i_ret = VLC_SUCCESS;
72 xml_reader_t *p_xml_reader = NULL;
73 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 );
83 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
88 /* locating the root node */
89 if( i_ret == VLC_SUCCESS )
93 if( xml_ReaderRead( p_xml_reader ) != 1 )
95 msg_Err( p_demux, "can't read xml stream" );
98 } while( i_ret == VLC_SUCCESS &&
99 xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
101 /* checking root node name */
102 if( i_ret == VLC_SUCCESS )
104 psz_name = xml_ReaderName( p_xml_reader );
105 if( !psz_name || strcmp( psz_name, "plist" ) )
107 msg_Err( p_demux, "invalid root node name: %s", psz_name );
108 i_ret = VLC_EGENERIC;
113 if( i_ret == VLC_SUCCESS )
115 xml_elem_hnd_t pl_elements[] =
116 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_plist_dict} } };
117 i_ret = parse_plist_node( p_demux, p_playlist, p_current_input,
118 NULL, p_xml_reader, "plist",
120 HANDLE_PLAY_AND_RELEASE;
124 xml_ReaderDelete( p_xml, p_xml_reader );
127 return 0; /* Needed for correct operation of go back */
130 /** \brief dummy function for demux callback interface */
131 static int Control( demux_t *p_demux, int i_query, va_list args )
137 * \brief parse the root node of the playlist
139 static vlc_bool_t parse_plist_node COMPLEX_INTERFACE
141 char *psz_name = NULL;
142 char *psz_value = NULL;
143 vlc_bool_t b_version_found = VLC_FALSE;
145 /* read all playlist attributes */
146 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
148 psz_name = xml_ReaderName( p_xml_reader );
149 psz_value = xml_ReaderValue( p_xml_reader );
150 if( !psz_name || !psz_value )
152 msg_Err( p_demux, "invalid xml stream @ <plist>" );
156 /* attribute: version */
157 if( !strcmp( psz_name, "version" ) )
159 b_version_found = VLC_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);
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_playlist, p_input_item, NULL, p_xml_reader,
174 "plist", p_handlers );
178 * \brief parse a <dict>
179 * \param COMPLEX_INTERFACE
181 static vlc_bool_t parse_dict COMPLEX_INTERFACE
184 char *psz_name = NULL;
185 char *psz_value = NULL;
186 char *psz_key = NULL;
187 xml_elem_hnd_t *p_handler = NULL;
189 while( xml_ReaderRead( p_xml_reader ) == 1 )
191 i_node = xml_ReaderNodeType( p_xml_reader );
194 case XML_READER_NONE:
197 case XML_READER_STARTELEM:
198 /* element start tag */
199 psz_name = xml_ReaderName( p_xml_reader );
200 if( !psz_name || !*psz_name )
202 msg_Err( p_demux, "invalid xml stream" );
207 for( p_handler = p_handlers;
208 p_handler->name && strcmp( psz_name, p_handler->name );
210 if( !p_handler->name )
212 msg_Err( p_demux, "unexpected element <%s>", psz_name );
217 /* complex content is parsed in a separate function */
218 if( p_handler->type == COMPLEX_CONTENT )
220 if( p_handler->pf_handler.cmplx( p_demux,
239 case XML_READER_TEXT:
240 /* simple element content */
242 psz_value = xml_ReaderValue( p_xml_reader );
245 msg_Err( p_demux, "invalid xml stream" );
251 case XML_READER_ENDELEM:
252 /* element end tag */
253 psz_name = xml_ReaderName( p_xml_reader );
256 msg_Err( p_demux, "invalid xml stream" );
260 /* leave if the current parent node <track> is terminated */
261 if( !strcmp( psz_name, psz_element ) )
266 /* there MUST have been a start tag for that element name */
267 if( !p_handler || !p_handler->name
268 || strcmp( p_handler->name, psz_name ))
270 msg_Err( p_demux, "there's no open element left for <%s>",
275 /* special case: key */
276 if( !strcmp( p_handler->name, "key" ) )
278 psz_key = strdup( psz_value );
280 /* call the simple handler */
281 else if( p_handler->pf_handler.smpl )
283 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
290 /* unknown/unexpected xml node */
291 msg_Err( p_demux, "unexpected xml node %i", i_node );
297 msg_Err( p_demux, "unexpected end of xml data" );
302 static vlc_bool_t parse_plist_dict COMPLEX_INTERFACE
304 xml_elem_hnd_t pl_elements[] =
305 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
306 {"array", SIMPLE_CONTENT, {NULL} },
307 {"key", SIMPLE_CONTENT, {NULL} },
308 {"integer", SIMPLE_CONTENT, {NULL} },
309 {"string", SIMPLE_CONTENT, {NULL} },
310 {"date", SIMPLE_CONTENT, {NULL} },
311 {"true", SIMPLE_CONTENT, {NULL} },
312 {"false", SIMPLE_CONTENT, {NULL} },
313 {NULL, UNKNOWN_CONTENT, {NULL} }
316 return parse_dict( p_demux, p_playlist, p_input_item, NULL, p_xml_reader,
317 "dict", pl_elements );
320 static vlc_bool_t parse_tracks_dict COMPLEX_INTERFACE
322 xml_elem_hnd_t tracks_elements[] =
323 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
324 {"key", SIMPLE_CONTENT, {NULL} },
325 {NULL, UNKNOWN_CONTENT, {NULL} }
328 parse_dict( p_demux, p_playlist, p_input_item, NULL, p_xml_reader,
329 "dict", tracks_elements );
331 msg_Info( p_demux, "added %i tracks successfully",
332 p_demux->p_sys->i_ntracks );
337 static vlc_bool_t parse_track_dict COMPLEX_INTERFACE
339 input_item_t *p_new_input = NULL;
341 char *psz_uri = NULL;
342 p_track = new_track();
344 xml_elem_hnd_t track_elements[] =
345 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
346 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
347 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
348 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
349 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
350 {"true", SIMPLE_CONTENT, {NULL} },
351 {"false", SIMPLE_CONTENT, {NULL} },
352 {NULL, UNKNOWN_CONTENT, {NULL} }
355 i_ret = parse_dict( p_demux, p_playlist, p_input_item, p_track,
356 p_xml_reader, "dict", track_elements );
358 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
359 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
361 if( !p_track->location )
363 msg_Err( p_demux, "Track needs Location" );
364 free_track( p_track );
368 psz_uri = decode_URI_duplicate( p_track->location );
372 if( strlen( psz_uri ) > 17 &&
373 !strncmp( psz_uri, "file://localhost/", 17 ) )
375 /* remove 'localhost/' */
376 strcpy( psz_uri + 7, psz_uri + 17 );
377 msg_Info( p_demux, "Adding '%s'", psz_uri );
379 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
381 input_ItemAddSubItem( p_input_item, p_new_input );
384 add_meta( p_new_input, p_track );
385 vlc_gc_decref( p_new_input );
387 p_demux->p_sys->i_ntracks++;
391 msg_Err( p_demux, "Don't know how to handle %s", psz_uri );
396 free_track( p_track );
400 static track_elem_t *new_track()
402 track_elem_t *p_track = NULL;
403 p_track = (track_elem_t *)malloc( sizeof( track_elem_t ) );
406 p_track->name = NULL;
407 p_track->artist = NULL;
408 p_track->album = NULL;
409 p_track->genre = NULL;
410 p_track->trackNum = NULL;
411 p_track->location = NULL;
412 p_track->duration = 0;
417 static void free_track( track_elem_t *p_track )
419 fprintf( stderr, "free track\n" );
423 FREE( p_track->name )
424 FREE( p_track->artist )
425 FREE( p_track->album )
426 FREE( p_track->genre )
427 FREE( p_track->trackNum )
428 FREE( p_track->location )
429 p_track->duration = 0;
433 static vlc_bool_t save_data SIMPLE_INTERFACE
435 /* exit if setting is impossible */
436 if( !psz_name || !psz_value || !p_track )
439 /* re-convert xml special characters inside psz_value */
440 resolve_xml_special_chars( psz_value );
442 #define SAVE_INFO( name, value ) \
443 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
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" ) )
453 long i_num = atol( psz_value );
454 p_track->duration = (mtime_t) i_num*1000;
460 * \brief handles the supported <track> sub-elements
462 static vlc_bool_t add_meta( input_item_t *p_input_item,
463 track_elem_t *p_track )
465 /* exit if setting is impossible */
466 if( !p_input_item || !p_track )
469 #define SET_INFO( func, prop ) \
470 if( p_track->prop ) { func( p_input_item, p_track->prop ); }
472 SET_INFO( input_item_SetTitle, name )
473 SET_INFO( input_item_SetArtist, artist )
474 SET_INFO( input_item_SetAlbum, album )
475 SET_INFO( input_item_SetGenre, genre )
476 SET_INFO( input_item_SetTrackNum, trackNum )
477 SET_INFO( input_item_SetDuration, duration )
482 * \brief skips complex element content that we can't manage
484 static vlc_bool_t skip_element COMPLEX_INTERFACE
488 while( xml_ReaderRead( p_xml_reader ) == 1 )
490 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
492 psz_endname = xml_ReaderName( p_xml_reader );
495 if( !strcmp( psz_element, psz_endname ) )