1 /******************************************************************************
2 * Copyright (C) 2006 Daniel Stränger <vlc at schmaller dot de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17 *******************************************************************************/
20 * \file modules/misc/playlist/xspf.c
21 * \brief XSPF playlist export functions
27 #include "vlc_strings.h"
31 * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
32 * and closes the open xml elements
33 * \param p_this the VLC playlist object
34 * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
36 int E_(xspf_export_playlist)( vlc_object_t *p_this )
38 const playlist_t *p_playlist = (playlist_t *)p_this;
39 const playlist_export_t *p_export =
40 (playlist_export_t *)p_playlist->p_private;
44 playlist_item_t **pp_items = NULL;
46 playlist_item_t *p_node;
48 /* write XSPF XML header - since we don't use <extension>,
49 * we get by with version 0 */
50 fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
51 fprintf( p_export->p_file,
52 "<playlist version=\"0\" xmlns=\"http://xspf.org/ns/0/\">\n" );
54 /* save tho whole playlist or only the current node */
55 #define p_item p_playlist->status.p_item
58 if ( p_item->p_parent->p_input->i_type == ITEM_TYPE_PLAYLIST )
60 /* set the current node and its children */
61 p_node = p_item->p_parent;
62 pp_items = p_node->pp_children;
63 i_size = p_node->i_children;
66 /* save name of the playlist node */
67 psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
69 fprintf( p_export->p_file, "\t<title>%s</title>\n",
73 /* save the creator of the playlist node */
74 psz = p_node->p_input->p_meta->psz_artist ?
75 strdup( p_node->p_input->p_meta->psz_artist ):
84 psz = p_node->p_input->p_meta->psz_author ?
85 strdup( p_node->p_input->p_meta->psz_author ):
88 psz_temp = convert_xml_special_chars( psz );
90 if ( psz ) free( psz );
92 fprintf( p_export->p_file, "\t<creator>%s</creator>\n",
96 /* save location of the playlist node */
97 psz = assertUTF8URI( p_export->psz_filename );
100 fprintf( p_export->p_file, "\t<location>%s</location>\n",
107 /* prepare all the playlist children for export */
110 pp_items = p_playlist->pp_items;
111 i_size = p_playlist->i_size;
114 /* export all items */
115 fprintf( p_export->p_file, "\t<trackList>\n" );
116 for ( i = 0; i < i_size; i++ )
118 xspf_export_item( pp_items[i], p_export->p_file );
121 /* close the header elements */
122 fprintf( p_export->p_file, "\t</trackList>\n" );
123 fprintf( p_export->p_file, "</playlist>\n" );
129 * \brief exports one item to file or traverse if item is a node
130 * \param p_item playlist item to export
131 * \param p_file file to write xml-converted item to
133 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file )
135 int i; /**< iterator for all children if the current item is a node */
142 /** \todo only "flat" playlists supported at this time.
143 * extend to save the tree structure.
145 /* if we get a node here, we must traverse it */
146 if ( p_item->i_children > 0 )
148 for ( i = 0; i < p_item->i_children; i++ )
150 xspf_export_item( p_item->pp_children[i], p_file );
155 /* leaves can be written directly */
156 fprintf( p_file, "\t\t<track>\n" );
158 /* -> the location */
159 if ( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
161 psz = assertUTF8URI( p_item->p_input->psz_uri );
162 fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
166 /* -> the name/title (only if different from uri)*/
167 if ( p_item->p_input->psz_name &&
168 p_item->p_input->psz_uri &&
169 strcmp( p_item->p_input->psz_uri, p_item->p_input->psz_name ) )
171 psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
173 fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
177 /* -> the artist/creator */
178 psz = p_item->p_input->p_meta->psz_artist ?
179 strdup( p_item->p_input->p_meta->psz_artist ):
187 psz = p_item->p_input->p_meta->psz_author ?
188 strdup( p_item->p_input->p_meta->psz_author ):
190 psz_temp = convert_xml_special_chars( psz );
191 if ( psz ) free( psz );
193 fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
197 psz = p_item->p_input->p_meta->psz_album ?
198 strdup( p_item->p_input->p_meta->psz_album ):
200 psz_temp = convert_xml_special_chars( psz );
201 if ( psz ) free( psz );
203 fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
206 /* -> the track number */
207 psz = p_item->p_input->p_meta->psz_tracknum ?
208 strdup( p_item->p_input->p_meta->psz_tracknum ):
213 fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
217 /* -> the duration */
218 if ( p_item->p_input->i_duration > 0 )
220 fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
221 (long)(p_item->p_input->i_duration / 1000) );
224 fprintf( p_file, "\t\t</track>\n" );
230 * \param psz_name the location of the media ressource (e.g. local file,
231 * device, network stream, etc.)
232 * \return a new char buffer which asserts that the location is valid UTF-8
234 * \note the returned buffer must be freed, when it isn't used anymore
236 static char *assertUTF8URI( char *psz_name )
238 char *psz_ret = NULL; /**< the new result buffer to return */
239 char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
240 vlc_bool_t b_name_is_uri = VLC_FALSE;
242 if ( !psz_name || !*psz_name )
245 /* check that string is valid UTF-8 */
246 /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
247 if( !( psz_s = EnsureUTF8( psz_name ) ) )
250 /* max. 3x for URI conversion (percent escaping) and
251 8 bytes for "file://" and NULL-termination */
252 psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
256 /** \todo check for a valid scheme part preceding the colon */
257 if ( strchr( psz_s, ':' ) )
260 b_name_is_uri = VLC_TRUE;
262 /* assume "file" scheme if no scheme-part is included */
265 strcpy( psz_ret, "file://" );
271 /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
272 if ( *psz_s & B10000000 ||
277 ( *psz_s == '%' && !b_name_is_uri ) )
280 *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
281 *psz_d++ = hexchars[*psz_s & B00001111];
289 return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );