1 /******************************************************************************
2 * xspf.c : XSPF playlist export functions
3 ******************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Daniel Stränger <vlc at schmaller dot de>
8 * Yoann Peronneau <yoann@videolan.org>
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.
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.
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 *******************************************************************************/
26 * \file modules/misc/playlist/xspf.c
27 * \brief XSPF playlist export functions
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_input.h>
38 #include <vlc_strings.h>
40 #include <vlc_charset.h>
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
49 int xspf_export_playlist( vlc_object_t *p_this )
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;
56 playlist_item_t *p_node = p_export->p_root;
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" );
63 if( !p_node ) return VLC_SUCCESS;
65 /* save name of the playlist node */
66 psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
69 fprintf( p_export->p_file, "\t<title>%s</title>\n", psz_temp );
73 /* save location of the playlist node */
74 psz_temp = assertUTF8URI( p_export->psz_filename );
75 if( psz_temp && *psz_temp )
77 fprintf( p_export->p_file, "\t<location>%s</location>\n", psz_temp );
81 /* export all items in a flat format */
82 fprintf( p_export->p_file, "\t<trackList>\n" );
84 for( i = 0; i < p_node->i_children; i++ )
86 xspf_export_item( p_node->pp_children[i], p_export->p_file,
89 fprintf( p_export->p_file, "\t</trackList>\n" );
91 /* export the tree structure in <extension> */
92 fprintf( p_export->p_file, "\t<extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" );
94 for( i = 0; i < p_node->i_children; i++ )
96 xspf_extension_item( p_node->pp_children[i], p_export->p_file,
99 fprintf( p_export->p_file, "\t</extension>\n" );
101 /* close the header elements */
102 fprintf( p_export->p_file, "</playlist>\n" );
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
113 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
121 if( !p_item ) return;
123 /* if we get a node here, we must traverse it */
124 if( p_item->i_children > 0 )
127 for( i = 0; i < p_item->i_children; i++ )
129 xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
134 /* don't write empty nodes */
135 if( p_item->i_children == 0 )
140 /* leaves can be written directly */
141 fprintf( p_file, "\t\t<track>\n" );
143 /* print identifier and increase the counter */
144 fprintf( p_file, "\t\t\t<identifier>%i</identifier>\n", *p_i_count );
147 /* -> the location */
149 char *psz_uri = input_item_GetURI( p_item->p_input );
151 if( psz_uri && *psz_uri )
153 psz = assertUTF8URI( psz_uri );
154 fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
158 /* -> the name/title (only if different from uri)*/
159 char *psz_name = input_item_GetTitle( p_item->p_input );
160 if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
162 psz_temp = convert_xml_special_chars( psz_name );
164 fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
170 if( p_item->p_input->p_meta == NULL )
172 goto xspfexportitem_end;
175 /* -> the artist/creator */
176 psz = input_item_GetArtist( p_item->p_input );
177 if( psz == NULL ) psz = strdup( "" );
178 psz_temp = convert_xml_special_chars( psz );
182 fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
187 psz = input_item_GetAlbum( p_item->p_input );
188 if( psz == NULL ) psz = strdup( "" );
189 psz_temp = convert_xml_special_chars( psz );
193 fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
197 /* -> the track number */
198 psz = input_item_GetTrackNum( p_item->p_input );
199 if( psz == NULL ) psz = strdup( "" );
202 int i_tracknum = atoi( psz );
204 fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
208 /* -> the description */
209 psz = input_item_GetDescription( p_item->p_input );
210 if( psz == NULL ) psz = strdup( "" );
211 psz_temp = convert_xml_special_chars( psz );
215 fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
219 psz = input_item_GetArtURL( p_item->p_input );
220 if( psz == NULL ) psz = strdup( "" );
221 if( !EMPTY_STR( psz ) )
223 psz_uri = assertUTF8URI( psz );
224 fprintf( p_file, "\t\t\t<image>%s</image>\n", psz_uri );
229 /* export the input's options (bookmarks, ...) in <extension> */
230 fprintf( p_file, "\t\t\t<extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" );
231 for( i = 0; i < p_item->p_input->i_options; i++ )
233 fprintf( p_file, "\t\t\t\t<option>%s</option>\n",
234 p_item->p_input->ppsz_options[i][0] == ':' ?
235 p_item->p_input->ppsz_options[i] + 1 :
236 p_item->p_input->ppsz_options[i] );
238 fprintf( p_file, "\t\t\t</extension>\n" );
241 /* -> the duration */
242 i_duration = input_item_GetDuration( p_item->p_input );
245 fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
246 (long)(i_duration / 1000) );
249 fprintf( p_file, "\t\t</track>\n" );
255 * \brief exports one item in extension to file and traverse if item is a node
256 * \param p_item playlist item to export
257 * \param p_file file to write xml-converted item to
258 * \param p_i_count counter for track identifiers
260 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
263 if( !p_item ) return;
265 /* if we get a node here, we must traverse it */
266 if( p_item->i_children >= 0 )
270 psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
271 fprintf( p_file, "\t\t<node title=\"%s\">\n",
272 *psz_temp ? psz_temp : "" );
275 for( i = 0; i < p_item->i_children; i++ )
277 xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
280 fprintf( p_file, "\t\t</node>\n" );
285 /* print leaf and increase the counter */
286 fprintf( p_file, "\t\t\t<item href=\"%i\" />\n", *p_i_count );
293 * \param psz_name the location of the media ressource (e.g. local file,
294 * device, network stream, etc.)
295 * \return a new char buffer which asserts that the location is valid UTF-8
297 * \note the returned buffer must be freed, when it isn't used anymore
299 static char *assertUTF8URI( char *psz_name )
301 char *psz_ret = NULL; /**< the new result buffer to return */
302 char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
303 bool b_uri_is_file = false; /**< we do additional %-encoding if the URI is a file:// one */
305 if( !psz_name || !*psz_name )
308 /* check that string is valid UTF-8 */
309 /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
310 if( !( psz_s = EnsureUTF8( psz_name ) ) )
313 /* max. 3x for URI conversion (percent escaping) and
314 8 bytes for "file://" and NULL-termination */
315 psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
319 /** \todo check for a valid scheme part preceding the colon */
320 size_t i_delim = strcspn( psz_s, ":" );
321 if( i_delim != strlen( psz_s ) )
323 i_delim++; /* skip the ':' */
324 strncpy( psz_ret, psz_s, i_delim );
325 psz_d = psz_ret + i_delim;
327 if( !strncmp( psz_s, "file://", 7 ) )
328 b_uri_is_file = true;
332 /* assume "file" scheme if no scheme-part is included */
335 strcpy( psz_ret, "file://" );
337 b_uri_is_file = true;
342 /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
343 if( *psz_s & B10000000 ||
362 *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
363 *psz_d++ = hexchars[*psz_s & B00001111];
374 return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );