Don't include config.h from the headers - refs #297.
[vlc.git] / 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/vlc.h>
33 #include <vlc_demux.h>
34
35 #include "playlist.h"
36 #include "vlc_xml.h"
37 #include "vlc_strings.h"
38 #include "vlc_url.h"
39 #include "itml.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 E_(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 E_(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     int i_ret = VLC_SUCCESS;
71     xml_t *p_xml = NULL;
72     xml_reader_t *p_xml_reader = NULL;
73     char *psz_name = NULL;
74     INIT_PLAYLIST_STUFF;
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         i_ret = VLC_ENOMOD;
81     else
82     {
83         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
84         if( !p_xml_reader )
85             i_ret = VLC_EGENERIC;
86     }
87
88     /* locating the root node */
89     if( i_ret == VLC_SUCCESS )
90     {
91         do
92         {
93             if( xml_ReaderRead( p_xml_reader ) != 1 )
94             {
95                 msg_Err( p_demux, "can't read xml stream" );
96                 i_ret = VLC_EGENERIC;
97             }
98         } while( i_ret == VLC_SUCCESS &&
99                  xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
100     }
101     /* checking root node name */
102     if( i_ret == VLC_SUCCESS )
103     {
104         psz_name = xml_ReaderName( p_xml_reader );
105         if( !psz_name || strcmp( psz_name, "plist" ) )
106         {
107             msg_Err( p_demux, "invalid root node name: %s", psz_name );
108             i_ret = VLC_EGENERIC;
109         }
110         FREE_NAME();
111     }
112
113     if( i_ret == VLC_SUCCESS )
114     {
115         xml_elem_hnd_t pl_elements[] =
116             { {"dict",    COMPLEX_CONTENT, {.cmplx = parse_plist_dict} } };
117         i_ret = parse_plist_node( p_demux, p_playlist, p_current_input,
118                                      NULL, p_xml_reader, "plist",
119                                      pl_elements );
120         HANDLE_PLAY_AND_RELEASE;
121     }
122
123     if( p_xml_reader )
124         xml_ReaderDelete( p_xml, p_xml_reader );
125     if( p_xml )
126         xml_Delete( p_xml );
127     return 0; /* Needed for correct operation of go back */
128 }
129
130 /** \brief dummy function for demux callback interface */
131 static int Control( demux_t *p_demux, int i_query, va_list args )
132 {
133     return VLC_EGENERIC;
134 }
135
136 /**
137  * \brief parse the root node of the playlist
138  */
139 static vlc_bool_t parse_plist_node COMPLEX_INTERFACE
140 {
141     char *psz_name = NULL;
142     char *psz_value = NULL;
143     vlc_bool_t b_version_found = VLC_FALSE;
144
145     /* read all playlist attributes */
146     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
147     {
148         psz_name = xml_ReaderName( p_xml_reader );
149         psz_value = xml_ReaderValue( p_xml_reader );
150         if( !psz_name || !psz_value )
151         {
152             msg_Err( p_demux, "invalid xml stream @ <plist>" );
153             FREE_ATT();
154             return VLC_FALSE;
155         }
156         /* attribute: version */
157         if( !strcmp( psz_name, "version" ) )
158         {
159             b_version_found = VLC_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_ATT();
168     }
169     /* attribute version is mandatory !!! */
170     if( !b_version_found )
171         msg_Warn( p_demux, "<plist> requires \"version\" attribute" );
172
173     return parse_dict( p_demux, p_playlist, p_input_item, NULL, p_xml_reader,
174                        "plist", p_handlers );
175 }
176
177 /**
178  * \brief parse a <dict>
179  * \param COMPLEX_INTERFACE
180  */
181 static vlc_bool_t parse_dict COMPLEX_INTERFACE
182 {
183     int i_node;
184     char *psz_name = NULL;
185     char *psz_value = NULL;
186     char *psz_key = NULL;
187     xml_elem_hnd_t *p_handler = NULL;
188
189     while( xml_ReaderRead( p_xml_reader ) == 1 )
190     {
191         i_node = xml_ReaderNodeType( p_xml_reader );
192         switch( i_node )
193         {
194             case XML_READER_NONE:
195                 break;
196
197             case XML_READER_STARTELEM:
198                 /*  element start tag  */
199                 psz_name = xml_ReaderName( p_xml_reader );
200                 if( !psz_name || !*psz_name )
201                 {
202                     msg_Err( p_demux, "invalid xml stream" );
203                     FREE_ATT_KEY();
204                     return VLC_FALSE;
205                 }
206                 /* choose handler */
207                 for( p_handler = p_handlers;
208                      p_handler->name && strcmp( psz_name, p_handler->name );
209                      p_handler++ );
210                 if( !p_handler->name )
211                 {
212                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
213                     FREE_ATT_KEY();
214                     return VLC_FALSE;
215                 }
216                 FREE_NAME();
217                 /* complex content is parsed in a separate function */
218                 if( p_handler->type == COMPLEX_CONTENT )
219                 {
220                     if( p_handler->pf_handler.cmplx( p_demux,
221                                                      p_playlist,
222                                                      p_input_item,
223                                                      NULL,
224                                                      p_xml_reader,
225                                                      p_handler->name,
226                                                      NULL ) )
227                     {
228                         p_handler = NULL;
229                         FREE_ATT_KEY();
230                     }
231                     else
232                     {
233                         FREE_ATT_KEY();
234                         return VLC_FALSE;
235                     }
236                 }
237                 break;
238
239             case XML_READER_TEXT:
240                 /* simple element content */
241                 FREE_ATT();
242                 psz_value = xml_ReaderValue( p_xml_reader );
243                 if( !psz_value )
244                 {
245                     msg_Err( p_demux, "invalid xml stream" );
246                     FREE_ATT_KEY();
247                     return VLC_FALSE;
248                 }
249                 break;
250
251             case XML_READER_ENDELEM:
252                 /* element end tag */
253                 psz_name = xml_ReaderName( p_xml_reader );
254                 if( !psz_name )
255                 {
256                     msg_Err( p_demux, "invalid xml stream" );
257                     FREE_ATT_KEY();
258                     return VLC_FALSE;
259                 }
260                 /* leave if the current parent node <track> is terminated */
261                 if( !strcmp( psz_name, psz_element ) )
262                 {
263                     FREE_ATT_KEY();
264                     return VLC_TRUE;
265                 }
266                 /* there MUST have been a start tag for that element name */
267                 if( !p_handler || !p_handler->name
268                     || strcmp( p_handler->name, psz_name ))
269                 {
270                     msg_Err( p_demux, "there's no open element left for <%s>",
271                              psz_name );
272                     FREE_ATT_KEY();
273                     return VLC_FALSE;
274                 }
275                 /* special case: key */
276                 if( !strcmp( p_handler->name, "key" ) )
277                 {
278                     psz_key = strdup( psz_value );
279                 }
280                 /* call the simple handler */
281                 else if( p_handler->pf_handler.smpl )
282                 {
283                     p_handler->pf_handler.smpl( p_track, psz_key, psz_value );
284                 }
285                 FREE_ATT();
286                 p_handler = NULL;
287                 break;
288
289             default:
290                 /* unknown/unexpected xml node */
291                 msg_Err( p_demux, "unexpected xml node %i", i_node );
292                 FREE_ATT_KEY();
293                 return VLC_FALSE;
294         }
295         FREE_NAME();
296     }
297     msg_Err( p_demux, "unexpected end of xml data" );
298     FREE_ATT_KEY();
299     return VLC_FALSE;
300 }
301
302 static vlc_bool_t parse_plist_dict COMPLEX_INTERFACE
303 {
304     xml_elem_hnd_t pl_elements[] =
305         { {"dict",    COMPLEX_CONTENT, {.cmplx = parse_tracks_dict} },
306           {"array",   SIMPLE_CONTENT,  {NULL} },
307           {"key",     SIMPLE_CONTENT,  {NULL} },
308           {"integer", SIMPLE_CONTENT,  {NULL} },
309           {"string",  SIMPLE_CONTENT,  {NULL} },
310           {"date",    SIMPLE_CONTENT,  {NULL} },
311           {"true",    SIMPLE_CONTENT,  {NULL} },
312           {"false",   SIMPLE_CONTENT,  {NULL} },
313           {NULL,      UNKNOWN_CONTENT, {NULL} }
314         };
315
316     return parse_dict( p_demux, p_playlist, p_input_item, NULL, p_xml_reader,
317                        "dict", pl_elements );
318 }
319
320 static vlc_bool_t parse_tracks_dict COMPLEX_INTERFACE
321 {
322     xml_elem_hnd_t tracks_elements[] =
323         { {"dict",    COMPLEX_CONTENT, {.cmplx = parse_track_dict} },
324           {"key",     SIMPLE_CONTENT,  {NULL} },
325           {NULL,      UNKNOWN_CONTENT, {NULL} }
326         };
327
328     parse_dict( p_demux, p_playlist, p_input_item, NULL, p_xml_reader,
329                 "dict", tracks_elements );
330
331     msg_Info( p_demux, "added %i tracks successfully",
332               p_demux->p_sys->i_ntracks );
333
334     return VLC_TRUE;
335 }
336
337 static vlc_bool_t parse_track_dict COMPLEX_INTERFACE
338 {
339     input_item_t *p_new_input = NULL;
340     int i_ret = -1;
341     char *psz_uri = NULL;
342     p_track = new_track();
343
344     xml_elem_hnd_t track_elements[] =
345         { {"array",   COMPLEX_CONTENT, {.cmplx = skip_element} },
346           {"key",     SIMPLE_CONTENT,  {.smpl = save_data} },
347           {"integer", SIMPLE_CONTENT,  {.smpl = save_data} },
348           {"string",  SIMPLE_CONTENT,  {.smpl = save_data} },
349           {"date",    SIMPLE_CONTENT,  {.smpl = save_data} },
350           {"true",    SIMPLE_CONTENT,  {NULL} },
351           {"false",   SIMPLE_CONTENT,  {NULL} },
352           {NULL,      UNKNOWN_CONTENT, {NULL} }
353         };
354
355     i_ret = parse_dict( p_demux, p_playlist, p_input_item, p_track,
356                         p_xml_reader, "dict", track_elements );
357
358     msg_Dbg( p_demux, "name: %s, artist: %s, album: %s, genre: %s, trackNum: %s, location: %s",
359              p_track->name, p_track->artist, p_track->album, p_track->genre, p_track->trackNum, p_track->location );
360
361     if( !p_track->location )
362     {
363         msg_Err( p_demux, "Track needs Location" );
364         free_track( p_track );
365         return VLC_FALSE;
366     }
367
368     psz_uri = decode_URI_duplicate( p_track->location );
369
370     if( psz_uri )
371     {
372         if( strlen( psz_uri ) > 17 &&
373             !strncmp( psz_uri, "file://localhost/", 17 ) )
374         {
375             /* remove 'localhost/' */
376             strcpy( psz_uri + 7, psz_uri + 17 );
377             msg_Info( p_demux, "Adding '%s'", psz_uri );
378
379             p_new_input = input_ItemNewExt( p_playlist, psz_uri,
380                                             NULL, 0, NULL, -1 );
381             input_ItemAddSubItem( p_input_item, p_new_input );
382
383             /* add meta info */
384             add_meta( p_new_input, p_track );
385             vlc_gc_decref( p_new_input );
386     
387             p_demux->p_sys->i_ntracks++;
388         }
389         else
390         {
391             msg_Err( p_demux, "Don't know how to handle %s", psz_uri );
392         }
393         free( psz_uri );
394     }
395
396     free_track( p_track );
397     return i_ret;
398 }
399
400 static track_elem_t *new_track()
401 {
402     track_elem_t *p_track = NULL;
403     p_track = (track_elem_t *)malloc( sizeof( track_elem_t ) );
404     if( p_track )
405     {
406         p_track->name = NULL;
407         p_track->artist = NULL;
408         p_track->album = NULL;
409         p_track->genre = NULL;
410         p_track->trackNum = NULL;
411         p_track->location = NULL;
412         p_track->duration = 0;
413     }
414     return p_track;
415 }
416
417 static void free_track( track_elem_t *p_track )
418 {
419     fprintf( stderr, "free track\n" );
420     if ( !p_track )
421         return;
422
423     FREE( p_track->name )
424     FREE( p_track->artist )
425     FREE( p_track->album )
426     FREE( p_track->genre )
427     FREE( p_track->trackNum )
428     FREE( p_track->location )
429     p_track->duration = 0;
430     free( p_track );
431 }
432
433 static vlc_bool_t save_data SIMPLE_INTERFACE
434 {
435     /* exit if setting is impossible */
436     if( !psz_name || !psz_value || !p_track )
437         return VLC_FALSE;
438
439     /* re-convert xml special characters inside psz_value */
440     resolve_xml_special_chars( psz_value );
441
442 #define SAVE_INFO( name, value ) \
443     if( !strcmp( psz_name, name ) ) { p_track->value = strdup( psz_value ); }
444
445     SAVE_INFO( "Name", name )
446     else SAVE_INFO( "Artist", artist )
447     else SAVE_INFO( "Album", album )
448     else SAVE_INFO( "Genre", genre )
449     else SAVE_INFO( "Track Number", trackNum )
450     else SAVE_INFO( "Location", location )
451     else if( !strcmp( psz_name, "Total Time" ) )
452     {
453         long i_num = atol( psz_value );
454         p_track->duration = (mtime_t) i_num*1000;
455     }
456     return VLC_TRUE;
457 }
458
459 /**
460  * \brief handles the supported <track> sub-elements
461  */
462 static vlc_bool_t add_meta( input_item_t *p_input_item,
463                             track_elem_t *p_track )
464 {
465     /* exit if setting is impossible */
466     if( !p_input_item || !p_track )
467         return VLC_FALSE;
468
469 #define SET_INFO( func, prop ) \
470     if( p_track->prop ) { func( p_input_item, p_track->prop ); }
471
472     SET_INFO( input_item_SetTitle, name )
473     SET_INFO( input_item_SetArtist, artist )
474     SET_INFO( input_item_SetAlbum, album )
475     SET_INFO( input_item_SetGenre, genre )
476     SET_INFO( input_item_SetTrackNum, trackNum )
477     SET_INFO( input_item_SetDuration, duration )
478     return VLC_TRUE;
479 }
480
481 /**
482  * \brief skips complex element content that we can't manage
483  */
484 static vlc_bool_t skip_element COMPLEX_INTERFACE
485 {
486     char *psz_endname;
487
488     while( xml_ReaderRead( p_xml_reader ) == 1 )
489     {
490         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
491         {
492             psz_endname = xml_ReaderName( p_xml_reader );
493             if( !psz_endname )
494                 return VLC_FALSE;
495             if( !strcmp( psz_element, psz_endname ) )
496             {
497                 free( psz_endname );
498                 return VLC_TRUE;
499             }
500             else
501                 free( psz_endname );
502         }
503     }
504     return VLC_FALSE;
505 }