1 /******************************************************************************
2 * xspf.c : XSPF playlist export functions
3 ******************************************************************************
4 * Copyright (C) 2006-2009 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
33 #include <vlc_common.h>
34 #include <vlc_playlist.h>
35 #include <vlc_input.h>
36 #include <vlc_strings.h>
42 static void xspf_export_item( playlist_item_t *, FILE *, int * );
43 static void xspf_extension_item( playlist_item_t *, FILE *, int * );
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
51 int xspf_export_playlist( vlc_object_t *p_this )
53 const playlist_export_t *p_export = (playlist_export_t *)p_this;
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/\" " \
62 "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n" );
64 if( !p_node ) return VLC_SUCCESS;
66 /* save name of the playlist node */
67 psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
70 fprintf( p_export->p_file, "\t<title>%s</title>\n", psz_temp );
74 /* export all items in a flat format */
75 fprintf( p_export->p_file, "\t<trackList>\n" );
77 for( i = 0; i < p_node->i_children; i++ )
79 xspf_export_item( p_node->pp_children[i], p_export->p_file,
82 fprintf( p_export->p_file, "\t</trackList>\n" );
84 /* export the tree structure in <extension> */
85 fprintf( p_export->p_file, "\t<extension application=\"" \
86 "http://www.videolan.org/vlc/playlist/0\">\n" );
88 for( i = 0; i < p_node->i_children; i++ )
90 xspf_extension_item( p_node->pp_children[i], p_export->p_file,
93 fprintf( p_export->p_file, "\t</extension>\n" );
95 /* close the header elements */
96 fprintf( p_export->p_file, "</playlist>\n" );
102 * \brief exports one item to file or traverse if item is a node
103 * \param p_item playlist item to export
104 * \param p_file file to write xml-converted item to
105 * \param p_i_count counter for track identifiers
107 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 /* -> the location */
139 char *psz_uri = input_item_GetURI( p_item->p_input );
141 if( psz_uri && *psz_uri )
142 fprintf( p_file, "\t\t\t<location>%s</location>\n", psz_uri );
144 /* -> the name/title (only if different from uri)*/
145 char *psz_name = input_item_GetTitle( p_item->p_input );
146 if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
148 psz_temp = convert_xml_special_chars( psz_name );
150 fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
156 if( p_item->p_input->p_meta == NULL )
158 goto xspfexportitem_end;
161 /* -> the artist/creator */
162 psz = input_item_GetArtist( p_item->p_input );
163 if( psz == NULL ) psz = strdup( "" );
164 psz_temp = convert_xml_special_chars( psz );
168 fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
173 psz = input_item_GetAlbum( p_item->p_input );
174 if( psz == NULL ) psz = strdup( "" );
175 psz_temp = convert_xml_special_chars( psz );
179 fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
183 /* -> the track number */
184 psz = input_item_GetTrackNum( p_item->p_input );
185 if( psz == NULL ) psz = strdup( "" );
188 int i_tracknum = atoi( psz );
190 fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
194 /* -> the description */
195 psz = input_item_GetDescription( p_item->p_input );
196 if( psz == NULL ) psz = strdup( "" );
197 psz_temp = convert_xml_special_chars( psz );
201 fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
205 psz = input_item_GetArtURL( p_item->p_input );
206 if( psz == NULL ) psz = strdup( "" );
207 if( !EMPTY_STR( psz ) )
209 fprintf( p_file, "\t\t\t<image>%s</image>\n", psz );
214 /* -> the duration */
215 i_duration = input_item_GetDuration( p_item->p_input );
218 fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
219 (long)(i_duration / 1000) );
222 /* export the intenal id and the input's options (bookmarks, ...)
224 fprintf( p_file, "\t\t\t<extension application=\"" \
225 "http://www.videolan.org/vlc/playlist/0\">\n" );
227 /* print the id and increase the counter */
228 fprintf( p_file, "\t\t\t\t<vlc:id>%i</vlc:id>\n", *p_i_count );
231 for( i = 0; i < p_item->p_input->i_options; i++ )
233 fprintf( p_file, "\t\t\t\t<vlc:option>%s</vlc: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" );
240 fprintf( p_file, "\t\t</track>\n" );
246 * \brief exports one item in extension to file and traverse if item is a node
247 * \param p_item playlist item to export
248 * \param p_file file to write xml-converted item to
249 * \param p_i_count counter for track identifiers
251 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
254 if( !p_item ) return;
256 /* if we get a node here, we must traverse it */
257 if( p_item->i_children >= 0 )
260 char *psz_temp = NULL;
261 if( p_item->p_input->psz_name )
262 psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
263 fprintf( p_file, "\t\t<vlc:node title=\"%s\">\n",
264 psz_temp ? psz_temp : "" );
267 for( i = 0; i < p_item->i_children; i++ )
269 xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
272 fprintf( p_file, "\t\t</vlc:node>\n" );
277 /* print leaf and increase the counter */
278 fprintf( p_file, "\t\t\t<vlc:item tid=\"%i\" />\n", *p_i_count );