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