]> git.sesse.net Git - vlc/blob - modules/misc/playlist/xspf.c
6360931e9a25d87dcf63c18458edd43b64e9d7fc
[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         fprintf( p_file, "\t\t\t<location>%s</location>\n", psz_uri );
145
146     /* -> the name/title (only if different from uri)*/
147     char *psz_name = input_item_GetTitle( p_item->p_input );
148     if( psz_name && psz_uri && strcmp( psz_uri, psz_name ) )
149     {
150         psz_temp = convert_xml_special_chars( psz_name );
151         if( *psz_temp )
152             fprintf( p_file, "\t\t\t<title>%s</title>\n", psz_temp );
153         free( psz_temp );
154     }
155     free( psz_name );
156     free( psz_uri );
157
158     if( p_item->p_input->p_meta == NULL )
159     {
160         goto xspfexportitem_end;
161     }
162
163     /* -> the artist/creator */
164     psz = input_item_GetArtist( p_item->p_input );
165     if( psz == NULL ) psz = strdup( "" );
166     psz_temp = convert_xml_special_chars( psz );
167     free( psz );
168     if( *psz_temp )
169     {
170         fprintf( p_file, "\t\t\t<creator>%s</creator>\n", psz_temp );
171     }
172     free( psz_temp );
173
174     /* -> the album */
175     psz = input_item_GetAlbum( p_item->p_input );
176     if( psz == NULL ) psz = strdup( "" );
177     psz_temp = convert_xml_special_chars( psz );
178     free( psz );
179     if( *psz_temp )
180     {
181         fprintf( p_file, "\t\t\t<album>%s</album>\n", psz_temp );
182     }
183     free( psz_temp );
184
185     /* -> the track number */
186     psz = input_item_GetTrackNum( p_item->p_input );
187     if( psz == NULL ) psz = strdup( "" );
188     if( psz && *psz )
189     {
190         int i_tracknum = atoi( psz );
191         if( i_tracknum > 0 )
192             fprintf( p_file, "\t\t\t<trackNum>%i</trackNum>\n", i_tracknum );
193     }
194     free( psz );
195
196     /* -> the description */
197     psz = input_item_GetDescription( p_item->p_input );
198     if( psz == NULL ) psz = strdup( "" );
199     psz_temp = convert_xml_special_chars( psz );
200     free( psz );
201     if( *psz_temp )
202     {
203         fprintf( p_file, "\t\t\t<annotation>%s</annotation>\n", psz_temp );
204     }
205     free( psz_temp );
206
207     psz = input_item_GetArtURL( p_item->p_input );
208     if( psz == NULL ) psz = strdup( "" );
209     if( !EMPTY_STR( psz ) )
210     {
211         fprintf( p_file, "\t\t\t<image>%s</image>\n", psz );
212     }
213     free( psz );
214
215 xspfexportitem_end:
216     /* -> the duration */
217     i_duration = input_item_GetDuration( p_item->p_input );
218     if( i_duration > 0 )
219     {
220         fprintf( p_file, "\t\t\t<duration>%ld</duration>\n",
221                  (long)(i_duration / 1000) );
222     }
223
224     /* export the intenal id and the input's options (bookmarks, ...)
225      * in <extension> */
226     fprintf( p_file, "\t\t\t<extension application=\"" \
227              "http://www.videolan.org/vlc/playlist/0\">\n" );
228
229     /* print the id and increase the counter */
230     fprintf( p_file, "\t\t\t\t<vlc:id>%i</vlc:id>\n", *p_i_count );
231     ( *p_i_count )++;
232
233     for( i = 0; i < p_item->p_input->i_options; i++ )
234     {
235         fprintf( p_file, "\t\t\t\t<vlc:option>%s</vlc:option>\n",
236                  p_item->p_input->ppsz_options[i][0] == ':' ?
237                  p_item->p_input->ppsz_options[i] + 1 :
238                  p_item->p_input->ppsz_options[i] );
239     }
240     fprintf( p_file, "\t\t\t</extension>\n" );
241
242     fprintf( p_file, "\t\t</track>\n" );
243
244     return;
245 }
246
247 /**
248  * \brief exports one item in extension to file and traverse if item is a node
249  * \param p_item playlist item to export
250  * \param p_file file to write xml-converted item to
251  * \param p_i_count counter for track identifiers
252  */
253 static void xspf_extension_item( playlist_item_t *p_item, FILE *p_file,
254                                  int *p_i_count )
255 {
256     if( !p_item ) return;
257
258     /* if we get a node here, we must traverse it */
259     if( p_item->i_children >= 0 )
260     {
261         int i;
262         char *psz_temp = NULL;
263         if( p_item->p_input->psz_name )
264             psz_temp = convert_xml_special_chars( p_item->p_input->psz_name );
265         fprintf( p_file, "\t\t<vlc:node title=\"%s\">\n",
266                  psz_temp ? psz_temp : "" );
267         free( psz_temp );
268
269         for( i = 0; i < p_item->i_children; i++ )
270         {
271             xspf_extension_item( p_item->pp_children[i], p_file, p_i_count );
272         }
273
274         fprintf( p_file, "\t\t</vlc:node>\n" );
275         return;
276     }
277
278
279     /* print leaf and increase the counter */
280     fprintf( p_file, "\t\t\t<vlc:item tid=\"%i\" />\n", *p_i_count );
281     ( *p_i_count )++;
282
283     return;
284 }