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