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