1 /*******************************************************************************
2 * itml.c : iTunes Music Library import functions
3 *******************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
7 * Authors: Yoann Peronneau <yoann@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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 Demux( demux_t * );
49 * \brief iTML submodule initialization function
51 int Import_iTML( vlc_object_t *p_this )
53 DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xml", "itml",
54 "using iTunes Media Library reader" );
58 void Close_iTML( vlc_object_t *p_this )
60 demux_t *p_demux = (demux_t *)p_this;
61 free( p_demux->p_sys );
65 * \brief demuxer function for iTML parsing
67 int Demux( demux_t *p_demux )
69 xml_reader_t *p_xml_reader;
72 input_item_t *p_current_input = GetCurrentItem(p_demux);
73 p_demux->p_sys->i_ntracks = 0;
75 /* create new xml parser from stream */
76 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
80 /* locating the root node */
84 type = xml_ReaderNextNode( p_xml_reader, &node );
87 msg_Err( p_demux, "can't read xml stream" );
91 while( type != XML_READER_STARTELEM );
93 /* checking root node name */
94 if( strcmp( node, "plist" ) )
96 msg_Err( p_demux, "invalid root node <%s>", node );
100 input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
101 xml_elem_hnd_t pl_elements[] =
102 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_plist_dict} } };
103 parse_plist_node( p_demux, p_subitems, NULL, p_xml_reader, "plist",
105 input_item_node_PostAndDelete( p_subitems );
107 vlc_gc_decref(p_current_input);
111 xml_ReaderDelete( p_xml_reader );
113 /* Needed for correct operation of go back */
118 * \brief parse the root node of the playlist
120 static bool parse_plist_node( demux_t *p_demux, input_item_node_t *p_input_node,
121 track_elem_t *p_track, xml_reader_t *p_xml_reader,
122 const char *psz_element,
123 xml_elem_hnd_t *p_handlers )
125 VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
126 const char *attr, *value;
127 bool b_version_found = false;
129 /* read all playlist attributes */
130 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
132 /* attribute: version */
133 if( !strcmp( attr, "version" ) )
135 b_version_found = true;
136 if( strcmp( value, "1.0" ) )
137 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
139 /* unknown attribute */
141 msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", attr );
144 /* attribute version is mandatory !!! */
145 if( !b_version_found )
146 msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
148 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
149 "plist", p_handlers );
153 * \brief parse a <dict>
154 * \param COMPLEX_INTERFACE
156 static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
157 track_elem_t *p_track, xml_reader_t *p_xml_reader,
158 const char *psz_element, xml_elem_hnd_t *p_handlers )
162 char *psz_value = NULL;
163 char *psz_key = NULL;
164 xml_elem_hnd_t *p_handler = NULL;
167 while( (i_node = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
171 /* element start tag */
172 case XML_READER_STARTELEM:
175 msg_Err( p_demux, "invalid XML stream" );
179 for( p_handler = p_handlers;
180 p_handler->name && strcmp( node, p_handler->name );
182 if( !p_handler->name )
184 msg_Err( p_demux, "unexpected element <%s>", node );
187 /* complex content is parsed in a separate function */
188 if( p_handler->type == COMPLEX_CONTENT )
190 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
191 p_xml_reader, p_handler->name,
202 /* simple element content */
203 case XML_READER_TEXT:
205 psz_value = strdup( node );
206 if( unlikely(!psz_value) )
210 /* element end tag */
211 case XML_READER_ENDELEM:
212 /* leave if the current parent node <track> is terminated */
213 if( !strcmp( node, psz_element ) )
218 /* there MUST have been a start tag for that element name */
219 if( !p_handler || !p_handler->name
220 || strcmp( p_handler->name, node ) )
222 msg_Err( p_demux, "there's no open element left for <%s>",
226 /* special case: key */
227 if( !strcmp( p_handler->name, "key" ) )
230 psz_key = strdup( psz_value );
232 /* call the simple handler */
233 else if( p_handler->pf_handler.smpl )
235 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
242 msg_Err( p_demux, "unexpected end of XML data" );
250 static bool parse_plist_dict( demux_t *p_demux, input_item_node_t *p_input_node,
251 track_elem_t *p_track, xml_reader_t *p_xml_reader,
252 const char *psz_element,
253 xml_elem_hnd_t *p_handlers )
255 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
256 xml_elem_hnd_t pl_elements[] =
257 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
258 {"array", SIMPLE_CONTENT, {NULL} },
259 {"key", SIMPLE_CONTENT, {NULL} },
260 {"integer", SIMPLE_CONTENT, {NULL} },
261 {"string", SIMPLE_CONTENT, {NULL} },
262 {"date", SIMPLE_CONTENT, {NULL} },
263 {"true", SIMPLE_CONTENT, {NULL} },
264 {"false", SIMPLE_CONTENT, {NULL} },
265 {NULL, UNKNOWN_CONTENT, {NULL} }
268 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
269 "dict", pl_elements );
272 static bool parse_tracks_dict( demux_t *p_demux, input_item_node_t *p_input_node,
273 track_elem_t *p_track, xml_reader_t *p_xml_reader,
274 const char *psz_element,
275 xml_elem_hnd_t *p_handlers )
277 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
278 xml_elem_hnd_t tracks_elements[] =
279 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
280 {"key", SIMPLE_CONTENT, {NULL} },
281 {NULL, UNKNOWN_CONTENT, {NULL} }
284 parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
285 "dict", tracks_elements );
287 msg_Info( p_demux, "added %i tracks successfully",
288 p_demux->p_sys->i_ntracks );
293 static bool parse_track_dict( demux_t *p_demux, input_item_node_t *p_input_node,
294 track_elem_t *p_track, xml_reader_t *p_xml_reader,
295 const char *psz_element,
296 xml_elem_hnd_t *p_handlers )
298 VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
299 input_item_t *p_new_input = NULL;
301 p_track = new_track();
303 xml_elem_hnd_t track_elements[] =
304 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
305 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
306 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
307 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
308 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
309 {"true", SIMPLE_CONTENT, {NULL} },
310 {"false", SIMPLE_CONTENT, {NULL} },
311 {NULL, UNKNOWN_CONTENT, {NULL} }
314 i_ret = parse_dict( p_demux, p_input_node, p_track,
315 p_xml_reader, "dict", track_elements );
317 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
318 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
320 if( !p_track->location )
322 msg_Err( p_demux, "Track needs Location" );
323 free_track( p_track );
327 msg_Info( p_demux, "Adding '%s'", p_track->location );
328 p_new_input = input_item_New( p_track->location, NULL );
329 input_item_node_AppendItem( p_input_node, p_new_input );
332 add_meta( p_new_input, p_track );
333 vlc_gc_decref( p_new_input );
335 p_demux->p_sys->i_ntracks++;
337 free_track( p_track );
341 static track_elem_t *new_track()
343 track_elem_t *p_track;
344 p_track = malloc( sizeof( track_elem_t ) );
347 p_track->name = NULL;
348 p_track->artist = NULL;
349 p_track->album = NULL;
350 p_track->genre = NULL;
351 p_track->trackNum = NULL;
352 p_track->location = NULL;
353 p_track->duration = 0;
358 static void free_track( track_elem_t *p_track )
360 fprintf( stderr, "free track\n" );
364 FREENULL( p_track->name );
365 FREENULL( p_track->artist );
366 FREENULL( p_track->album );
367 FREENULL( p_track->genre );
368 FREENULL( p_track->trackNum );
369 FREENULL( p_track->location );
370 p_track->duration = 0;
374 static bool save_data( track_elem_t *p_track, const char *psz_name,
377 /* exit if setting is impossible */
378 if( !psz_name || !psz_value || !p_track )
381 /* re-convert xml special characters inside psz_value */
382 resolve_xml_special_chars( psz_value );
384 #define SAVE_INFO( name, value ) \
385 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
387 SAVE_INFO( "Name", name )
388 else SAVE_INFO( "Artist", artist )
389 else SAVE_INFO( "Album", album )
390 else SAVE_INFO( "Genre", genre )
391 else SAVE_INFO( "Track Number", trackNum )
392 else SAVE_INFO( "Location", location )
393 else if( !strcmp( psz_name, "Total Time" ) )
395 long i_num = atol( psz_value );
396 p_track->duration = (mtime_t) i_num*1000;
403 * \brief handles the supported <track> sub-elements
405 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
407 /* exit if setting is impossible */
408 if( !p_input_item || !p_track )
411 #define SET_INFO( type, prop ) \
412 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
413 SET_INFO( Title, name )
414 SET_INFO( Artist, artist )
415 SET_INFO( Album, album )
416 SET_INFO( Genre, genre )
417 SET_INFO( TrackNum, trackNum )
418 SET_INFO( Duration, duration )
424 * \brief skips complex element content that we can't manage
426 static bool skip_element( demux_t *p_demux, input_item_node_t *p_input_node,
427 track_elem_t *p_track, xml_reader_t *p_xml_reader,
428 const char *psz_element, xml_elem_hnd_t *p_handlers )
430 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
431 VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
435 while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
436 if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )