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