]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[vlc] / modules / misc / playlist / xspf.c
1 /******************************************************************************
2  * Copyright (C) 2006 Daniel Stränger <vlc at schmaller dot de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17  *******************************************************************************/
18
19 /**
20  * \file modules/misc/playlist/xspf.c
21  * \brief XSPF playlist export functions
22  */
23 #include <stdio.h>
24 #include <vlc/vlc.h>
25 #include <vlc/intf.h>
26 #include "vlc_meta.h"
27 #include "vlc_strings.h"
28 #include "xspf.h"
29
30 /**
31  * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
32  * and closes the open xml elements
33  * \param p_this the VLC playlist object
34  * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
35  */
36 int E_(xspf_export_playlist)( vlc_object_t *p_this )
37 {
38     const playlist_t *p_playlist = (playlist_t *)p_this;
39     const playlist_export_t *p_export =
40         (playlist_export_t *)p_playlist->p_private;
41     int              i;
42     char             *psz;
43     char             *psz_temp;
44     playlist_item_t **pp_items = NULL;
45     int               i_size;
46     playlist_item_t  *p_node;
47
48     /* write XSPF XML header - since we don't use <extension>,
49      * we get by with version 0 */
50     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
51     fprintf( p_export->p_file,
52              "<playlist version=\"0\" xmlns=\"http://xspf.org/ns/0/\">\n" );
53
54     /* save tho whole playlist or only the current node */
55 #define p_item p_playlist->status.p_item
56     if ( p_item )
57     {
58         if ( p_item->p_parent->p_input->i_type == ITEM_TYPE_PLAYLIST )
59         {
60             /* set the current node and its children */
61             p_node   = p_item->p_parent;
62             pp_items = p_node->pp_children;
63             i_size   = p_node->i_children;
64 #undef p_item
65
66             /* save name of the playlist node */
67             psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
68             if ( *psz_temp )
69                 fprintf(  p_export->p_file, "\t<title>%s</title>\n",
70                           psz_temp );
71             free( psz_temp );
72
73             /* save the creator of the playlist node */
74             psz = p_node->p_input->p_meta->psz_artist ? 
75                         strdup( p_node->p_input->p_meta->psz_artist ):
76                         strdup( "" );
77             if ( psz && !*psz )
78             {
79                 free ( psz );
80                 psz = NULL;
81             }
82
83             if ( !psz )
84                psz = p_node->p_input->p_meta->psz_author ? 
85                             strdup( p_node->p_input->p_meta->psz_author ):
86                             strdup( "" );
87
88             psz_temp = convert_xml_special_chars( psz );
89
90             if ( psz ) free( psz );
91             if ( *psz_temp )
92                 fprintf(  p_export->p_file, "\t<creator>%s</creator>\n",
93                           psz_temp );
94             free( psz_temp );
95
96             /* save location of the playlist node */
97             psz = assertUTF8URI( p_export->psz_filename );
98             if ( psz && *psz )
99             {
100                 fprintf( p_export->p_file, "\t<location>%s</location>\n",
101                          psz );
102                 free( psz );
103             }
104         }
105     }
106
107     /* prepare all the playlist children for export */
108     if ( !pp_items )
109     {
110         pp_items = p_playlist->pp_items;
111         i_size   = p_playlist->i_size;
112     }
113
114     /* export all items */
115     fprintf( p_export->p_file, "\t<trackList>\n" );
116     for ( i = 0; i < i_size; i++ )
117     {
118         xspf_export_item( pp_items[i], p_export->p_file );
119     }
120
121     /* close the header elements */
122     fprintf( p_export->p_file, "\t</trackList>\n" );
123     fprintf( p_export->p_file, "</playlist>\n" );
124
125     return VLC_SUCCESS;
126 }
127
128 /**
129  * \brief exports one item to file or traverse if item is a node
130  * \param p_item playlist item to export
131  * \param p_file file to write xml-converted item to
132  */
133 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file )
134 {
135     int i;       /**< iterator for all children if the current item is a node */
136     char *psz;
137     char *psz_temp;
138
139     if ( !p_item )
140         return;
141
142     /** \todo only "flat" playlists supported at this time.
143      *  extend to save the tree structure.
144      */
145     /* if we get a node here, we must traverse it */
146     if ( p_item->i_children > 0 )
147     {
148         for ( i = 0; i < p_item->i_children; i++ )
149         {
150             xspf_export_item( p_item->pp_children[i], p_file );
151         }
152         return;
153     }
154
155     /* leaves can be written directly */
156     fprintf( p_file, "\t\t<track>\n" );
157
158     /* -> the location */
159     if ( p_item->p_input->psz_uri && *p_item->p_input->psz_uri )
160     {
161         psz = assertUTF8URI( p_item->p_input->psz_uri );
162         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
163         free( psz );
164     }
165
166     /* -> the name/title (only if different from uri)*/
167     if ( p_item->p_input->psz_name &&
168          p_item->p_input->psz_uri &&
169          strcmp( p_item->p_input->psz_uri, p_item->p_input->psz_name ) )
170     {
171         psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
172         if ( *psz_temp )
173             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
174         free( psz_temp );
175     }
176
177     /* -> the artist/creator */
178     psz = p_item->p_input->p_meta->psz_artist ? 
179                         strdup( p_item->p_input->p_meta->psz_artist ):
180                         strdup( "" );
181     if ( psz && !*psz )
182     {
183         free ( psz );
184         psz = NULL;
185     }
186     if ( !psz )
187         psz = p_item->p_input->p_meta->psz_author ? 
188                         strdup( p_item->p_input->p_meta->psz_author ):
189                         strdup( "" );
190     psz_temp = convert_xml_special_chars( psz );
191     if ( psz ) free( psz );
192     if ( *psz_temp )
193         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
194     free( psz_temp );
195
196     /* -> the album */
197     psz = p_item->p_input->p_meta->psz_album ?
198                         strdup( p_item->p_input->p_meta->psz_album ):
199                         strdup( "" );
200     psz_temp = convert_xml_special_chars( psz );
201     if ( psz ) free( psz );
202     if ( *psz_temp )
203         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
204     free( psz_temp );
205
206     /* -> the track number */
207     psz = p_item->p_input->p_meta->psz_tracknum ?
208                         strdup( p_item->p_input->p_meta->psz_tracknum ):
209                         strdup( "" );
210     if ( psz )
211     {
212         if ( *psz )
213             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", atoi( psz ) );
214         free( psz );
215     }
216
217     /* -> the duration */
218     if ( p_item->p_input->i_duration > 0 )
219     {
220         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
221                  (long)(p_item->p_input->i_duration / 1000) );
222     }
223
224     fprintf( p_file, "\t\t</track>\n" );
225
226     return;
227 }
228
229 /**
230  * \param psz_name the location of the media ressource (e.g. local file,
231  *        device, network stream, etc.)
232  * \return a new char buffer which asserts that the location is valid UTF-8
233  *         and a valid URI
234  * \note the returned buffer must be freed, when it isn't used anymore
235  */
236 static char *assertUTF8URI( char *psz_name )
237 {
238     char *psz_ret = NULL;              /**< the new result buffer to return */
239     char *psz_s = NULL, *psz_d = NULL; /**< src & dest pointers for URI conversion */
240     vlc_bool_t b_name_is_uri = VLC_FALSE;
241
242     if ( !psz_name || !*psz_name )
243         return NULL;
244
245     /* check that string is valid UTF-8 */
246     /* XXX: Why do we even need to do that ? (all strings in core are UTF-8 encoded */
247     if( !( psz_s = EnsureUTF8( psz_name ) ) )
248         return NULL;
249
250     /* max. 3x for URI conversion (percent escaping) and
251        8 bytes for "file://" and NULL-termination */
252     psz_ret = (char *)malloc( sizeof(char)*strlen(psz_name)*6*3+8 );
253     if ( !psz_ret )
254         return NULL;
255
256     /** \todo check for a valid scheme part preceding the colon */
257     if ( strchr( psz_s, ':' ) )
258     {
259         psz_d = psz_ret;
260         b_name_is_uri = VLC_TRUE;
261     }
262     /* assume "file" scheme if no scheme-part is included */
263     else
264     {
265         strcpy( psz_ret, "file://" );
266         psz_d = psz_ret + 7;
267     }
268
269     while ( *psz_s )
270     {
271         /* percent-encode all non-ASCII and the XML special characters and the percent sign itself */
272         if ( *psz_s & B10000000 ||
273              *psz_s == '<' ||
274              *psz_s == '>' ||
275              *psz_s == '&' ||
276              *psz_s == ' ' ||
277              ( *psz_s == '%' && !b_name_is_uri ) )
278         {
279             *psz_d++ = '%';
280             *psz_d++ = hexchars[(*psz_s >> 4) & B00001111];
281             *psz_d++ = hexchars[*psz_s & B00001111];
282         } else
283             *psz_d++ = *psz_s;
284
285         psz_s++;
286     }
287     *psz_d = '\0';
288
289     return (char *)realloc( psz_ret, sizeof(char)*strlen( psz_ret ) + 1 );
290 }