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