Fix XSPF (Closes:#651)
[vlc.git] / modules / demux / playlist / xspf.c
1 /*******************************************************************************
2  * Copyright (C) 2006 Daniel Stränger <vlc at schmaller dot de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17  *******************************************************************************/
18 /**
19  * \file modules/demux/playlist/xspf.c
20  * \brief XSPF playlist import functions
21  */
22
23 #include <vlc/vlc.h>
24 #include <vlc/input.h>
25 #include <vlc/intf.h>
26
27 #include "playlist.h"
28 #include "vlc_xml.h"
29 #include "vlc_strings.h"
30 #include "xspf.h"
31
32 struct demux_sys_t 
33 {
34     playlist_item_t *p_item_in_category;
35     int i_parent_id;
36 };
37
38 /**
39  * \brief XSPF submodule initialization function
40  */
41 int E_(xspf_import_Activate)( vlc_object_t *p_this )
42 {
43     demux_t *p_demux = (demux_t *)p_this;
44     char    *psz_ext;
45
46     psz_ext = strrchr ( p_demux->psz_path, '.' );
47
48     if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
49         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
50     {
51         ;
52     }
53     else
54     {
55         return VLC_EGENERIC;
56     }
57     msg_Dbg( p_demux, "using xspf playlist import");
58
59     p_demux->p_sys = (demux_sys_t*)malloc(sizeof(demux_sys_t ) );
60
61     p_demux->pf_control = xspf_import_Control;
62     p_demux->pf_demux = xspf_import_Demux;
63
64     return VLC_SUCCESS;
65 }
66
67 /**
68  * \brief demuxer function for XSPF parsing
69  */
70 int xspf_import_Demux( demux_t *p_demux )
71 {
72     int i_ret = VLC_SUCCESS;
73     xml_t *p_xml = NULL;
74     xml_reader_t *p_xml_reader = NULL;
75     char *psz_name = NULL;
76     INIT_PLAYLIST_STUFF;
77     p_demux->p_sys->p_item_in_category = p_item_in_category;
78     p_demux->p_sys->i_parent_id = i_parent_id;
79
80     /* create new xml parser from stream */
81     p_xml = xml_Create( p_demux );
82     if( !p_xml )
83         i_ret = VLC_ENOMOD;
84     else
85     {
86         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
87         if( !p_xml_reader )
88             i_ret = VLC_EGENERIC;
89     }
90
91     /* start with parsing the root node */
92     if ( i_ret == VLC_SUCCESS )
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     /* checking root node type */
99     if ( i_ret == VLC_SUCCESS )
100         if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM )
101         {
102             msg_Err( p_demux, "invalid root node type: %i",
103                               xml_ReaderNodeType( p_xml_reader ) );
104             i_ret = VLC_EGENERIC;
105         }
106     /* checking root node name */
107     if ( i_ret == VLC_SUCCESS )
108         psz_name = xml_ReaderName( p_xml_reader );
109     if ( !psz_name || strcmp( psz_name, "playlist" ) )
110     {
111         msg_Err( p_demux, "invalid root node name: %s", psz_name );
112         i_ret = VLC_EGENERIC;
113     }
114     FREE_NAME();
115
116     i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL, 
117                                  p_xml_reader, "playlist" );
118     HANDLE_PLAY_AND_RELEASE;
119     if ( p_xml_reader )
120         xml_ReaderDelete( p_xml, p_xml_reader );
121     if ( p_xml )
122         xml_Delete( p_xml );
123
124     return i_ret;
125     return 0;
126 }
127
128 /** \brief dummy function for demux callback interface */
129 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
130 {
131     return VLC_EGENERIC;
132 }
133
134 /**
135  * \brief parse the root node of a XSPF playlist
136  * \param p_demux demuxer instance
137  * \param p_playlist playlist instance
138  * \param p_item current playlist node
139  * \param p_xml_reader xml reader instance
140  * \param psz_element name of element to parse
141  */
142 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
143 {
144     char *psz_name=NULL;
145     char *psz_value=NULL;
146     vlc_bool_t b_version_found = VLC_FALSE;
147     int i_node;
148     xml_elem_hnd_t *p_handler=NULL;
149
150     xml_elem_hnd_t pl_elements[] =
151         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
152           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
153           {"annotation",   SIMPLE_CONTENT,  {NULL} },
154           {"info",         SIMPLE_CONTENT,  {NULL} },
155           {"location",     SIMPLE_CONTENT,  {NULL} },
156           {"identifier",   SIMPLE_CONTENT,  {NULL} },
157           {"image",        SIMPLE_CONTENT,  {NULL} },
158           {"date",         SIMPLE_CONTENT,  {NULL} },
159           {"license",      SIMPLE_CONTENT,  {NULL} },
160           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
161           {"link",         SIMPLE_CONTENT,  {NULL} },
162           {"meta",         SIMPLE_CONTENT,  {NULL} },
163           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
164           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
165           {NULL,           UNKNOWN_CONTENT, {NULL} }
166         };
167
168     /* read all playlist attributes */
169     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
170     {
171         psz_name = xml_ReaderName ( p_xml_reader );
172         psz_value = xml_ReaderValue ( p_xml_reader );
173         if ( !psz_name || !psz_value )
174         {
175             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
176             FREE_ATT();
177             return VLC_FALSE;
178         }
179         /* attribute: version */
180         if ( !strcmp( psz_name, "version" ) )
181         {
182             b_version_found = VLC_TRUE;
183             if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
184                 msg_Warn( p_demux, "unsupported XSPF version" );
185         }
186         /* attribute: xmlns */
187         else if ( !strcmp ( psz_name, "xmlns" ) )
188             ;
189         /* unknown attribute */
190         else
191             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
192
193         FREE_ATT();
194     }
195     /* attribute version is mandatory !!! */
196     if ( !b_version_found )
197         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
198
199     /* parse the child elements - we only take care of <trackList> */
200     while ( xml_ReaderRead( p_xml_reader ) == 1 )
201     {
202         i_node = xml_ReaderNodeType( p_xml_reader );
203         switch ( i_node )
204         {
205             case XML_READER_NONE:
206                 break;
207             case XML_READER_STARTELEM:
208                 /*  element start tag  */
209                 psz_name = xml_ReaderName( p_xml_reader );
210                 if ( !psz_name || !*psz_name )
211                 {
212                     msg_Err( p_demux, "invalid xml stream" );
213                     FREE_ATT();
214                     return VLC_FALSE;
215                 }
216                 /* choose handler */
217                 for( p_handler = pl_elements;
218                      p_handler->name && strcmp( psz_name, p_handler->name );
219                      p_handler++ );
220                 if ( !p_handler->name )
221                 {
222                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
223                     FREE_ATT();
224                     return VLC_FALSE;
225                 }
226                 FREE_NAME();
227                 /* complex content is parsed in a separate function */
228                 if ( p_handler->type == COMPLEX_CONTENT )
229                 {
230                     if ( p_handler->pf_handler.cmplx( p_demux,
231                                                       p_playlist,
232                                                       p_item,NULL,
233                                                       p_xml_reader,
234                                                       p_handler->name ) )
235                     {
236                         p_handler = NULL;
237                         FREE_ATT();
238                     }
239                     else
240                     {
241                         FREE_ATT();
242                         return VLC_FALSE;
243                     }
244                 }
245                 break;
246
247             case XML_READER_TEXT:
248                 /* simple element content */
249                 FREE_ATT();
250                 psz_value = xml_ReaderValue( p_xml_reader );
251                 if ( !psz_value )
252                 {
253                     msg_Err( p_demux, "invalid xml stream" );
254                     FREE_ATT();
255                     return VLC_FALSE;
256                 }
257                 break;
258
259             case XML_READER_ENDELEM:
260                 /* element end tag */
261                 psz_name = xml_ReaderName( p_xml_reader );
262                 if ( !psz_name )
263                 {
264                     msg_Err( p_demux, "invalid xml stream" );
265                     FREE_ATT();
266                     return VLC_FALSE;
267                 }
268                 /* leave if the current parent node <playlist> is terminated */
269                 if ( !strcmp( psz_name, psz_element ) )
270                 {
271                     FREE_ATT();
272                     return VLC_TRUE;
273                 }
274                 /* there MUST have been a start tag for that element name */
275                 if ( !p_handler || !p_handler->name
276                      || strcmp( p_handler->name, psz_name ))
277                 {
278                     msg_Err( p_demux, "there's no open element left for <%s>",
279                              psz_name );
280                     FREE_ATT();
281                     return VLC_FALSE;
282                 }
283
284                 if ( p_handler->pf_handler.smpl )
285                 {
286                     p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
287                                                 psz_value );
288                 }
289                 FREE_ATT();
290                 p_handler = NULL;
291                 break;
292
293             default:
294                 /* unknown/unexpected xml node */
295                 msg_Err( p_demux, "unexpected xml node %i", i_node );
296                 FREE_ATT();
297                 return VLC_FALSE;
298         }
299         FREE_NAME();
300     }
301     return VLC_FALSE;
302 }
303
304 /**
305  * \brief parses the tracklist node which only may contain <track>s
306  */
307 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
308 {
309     char *psz_name=NULL;
310     int i_node;
311     int i_ntracks = 0;
312
313     /* now parse the <track>s */
314     while ( xml_ReaderRead( p_xml_reader ) == 1 )
315     {
316         i_node = xml_ReaderNodeType( p_xml_reader );
317         if ( i_node == XML_READER_STARTELEM )
318         {
319             psz_name = xml_ReaderName( p_xml_reader );
320             if ( !psz_name )
321             {
322                 msg_Err( p_demux, "unexpected end of xml data" );
323                 FREE_NAME();
324                 return VLC_FALSE;
325             }
326             if ( strcmp( psz_name, "track") )
327             {
328                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
329                          psz_name );
330                 FREE_NAME();
331                 return VLC_FALSE;
332             }
333             FREE_NAME();
334
335             /* parse the track data in a separate function */
336             if ( parse_track_node( p_demux, p_playlist, p_item, NULL,
337                                    p_xml_reader,"track" ) == VLC_TRUE )
338                 i_ntracks++;
339         }
340         else if ( i_node == XML_READER_ENDELEM )
341             break;
342     }
343
344     /* the <trackList> has to be terminated */
345     if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
346     {
347         msg_Err( p_demux, "there's a missing </trackList>" );
348         FREE_NAME();
349         return VLC_FALSE;
350     }
351     psz_name = xml_ReaderName( p_xml_reader );
352     if ( !psz_name || strcmp( psz_name, "trackList" ) )
353     {
354         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
355         FREE_NAME();
356         return VLC_FALSE;
357     }
358     FREE_NAME();
359
360     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
361
362     return VLC_TRUE;
363 }
364
365 /**
366  * \brief parse one track element
367  * \param COMPLEX_INTERFACE
368  */
369 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
370 {
371     input_item_t *p_new_input = NULL;
372     int i_node;
373     char *psz_name=NULL;
374     char *psz_value=NULL;
375     xml_elem_hnd_t *p_handler=NULL;
376
377     xml_elem_hnd_t track_elements[] =
378         { {"location",     SIMPLE_CONTENT,  {NULL} },
379           {"identifier",   SIMPLE_CONTENT,  {NULL} },
380           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
381           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
382           {"annotation",   SIMPLE_CONTENT,  {NULL} },
383           {"info",         SIMPLE_CONTENT,  {NULL} },
384           {"image",        SIMPLE_CONTENT,  {NULL} },
385           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
386           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
387           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
388           {"link",         SIMPLE_CONTENT,  {NULL} },
389           {"meta",         SIMPLE_CONTENT,  {NULL} },
390           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
391           {NULL,           UNKNOWN_CONTENT, {NULL} }
392         };
393
394     while ( xml_ReaderRead( p_xml_reader ) == 1 )
395     {
396         i_node = xml_ReaderNodeType( p_xml_reader );
397         switch ( i_node )
398         {
399             case XML_READER_NONE:
400                 break;
401
402             case XML_READER_STARTELEM:
403                 /*  element start tag  */
404                 psz_name = xml_ReaderName( p_xml_reader );
405                 if ( !psz_name || !*psz_name )
406                 {
407                     msg_Err( p_demux, "invalid xml stream" );
408                     FREE_ATT();
409                     return VLC_FALSE;
410                 }
411                 /* choose handler */
412                 for( p_handler = track_elements;
413                      p_handler->name && strcmp( psz_name, p_handler->name );
414                      p_handler++ );
415                 if ( !p_handler->name )
416                 {
417                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
418                     FREE_ATT();
419                     return VLC_FALSE;
420                 }
421                 FREE_NAME();
422                 /* complex content is parsed in a separate function */
423                 if ( p_handler->type == COMPLEX_CONTENT )
424                 {
425                     if ( !p_new_input )
426                     {
427                         msg_Err( p_demux,
428                                  "at <%s> level no new item has been allocated",
429                                  p_handler->name );
430                         FREE_ATT();
431                         return VLC_FALSE;
432                     }
433                     if ( p_handler->pf_handler.cmplx( p_demux,
434                                                       p_playlist,
435                                                       NULL, p_new_input,
436                                                       p_xml_reader,
437                                                       p_handler->name ) )
438                     {
439                         p_handler = NULL;
440                         FREE_ATT();
441                     }
442                     else
443                     {
444                         FREE_ATT();
445                         return VLC_FALSE;
446                     }
447                 }
448                 break;
449
450             case XML_READER_TEXT:
451                 /* simple element content */
452                 FREE_ATT();
453                 psz_value = xml_ReaderValue( p_xml_reader );
454                 if ( !psz_value )
455                 {
456                     msg_Err( p_demux, "invalid xml stream" );
457                     FREE_ATT();
458                     return VLC_FALSE;
459                 }
460                 break;
461
462             case XML_READER_ENDELEM:
463                 /* element end tag */
464                 psz_name = xml_ReaderName( p_xml_reader );
465                 if ( !psz_name )
466                 {
467                     msg_Err( p_demux, "invalid xml stream" );
468                     FREE_ATT();
469                     return VLC_FALSE;
470                 }
471                 /* leave if the current parent node <track> is terminated */
472                 if ( !strcmp( psz_name, psz_element ) )
473                 {
474                     FREE_ATT();
475                     /* Add it */
476                     playlist_AddWhereverNeeded( p_playlist, p_new_input,
477                               p_item, p_demux->p_sys->p_item_in_category,
478                               (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
479                               VLC_FALSE, PLAYLIST_APPEND );
480                     return VLC_TRUE;
481                 }
482                 /* there MUST have been a start tag for that element name */
483                 if ( !p_handler || !p_handler->name
484                      || strcmp( p_handler->name, psz_name ))
485                 {
486                     msg_Err( p_demux, "there's no open element left for <%s>",
487                              psz_name );
488                     FREE_ATT();
489                     return VLC_FALSE;
490                 }
491
492                 /* special case: location */
493                 if ( !strcmp( p_handler->name, "location" ) )
494                 {
495                     char *psz_uri=NULL;
496                     /* there MUST NOT be an item */
497                     if ( p_new_input )
498                     {
499                         msg_Err( p_demux, "item <%s> already created",
500                                  psz_name );
501                         FREE_ATT();
502                         return VLC_FALSE;
503                     }
504                     psz_uri = unescape_URI_duplicate( psz_value );
505
506                     if ( psz_uri )
507                     {
508                         p_new_input = input_ItemNewExt( p_playlist, psz_uri,
509                                                         NULL, 0, NULL, -1 );
510                         p_new_input->p_meta = vlc_meta_New();
511                         free( psz_uri );
512                         vlc_input_item_CopyOptions( p_item->p_input, p_new_input );
513                         psz_uri = NULL;
514                         FREE_ATT();
515                         p_handler = NULL;
516                     }
517                     else
518                     {
519                         FREE_ATT();
520                         return VLC_FALSE;
521                     }
522                 }
523                 else
524                 {
525                     /* there MUST be an item */
526                     if ( !p_new_input )
527                     {
528                         msg_Err( p_demux, "item not yet created at <%s>",
529                                  psz_name );
530                         FREE_ATT();
531                         return VLC_FALSE;
532                     }
533                     if ( p_handler->pf_handler.smpl )
534                     {
535                         p_handler->pf_handler.smpl( NULL, p_input,
536                                                     p_handler->name,
537                                                     psz_value );
538                         FREE_ATT();
539                     }
540                 }
541                 FREE_ATT();
542                 p_handler = NULL;
543                 break;
544
545             default:
546                 /* unknown/unexpected xml node */
547                 msg_Err( p_demux, "unexpected xml node %i", i_node );
548                 FREE_ATT();
549                 return VLC_FALSE;
550         }
551         FREE_NAME();
552     }
553     msg_Err( p_demux, "unexpected end of xml data" );
554     FREE_ATT();
555     return VLC_FALSE;
556 }
557
558 /**
559  * \brief handles the supported <track> sub-elements
560  */
561 static vlc_bool_t set_item_info SIMPLE_INTERFACE
562 {
563     /* exit if setting is impossible */
564     if ( !psz_name || !psz_value || !p_input )
565         return VLC_FALSE;
566
567     /* re-convert xml special characters inside psz_value */
568     resolve_xml_special_chars ( psz_value );
569
570     /* handle each info element in a separate "if" clause */
571     if ( !strcmp( psz_name, "title" ) )
572     {
573         p_input->psz_name = strdup( (char*)psz_value );
574     }
575     else if ( !strcmp( psz_name, "creator" ) )
576     {
577         vlc_meta_SetArtist( p_input->p_meta, psz_value );
578     }
579     else if ( !strcmp( psz_name, "album" ) )
580     {
581         vlc_meta_SetAlbum( p_input->p_meta, psz_value );
582
583     }
584     else if ( !strcmp( psz_name, "trackNum" ) )
585     {
586         vlc_meta_SetTracknum( p_input->p_meta, psz_value );    
587     }
588     else if ( !strcmp( psz_name, "duration" ) )
589     {
590         long i_num = atol( psz_value );
591         p_input->i_duration = i_num*1000;
592     }
593     return VLC_TRUE;
594 }
595
596 /**
597  * \brief skips complex element content that we can't manage
598  */
599 static vlc_bool_t skip_element COMPLEX_INTERFACE
600 {
601     char *psz_endname;
602
603     while ( xml_ReaderRead( p_xml_reader ) == 1 )
604     {
605         if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
606         {
607             psz_endname = xml_ReaderName( p_xml_reader );
608             if ( !psz_endname )
609                 return VLC_FALSE;
610             if ( !strcmp( psz_element, psz_endname ) )
611             {
612                 free( psz_endname );
613                 return VLC_TRUE;
614             }
615             else
616                 free( psz_endname );
617         }
618     }
619     return VLC_FALSE;
620 }