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