]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
xspf muxer: %encode '+' since decode_URI() will decode it as a space
[vlc] / modules / misc / playlist / xspf.c
1 /******************************************************************************
2  * xspf.c : XSPF playlist export functions
3  ******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *******************************************************************************/
24
25 /**
26  * \file modules/misc/playlist/xspf.c
27  * \brief XSPF playlist export functions
28  */
29 #include <vlc/vlc.h>
30 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_input.h>
33 #include <vlc_meta.h>
34 #include <vlc_strings.h>
35 #include <vlc_url.h>
36 #include <vlc_charset.h>
37 #include "xspf.h"
38
39 /**
40  * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
41  * and closes the open xml elements
42  * \param p_this the VLC playlist object
43  * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
44  */
45 int E_(xspf_export_playlist)( vlc_object_t *p_this )
46 {
47     const playlist_t *p_playlist = (playlist_t *)p_this;
48     const playlist_export_t *p_export =
49         (playlist_export_t *)p_playlist->p_private;
50     int               i, i_count;
51     char             *psz_temp;
52     playlist_item_t  *p_node = p_export->p_root;
53
54     /* write XSPF XML header */
55     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
56     fprintf( p_export->p_file,
57              "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n" );
58
59     if( !p_node ) return VLC_SUCCESS;
60
61     /* save name of the playlist node */
62     psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
63     if( *psz_temp )
64     {
65         fprintf(  p_export->p_file, "\t<title>%s</title>\n", psz_temp );
66     }
67     free( psz_temp );
68
69     /* save location of the playlist node */
70     psz_temp = assertUTF8URI( p_export->psz_filename );
71     if( psz_temp && *psz_temp )
72     {
73         fprintf( p_export->p_file, "\t<location>%s</location>\n", psz_temp );
74         free( psz_temp );
75     }
76
77     /* export all items in a flat format */
78     fprintf( p_export->p_file, "\t<trackList>\n" );
79     i_count = 0;
80     for( i = 0; i < p_node->i_children; i++ )
81     {
82         xspf_export_item( p_node->pp_children[i], p_export->p_file,
83                           &i_count );
84     }
85     fprintf( p_export->p_file, "\t</trackList>\n" );
86
87     /* export the tree structure in <extension> */
88     fprintf( p_export->p_file, "\t<extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" );
89     i_count = 0;
90     for( i = 0; i < p_node->i_children; i++ )
91     {
92         xspf_extension_item( p_node->pp_children[i], p_export->p_file,
93                              &i_count );
94     }
95     fprintf( p_export->p_file, "\t</extension>\n" );
96
97     /* close the header elements */
98     fprintf( p_export->p_file, "</playlist>\n" );
99
100     return VLC_SUCCESS;
101 }
102
103 /**
104  * \brief exports one item to file or traverse if item is a node
105  * \param p_item playlist item to export
106  * \param p_file file to write xml-converted item to
107  * \param p_i_count counter for track identifiers
108  */
109 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
110                               int *p_i_count )
111 {
112     char *psz;
113     char *psz_temp;
114     mtime_t i_duration;
115
116     if( !p_item ) return;
117
118     /* if we get a node here, we must traverse it */
119     if( p_item->i_children > 0 )
120     {
121         int i;
122         for( i = 0; i < p_item->i_children; i++ )
123         {
124             xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
125         }
126         return;
127     }
128
129     /* don't write empty nodes */
130     if( p_item->i_children == 0 )
131     {
132         return;
133     }
134
135     /* leaves can be written directly */
136     fprintf( p_file, "\t\t<track>\n" );
137
138     /* print identifier and increase the counter */
139     fprintf( p_file, "\t\t\t<identifier>%i</identifier>\n", *p_i_count );
140     ( *p_i_count )++;
141
142     /* -> the location */
143
144     char *psz_uri = input_item_GetURI( p_item->p_input );
145
146     if( psz_uri && *psz_uri )
147     {
148         psz = assertUTF8URI( psz_uri );
149         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
150         free( psz );
151     }
152
153     /* -> the name/title (only if different from uri)*/
154     char *psz_name = input_item_GetTitle( p_item->p_input );
155     if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
156     {
157         psz_temp = convert_xml_special_chars( psz_name );
158         if( *psz_temp )
159             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
160         free( psz_temp );
161     }
162     free( psz_name );
163     free( psz_uri );
164
165     if( p_item->p_input->p_meta == NULL )
166     {
167         goto xspfexportitem_end;
168     }
169
170     /* -> the artist/creator */
171     psz = input_item_GetArtist( p_item->p_input );
172     if( psz == NULL ) psz = strdup( "" );
173     psz_temp = convert_xml_special_chars( psz );
174     free( psz );
175     if( *psz_temp )
176     {
177         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
178     }
179     free( psz_temp );
180
181     /* -> the album */
182     psz = input_item_GetAlbum( p_item->p_input );
183     if( psz == NULL ) psz = strdup( "" );
184     psz_temp = convert_xml_special_chars( psz );
185     free( psz );
186     if( *psz_temp )
187     {
188         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
189     }
190     free( psz_temp );
191
192     /* -> the track number */
193     psz = input_item_GetTrackNum( p_item->p_input );
194     if( psz == NULL ) psz = strdup( "" );
195     if( psz && *psz )
196     {
197         int i_tracknum = atoi( psz );
198         if( i_tracknum > 0 )
199             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
200     }
201     free( psz );
202
203     /* -> the description */
204     psz = input_item_GetDescription( p_item->p_input );
205     if( psz == NULL ) psz = strdup( "" );
206     psz_temp = convert_xml_special_chars( psz );
207     free( psz );
208     if( *psz_temp )
209     {
210         fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
211     }
212     free( psz_temp );
213
214     psz = input_item_GetArtURL( p_item->p_input );
215     if( psz == NULL ) psz = strdup( "" );
216     if( !EMPTY_STR( psz ) )
217     {
218         psz_uri = assertUTF8URI( psz );
219         fprintf( p_file, "\t\t\t<image>%s</image>\n", psz_uri );
220         free( psz_uri );
221     }
222     free( psz );
223
224 xspfexportitem_end:
225     /* -> the duration */
226     i_duration = input_item_GetDuration( p_item->p_input );
227     if( i_duration > 0 )
228     {
229         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
230                  (long)(i_duration / 1000) );
231     }
232
233     fprintf( p_file, "\t\t</track>\n" );
234
235     return;
236 }
237
238 /**
239  * \brief exports one item in extension to file and traverse if item is a node
240  * \param p_item playlist item to export
241  * \param p_file file to write xml-converted item to
242  * \param p_i_count counter for track identifiers
243  */
244 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
245                                  int *p_i_count )
246 {
247     if( !p_item ) return;
248
249     /* if we get a node here, we must traverse it */
250     if( p_item->i_children >= 0 )
251     {
252         int i;
253         char *psz_temp;
254         psz_temp = encode_URI_component( p_item->p_input->psz_name );
255         fprintf( p_file, "\t\t<node title=\"%s\">\n",
256                  *psz_temp ? psz_temp : "" );
257         free( psz_temp );
258
259         for( i = 0; i < p_item->i_children; i++ )
260         {
261             xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
262         }
263
264         fprintf( p_file, "\t\t</node>\n" );
265         return;
266     }
267
268
269     /* print leaf and increase the counter */
270     fprintf( p_file, "\t\t\t<item href=\"%i\" />\n", *p_i_count );
271     ( *p_i_count )++;
272
273     return;
274 }
275
276 /**
277  * \param psz_name the location of the media ressource (e.g. local file,
278  *        device, network stream, etc.)
279  * \return a new char buffer which asserts that the location is valid UTF-8
280  *         and a valid URI
281  * \note the returned buffer must be freed, when it isn't used anymore
282  */
283 static char *assertUTF8URI( char *psz_name )
284 {
285     char *psz_ret = NULL;              /**< the new result buffer to return */
286     char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
287     vlc_bool_t b_uri_is_file = VLC_FALSE; /**< we do additional %-encoding if the URI is a file:// one */
288
289     if( !psz_name || !*psz_name )
290         return NULL;
291
292     /* check that string is valid UTF-8 */
293     /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
294     if( !( psz_s = EnsureUTF8( psz_name ) ) )
295         return NULL;
296
297     /* max. 3x for URI conversion (percent escaping) and
298        8 bytes for "file://" and NULL-termination */
299     psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
300     if( !psz_ret )
301         return NULL;
302
303     /** \todo check for a valid scheme part preceding the colon */
304     size_t i_delim = strcspn( psz_s, ":" );
305     if( i_delim != strlen( psz_s ) )
306     {
307         i_delim++; /* skip the ':' */
308         strncpy( psz_ret, psz_s, i_delim );
309         psz_d = psz_ret + i_delim;
310         psz_s += i_delim;
311
312         if( !strncmp( psz_s, "file://", 7 ) )
313             b_uri_is_file = VLC_TRUE;
314     }
315     /* assume "file" scheme if no scheme-part is included */
316     else
317     {
318         strcpy( psz_ret, "file://" );
319         psz_d = psz_ret + 7;
320         b_uri_is_file = VLC_TRUE;
321     }
322
323     while( *psz_s )
324     {
325         /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
326         if( *psz_s & B10000000 ||
327             *psz_s == '<' ||
328             *psz_s == '>' ||
329             *psz_s == '&' ||
330             *psz_s == ' ' ||
331             ( b_uri_is_file && (
332             *psz_s == ':' ||
333             *psz_s == '"' ||
334             *psz_s == '?' ||
335             *psz_s == '#' ||
336             *psz_s == '[' ||
337             *psz_s == ']' ||
338             *psz_s == '@' ||
339             *psz_s == '+' ||
340             *psz_s == '%' )
341             )
342           )
343         {
344             *psz_d++ = '%';
345             *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
346             *psz_d++ = hexchars[*psz_s & B00001111];
347         }
348         else
349         {
350             *psz_d++ = *psz_s;
351         }
352
353         psz_s++;
354     }
355     *psz_d = '\0';
356
357     return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );
358 }