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_t *p_playlist = (playlist_t *)p_this;
54 const playlist_export_t *p_export =
55 (playlist_export_t *)p_playlist->p_private;
58 playlist_item_t *p_node = p_export->p_root;
60 /* write XSPF XML header */
61 fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
62 fprintf( p_export->p_file,
63 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" " \
64 "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n" );
66 if( !p_node ) return VLC_SUCCESS;
68 /* save name of the playlist node */
69 psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
72 fprintf( p_export->p_file, "\t<title>%s</title>\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 application=\"" \
88 "http://www.videolan.org/vlc/playlist/0\">\n" );
90 for( i = 0; i < p_node->i_children; i++ )
92 xspf_extension_item( p_node->pp_children[i], p_export->p_file,
95 fprintf( p_export->p_file, "\t</extension>\n" );
97 /* close the header elements */
98 fprintf( p_export->p_file, "</playlist>\n" );
104 * \brief exports one item to file or traverse if item is a node
105 * \param p_item playlist item to export
106 * \param p_file file to write xml-converted item to
107 * \param p_i_count counter for track identifiers
109 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
117 if( !p_item ) return;
119 /* if we get a node here, we must traverse it */
120 if( p_item->i_children > 0 )
123 for( i = 0; i < p_item->i_children; i++ )
125 xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
130 /* don't write empty nodes */
131 if( p_item->i_children == 0 )
136 /* leaves can be written directly */
137 fprintf( p_file, "\t\t<track>\n" );
139 /* -> the location */
141 char *psz_uri = input_item_GetURI( p_item->p_input );
143 if( psz_uri && *psz_uri )
145 psz = make_URI( psz_uri );
146 fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
150 /* -> the name/title (only if different from uri)*/
151 char *psz_name = input_item_GetTitle( p_item->p_input );
152 if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
154 psz_temp = convert_xml_special_chars( psz_name );
156 fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
162 if( p_item->p_input->p_meta == NULL )
164 goto xspfexportitem_end;
167 /* -> the artist/creator */
168 psz = input_item_GetArtist( p_item->p_input );
169 if( psz == NULL ) psz = strdup( "" );
170 psz_temp = convert_xml_special_chars( psz );
174 fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
179 psz = input_item_GetAlbum( p_item->p_input );
180 if( psz == NULL ) psz = strdup( "" );
181 psz_temp = convert_xml_special_chars( psz );
185 fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
189 /* -> the track number */
190 psz = input_item_GetTrackNum( p_item->p_input );
191 if( psz == NULL ) psz = strdup( "" );
194 int i_tracknum = atoi( psz );
196 fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
200 /* -> the description */
201 psz = input_item_GetDescription( p_item->p_input );
202 if( psz == NULL ) psz = strdup( "" );
203 psz_temp = convert_xml_special_chars( psz );
207 fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
211 psz = input_item_GetArtURL( p_item->p_input );
212 if( psz == NULL ) psz = strdup( "" );
213 if( !EMPTY_STR( psz ) )
215 fprintf( p_file, "\t\t\t<image>%s</image>\n", psz );
220 /* -> the duration */
221 i_duration = input_item_GetDuration( p_item->p_input );
224 fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
225 (long)(i_duration / 1000) );
228 /* export the intenal id and the input's options (bookmarks, ...)
230 fprintf( p_file, "\t\t\t<extension application=\"" \
231 "http://www.videolan.org/vlc/playlist/0\">\n" );
233 /* print the id and increase the counter */
234 fprintf( p_file, "\t\t\t\t<vlc:id>%i</vlc:id>\n", *p_i_count );
237 for( i = 0; i < p_item->p_input->i_options; i++ )
239 fprintf( p_file, "\t\t\t\t<vlc:option>%s</vlc:option>\n",
240 p_item->p_input->ppsz_options[i][0] == ':' ?
241 p_item->p_input->ppsz_options[i] + 1 :
242 p_item->p_input->ppsz_options[i] );
244 fprintf( p_file, "\t\t\t</extension>\n" );
246 fprintf( p_file, "\t\t</track>\n" );
252 * \brief exports one item in extension to file and traverse if item is a node
253 * \param p_item playlist item to export
254 * \param p_file file to write xml-converted item to
255 * \param p_i_count counter for track identifiers
257 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
260 if( !p_item ) return;
262 /* if we get a node here, we must traverse it */
263 if( p_item->i_children >= 0 )
267 psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
268 fprintf( p_file, "\t\t<vlc:node title=\"%s\">\n",
269 *psz_temp ? psz_temp : "" );
272 for( i = 0; i < p_item->i_children; i++ )
274 xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
277 fprintf( p_file, "\t\t</vlc:node>\n" );
282 /* print leaf and increase the counter */
283 fprintf( p_file, "\t\t\t<vlc:item tid=\"%i\" />\n", *p_i_count );