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