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 for (i = 0; i < p_item->i_parents; i++ )
60 if ( p_item->pp_parents[i]->p_parent->input.i_type
61 == ITEM_TYPE_PLAYLIST )
63 /* set the current node and its children */
64 p_node = p_item->pp_parents[i]->p_parent;
65 pp_items = p_node->pp_children;
66 i_size = p_node->i_children;
69 /* save name of the playlist node */
70 psz_temp = convert_xml_special_chars( p_node->input.psz_name );
72 fprintf( p_export->p_file, "\t<title>%s</title>\n",
76 /* save the creator of the playlist node */
77 psz = vlc_input_item_GetInfo( &p_node->input,
87 psz = vlc_input_item_GetInfo( &p_node->input,
91 psz_temp = convert_xml_special_chars( psz );
93 if ( psz ) free( psz );
95 fprintf( p_export->p_file, "\t<creator>%s</creator>\n",
99 /* save location of the playlist node */
100 psz = assertUTF8URI( p_export->psz_filename );
103 fprintf( p_export->p_file, "\t<location>%s</location>\n",
112 /* prepare all the playlist children for export */
115 pp_items = p_playlist->pp_items;
116 i_size = p_playlist->i_size;
119 /* export all items */
120 fprintf( p_export->p_file, "\t<trackList>\n" );
121 for ( i = 0; i < i_size; i++ )
123 xspf_export_item( pp_items[i], p_export->p_file );
126 /* close the header elements */
127 fprintf( p_export->p_file, "\t</trackList>\n" );
128 fprintf( p_export->p_file, "</playlist>\n" );
134 * \brief exports one item to file or traverse if item is a node
135 * \param p_item playlist item to export
136 * \param p_file file to write xml-converted item to
138 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file )
140 int i; /**< iterator for all children if the current item is a node */
147 /** \todo only "flat" playlists supported at this time.
148 * extend to save the tree structure.
150 /* if we get a node here, we must traverse it */
151 if ( p_item->i_children > 0 )
153 for ( i = 0; i < p_item->i_children; i++ )
155 xspf_export_item( p_item->pp_children[i], p_file );
160 /* leaves can be written directly */
161 fprintf( p_file, "\t\t<track>\n" );
163 /* -> the location */
164 if ( p_item->input.psz_uri && *p_item->input.psz_uri )
166 psz = assertUTF8URI( p_item->input.psz_uri );
167 fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
171 /* -> the name/title (only if different from uri)*/
172 if ( p_item->input.psz_name &&
173 p_item->input.psz_uri &&
174 strcmp( p_item->input.psz_uri, p_item->input.psz_name ) )
176 psz_temp = convert_xml_special_chars( p_item->input.psz_name );
178 fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
182 /* -> the artist/creator */
183 psz = vlc_input_item_GetInfo( &p_item->input,
184 _(VLC_META_INFO_CAT),
185 _(VLC_META_ARTIST) );
192 psz = vlc_input_item_GetInfo( &p_item->input,
193 _(VLC_META_INFO_CAT),
194 _(VLC_META_AUTHOR) );
195 psz_temp = convert_xml_special_chars( psz );
196 if ( psz ) free( psz );
198 fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
202 psz = vlc_input_item_GetInfo( &p_item->input,
203 _(VLC_META_INFO_CAT),
204 _(VLC_META_COLLECTION) );
205 psz_temp = convert_xml_special_chars( psz );
206 if ( psz ) free( psz );
208 fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
211 /* -> the track number */
212 psz = vlc_input_item_GetInfo( &p_item->input,
213 _(VLC_META_INFO_CAT),
214 _(VLC_META_SEQ_NUM) );
218 fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
222 /* -> the duration */
223 if ( p_item->input.i_duration > 0 )
225 fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
226 (long)(p_item->input.i_duration / 1000) );
229 fprintf( p_file, "\t\t</track>\n" );
235 * \param psz_name the location of the media ressource (e.g. local file,
236 * device, network stream, etc.)
237 * \return a new char buffer which asserts that the location is valid UTF-8
239 * \note the returned buffer must be freed, when it isn't used anymore
241 static char *assertUTF8URI( char *psz_name )
243 char *psz_ret = NULL; /**< the new result buffer to return */
244 char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
245 vlc_bool_t b_name_is_uri = VLC_FALSE;
247 if ( !psz_name || !*psz_name )
250 /* check that string is valid UTF-8 */
251 /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
252 if( !( psz_s = EnsureUTF8( psz_name ) ) )
255 /* max. 3x for URI conversion (percent escaping) and
256 8 bytes for "file://" and NULL-termination */
257 psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
261 /** \todo check for a valid scheme part preceding the colon */
262 if ( strchr( psz_s, ':' ) )
265 b_name_is_uri = VLC_TRUE;
267 /* assume "file" scheme if no scheme-part is included */
270 strcpy( psz_ret, "file://" );
276 /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
277 if ( *psz_s & B10000000 ||
282 ( *psz_s == '%' && !b_name_is_uri ) )
285 *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
286 *psz_d++ = hexchars[*psz_s & B00001111];
294 return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );