]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
Merge branch 'master' into lpcm_encoder
[vlc] / modules / misc / playlist / xspf.c
1 /******************************************************************************
2  * xspf.c : XSPF playlist export functions
3  ******************************************************************************
4  * Copyright (C) 2006-2009 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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_playlist.h>
35 #include <vlc_input.h>
36 #include <vlc_strings.h>
37 #include <vlc_url.h>
38 #include "xspf.h"
39
40 #include <assert.h>
41
42 static void xspf_export_item( playlist_item_t *, FILE *, int * );
43 static void xspf_extension_item( playlist_item_t *, FILE *, int * );
44
45 /**
46  * \brief Prints the XSPF header to file, writes each item by xspf_export_item()
47  * and closes the open xml elements
48  * \param p_this the VLC playlist object
49  * \return VLC_SUCCESS if some memory is available, otherwise VLC_ENONMEM
50  */
51 int xspf_export_playlist( vlc_object_t *p_this )
52 {
53     const playlist_export_t *p_export = (playlist_export_t *)p_this;
54     int               i, i_count;
55     char             *psz_temp;
56     playlist_item_t  *p_node = p_export->p_root;
57
58     /* write XSPF XML header */
59     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
60     fprintf( p_export->p_file,
61              "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" " \
62              "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n" );
63
64     if( !p_node ) return VLC_SUCCESS;
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     {
70         fprintf(  p_export->p_file, "\t<title>%s</title>\n", psz_temp );
71     }
72     free( psz_temp );
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 application=\"" \
86              "http://www.videolan.org/vlc/playlist/0\">\n" );
87     i_count = 0;
88     for( i = 0; i < p_node->i_children; i++ )
89     {
90         xspf_extension_item( p_node->pp_children[i], p_export->p_file,
91                              &i_count );
92     }
93     fprintf( p_export->p_file, "\t</extension>\n" );
94
95     /* close the header elements */
96     fprintf( p_export->p_file, "</playlist>\n" );
97
98     return VLC_SUCCESS;
99 }
100
101 static char *input_xml( input_item_t *p_item, char *(*func)(input_item_t *) )
102 {
103     char *tmp = func( p_item );
104     if( tmp == NULL )
105         return NULL;
106     char *ret = convert_xml_special_chars( tmp );
107     free( tmp );
108     return ret;
109 }
110
111 /**
112  * \brief exports one item to file or traverse if item is a node
113  * \param p_item playlist item to export
114  * \param p_file file to write xml-converted item to
115  * \param p_i_count counter for track identifiers
116  */
117 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
118                               int *p_i_count )
119 {
120     if( !p_item ) return;
121
122     /* if we get a node here, we must traverse it */
123     if( p_item->i_children > 0 )
124     {
125         for( int i = 0; i < p_item->i_children; i++ )
126             xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
127         return;
128     }
129
130     /* don't write empty nodes */
131     if( p_item->i_children == 0 )
132         return;
133
134     input_item_t *p_input = p_item->p_input;
135     char *psz;
136     mtime_t i_duration;
137
138     /* leaves can be written directly */
139     fputs( "\t\t<track>\n", p_file );
140
141     /* -> the location */
142
143     char *psz_uri = input_xml( p_input, input_item_GetURI );
144     if( psz_uri && *psz_uri )
145         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz_uri );
146
147     /* -> the name/title (only if different from uri)*/
148     psz = input_xml( p_input, input_item_GetTitle );
149     if( psz && strcmp( psz_uri, psz ) )
150         fprintf( p_file, "\t\t\t<title>%s</title>\n", psz );
151     free( psz );
152     free( psz_uri );
153
154     if( p_item->p_input->p_meta == NULL )
155     {
156         goto xspfexportitem_end;
157     }
158
159     /* -> the artist/creator */
160     psz = input_xml( p_input, input_item_GetArtist );
161     if( psz && *psz )
162         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz );
163     free( psz );
164
165     /* -> the album */
166     psz = input_xml( p_input, input_item_GetAlbum );
167     if( psz && *psz )
168         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz );
169     free( psz );
170
171     /* -> the track number */
172     psz = input_xml( p_input, input_item_GetTrackNum );
173     if( psz )
174     {
175         int i_tracknum = atoi( psz );
176
177         free( psz );
178         if( i_tracknum > 0 )
179             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
180     }
181
182     /* -> the description */
183     psz = input_xml( p_input, input_item_GetDescription );
184     if( psz && *psz )
185         fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz );
186     free( psz );
187
188     psz = input_xml( p_input, input_item_GetArtURL );
189     if( psz && *psz )
190         fprintf( p_file, "\t\t\t<image>%s</image>\n", psz );
191     free( psz );
192
193 xspfexportitem_end:
194     /* -> the duration */
195     i_duration = input_item_GetDuration( p_item->p_input );
196     if( i_duration > 0 )
197         fprintf( p_file, "\t\t\t<duration>%"PRIu64"</duration>\n",
198                  i_duration / 1000 );
199
200     /* export the intenal id and the input's options (bookmarks, ...)
201      * in <extension> */
202     fputs( "\t\t\t<extension application=\""
203            "http://www.videolan.org/vlc/playlist/0\">\n", p_file );
204
205     /* print the id and increase the counter */
206     fprintf( p_file, "\t\t\t\t<vlc:id>%i</vlc:id>\n", *p_i_count );
207     ( *p_i_count )++;
208
209     for( int i = 0; i < p_item->p_input->i_options; i++ )
210     {
211         char* psz_src = p_item->p_input->ppsz_options[i];
212         char* psz_ret = NULL;
213
214         if ( psz_src[0] == ':' )
215             psz_src++;
216
217         psz_ret = convert_xml_special_chars( psz_src );
218         if ( psz_ret == NULL )
219             continue;
220
221         fprintf( p_file, "\t\t\t\t<vlc:option>%s</vlc:option>\n", psz_ret );
222         free( psz_ret );
223     }
224     fputs( "\t\t\t</extension>\n", p_file );
225     fputs( "\t\t</track>\n", p_file );
226 }
227
228 /**
229  * \brief exports one item in extension to file and traverse if item is a node
230  * \param p_item playlist item to export
231  * \param p_file file to write xml-converted item to
232  * \param p_i_count counter for track identifiers
233  */
234 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
235                                  int *p_i_count )
236 {
237     if( !p_item ) return;
238
239     /* if we get a node here, we must traverse it */
240     if( p_item->i_children >= 0 )
241     {
242         int i;
243         char *psz_temp = NULL;
244         if( p_item->p_input->psz_name )
245             psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
246         fprintf( p_file, "\t\t<vlc:node title=\"%s\">\n",
247                  psz_temp ? psz_temp : "" );
248         free( psz_temp );
249
250         for( i = 0; i < p_item->i_children; i++ )
251         {
252             xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
253         }
254
255         fprintf( p_file, "\t\t</vlc:node>\n" );
256         return;
257     }
258
259
260     /* print leaf and increase the counter */
261     fprintf( p_file, "\t\t\t<vlc:item tid=\"%i\" />\n", *p_i_count );
262     ( *p_i_count )++;
263
264     return;
265 }