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
30 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_input.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
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
44 int E_(xspf_export_playlist)( vlc_object_t *p_this )
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;
51 playlist_item_t *p_node = p_export->p_root;
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" );
58 if( !p_node ) return VLC_SUCCESS;
60 /* save name of the playlist node */
61 psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
64 fprintf( p_export->p_file, "\t<title>%s</title>\n", psz_temp );
68 /* save location of the playlist node */
69 psz_temp = assertUTF8URI( p_export->psz_filename );
70 if( psz_temp && *psz_temp )
72 fprintf( p_export->p_file, "\t<location>%s</location>\n", psz_temp );
76 /* export all items in a flat format */
77 fprintf( p_export->p_file, "\t<trackList>\n" );
79 for( i = 0; i < p_node->i_children; i++ )
81 xspf_export_item( p_node->pp_children[i], p_export->p_file,
84 fprintf( p_export->p_file, "\t</trackList>\n" );
86 /* export the tree structure in <extension> */
87 fprintf( p_export->p_file, "\t<extension>\n" );
89 for( i = 0; i < p_node->i_children; i++ )
91 xspf_extension_item( p_node->pp_children[i], p_export->p_file,
94 fprintf( p_export->p_file, "\t</extension>\n" );
96 /* close the header elements */
97 fprintf( p_export->p_file, "</playlist>\n" );
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
108 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
115 if( !p_item ) return;
117 /* if we get a node here, we must traverse it */
118 if( p_item->i_children > 0 )
121 for( i = 0; i < p_item->i_children; i++ )
123 xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
128 /* don't write empty nodes */
129 if( p_item->i_children == 0 )
134 /* leaves can be written directly */
135 fprintf( p_file, "\t\t<track>\n" );
137 /* print identifier and increase the counter */
138 fprintf( p_file, "\t\t\t<identifier>%i</identifier>\n", *p_i_count );
141 /* -> the location */
143 char *psz_uri = input_item_GetURI( p_item->p_input );
145 if( psz_uri && *psz_uri )
147 psz = assertUTF8URI( psz_uri );
148 fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
152 /* -> the name/title (only if different from uri)*/
153 char *psz_name = input_item_GetName( p_item->p_input );
154 if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
156 psz_temp = convert_xml_special_chars( psz_name );
158 fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
164 if( p_item->p_input->p_meta == NULL )
166 goto xspfexportitem_end;
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 );
176 fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
181 psz = input_item_GetAlbum( p_item->p_input );
182 if( psz == NULL ) psz = strdup( "" );
183 psz_temp = convert_xml_special_chars( psz );
187 fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
191 /* -> the track number */
192 psz = input_item_GetTrackNum( p_item->p_input );
193 if( psz == NULL ) psz = strdup( "" );
198 fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
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 );
210 fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
215 /* -> the duration */
216 i_duration = input_item_GetDuration( p_item->p_input );
219 fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
220 (long)(i_duration / 1000) );
223 fprintf( p_file, "\t\t</track>\n" );
229 * \brief exports one item in extension to file and traverse if item is a node
230 * \param p_item playlist item to export
231 * \param p_file file to write xml-converted item to
232 * \param p_i_count counter for track identifiers
234 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
237 if( !p_item ) return;
239 /* if we get a node here, we must traverse it */
240 if( p_item->i_children >= 0 )
244 psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
245 fprintf( p_file, "\t\t<node title=\"%s\">\n",
246 *psz_temp ? p_item->p_input->psz_name : "" );
249 for( i = 0; i < p_item->i_children; i++ )
251 xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
254 fprintf( p_file, "\t\t</node>\n" );
259 /* print leaf and increase the counter */
260 fprintf( p_file, "\t\t\t<item href=\"%i\" />\n", *p_i_count );
267 * \param psz_name the location of the media ressource (e.g. local file,
268 * device, network stream, etc.)
269 * \return a new char buffer which asserts that the location is valid UTF-8
271 * \note the returned buffer must be freed, when it isn't used anymore
273 static char *assertUTF8URI( char *psz_name )
275 char *psz_ret = NULL; /**< the new result buffer to return */
276 char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
277 vlc_bool_t b_name_is_uri = VLC_FALSE;
279 if( !psz_name || !*psz_name )
282 /* check that string is valid UTF-8 */
283 /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
284 if( !( psz_s = EnsureUTF8( psz_name ) ) )
287 /* max. 3x for URI conversion (percent escaping) and
288 8 bytes for "file://" and NULL-termination */
289 psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
293 /** \todo check for a valid scheme part preceding the colon */
294 if( strchr( psz_s, ':' ) )
297 b_name_is_uri = VLC_TRUE;
299 /* assume "file" scheme if no scheme-part is included */
302 strcpy( psz_ret, "file://" );
308 /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
309 if( *psz_s & B10000000 ||
314 ( *psz_s == '%' && !b_name_is_uri ) )
317 *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
318 *psz_d++ = hexchars[*psz_s & B00001111];
329 return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );