]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
13c0d30939734233fa085e6d1fb47a96ca670eda
[vlc] / 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 /**
33  * \brief XSPF submodule initialization function
34  */
35 int E_(xspf_import_Activate)( vlc_object_t *p_this )
36 {
37     demux_t *p_demux = (demux_t *)p_this;
38     char    *psz_ext;
39
40     psz_ext = strrchr ( p_demux->psz_path, '.' );
41
42     if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
43         ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
44     {
45         ;
46     }
47     else
48     {
49         return VLC_EGENERIC;
50     }
51     msg_Dbg( p_demux, "using xspf playlist import");
52
53     p_demux->pf_control = xspf_import_Control;
54     p_demux->pf_demux = xspf_import_Demux;
55
56     return VLC_SUCCESS;
57 }
58
59 /**
60  * \brief demuxer function for XSPF parsing
61  */
62 int xspf_import_Demux( demux_t *p_demux )
63 {
64 #if 0
65     playlist_t *p_playlist = NULL;
66     playlist_item_t *p_current = NULL;
67
68     vlc_bool_t b_play;
69     int i_ret = VLC_SUCCESS;
70
71     xml_t *p_xml = NULL;
72     xml_reader_t *p_xml_reader = NULL;
73     char *psz_name = NULL;
74
75     /* create new xml parser from stream */
76     p_xml = xml_Create( p_demux );
77     if( !p_xml )
78         i_ret = VLC_ENOMOD;
79     else
80     {
81         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
82         if( !p_xml_reader )
83             i_ret = VLC_EGENERIC;
84     }
85
86     /* start with parsing the root node */
87     if ( i_ret == VLC_SUCCESS )
88         if ( xml_ReaderRead( p_xml_reader ) != 1 )
89         {
90             msg_Err( p_demux, "can't read xml stream" );
91             i_ret = VLC_EGENERIC;
92         }
93     /* checking root nody type */
94     if ( i_ret == VLC_SUCCESS )
95         if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM )
96         {
97             msg_Err( p_demux, "invalid root node type: %i", xml_ReaderNodeType( p_xml_reader ) );
98             i_ret = VLC_EGENERIC;
99         }
100     /* checking root node name */
101     if ( i_ret == VLC_SUCCESS )
102         psz_name = xml_ReaderName( p_xml_reader );
103     if ( !psz_name || strcmp( psz_name, "playlist" ) )
104     {
105         msg_Err( p_demux, "invalid root node name: %s", psz_name );
106         i_ret = VLC_EGENERIC;
107     }
108     FREE_NAME();
109
110     /* get the playlist ... */
111     if ( i_ret == VLC_SUCCESS )
112     {
113         p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST, FIND_PARENT );
114         if( !p_playlist )
115         {
116             msg_Err( p_demux, "can't find playlist" );
117             i_ret = VLC_ENOOBJ;
118         }
119     }
120     /* ... and its current item (to convert it to a node) */
121     if ( i_ret == VLC_SUCCESS )
122     {
123         b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
124         playlist_ItemToNode( p_playlist, p_current );
125         p_current->input.i_type = ITEM_TYPE_PLAYLIST;
126         /* parse the playlist node */
127         i_ret = parse_playlist_node( p_demux, p_playlist, p_current,
128                                      p_xml_reader, "playlist" );
129         /* true/false - success/egeneric mapping */
130         i_ret = ( i_ret==VLC_TRUE ? VLC_SUCCESS : VLC_EGENERIC );
131
132         if( b_play )
133         {
134             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
135                               p_playlist->status.i_view,
136                               p_playlist->status.p_item, NULL );
137         }
138     }
139
140     if ( p_playlist )
141         vlc_object_release( p_playlist );
142     if ( p_xml_reader )
143         xml_ReaderDelete( p_xml, p_xml_reader );
144     if ( p_xml )
145         xml_Delete( p_xml );
146
147     return i_ret;
148 #endif
149     return 0;
150 }
151
152 /** \brief dummy function for demux callback interface */
153 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
154 {
155     return VLC_EGENERIC;
156 }
157 #if 0
158 /**
159  * \brief parse the root node of a XSPF playlist
160  * \param p_demux demuxer instance
161  * \param p_playlist playlist instance
162  * \param p_item current playlist node
163  * \param p_xml_reader xml reader instance
164  * \param psz_element name of element to parse
165  */
166 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
167 {
168     char *psz_name=NULL;
169     char *psz_value=NULL;
170     vlc_bool_t b_version_found = VLC_FALSE;
171     int i_node;
172     xml_elem_hnd_t *p_handler=NULL;
173
174     xml_elem_hnd_t pl_elements[] =
175         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
176           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
177           {"annotation",   SIMPLE_CONTENT,  {NULL} },
178           {"info",         SIMPLE_CONTENT,  {NULL} },
179           {"location",     SIMPLE_CONTENT,  {NULL} },
180           {"identifier",   SIMPLE_CONTENT,  {NULL} },
181           {"image",        SIMPLE_CONTENT,  {NULL} },
182           {"date",         SIMPLE_CONTENT,  {NULL} },
183           {"license",      SIMPLE_CONTENT,  {NULL} },
184           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
185           {"link",         SIMPLE_CONTENT,  {NULL} },
186           {"meta",         SIMPLE_CONTENT,  {NULL} },
187           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
188           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
189           {NULL,           UNKNOWN_CONTENT, {NULL} }
190         };
191
192     /* read all playlist attributes */
193     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
194     {
195         psz_name = xml_ReaderName ( p_xml_reader );
196         psz_value = xml_ReaderValue ( p_xml_reader );
197         if ( !psz_name || !psz_value )
198         {
199             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
200             FREE_ATT();
201             return VLC_FALSE;
202         }
203         /* attribute: version */
204         if ( !strcmp( psz_name, "version" ) )
205         {
206             b_version_found = VLC_TRUE;
207             if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
208                 msg_Warn( p_demux, "unsupported XSPF version" );
209         }
210         /* attribute: xmlns */
211         else if ( !strcmp ( psz_name, "xmlns" ) )
212             ;
213         /* unknown attribute */
214         else
215             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
216
217         FREE_ATT();
218     }
219     /* attribute version is mandatory !!! */
220     if ( !b_version_found )
221         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
222
223     /* parse the child elements - we only take care of <trackList> */
224     while ( xml_ReaderRead( p_xml_reader ) == 1 )
225     {
226         i_node = xml_ReaderNodeType( p_xml_reader );
227         switch ( i_node )
228         {
229             case XML_READER_NONE:
230                 break;
231             case XML_READER_STARTELEM:
232                 /*  element start tag  */
233                 psz_name = xml_ReaderName( p_xml_reader );
234                 if ( !psz_name || !*psz_name )
235                 {
236                     msg_Err( p_demux, "invalid xml stream" );
237                     FREE_ATT();
238                     return VLC_FALSE;
239                 }
240                 /* choose handler */
241                 for( p_handler = pl_elements;
242                      p_handler->name && strcmp( psz_name, p_handler->name );
243                      p_handler++ );
244                 if ( !p_handler->name )
245                 {
246                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
247                     FREE_ATT();
248                     return VLC_FALSE;
249                 }
250                 FREE_NAME();
251                 /* complex content is parsed in a separate function */
252                 if ( p_handler->type == COMPLEX_CONTENT )
253                 {
254                     if ( p_handler->pf_handler.cmplx( p_demux,
255                                                       p_playlist,
256                                                       p_item,
257                                                       p_xml_reader,
258                                                       p_handler->name ) )
259                     {
260                         p_handler = NULL;
261                         FREE_ATT();
262                     }
263                     else
264                     {
265                         FREE_ATT();
266                         return VLC_FALSE;
267                     }
268                 }
269                 break;
270
271             case XML_READER_TEXT:
272                 /* simple element content */
273                 FREE_ATT();
274                 psz_value = xml_ReaderValue( p_xml_reader );
275                 if ( !psz_value )
276                 {
277                     msg_Err( p_demux, "invalid xml stream" );
278                     FREE_ATT();
279                     return VLC_FALSE;
280                 }
281                 break;
282
283             case XML_READER_ENDELEM:
284                 /* element end tag */
285                 psz_name = xml_ReaderName( p_xml_reader );
286                 if ( !psz_name )
287                 {
288                     msg_Err( p_demux, "invalid xml stream" );
289                     FREE_ATT();
290                     return VLC_FALSE;
291                 }
292                 /* leave if the current parent node <playlist> is terminated */
293                 if ( !strcmp( psz_name, psz_element ) )
294                 {
295                     FREE_ATT();
296                     return VLC_TRUE;
297                 }
298                 /* there MUST have been a start tag for that element name */
299                 if ( !p_handler || !p_handler->name
300                      || strcmp( p_handler->name, psz_name ))
301                 {
302                     msg_Err( p_demux, "there's no open element left for <%s>",
303                              psz_name );
304                     FREE_ATT();
305                     return VLC_FALSE;
306                 }
307
308                 if ( p_handler->pf_handler.smpl )
309                 {
310                     p_handler->pf_handler.smpl( p_item, p_handler->name,
311                                                 psz_value );
312                 }
313                 FREE_ATT();
314                 p_handler = NULL;
315                 break;
316
317             default:
318                 /* unknown/unexpected xml node */
319                 msg_Err( p_demux, "unexpected xml node %i", i_node );
320                 FREE_ATT();
321                 return VLC_FALSE;
322         }
323         FREE_NAME();
324     }
325     return VLC_FALSE;
326 }
327
328 /**
329  * \brief parses the tracklist node which only may contain <track>s
330  */
331 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
332 {
333     char *psz_name=NULL;
334     int i_node;
335     int i_ntracks = 0;
336
337     /* now parse the <track>s */
338     while ( xml_ReaderRead( p_xml_reader ) == 1 )
339     {
340         i_node = xml_ReaderNodeType( p_xml_reader );
341         if ( i_node == XML_READER_STARTELEM )
342         {
343             psz_name = xml_ReaderName( p_xml_reader );
344             if ( !psz_name )
345             {
346                 msg_Err( p_demux, "unexpected end of xml data" );
347                 FREE_NAME();
348                 return VLC_FALSE;
349             }
350             if ( strcmp( psz_name, "track") )
351             {
352                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
353                          psz_name );
354                 FREE_NAME();
355                 return VLC_FALSE;
356             }
357             FREE_NAME();
358
359             /* parse the track data in a separate function */
360             if ( parse_track_node( p_demux, p_playlist, p_item, p_xml_reader,
361                                    "track" ) == VLC_TRUE )
362                 i_ntracks++;
363         }
364         else if ( i_node == XML_READER_ENDELEM )
365             break;
366     }
367
368     /* the <trackList> has to be terminated */
369     if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
370     {
371         msg_Err( p_demux, "there's a missing </trackList>" );
372         FREE_NAME();
373         return VLC_FALSE;
374     }
375     psz_name = xml_ReaderName( p_xml_reader );
376     if ( !psz_name || strcmp( psz_name, "trackList" ) )
377     {
378         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
379         FREE_NAME();
380         return VLC_FALSE;
381     }
382     FREE_NAME();
383
384     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
385
386     return VLC_TRUE;
387 }
388
389 /**
390  * \brief parse one track element
391  * \param COMPLEX_INTERFACE
392  */
393 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
394 {
395     playlist_item_t *p_new=NULL;
396     int i_node;
397     char *psz_name=NULL;
398     char *psz_value=NULL;
399     xml_elem_hnd_t *p_handler=NULL;
400
401     xml_elem_hnd_t track_elements[] =
402         { {"location",     SIMPLE_CONTENT,  {NULL} },
403           {"identifier",   SIMPLE_CONTENT,  {NULL} },
404           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
405           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
406           {"annotation",   SIMPLE_CONTENT,  {NULL} },
407           {"info",         SIMPLE_CONTENT,  {NULL} },
408           {"image",        SIMPLE_CONTENT,  {NULL} },
409           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
410           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
411           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
412           {"link",         SIMPLE_CONTENT,  {NULL} },
413           {"meta",         SIMPLE_CONTENT,  {NULL} },
414           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
415           {NULL,           UNKNOWN_CONTENT, {NULL} }
416         };
417
418     while ( xml_ReaderRead( p_xml_reader ) == 1 )
419     {
420         i_node = xml_ReaderNodeType( p_xml_reader );
421         switch ( i_node )
422         {
423             case XML_READER_NONE:
424                 break;
425
426             case XML_READER_STARTELEM:
427                 /*  element start tag  */
428                 psz_name = xml_ReaderName( p_xml_reader );
429                 if ( !psz_name || !*psz_name )
430                 {
431                     msg_Err( p_demux, "invalid xml stream" );
432                     FREE_ATT();
433                     return VLC_FALSE;
434                 }
435                 /* choose handler */
436                 for( p_handler = track_elements;
437                      p_handler->name && strcmp( psz_name, p_handler->name );
438                      p_handler++ );
439                 if ( !p_handler->name )
440                 {
441                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
442                     FREE_ATT();
443                     return VLC_FALSE;
444                 }
445                 FREE_NAME();
446                 /* complex content is parsed in a separate function */
447                 if ( p_handler->type == COMPLEX_CONTENT )
448                 {
449                     if ( !p_new )
450                     {
451                         msg_Err( p_demux,
452                                  "at <%s> level no new item has been allocated",
453                                  p_handler->name );
454                         FREE_ATT();
455                         return VLC_FALSE;
456                     }
457                     if ( p_handler->pf_handler.cmplx( p_demux,
458                                                       p_playlist,
459                                                       p_new,
460                                                       p_xml_reader,
461                                                       p_handler->name ) )
462                     {
463                         p_handler = NULL;
464                         FREE_ATT();
465                     }
466                     else
467                     {
468                         FREE_ATT();
469                         return VLC_FALSE;
470                     }
471                 }
472                 break;
473
474             case XML_READER_TEXT:
475                 /* simple element content */
476                 FREE_ATT();
477                 psz_value = xml_ReaderValue( p_xml_reader );
478                 if ( !psz_value )
479                 {
480                     msg_Err( p_demux, "invalid xml stream" );
481                     FREE_ATT();
482                     return VLC_FALSE;
483                 }
484                 break;
485
486             case XML_READER_ENDELEM:
487                 /* element end tag */
488                 psz_name = xml_ReaderName( p_xml_reader );
489                 if ( !psz_name )
490                 {
491                     msg_Err( p_demux, "invalid xml stream" );
492                     FREE_ATT();
493                     return VLC_FALSE;
494                 }
495                 /* leave if the current parent node <track> is terminated */
496                 if ( !strcmp( psz_name, psz_element ) )
497                 {
498                     FREE_ATT();
499                     return VLC_TRUE;
500                 }
501                 /* there MUST have been a start tag for that element name */
502                 if ( !p_handler || !p_handler->name
503                      || strcmp( p_handler->name, psz_name ))
504                 {
505                     msg_Err( p_demux, "there's no open element left for <%s>",
506                              psz_name );
507                     FREE_ATT();
508                     return VLC_FALSE;
509                 }
510
511                 /* special case: location */
512                 if ( !strcmp( p_handler->name, "location" ) )
513                 {
514                     /* there MUST NOT be an item */
515                     if ( p_new )
516                     {
517                         msg_Err( p_demux,
518                                  "a new item has just been created <%s>",
519                                  psz_name );
520                         FREE_ATT();
521                         return VLC_FALSE;
522                     }
523                     /* create it now */
524                     if ( insert_new_item( p_playlist, p_item,
525                                           &p_new, psz_value ) )
526                     {
527                         FREE_ATT();
528                         p_handler = NULL;
529                     }
530                     else
531                     {
532                         FREE_ATT();
533                         return VLC_FALSE;
534                     }
535                 }
536                 else
537                 {
538                     /* there MUST be an item */
539                     if ( !p_new )
540                     {
541                         msg_Err( p_demux,
542                                  "an item hasn't been created yet <%s>",
543                                  psz_name );
544                         FREE_ATT();
545                         return VLC_FALSE;
546                     }
547                     if ( p_handler->pf_handler.smpl )
548                     {
549                         p_handler->pf_handler.smpl( p_new, p_handler->name,
550                                                     psz_value );
551                         FREE_ATT();
552                     }
553                 }
554                 FREE_ATT();
555                 p_handler = NULL;
556                 break;
557
558             default:
559                 /* unknown/unexpected xml node */
560                 msg_Err( p_demux, "unexpected xml node %i", i_node );
561                 FREE_ATT();
562                 return VLC_FALSE;
563         }
564         FREE_NAME();
565     }
566     msg_Err( p_demux, "unexpected end of xml data" );
567     FREE_ATT();
568     return VLC_FALSE;
569 }
570
571 /**
572  * \brief handles the supported <track> sub-elements
573  */
574 static vlc_bool_t set_item_info SIMPLE_INTERFACE
575 {
576     /* exit if setting is impossible */
577     if ( !psz_name || !psz_value || !p_item )
578         return VLC_FALSE;
579
580     /* re-convert xml special characters inside psz_value */
581     resolve_xml_special_chars ( psz_value );
582
583     /* handle each info element in a separate "if" clause */
584     if ( !strcmp( psz_name, "title" ) )
585     {
586         if ( playlist_ItemSetName ( p_item, (char *)psz_value ) == VLC_SUCCESS )
587             return VLC_TRUE;
588         return VLC_FALSE;
589     }
590     else if ( !strcmp( psz_name, "creator" ) )
591     {
592         if ( vlc_input_item_AddInfo( &(p_item->input),
593                                      _(VLC_META_INFO_CAT), _(VLC_META_ARTIST),
594                                      "%s", psz_value ) == VLC_SUCCESS )
595             return VLC_TRUE;
596         return VLC_FALSE;
597
598     }
599     else if ( !strcmp( psz_name, "album" ) )
600     {
601         if ( vlc_input_item_AddInfo( &(p_item->input),
602                                      _(VLC_META_INFO_CAT),
603                                      _(VLC_META_COLLECTION),
604                                      "%s", psz_value ) == VLC_SUCCESS )
605             return VLC_TRUE;
606         return VLC_FALSE;
607
608     } else if ( !strcmp( psz_name, "trackNum" ) )
609     {
610         long i_num = atol( psz_value );
611         if ( i_num > 0
612              && vlc_input_item_AddInfo( &(p_item->input),
613                                          _(VLC_META_INFO_CAT),
614                                          _(VLC_META_SEQ_NUM),
615                                          "%s", psz_value ) == VLC_SUCCESS )
616                 return VLC_TRUE;
617         return VLC_FALSE;
618
619     } else if ( !strcmp( psz_name, "duration" ) )
620     {
621         long i_num = atol( psz_value );
622         if ( i_num > 0
623              && playlist_ItemSetDuration( p_item, i_num*1000 ) == VLC_SUCCESS )
624                 return VLC_TRUE;
625         return VLC_FALSE;
626
627     }
628
629     return VLC_TRUE;
630 }
631
632 /**
633  * \brief skips complex element content that we can't manage
634  */
635 static vlc_bool_t skip_element COMPLEX_INTERFACE
636 {
637     char *psz_endname;
638
639     while ( xml_ReaderRead( p_xml_reader ) == 1 )
640     {
641         if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
642         {
643             psz_endname = xml_ReaderName( p_xml_reader );
644             if ( !psz_endname )
645                 return VLC_FALSE;
646             if ( !strcmp( psz_element, psz_endname ) )
647             {
648                 free( psz_endname );
649                 return VLC_TRUE;
650             }
651             else
652                 free( psz_endname );
653         }
654     }
655     return VLC_FALSE;
656 }
657
658 /**
659  * \brief creates a new playlist item from the given mrl
660  */
661 static vlc_bool_t insert_new_item( playlist_t *p_pl, playlist_item_t *p_cur,
662                                    playlist_item_t **pp_new, char *psz_location )
663 {
664     char *psz_uri=NULL;
665     psz_uri = unescape_URI_duplicate( psz_location );
666
667     if ( psz_uri )
668     {
669         *pp_new = playlist_ItemNew( p_pl, psz_uri, NULL );
670         free( psz_uri );
671         psz_uri = NULL;
672     }
673
674     if ( !*pp_new )
675         return VLC_FALSE;
676
677     playlist_NodeAddItem( p_pl,  *pp_new,         p_cur->pp_parents[0]->i_view,
678                           p_cur, PLAYLIST_APPEND, PLAYLIST_END );
679
680     playlist_CopyParents( p_cur, *pp_new );
681
682     vlc_input_item_CopyOptions( &p_cur->input, &((*pp_new)->input) );
683
684     return VLC_TRUE;
685 }
686 #endif