]> git.sesse.net Git - vlc/blob - modules/demux/playlist/itml.c
xml_ReaderDelete: remove useless parameter
[vlc] / modules / demux / playlist / itml.c
1 /*******************************************************************************
2  * itml.c : iTunes Music Library import functions
3  *******************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Yoann Peronneau <yoann@videolan.org>
8  *
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.
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 General Public License for more details.
18  *
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  *******************************************************************************/
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_demux.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 struct demux_sys_t
42 {
43     int i_ntracks;
44 };
45
46 static int Control( demux_t *, int, va_list );
47 static int Demux( demux_t * );
48
49 /**
50  * \brief iTML submodule initialization function
51  */
52 int Import_iTML( vlc_object_t *p_this )
53 {
54     DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xml", "itml",
55                                       "using iTunes Media Library reader" );
56     return VLC_SUCCESS;
57 }
58
59 void Close_iTML( vlc_object_t *p_this )
60 {
61     demux_t *p_demux = (demux_t *)p_this;
62     free( p_demux->p_sys );
63 }
64
65 /**
66  * \brief demuxer function for iTML parsing
67  */
68 int Demux( demux_t *p_demux )
69 {
70     xml_t *p_xml;
71     xml_reader_t *p_xml_reader = NULL;
72     char *psz_name = NULL;
73
74     input_item_t *p_current_input = GetCurrentItem(p_demux);
75     p_demux->p_sys->i_ntracks = 0;
76
77     /* create new xml parser from stream */
78     p_xml = xml_Create( p_demux );
79     if( !p_xml )
80         goto end;
81
82     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
83     if( !p_xml_reader )
84         goto end;
85
86     /* locating the root node */
87     do
88     {
89         if( xml_ReaderRead( p_xml_reader ) != 1 )
90         {
91             msg_Err( p_demux, "can't read xml stream" );
92             goto end;
93         }
94     } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
95
96     /* checking root node name */
97     psz_name = xml_ReaderName( p_xml_reader );
98     if( !psz_name || strcmp( psz_name, "plist" ) )
99     {
100         msg_Err( p_demux, "invalid root node name: %s", psz_name );
101         goto end;
102     }
103
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",
108                       pl_elements );
109     input_item_node_PostAndDelete( p_subitems );
110
111     vlc_gc_decref(p_current_input);
112
113 end:
114     free( psz_name );
115     if( p_xml_reader )
116         xml_ReaderDelete( p_xml_reader );
117     if( p_xml )
118         xml_Delete( p_xml );
119
120     /* Needed for correct operation of go back */
121     return 0;
122 }
123
124 /** \brief dummy function for demux callback interface */
125 static int Control( demux_t *p_demux, int i_query, va_list args )
126 {
127     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
128     return VLC_EGENERIC;
129 }
130
131 /**
132  * \brief parse the root node of the playlist
133  */
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 )
138 {
139     VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
140     char *psz_name;
141     char *psz_value;
142     bool b_version_found = false;
143
144     /* read all playlist attributes */
145     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
146     {
147         psz_name = xml_ReaderName( p_xml_reader );
148         psz_value = xml_ReaderValue( p_xml_reader );
149         if( !psz_name || !psz_value )
150         {
151             msg_Err( p_demux, "invalid xml stream @ <plist>" );
152             free( psz_name );
153             free( psz_value );
154             return false;
155         }
156         /* attribute: version */
157         if( !strcmp( psz_name, "version" ) )
158         {
159             b_version_found = true;
160             if( strcmp( psz_value, "1.0" ) )
161                 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
162         }
163         /* unknown attribute */
164         else
165             msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", psz_name);
166
167         free( psz_name );
168         free( psz_value );
169     }
170
171     /* attribute version is mandatory !!! */
172     if( !b_version_found )
173         msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
174
175     return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
176                        "plist", p_handlers );
177 }
178
179 /**
180  * \brief parse a <dict>
181  * \param COMPLEX_INTERFACE
182  */
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 )
186 {
187     int i_node;
188     char *psz_name = NULL;
189     char *psz_value = NULL;
190     char *psz_key = NULL;
191     xml_elem_hnd_t *p_handler = NULL;
192     bool b_ret = false;
193
194     while( xml_ReaderRead( p_xml_reader ) == 1 )
195     {
196         i_node = xml_ReaderNodeType( p_xml_reader );
197         switch( i_node )
198         {
199         case XML_READER_NONE:
200             break;
201
202         case XML_READER_STARTELEM:
203             /*  element start tag  */
204             psz_name = xml_ReaderName( p_xml_reader );
205             if( !psz_name || !*psz_name )
206             {
207                 msg_Err( p_demux, "invalid xml stream" );
208                 goto end;
209             }
210             /* choose handler */
211             for( p_handler = p_handlers;
212                      p_handler->name && strcmp( psz_name, p_handler->name );
213                      p_handler++ );
214             if( !p_handler->name )
215             {
216                 msg_Err( p_demux, "unexpected element <%s>", psz_name );
217                 goto end;
218             }
219             FREE_NAME();
220             /* complex content is parsed in a separate function */
221             if( p_handler->type == COMPLEX_CONTENT )
222             {
223                 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
224                                                  p_xml_reader, p_handler->name,
225                                                  NULL ) )
226                 {
227                     p_handler = NULL;
228                     FREE_ATT_KEY();
229                 }
230                 else
231                     goto end;
232             }
233             break;
234
235         case XML_READER_TEXT:
236             /* simple element content */
237             free( psz_value );
238             psz_value = xml_ReaderValue( p_xml_reader );
239             if( !psz_value )
240             {
241                 msg_Err( p_demux, "invalid xml stream" );
242                 goto end;
243             }
244             break;
245
246         case XML_READER_ENDELEM:
247             /* element end tag */
248             psz_name = xml_ReaderName( p_xml_reader );
249             if( !psz_name )
250             {
251                 msg_Err( p_demux, "invalid xml stream" );
252                 goto end;
253             }
254             /* leave if the current parent node <track> is terminated */
255             if( !strcmp( psz_name, psz_element ) )
256             {
257                 b_ret = true;
258                 goto end;
259             }
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 ) )
263             {
264                 msg_Err( p_demux, "there's no open element left for <%s>",
265                          psz_name );
266                 goto end;
267             }
268             /* special case: key */
269             if( !strcmp( p_handler->name, "key" ) )
270             {
271                 free( psz_key );
272                 psz_key = strdup( psz_value );
273             }
274             /* call the simple handler */
275             else if( p_handler->pf_handler.smpl )
276             {
277                 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
278             }
279             FREE_ATT();
280             p_handler = NULL;
281             break;
282
283         default:
284             /* unknown/unexpected xml node */
285             msg_Err( p_demux, "unexpected xml node %i", i_node );
286             goto end;
287         }
288         FREE_NAME();
289     }
290     msg_Err( p_demux, "unexpected end of xml data" );
291
292 end:
293     free( psz_name );
294     free( psz_value );
295     free( psz_key );
296     return b_ret;
297 }
298
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 )
303 {
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} }
315         };
316
317     return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
318                        "dict", pl_elements );
319 }
320
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 )
325 {
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} }
331         };
332
333     parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
334                 "dict", tracks_elements );
335
336     msg_Info( p_demux, "added %i tracks successfully",
337               p_demux->p_sys->i_ntracks );
338
339     return true;
340 }
341
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 )
346 {
347     VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
348     input_item_t *p_new_input = NULL;
349     int i_ret;
350     p_track = new_track();
351
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} }
361         };
362
363     i_ret = parse_dict( p_demux, p_input_node, p_track,
364                         p_xml_reader, "dict", track_elements );
365
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 );
368
369     if( !p_track->location )
370     {
371         msg_Err( p_demux, "Track needs Location" );
372         free_track( p_track );
373         return false;
374     }
375
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 );
379
380     /* add meta info */
381     add_meta( p_new_input, p_track );
382     vlc_gc_decref( p_new_input );
383
384     p_demux->p_sys->i_ntracks++;
385
386     free_track( p_track );
387     return i_ret;
388 }
389
390 static track_elem_t *new_track()
391 {
392     track_elem_t *p_track;
393     p_track = malloc( sizeof( track_elem_t ) );
394     if( p_track )
395     {
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;
403     }
404     return p_track;
405 }
406
407 static void free_track( track_elem_t *p_track )
408 {
409     fprintf( stderr, "free track\n" );
410     if ( !p_track )
411         return;
412
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;
420     free( p_track );
421 }
422
423 static bool save_data( track_elem_t *p_track, const char *psz_name,
424                        char *psz_value)
425 {
426     /* exit if setting is impossible */
427     if( !psz_name || !psz_value || !p_track )
428         return false;
429
430     /* re-convert xml special characters inside psz_value */
431     resolve_xml_special_chars( psz_value );
432
433 #define SAVE_INFO( name, value ) \
434     if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
435
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" ) )
443     {
444         long i_num = atol( psz_value );
445         p_track->duration = (mtime_t) i_num*1000;
446     }
447 #undef SAVE_INFO
448     return true;
449 }
450
451 /**
452  * \brief handles the supported <track> sub-elements
453  */
454 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
455 {
456     /* exit if setting is impossible */
457     if( !p_input_item || !p_track )
458         return false;
459
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 )
468 #undef SET_INFO
469     return true;
470 }
471
472 /**
473  * \brief skips complex element content that we can't manage
474  */
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 )
478 {
479     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
480     VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
481     char *psz_endname;
482
483     while( xml_ReaderRead( p_xml_reader ) == 1 )
484     {
485         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
486         {
487             psz_endname = xml_ReaderName( p_xml_reader );
488             if( !psz_endname )
489                 return false;
490             if( !strcmp( psz_element, psz_endname ) )
491             {
492                 free( psz_endname );
493                 return true;
494             }
495             else
496                 free( psz_endname );
497         }
498     }
499     return false;
500 }