]> git.sesse.net Git - vlc/blob - modules/demux/playlist/itml.c
xspf: fix type limit warning
[vlc] / modules / demux / playlist / itml.c
1 /*******************************************************************************
2  * itml.c : iTunes Music Library import functions
3  *******************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
5  * $Id$
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_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 Demux( demux_t * );
47
48 /**
49  * \brief iTML submodule initialization function
50  */
51 int Import_iTML( vlc_object_t *p_this )
52 {
53     DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xml", "itml",
54                                       "using iTunes Media Library reader" );
55     return VLC_SUCCESS;
56 }
57
58 void Close_iTML( vlc_object_t *p_this )
59 {
60     demux_t *p_demux = (demux_t *)p_this;
61     free( p_demux->p_sys );
62 }
63
64 /**
65  * \brief demuxer function for iTML parsing
66  */
67 int Demux( demux_t *p_demux )
68 {
69     xml_reader_t *p_xml_reader;
70     const char *node;
71
72     input_item_t *p_current_input = GetCurrentItem(p_demux);
73     p_demux->p_sys->i_ntracks = 0;
74
75     /* create new xml parser from stream */
76     p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
77     if( !p_xml_reader )
78         goto end;
79
80     /* locating the root node */
81     int type;
82     do
83     {
84         type = xml_ReaderNextNode( p_xml_reader, &node );
85         if( type <= 0 )
86         {
87             msg_Err( p_demux, "can't read xml stream" );
88             goto end;
89         }
90     }
91     while( type != XML_READER_STARTELEM );
92
93     /* checking root node name */
94     if( strcmp( node, "plist" ) )
95     {
96         msg_Err( p_demux, "invalid root node <%s>", node );
97         goto end;
98     }
99
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",
104                       pl_elements );
105     input_item_node_PostAndDelete( p_subitems );
106
107     vlc_gc_decref(p_current_input);
108
109 end:
110     if( p_xml_reader )
111         xml_ReaderDelete( p_xml_reader );
112
113     /* Needed for correct operation of go back */
114     return 0;
115 }
116
117 /**
118  * \brief parse the root node of the playlist
119  */
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 )
124 {
125     VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
126     const char *attr, *value;
127     bool b_version_found = false;
128
129     /* read all playlist attributes */
130     while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
131     {
132         /* attribute: version */
133         if( !strcmp( attr, "version" ) )
134         {
135             b_version_found = true;
136             if( strcmp( value, "1.0" ) )
137                 msg_Warn( p_demux, "unsupported iTunes Media Library version" );
138         }
139         /* unknown attribute */
140         else
141             msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", attr );
142     }
143
144     /* attribute version is mandatory !!! */
145     if( !b_version_found )
146         msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
147
148     return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
149                        "plist", p_handlers );
150 }
151
152 /**
153  * \brief parse a <dict>
154  * \param COMPLEX_INTERFACE
155  */
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 )
159 {
160     int i_node;
161     const char *node;
162     char *psz_value = NULL;
163     char *psz_key = NULL;
164     xml_elem_hnd_t *p_handler = NULL;
165     bool b_ret = false;
166
167     while( (i_node = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
168     {
169         switch( i_node )
170         {
171         /*  element start tag  */
172         case XML_READER_STARTELEM:
173             if( !*node )
174             {
175                 msg_Err( p_demux, "invalid XML stream" );
176                 goto end;
177             }
178             /* choose handler */
179             for( p_handler = p_handlers;
180                      p_handler->name && strcmp( node, p_handler->name );
181                      p_handler++ );
182             if( !p_handler->name )
183             {
184                 msg_Err( p_demux, "unexpected element <%s>", node );
185                 goto end;
186             }
187             /* complex content is parsed in a separate function */
188             if( p_handler->type == COMPLEX_CONTENT )
189             {
190                 if( p_handler->pf_handler.cmplx( p_demux, p_input_node, NULL,
191                                                  p_xml_reader, p_handler->name,
192                                                  NULL ) )
193                 {
194                     p_handler = NULL;
195                     FREE_ATT_KEY();
196                 }
197                 else
198                     goto end;
199             }
200             break;
201
202         /* simple element content */
203         case XML_READER_TEXT:
204             free( psz_value );
205             psz_value = strdup( node );
206             if( unlikely(!psz_value) )
207                 goto end;
208             break;
209
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 ) )
214             {
215                 b_ret = true;
216                 goto end;
217             }
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 ) )
221             {
222                 msg_Err( p_demux, "there's no open element left for <%s>",
223                          node );
224                 goto end;
225             }
226             /* special case: key */
227             if( !strcmp( p_handler->name, "key" ) )
228             {
229                 free( psz_key );
230                 psz_key = strdup( psz_value );
231             }
232             /* call the simple handler */
233             else if( p_handler->pf_handler.smpl )
234             {
235                 p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
236             }
237             FREE_ATT();
238             p_handler = NULL;
239             break;
240         }
241     }
242     msg_Err( p_demux, "unexpected end of XML data" );
243
244 end:
245     free( psz_value );
246     free( psz_key );
247     return b_ret;
248 }
249
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 )
254 {
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} }
266         };
267
268     return parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
269                        "dict", pl_elements );
270 }
271
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 )
276 {
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} }
282         };
283
284     parse_dict( p_demux, p_input_node, NULL, p_xml_reader,
285                 "dict", tracks_elements );
286
287     msg_Info( p_demux, "added %i tracks successfully",
288               p_demux->p_sys->i_ntracks );
289
290     return true;
291 }
292
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 )
297 {
298     VLC_UNUSED(psz_element); VLC_UNUSED(p_handlers);
299     input_item_t *p_new_input = NULL;
300     int i_ret;
301     p_track = new_track();
302
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} }
312         };
313
314     i_ret = parse_dict( p_demux, p_input_node, p_track,
315                         p_xml_reader, "dict", track_elements );
316
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 );
319
320     if( !p_track->location )
321     {
322         msg_Err( p_demux, "Track needs Location" );
323         free_track( p_track );
324         return false;
325     }
326
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 );
330
331     /* add meta info */
332     add_meta( p_new_input, p_track );
333     vlc_gc_decref( p_new_input );
334
335     p_demux->p_sys->i_ntracks++;
336
337     free_track( p_track );
338     return i_ret;
339 }
340
341 static track_elem_t *new_track()
342 {
343     track_elem_t *p_track;
344     p_track = malloc( sizeof( track_elem_t ) );
345     if( p_track )
346     {
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;
354     }
355     return p_track;
356 }
357
358 static void free_track( track_elem_t *p_track )
359 {
360     fprintf( stderr, "free track\n" );
361     if ( !p_track )
362         return;
363
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;
371     free( p_track );
372 }
373
374 static bool save_data( track_elem_t *p_track, const char *psz_name,
375                        char *psz_value)
376 {
377     /* exit if setting is impossible */
378     if( !psz_name || !psz_value || !p_track )
379         return false;
380
381     /* re-convert xml special characters inside psz_value */
382     resolve_xml_special_chars( psz_value );
383
384 #define SAVE_INFO( name, value ) \
385     if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
386
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" ) )
394     {
395         long i_num = atol( psz_value );
396         p_track->duration = (mtime_t) i_num*1000;
397     }
398 #undef SAVE_INFO
399     return true;
400 }
401
402 /**
403  * \brief handles the supported <track> sub-elements
404  */
405 static bool add_meta( input_item_t *p_input_item, track_elem_t *p_track )
406 {
407     /* exit if setting is impossible */
408     if( !p_input_item || !p_track )
409         return false;
410
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 )
419 #undef SET_INFO
420     return true;
421 }
422
423 /**
424  * \brief skips complex element content that we can't manage
425  */
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 )
429 {
430     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
431     VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
432     const char *node;
433     int type;
434
435     while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
436         if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )
437             return true;
438     return false;
439 }