]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
* xpsf.c: ...but <playlist> has... grmbl
[vlc] / modules / misc / playlist / xspf.c
1 /******************************************************************************
2  * xspf.c : XSPF playlist export functions
3  ******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
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.
14  *
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.
19  *
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  *******************************************************************************/
24
25 /**
26  * \file modules/misc/playlist/xspf.c
27  * \brief XSPF playlist export functions
28  */
29 #include <stdio.h>
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include "vlc_meta.h"
33 #include "vlc_strings.h"
34 #include "xspf.h"
35
36 /**
37  * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
38  * and closes the open xml elements
39  * \param p_this the VLC playlist object
40  * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
41  */
42 int E_(xspf_export_playlist)( vlc_object_t *p_this )
43 {
44     const playlist_t *p_playlist = (playlist_t *)p_this;
45     const playlist_export_t *p_export =
46         (playlist_export_t *)p_playlist->p_private;
47     int               i, i_count;
48     char             *psz_temp;
49     playlist_item_t  *p_node = p_export->p_root;
50
51     /* write XSPF XML header */
52     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
53     fprintf( p_export->p_file,
54              "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n" );
55
56     if( !p_node ) return VLC_SUCCESS;
57
58     /* save name of the playlist node */
59     psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
60     if( *psz_temp )
61     {
62         fprintf(  p_export->p_file, "\t<title>%s</title>\n", psz_temp );
63     }
64     free( psz_temp );
65
66     /* save location of the playlist node */
67     psz_temp = assertUTF8URI( p_export->psz_filename );
68     if( psz_temp && *psz_temp )
69     {
70         fprintf( p_export->p_file, "\t<location>%s</location>\n", psz_temp );
71         free( psz_temp );
72     }
73
74     /* export all items in a flat format */
75     fprintf( p_export->p_file, "\t<trackList>\n" );
76     i_count = 0;
77     for( i = 0; i < p_node->i_children; i++ )
78     {
79         xspf_export_item( p_node->pp_children[i], p_export->p_file,
80                           &i_count );
81     }
82     fprintf( p_export->p_file, "\t</trackList>\n" );
83
84     /* export the tree structure in <extension> */
85     fprintf( p_export->p_file, "\t<extension>\n" );
86     i_count = 0;
87     for( i = 0; i < p_node->i_children; i++ )
88     {
89         xspf_extension_item( p_node->pp_children[i], p_export->p_file,
90                              &i_count );
91     }
92     fprintf( p_export->p_file, "\t</extension>\n" );
93
94     /* close the header elements */
95     fprintf( p_export->p_file, "</playlist>\n" );
96
97     return VLC_SUCCESS;
98 }
99
100 /**
101  * \brief exports one item to file or traverse if item is a node
102  * \param p_item playlist item to export
103  * \param p_file file to write xml-converted item to
104  * \param p_i_count counter for track identifiers
105  */
106 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
107                               int *p_i_count )
108 {
109     char *psz;
110     char *psz_temp;
111
112     if( !p_item ) return;
113
114     /* if we get a node here, we must traverse it */
115     if( p_item->i_children > 0 )
116     {
117         int i;
118         for( i = 0; i < p_item->i_children; i++ )
119         {
120             xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
121         }
122         return;
123     }
124
125     /* leaves can be written directly */
126     fprintf( p_file, "\t\t<track>\n" );
127
128     /* print identifier and increase the counter */
129     fprintf( p_file, "\t\t\t<identifier>%d</identifier>\n", *p_i_count );
130     ( *p_i_count )++;
131
132     /* -> the location */
133     if( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
134     {
135         psz = assertUTF8URI( p_item->p_input->psz_uri );
136         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
137         free( psz );
138     }
139
140     /* -> the name/title (only if different from uri)*/
141     if( p_item->p_input->psz_name &&
142         p_item->p_input->psz_uri &&
143         strcmp( p_item->p_input->psz_uri, p_item->p_input->psz_name ) )
144     {
145         psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
146         if( *psz_temp )
147             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
148         free( psz_temp );
149     }
150
151     if( p_item->p_input->p_meta == NULL )
152     {
153         goto xspfexportitem_end;
154     }
155
156     /* -> the artist/creator */
157     psz = p_item->p_input->p_meta->psz_artist ?
158                         strdup( p_item->p_input->p_meta->psz_artist ):
159                         strdup( "" );
160     if( psz && !*psz )
161     {
162         free( psz );
163         psz = NULL;
164     }
165     if( !psz )
166     {
167         psz = p_item->p_input->p_meta->psz_author ?
168                         strdup( p_item->p_input->p_meta->psz_author ):
169                         strdup( "" );
170     }
171     psz_temp = convert_xml_special_chars( psz );
172     if( psz ) free( psz );
173     if( *psz_temp )
174     {
175         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
176     }
177     free( psz_temp );
178
179     /* -> the album */
180     psz = p_item->p_input->p_meta->psz_album ?
181                         strdup( p_item->p_input->p_meta->psz_album ):
182                         strdup( "" );
183     psz_temp = convert_xml_special_chars( psz );
184     if( psz ) free( psz );
185     if( *psz_temp )
186     {
187         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
188     }
189     free( psz_temp );
190
191     /* -> the track number */
192     psz = p_item->p_input->p_meta->psz_tracknum ?
193                         strdup( p_item->p_input->p_meta->psz_tracknum ):
194                         strdup( "" );
195     if( psz )
196     {
197         if( *psz )
198         {
199             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
200         }
201         free( psz );
202     }
203
204 xspfexportitem_end:
205     /* -> the duration */
206     if( p_item->p_input->i_duration > 0 )
207     {
208         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
209                  (long)(p_item->p_input->i_duration / 1000) );
210     }
211
212     fprintf( p_file, "\t\t</track>\n" );
213
214     return;
215 }
216
217 /**
218  * \brief exports one item in extension to file and traverse if item is a node
219  * \param p_item playlist item to export
220  * \param p_file file to write xml-converted item to
221  * \param p_i_count counter for track identifiers
222  */
223 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
224                                  int *p_i_count )
225 {
226     if( !p_item ) return;
227
228     /* if we get a node here, we must traverse it */
229     if( p_item->i_children > 0 )
230     {
231         int i;
232
233         fprintf( p_file, "\t\t<node>\n" );
234         fprintf( p_file, "\t\t\t<title>%s</title>\n",
235                  p_item->p_input->psz_name );
236
237         for( i = 0; i < p_item->i_children; i++ )
238         {
239             xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
240         }
241
242         fprintf( p_file, "\t\t</node>\n" );
243         return;
244     }
245
246
247     /* print leaf and increase the counter */
248     fprintf( p_file, "\t\t\t<item href=\"%d\" />\n", *p_i_count );
249     ( *p_i_count )++;
250
251     return;
252 }
253
254 /**
255  * \param psz_name the location of the media ressource (e.g. local file,
256  *        device, network stream, etc.)
257  * \return a new char buffer which asserts that the location is valid UTF-8
258  *         and a valid URI
259  * \note the returned buffer must be freed, when it isn't used anymore
260  */
261 static char *assertUTF8URI( char *psz_name )
262 {
263     char *psz_ret = NULL;              /**< the new result buffer to return */
264     char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
265     vlc_bool_t b_name_is_uri = VLC_FALSE;
266
267     if( !psz_name || !*psz_name )
268         return NULL;
269
270     /* check that string is valid UTF-8 */
271     /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
272     if( !( psz_s = EnsureUTF8( psz_name ) ) )
273         return NULL;
274
275     /* max. 3x for URI conversion (percent escaping) and
276        8 bytes for "file://" and NULL-termination */
277     psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
278     if( !psz_ret )
279         return NULL;
280
281     /** \todo check for a valid scheme part preceding the colon */
282     if( strchr( psz_s, ':' ) )
283     {
284         psz_d = psz_ret;
285         b_name_is_uri = VLC_TRUE;
286     }
287     /* assume "file" scheme if no scheme-part is included */
288     else
289     {
290         strcpy( psz_ret, "file://" );
291         psz_d = psz_ret + 7;
292     }
293
294     while( *psz_s )
295     {
296         /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
297         if( *psz_s & B10000000 ||
298             *psz_s == '<' ||
299             *psz_s == '>' ||
300             *psz_s == '&' ||
301             *psz_s == ' ' ||
302             ( *psz_s == '%' && !b_name_is_uri ) )
303         {
304             *psz_d++ = '%';
305             *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
306             *psz_d++ = hexchars[*psz_s & B00001111];
307         }
308         else
309         {
310             *psz_d++ = *psz_s;
311         }
312
313         psz_s++;
314     }
315     *psz_d = '\0';
316
317     return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );
318 }