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_access.h>
35 #include <vlc_strings.h>
41 static int ReadDir( stream_t
*, input_item_node_t
* );
44 * \brief iTML submodule initialization function
46 int Import_iTML( vlc_object_t
*p_this
)
48 stream_t
*p_demux
= (stream_t
*)p_this
;
50 if( !stream_HasExtension( p_demux
, ".xml" )
51 && !p_demux
->obj
.force
)
54 const uint8_t *p_peek
;
55 const ssize_t i_peek
= vlc_stream_Peek( p_demux
->p_source
, &p_peek
, 128 );
57 !strnstr( (const char *) p_peek
, "<!DOCTYPE plist ", i_peek
) )
60 msg_Dbg( p_demux
, "using iTunes Media Library reader" );
62 p_demux
->pf_readdir
= ReadDir
;
63 p_demux
->pf_control
= access_vaDirectoryControlHelper
;
69 * \brief demuxer function for iTML parsing
71 static int ReadDir( stream_t
*p_demux
, input_item_node_t
*p_subitems
)
73 xml_reader_t
*p_xml_reader
;
76 p_demux
->p_sys
= (void *)(uintptr_t)0;
78 /* create new xml parser from stream */
79 p_xml_reader
= xml_ReaderCreate( p_demux
, p_demux
->p_source
);
83 /* locating the root node */
87 type
= xml_ReaderNextNode( p_xml_reader
, &node
);
90 msg_Err( p_demux
, "can't read xml stream" );
94 while( type
!= XML_READER_STARTELEM
);
96 /* checking root node name */
97 if( strcmp( node
, "plist" ) )
99 msg_Err( p_demux
, "invalid root node <%s>", node
);
103 xml_elem_hnd_t pl_elements
[] =
104 { {"dict", COMPLEX_CONTENT
, {.cmplx
= parse_plist_dict
} } };
105 parse_plist_node( p_demux
, p_subitems
, NULL
, p_xml_reader
, "plist",
110 xml_ReaderDelete( p_xml_reader
);
112 /* Needed for correct operation of go back */
117 * \brief parse the root node of the playlist
119 static bool parse_plist_node( stream_t
*p_demux
, input_item_node_t
*p_input_node
,
120 track_elem_t
*p_track
, xml_reader_t
*p_xml_reader
,
121 const char *psz_element
,
122 xml_elem_hnd_t
*p_handlers
)
124 VLC_UNUSED(p_track
); VLC_UNUSED(psz_element
);
125 const char *attr
, *value
;
126 bool b_version_found
= false;
128 /* read all playlist attributes */
129 while( (attr
= xml_ReaderNextAttr( p_xml_reader
, &value
)) != NULL
)
131 /* attribute: version */
132 if( !strcmp( attr
, "version" ) )
134 b_version_found
= true;
135 if( strcmp( value
, "1.0" ) )
136 msg_Warn( p_demux
, "unsupported iTunes Media Library version" );
138 /* unknown attribute */
140 msg_Warn( p_demux
, "invalid <plist> attribute:\"%s\"", attr
);
143 /* attribute version is mandatory !!! */
144 if( !b_version_found
)
145 msg_Warn( p_demux
, "<plist> requires \"version\" attribute" );
147 return parse_dict( p_demux
, p_input_node
, NULL
, p_xml_reader
,
148 "plist", p_handlers
);
152 * \brief parse a <dict>
153 * \param COMPLEX_INTERFACE
155 static bool parse_dict( stream_t
*p_demux
, input_item_node_t
*p_input_node
,
156 track_elem_t
*p_track
, xml_reader_t
*p_xml_reader
,
157 const char *psz_element
, xml_elem_hnd_t
*p_handlers
)
161 char *psz_value
= NULL
;
162 char *psz_key
= NULL
;
163 xml_elem_hnd_t
*p_handler
= NULL
;
166 while( (i_node
= xml_ReaderNextNode( p_xml_reader
, &node
)) > 0 )
170 /* element start tag */
171 case XML_READER_STARTELEM
:
173 for( p_handler
= p_handlers
;
174 p_handler
->name
&& strcmp( node
, p_handler
->name
);
176 if( !p_handler
->name
)
178 msg_Err( p_demux
, "unexpected element <%s>", node
);
181 /* complex content is parsed in a separate function */
182 if( p_handler
->type
== COMPLEX_CONTENT
)
184 if( p_handler
->pf_handler
.cmplx( p_demux
, p_input_node
, NULL
,
185 p_xml_reader
, p_handler
->name
,
190 FREENULL( psz_value
);
197 /* simple element content */
198 case XML_READER_TEXT
:
200 psz_value
= strdup( node
);
201 if( unlikely(!psz_value
) )
205 /* element end tag */
206 case XML_READER_ENDELEM
:
207 /* leave if the current parent node <track> is terminated */
208 if( !strcmp( node
, psz_element
) )
213 /* there MUST have been a start tag for that element name */
214 if( !p_handler
|| !p_handler
->name
215 || strcmp( p_handler
->name
, node
) )
217 msg_Err( p_demux
, "there's no open element left for <%s>",
221 /* special case: key */
222 if( !strcmp( p_handler
->name
, "key" ) )
225 psz_key
= strdup( psz_value
);
227 /* call the simple handler */
228 else if( p_handler
->pf_handler
.smpl
)
230 p_handler
->pf_handler
.smpl( p_track
, psz_key
, psz_value
);
237 msg_Err( p_demux
, "unexpected end of XML data" );
245 static bool parse_plist_dict( stream_t
*p_demux
, input_item_node_t
*p_input_node
,
246 track_elem_t
*p_track
, xml_reader_t
*p_xml_reader
,
247 const char *psz_element
,
248 xml_elem_hnd_t
*p_handlers
)
250 VLC_UNUSED(p_track
); VLC_UNUSED(psz_element
); VLC_UNUSED(p_handlers
);
251 xml_elem_hnd_t pl_elements
[] =
252 { {"dict", COMPLEX_CONTENT
, {.cmplx
= parse_tracks_dict
} },
253 {"array", SIMPLE_CONTENT
, {NULL
} },
254 {"key", SIMPLE_CONTENT
, {NULL
} },
255 {"integer", SIMPLE_CONTENT
, {NULL
} },
256 {"string", SIMPLE_CONTENT
, {NULL
} },
257 {"date", SIMPLE_CONTENT
, {NULL
} },
258 {"true", SIMPLE_CONTENT
, {NULL
} },
259 {"false", SIMPLE_CONTENT
, {NULL
} },
260 {NULL
, UNKNOWN_CONTENT
, {NULL
} }
263 return parse_dict( p_demux
, p_input_node
, NULL
, p_xml_reader
,
264 "dict", pl_elements
);
267 static bool parse_tracks_dict( stream_t
*p_demux
, input_item_node_t
*p_input_node
,
268 track_elem_t
*p_track
, xml_reader_t
*p_xml_reader
,
269 const char *psz_element
,
270 xml_elem_hnd_t
*p_handlers
)
272 VLC_UNUSED(p_track
); VLC_UNUSED(psz_element
); VLC_UNUSED(p_handlers
);
273 xml_elem_hnd_t tracks_elements
[] =
274 { {"dict", COMPLEX_CONTENT
, {.cmplx
= parse_track_dict
} },
275 {"key", SIMPLE_CONTENT
, {NULL
} },
276 {NULL
, UNKNOWN_CONTENT
, {NULL
} }
279 parse_dict( p_demux
, p_input_node
, NULL
, p_xml_reader
,
280 "dict", tracks_elements
);
282 msg_Info( p_demux
, "added %zi tracks successfully",
283 (size_t)p_demux
->p_sys
);
288 static bool parse_track_dict( stream_t
*p_demux
, input_item_node_t
*p_input_node
,
289 track_elem_t
*p_track
, xml_reader_t
*p_xml_reader
,
290 const char *psz_element
,
291 xml_elem_hnd_t
*p_handlers
)
293 VLC_UNUSED(psz_element
); VLC_UNUSED(p_handlers
);
294 input_item_t
*p_new_input
= NULL
;
296 p_track
= new_track();
298 xml_elem_hnd_t track_elements
[] =
299 { {"array", COMPLEX_CONTENT
, {.cmplx
= skip_element
} },
300 {"key", SIMPLE_CONTENT
, {.smpl
= save_data
} },
301 {"integer", SIMPLE_CONTENT
, {.smpl
= save_data
} },
302 {"string", SIMPLE_CONTENT
, {.smpl
= save_data
} },
303 {"date", SIMPLE_CONTENT
, {.smpl
= save_data
} },
304 {"true", SIMPLE_CONTENT
, {NULL
} },
305 {"false", SIMPLE_CONTENT
, {NULL
} },
306 {NULL
, UNKNOWN_CONTENT
, {NULL
} }
309 i_ret
= parse_dict( p_demux
, p_input_node
, p_track
,
310 p_xml_reader
, "dict", track_elements
);
312 msg_Dbg( p_demux
, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
313 p_track
->name
, p_track
->artist
, p_track
->album
, p_track
->genre
, p_track
->trackNum
, p_track
->location
);
315 if( !p_track
->location
)
317 msg_Warn( p_demux
, "ignoring track without Location entry" );
318 free_track( p_track
);
322 msg_Info( p_demux
, "Adding '%s'", p_track
->location
);
323 p_new_input
= input_item_New( p_track
->location
, NULL
);
324 input_item_node_AppendItem( p_input_node
, p_new_input
);
327 add_meta( p_new_input
, p_track
);
328 input_item_Release( p_new_input
);
330 p_demux
->p_sys
= (void *)((uintptr_t)p_demux
->p_sys
+ 1);
332 free_track( p_track
);
336 static track_elem_t
*new_track()
338 track_elem_t
*p_track
= malloc( sizeof *p_track
);
339 if( likely( p_track
) )
341 p_track
->name
= NULL
;
342 p_track
->artist
= NULL
;
343 p_track
->album
= NULL
;
344 p_track
->genre
= NULL
;
345 p_track
->trackNum
= NULL
;
346 p_track
->location
= NULL
;
347 p_track
->duration
= 0;
352 static void free_track( track_elem_t
*p_track
)
357 free( p_track
->name
);
358 free( p_track
->artist
);
359 free( p_track
->album
);
360 free( p_track
->genre
);
361 free( p_track
->trackNum
);
362 free( p_track
->location
);
366 static bool save_data( track_elem_t
*p_track
, const char *psz_name
,
369 /* exit if setting is impossible */
370 if( !psz_name
|| !psz_value
|| !p_track
)
373 /* re-convert xml special characters inside psz_value */
374 vlc_xml_decode( psz_value
);
376 #define SAVE_INFO( name, value ) \
377 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
379 SAVE_INFO( "Name", name
)
380 else SAVE_INFO( "Artist", artist
)
381 else SAVE_INFO( "Album", album
)
382 else SAVE_INFO( "Genre", genre
)
383 else SAVE_INFO( "Track Number", trackNum
)
384 else SAVE_INFO( "Location", location
)
385 else if( !strcmp( psz_name
, "Total Time" ) )
387 long i_num
= atol( psz_value
);
388 p_track
->duration
= (mtime_t
) i_num
*1000;
395 * \brief handles the supported <track> sub-elements
397 static bool add_meta( input_item_t
*p_input_item
, track_elem_t
*p_track
)
399 /* exit if setting is impossible */
400 if( !p_input_item
|| !p_track
)
403 #define SET_INFO( type, prop ) \
404 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
405 SET_INFO( Title
, name
)
406 SET_INFO( Artist
, artist
)
407 SET_INFO( Album
, album
)
408 SET_INFO( Genre
, genre
)
409 SET_INFO( TrackNum
, trackNum
)
410 SET_INFO( Duration
, duration
)
416 * \brief skips complex element content that we can't manage
418 static bool skip_element( stream_t
*p_demux
, input_item_node_t
*p_input_node
,
419 track_elem_t
*p_track
, xml_reader_t
*p_xml_reader
,
420 const char *psz_element
, xml_elem_hnd_t
*p_handlers
)
422 VLC_UNUSED(p_demux
); VLC_UNUSED(p_input_node
);
423 VLC_UNUSED(p_track
); VLC_UNUSED(p_handlers
);
427 while( (type
= xml_ReaderNextNode( p_xml_reader
, &node
)) > 0 )
428 if( type
== XML_READER_ENDELEM
&& !strcmp( psz_element
, node
) )