1 /*******************************************************************************
2 * itml.c : iTunes Music Library import functions
3 *******************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id: ebeb0eec113d990ca15dade30c62800dcda6827c $
6 *
7 * Authors: Yoann Peronneau <yoann@videolan.org>
8 *
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.
13 *
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.
18 *
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 *******************************************************************************/
23 /**
24 * \file modules/demux/playlist/itml.c
25 * \brief iTunes Music Library import functions
26 */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_access.h>
34 #include <vlc_xml.h>
35 #include <vlc_strings.h>
36 #include <vlc_url.h>
37
38 #include "itml.h"
39 #include "playlist.h"
40
41 static int ReadDir( stream_t *, input_item_node_t * );
42
43 /**
44 * \brief iTML submodule initialization function
45 */
Import_iTML(vlc_object_t * p_this)46 int Import_iTML( vlc_object_t *p_this )
47 {
48 stream_t *p_demux = (stream_t *)p_this;
49 CHECK_FILE(p_demux);
50 if( !stream_HasExtension( p_demux, ".xml" )
51 && !p_demux->obj.force )
52 return VLC_EGENERIC;
53
54 const uint8_t *p_peek;
55 const ssize_t i_peek = vlc_stream_Peek( p_demux->p_source, &p_peek, 128 );
56 if ( i_peek < 32 ||
57 !strnstr( (const char *) p_peek, "<!DOCTYPE plist ", i_peek ) )
58 return VLC_EGENERIC;
59
60 msg_Dbg( p_demux, "using iTunes Media Library reader" );
61
62 p_demux->pf_readdir = ReadDir;
63 p_demux->pf_control = access_vaDirectoryControlHelper;
64
65 return VLC_SUCCESS;
66 }
67
68 /**
69 * \brief demuxer function for iTML parsing
70 */
ReadDir(stream_t * p_demux,input_item_node_t * p_subitems)71 static int ReadDir( stream_t *p_demux, input_item_node_t *p_subitems )
72 {
73 xml_reader_t *p_xml_reader;
74 const char *node;
75
76 p_demux->p_sys = (void *)(uintptr_t)0;
77
78 /* create new xml parser from stream */
79 p_xml_reader = xml_ReaderCreate( p_demux, p_demux->p_source );
80 if( !p_xml_reader )
81 goto end;
82
83 /* locating the root node */
84 int type;
85 do
86 {
87 type = xml_ReaderNextNode( p_xml_reader, &node );
88 if( type <= 0 )
89 {
90 msg_Err( p_demux, "can't read xml stream" );
91 goto end;
92 }
93 }
94 while( type != XML_READER_STARTELEM );
95
96 /* checking root node name */
97 if( strcmp( node, "plist" ) )
98 {
99 msg_Err( p_demux, "invalid root node <%s>", node );
100 goto end;
101 }
102
103 xml_elem_hnd_t pl_elements[] =
104 {
105 {"dict", COMPLEX_CONTENT, {.cmplx = parse_plist_dict} },
106 {NULL, UNKNOWN_CONTENT, {NULL} }
107 };
108 parse_plist_node( p_demux, p_subitems, NULL, p_xml_reader, "plist",
109 pl_elements );
110
111 end:
112 if( p_xml_reader )
113 xml_ReaderDelete( p_xml_reader );
114
115 /* Needed for correct operation of go back */
116 return 0;
117 }
118
119 /**
120 * \brief parse the root node of the playlist
121 */
parse_plist_node(stream_t * p_demux,input_item_node_t * p_input_node,track_elem_t * p_track,xml_reader_t * p_xml_reader,const char * psz_element,xml_elem_hnd_t * p_handlers)122 static bool parse_plist_node( stream_t *p_demux, input_item_node_t *p_input_node,
123 track_elem_t *p_track, xml_reader_t *p_xml_reader,
124 const char *psz_element,
125 xml_elem_hnd_t *p_handlers )
126 {
127 VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
128 const char *attr, *value;
129 bool b_version_found = false;
130
131 /* read all playlist attributes */
132 while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
133 {
134 /* attribute: version */
135 if( !strcmp( attr, "version" ) )
136 {
137 b_version_found = true;
138 if( strcmp( value, "1.0" ) )
139 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
140 }
141 /* unknown attribute */
142 else
143 msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", attr );
144 }
145
146 /* attribute version is mandatory !!! */
147 if( !b_version_found )
148 msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
149
150 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
151 "plist", p_handlers );
152 }
153
154 /**
155 * \brief parse a <dict>
156 * \param COMPLEX_INTERFACE
157 */
parse_dict(stream_t * p_demux,input_item_node_t * p_input_node,track_elem_t * p_track,xml_reader_t * p_xml_reader,const char * psz_element,xml_elem_hnd_t * p_handlers)158 static bool parse_dict( stream_t *p_demux, input_item_node_t *p_input_node,
159 track_elem_t *p_track, xml_reader_t *p_xml_reader,
160 const char *psz_element, xml_elem_hnd_t *p_handlers )
161 {
162 int i_node;
163 const char *node;
164 char *psz_value = NULL;
165 char *psz_key = NULL;
166 xml_elem_hnd_t *p_handler = NULL;
167 bool b_ret = false;
168
169 while( (i_node = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
170 {
171 switch( i_node )
172 {
173 /* element start tag */
174 case XML_READER_STARTELEM:
175 /* choose handler */
176 for( p_handler = p_handlers;
177 p_handler->name && strcmp( node, p_handler->name );
178 p_handler++ );
179 if( !p_handler->name )
180 {
181 msg_Err( p_demux, "unexpected element <%s>", node );
182 goto end;
183 }
184 /* complex content is parsed in a separate function */
185 if( p_handler->type == COMPLEX_CONTENT )
186 {
187 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
188 p_xml_reader, p_handler->name,
189 NULL ) )
190 {
191 p_handler = NULL;
192 FREENULL( psz_key );
193 FREENULL( psz_value );
194 }
195 else
196 goto end;
197 }
198 break;
199
200 /* simple element content */
201 case XML_READER_TEXT:
202 free( psz_value );
203 psz_value = strdup( node );
204 if( unlikely(!psz_value) )
205 goto end;
206 break;
207
208 /* element end tag */
209 case XML_READER_ENDELEM:
210 /* leave if the current parent node <track> is terminated */
211 if( !strcmp( node, psz_element ) )
212 {
213 b_ret = true;
214 goto end;
215 }
216 /* there MUST have been a start tag for that element name */
217 if( !p_handler || !p_handler->name
218 || strcmp( p_handler->name, node ) )
219 {
220 msg_Err( p_demux, "there's no open element left for <%s>",
221 node );
222 goto end;
223 }
224 /* special case: key */
225 if( !strcmp( p_handler->name, "key" ) )
226 {
227 free( psz_key );
228 psz_key = strdup( psz_value );
229 }
230 /* call the simple handler */
231 else if( p_handler->pf_handler.smpl )
232 {
233 p_handler->pf_handler.smpl( p_track, psz_key, psz_value, p_demux->p_sys );
234 }
235 FREENULL(psz_value);
236 p_handler = NULL;
237 break;
238 }
239 }
240 msg_Err( p_demux, "unexpected end of XML data" );
241
242 end:
243 free( psz_value );
244 free( psz_key );
245 return b_ret;
246 }
247
parse_plist_dict(stream_t * p_demux,input_item_node_t * p_input_node,track_elem_t * p_track,xml_reader_t * p_xml_reader,const char * psz_element,xml_elem_hnd_t * p_handlers)248 static bool parse_plist_dict( stream_t *p_demux, input_item_node_t *p_input_node,
249 track_elem_t *p_track, xml_reader_t *p_xml_reader,
250 const char *psz_element,
251 xml_elem_hnd_t *p_handlers )
252 {
253 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
254 xml_elem_hnd_t pl_elements[] =
255 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
256 {"array", SIMPLE_CONTENT, {NULL} },
257 {"key", SIMPLE_CONTENT, {NULL} },
258 {"integer", SIMPLE_CONTENT, {NULL} },
259 {"string", SIMPLE_CONTENT, {NULL} },
260 {"date", SIMPLE_CONTENT, {NULL} },
261 {"true", SIMPLE_CONTENT, {NULL} },
262 {"false", SIMPLE_CONTENT, {NULL} },
263 {NULL, UNKNOWN_CONTENT, {NULL} }
264 };
265
266 return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
267 "dict", pl_elements );
268 }
269
parse_tracks_dict(stream_t * p_demux,input_item_node_t * p_input_node,track_elem_t * p_track,xml_reader_t * p_xml_reader,const char * psz_element,xml_elem_hnd_t * p_handlers)270 static bool parse_tracks_dict( stream_t *p_demux, input_item_node_t *p_input_node,
271 track_elem_t *p_track, xml_reader_t *p_xml_reader,
272 const char *psz_element,
273 xml_elem_hnd_t *p_handlers )
274 {
275 VLC_UNUSED(p_track); VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
276 xml_elem_hnd_t tracks_elements[] =
277 { {"dict", COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
278 {"key", SIMPLE_CONTENT, {NULL} },
279 {NULL, UNKNOWN_CONTENT, {NULL} }
280 };
281
282 parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
283 "dict", tracks_elements );
284
285 msg_Info( p_demux, "added %zi tracks successfully",
286 (size_t)p_demux->p_sys );
287
288 return true;
289 }
290
parse_track_dict(stream_t * p_demux,input_item_node_t * p_input_node,track_elem_t * p_track,xml_reader_t * p_xml_reader,const char * psz_element,xml_elem_hnd_t * p_handlers)291 static bool parse_track_dict( stream_t *p_demux, input_item_node_t *p_input_node,
292 track_elem_t *p_track, xml_reader_t *p_xml_reader,
293 const char *psz_element,
294 xml_elem_hnd_t *p_handlers )
295 {
296 VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
297 input_item_t *p_new_input = NULL;
298 int i_ret;
299 p_track = new_track();
300
301 xml_elem_hnd_t track_elements[] =
302 { {"array", COMPLEX_CONTENT, {.cmplx = skip_element} },
303 {"key", SIMPLE_CONTENT, {.smpl = save_data} },
304 {"integer", SIMPLE_CONTENT, {.smpl = save_data} },
305 {"string", SIMPLE_CONTENT, {.smpl = save_data} },
306 {"date", SIMPLE_CONTENT, {.smpl = save_data} },
307 {"true", SIMPLE_CONTENT, {NULL} },
308 {"false", SIMPLE_CONTENT, {NULL} },
309 {NULL, UNKNOWN_CONTENT, {NULL} }
310 };
311
312 i_ret = parse_dict( p_demux, p_input_node, p_track,
313 p_xml_reader, "dict", track_elements );
314
315 msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
316 p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
317
318 if( !p_track->location )
319 {
320 msg_Warn( p_demux, "ignoring track without Location entry" );
321 free_track( p_track );
322 return true;
323 }
324
325 msg_Info( p_demux, "Adding '%s'", p_track->location );
326 p_new_input = input_item_New( p_track->location, NULL );
327 input_item_node_AppendItem( p_input_node, p_new_input );
328
329 /* add meta info */
330 add_meta( p_new_input, p_track );
331 input_item_Release( p_new_input );
332
333 p_demux->p_sys = (void *)((uintptr_t)p_demux->p_sys + 1);
334
335 free_track( p_track );
336 return i_ret;
337 }
338
new_track()339 static track_elem_t *new_track()
340 {
341 track_elem_t *p_track = malloc( sizeof *p_track );
342 if( likely( p_track ) )
343 {
344 p_track->name = NULL;
345 p_track->artist = NULL;
346 p_track->album = NULL;
347 p_track->genre = NULL;
348 p_track->trackNum = NULL;
349 p_track->location = NULL;
350 p_track->duration = 0;
351 }
352 return p_track;
353 }
354
free_track(track_elem_t * p_track)355 static void free_track( track_elem_t *p_track )
356 {
357 if ( !p_track )
358 return;
359
360 free( p_track->name );
361 free( p_track->artist );
362 free( p_track->album );
363 free( p_track->genre );
364 free( p_track->trackNum );
365 free( p_track->location );
366 free( p_track );
367 }
368
save_data(track_elem_t * p_track,const char * psz_name,char * psz_value,void * opaque)369 static bool save_data( track_elem_t *p_track, const char *psz_name,
370 char *psz_value, void *opaque )
371 {
372 VLC_UNUSED(opaque);
373 /* exit if setting is impossible */
374 if( !psz_name || !psz_value || !p_track )
375 return false;
376
377 /* re-convert xml special characters inside psz_value */
378 vlc_xml_decode( psz_value );
379
380 #define SAVE_INFO( name, value ) \
381 if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
382
383 SAVE_INFO( "Name", name )
384 else SAVE_INFO( "Artist", artist )
385 else SAVE_INFO( "Album", album )
386 else SAVE_INFO( "Genre", genre )
387 else SAVE_INFO( "Track Number", trackNum )
388 else SAVE_INFO( "Location", location )
389 else if( !strcmp( psz_name, "Total Time" ) )
390 {
391 long i_num = atol( psz_value );
392 p_track->duration = (mtime_t) i_num*1000;
393 }
394 #undef SAVE_INFO
395 return true;
396 }
397
398 /**
399 * \brief handles the supported <track> sub-elements
400 */
add_meta(input_item_t * p_input_item,track_elem_t * p_track)401 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
402 {
403 /* exit if setting is impossible */
404 if( !p_input_item || !p_track )
405 return false;
406
407 #define SET_INFO( type, prop ) \
408 if( p_track->prop ) {input_item_Set##type( p_input_item, p_track->prop );}
409 SET_INFO( Title, name )
410 SET_INFO( Artist, artist )
411 SET_INFO( Album, album )
412 SET_INFO( Genre, genre )
413 SET_INFO( TrackNum, trackNum )
414 SET_INFO( Duration, duration )
415 #undef SET_INFO
416 return true;
417 }
418
419 /**
420 * \brief skips complex element content that we can't manage
421 */
skip_element(stream_t * p_demux,input_item_node_t * p_input_node,track_elem_t * p_track,xml_reader_t * p_xml_reader,const char * psz_element,xml_elem_hnd_t * p_handlers)422 static bool skip_element( stream_t *p_demux, input_item_node_t *p_input_node,
423 track_elem_t *p_track, xml_reader_t *p_xml_reader,
424 const char *psz_element, xml_elem_hnd_t *p_handlers )
425 {
426 VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
427 VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
428 const char *node;
429 int type;
430
431 while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
432 if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )
433 return true;
434 return false;
435 }
436