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