]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
0f5f82c5c06b2ced38876759c4785071cd762a6a
[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_t *p_playlist = (playlist_t *)p_this;
54     const playlist_export_t *p_export =
55         (playlist_export_t *)p_playlist->p_private;
56     int               i, i_count;
57     char             *psz_temp;
58     playlist_item_t  *p_node = p_export->p_root;
59
60     /* write XSPF XML header */
61     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
62     fprintf( p_export->p_file,
63              "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" " \
64              "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n" );
65
66     if( !p_node ) return VLC_SUCCESS;
67
68     /* save name of the playlist node */
69     psz_temp = convert_xml_special_chars( p_node->p_input->psz_name );
70     if( *psz_temp )
71     {
72         fprintf(  p_export->p_file, "\t<title>%s</title>\n", psz_temp );
73     }
74     free( psz_temp );
75
76     /* export all items in a flat format */
77     fprintf( p_export->p_file, "\t<trackList>\n" );
78     i_count = 0;
79     for( i = 0; i < p_node->i_children; i++ )
80     {
81         xspf_export_item( p_node->pp_children[i], p_export->p_file,
82                           &i_count );
83     }
84     fprintf( p_export->p_file, "\t</trackList>\n" );
85
86     /* export the tree structure in <extension> */
87     fprintf( p_export->p_file, "\t<extension application=\"" \
88              "http://www.videolan.org/vlc/playlist/0\">\n" );
89     i_count = 0;
90     for( i = 0; i < p_node->i_children; i++ )
91     {
92         xspf_extension_item( p_node->pp_children[i], p_export->p_file,
93                              &i_count );
94     }
95     fprintf( p_export->p_file, "\t</extension>\n" );
96
97     /* close the header elements */
98     fprintf( p_export->p_file, "</playlist>\n" );
99
100     return VLC_SUCCESS;
101 }
102
103 /**
104  * \brief exports one item to file or traverse if item is a node
105  * \param p_item playlist item to export
106  * \param p_file file to write xml-converted item to
107  * \param p_i_count counter for track identifiers
108  */
109 static void xspf_export_item( playlist_item_t *p_item, FILE *p_file,
110                               int *p_i_count )
111 {
112     char *psz;
113     char *psz_temp;
114     int i;
115     mtime_t i_duration;
116
117     if( !p_item ) return;
118
119     /* if we get a node here, we must traverse it */
120     if( p_item->i_children > 0 )
121     {
122         int i;
123         for( i = 0; i < p_item->i_children; i++ )
124         {
125             xspf_export_item( p_item->pp_children[i], p_file, p_i_count );
126         }
127         return;
128     }
129
130     /* don't write empty nodes */
131     if( p_item->i_children == 0 )
132     {
133         return;
134     }
135
136     /* leaves can be written directly */
137     fprintf( p_file, "\t\t<track>\n" );
138
139     /* -> the location */
140
141     char *psz_uri = input_item_GetURI( p_item->p_input );
142
143     if( psz_uri && *psz_uri )
144     {
145         psz = make_URI( psz_uri );
146         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz );
147         free( psz );
148     }
149
150     /* -> the name/title (only if different from uri)*/
151     char *psz_name = input_item_GetTitle( p_item->p_input );
152     if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
153     {
154         psz_temp = convert_xml_special_chars( psz_name );
155         if( *psz_temp )
156             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
157         free( psz_temp );
158     }
159     free( psz_name );
160     free( psz_uri );
161
162     if( p_item->p_input->p_meta == NULL )
163     {
164         goto xspfexportitem_end;
165     }
166
167     /* -> the artist/creator */
168     psz = input_item_GetArtist( p_item->p_input );
169     if( psz == NULL ) psz = strdup( "" );
170     psz_temp = convert_xml_special_chars( psz );
171     free( psz );
172     if( *psz_temp )
173     {
174         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
175     }
176     free( psz_temp );
177
178     /* -> the album */
179     psz = input_item_GetAlbum( p_item->p_input );
180     if( psz == NULL ) psz = strdup( "" );
181     psz_temp = convert_xml_special_chars( psz );
182     free( psz );
183     if( *psz_temp )
184     {
185         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
186     }
187     free( psz_temp );
188
189     /* -> the track number */
190     psz = input_item_GetTrackNum( p_item->p_input );
191     if( psz == NULL ) psz = strdup( "" );
192     if( psz && *psz )
193     {
194         int i_tracknum = atoi( psz );
195         if( i_tracknum > 0 )
196             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
197     }
198     free( psz );
199
200     /* -> the description */
201     psz = input_item_GetDescription( p_item->p_input );
202     if( psz == NULL ) psz = strdup( "" );
203     psz_temp = convert_xml_special_chars( psz );
204     free( psz );
205     if( *psz_temp )
206     {
207         fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
208     }
209     free( psz_temp );
210
211     psz = input_item_GetArtURL( p_item->p_input );
212     if( psz == NULL ) psz = strdup( "" );
213     if( !EMPTY_STR( psz ) )
214     {
215         psz_uri = make_URI( psz );
216         fprintf( p_file, "\t\t\t<image>%s</image>\n", psz_uri );
217         free( psz_uri );
218     }
219     free( psz );
220
221 xspfexportitem_end:
222     /* -> the duration */
223     i_duration = input_item_GetDuration( p_item->p_input );
224     if( i_duration > 0 )
225     {
226         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
227                  (long)(i_duration / 1000) );
228     }
229
230     /* export the intenal id and the input's options (bookmarks, ...)
231      * in <extension> */
232     fprintf( p_file, "\t\t\t<extension application=\"" \
233              "http://www.videolan.org/vlc/playlist/0\">\n" );
234
235     /* print the id and increase the counter */
236     fprintf( p_file, "\t\t\t\t<vlc:id>%i</vlc:id>\n", *p_i_count );
237     ( *p_i_count )++;
238
239     for( i = 0; i < p_item->p_input->i_options; i++ )
240     {
241         fprintf( p_file, "\t\t\t\t<vlc:option>%s</vlc:option>\n",
242                  p_item->p_input->ppsz_options[i][0] == ':' ?
243                  p_item->p_input->ppsz_options[i] + 1 :
244                  p_item->p_input->ppsz_options[i] );
245     }
246     fprintf( p_file, "\t\t\t</extension>\n" );
247
248     fprintf( p_file, "\t\t</track>\n" );
249
250     return;
251 }
252
253 /**
254  * \brief exports one item in extension to file and traverse if item is a node
255  * \param p_item playlist item to export
256  * \param p_file file to write xml-converted item to
257  * \param p_i_count counter for track identifiers
258  */
259 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
260                                  int *p_i_count )
261 {
262     if( !p_item ) return;
263
264     /* if we get a node here, we must traverse it */
265     if( p_item->i_children >= 0 )
266     {
267         int i;
268         char *psz_temp;
269         psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
270         fprintf( p_file, "\t\t<vlc:node title=\"%s\">\n",
271                  *psz_temp ? psz_temp : "" );
272         free( psz_temp );
273
274         for( i = 0; i < p_item->i_children; i++ )
275         {
276             xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
277         }
278
279         fprintf( p_file, "\t\t</vlc:node>\n" );
280         return;
281     }
282
283
284     /* print leaf and increase the counter */
285     fprintf( p_file, "\t\t\t<vlc:item tid=\"%i\" />\n", *p_i_count );
286     ( *p_i_count )++;
287
288     return;
289 }