demux/playlist/ : removes useless unused parameter warnings
[vlc.git] / modules / demux / playlist / podcast.c
1 /*****************************************************************************
2  * podcast.c : podcast playlist imports
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_demux.h>
33
34 #include "playlist.h"
35 #include "vlc_xml.h"
36
37 struct demux_sys_t
38 {
39     char *psz_prefix;
40     playlist_t *p_playlist;
41     xml_t *p_xml;
42     xml_reader_t *p_xml_reader;
43 };
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int Demux( demux_t *p_demux);
49 static int Control( demux_t *p_demux, int i_query, va_list args );
50
51 /*****************************************************************************
52  * Import_podcast: main import function
53  *****************************************************************************/
54 int E_(Import_podcast)( vlc_object_t *p_this )
55 {
56     demux_t *p_demux = (demux_t *)p_this;
57
58     if( !demux2_IsForced( p_demux, "podcast" ) )
59         return VLC_EGENERIC;
60
61     STANDARD_DEMUX_INIT_MSG( "using podcast reader" );
62     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
63     p_demux->p_sys->p_playlist = NULL;
64     p_demux->p_sys->p_xml = NULL;
65     p_demux->p_sys->p_xml_reader = NULL;
66
67     return VLC_SUCCESS;
68 }
69
70 /*****************************************************************************
71  * Deactivate: frees unused data
72  *****************************************************************************/
73 void E_(Close_podcast)( vlc_object_t *p_this )
74 {
75     demux_t *p_demux = (demux_t *)p_this;
76     demux_sys_t *p_sys = p_demux->p_sys;
77
78     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
79     if( p_sys->p_playlist ) vlc_object_release( p_sys->p_playlist );
80     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
81     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
82     free( p_sys );
83 }
84
85 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
86 static int Demux( demux_t *p_demux )
87 {
88     demux_sys_t *p_sys = p_demux->p_sys;
89
90     vlc_bool_t b_item = VLC_FALSE;
91     vlc_bool_t b_image = VLC_FALSE;
92     int i_ret;
93
94     xml_t *p_xml;
95     xml_reader_t *p_xml_reader;
96     char *psz_elname = NULL;
97     char *psz_item_mrl = NULL;
98     char *psz_item_size = NULL;
99     char *psz_item_type = NULL;
100     char *psz_item_name = NULL;
101     char *psz_item_date = NULL;
102     char *psz_item_author = NULL;
103     char *psz_item_category = NULL;
104     char *psz_item_duration = NULL;
105     char *psz_item_keywords = NULL;
106     char *psz_item_subtitle = NULL;
107     char *psz_item_summary = NULL;
108     int i_type;
109     input_item_t *p_input;
110
111     INIT_PLAYLIST_STUFF;
112
113     p_xml = p_sys->p_xml = xml_Create( p_demux );
114     if( !p_xml ) return -1;
115
116 /*    psz_elname = stream_ReadLine( p_demux->s );
117     if( psz_elname ) free( psz_elname );
118     psz_elname = 0;*/
119
120     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
121     if( !p_xml_reader ) return -1;
122     p_sys->p_xml_reader = p_xml_reader;
123
124     /* xml */
125     /* check root node */
126     if( xml_ReaderRead( p_xml_reader ) != 1 )
127     {
128         msg_Err( p_demux, "invalid file (no root node)" );
129         return -1;
130     }
131     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
132         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
133         strcmp( psz_elname, "rss" ) )
134     {
135         msg_Err( p_demux, "invalid root node %i, %s",
136                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
137         if( psz_elname ) free( psz_elname );
138         return -1;
139     }
140     free( psz_elname ); psz_elname = NULL;
141
142     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
143     {
144         // Get the node type
145         i_type = xml_ReaderNodeType( p_xml_reader );
146         switch( i_type )
147         {
148             // Error
149             case -1:
150                 return -1;
151                 break;
152
153             case XML_READER_STARTELEM:
154             {
155                 // Read the element name
156                 if( psz_elname ) free( psz_elname );
157                 psz_elname = xml_ReaderName( p_xml_reader );
158                 if( !psz_elname ) return -1;
159
160                 if( !strcmp( psz_elname, "item" ) )
161                 {
162                     b_item = VLC_TRUE;
163                 }
164                 else if( !strcmp( psz_elname, "image" ) )
165                 {
166                     b_item = VLC_TRUE;
167                 }
168
169                 // Read the attributes
170                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
171                 {
172                     char *psz_name = xml_ReaderName( p_xml_reader );
173                     char *psz_value = xml_ReaderValue( p_xml_reader );
174                     if( !psz_name || !psz_value ) return -1;
175                     if( !strcmp( psz_elname, "enclosure" ) &&
176                         !strcmp( psz_name, "url" ) )
177                     {
178                         psz_item_mrl = strdup( psz_value );
179                     }
180                     else if( !strcmp( psz_elname, "enclosure" ) &&
181                         !strcmp( psz_name, "length" ) )
182                     {
183                         psz_item_size = strdup( psz_value );
184                     }
185                     else if( !strcmp( psz_elname, "enclosure" ) &&
186                         !strcmp( psz_name, "type" ) )
187                     {
188                         psz_item_type = strdup( psz_value );
189                     }
190                     else
191                     {
192                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
193                                   psz_name, psz_elname );
194                     }
195                     free( psz_name );
196                     free( psz_value );
197                 }
198                 break;
199             }
200             case XML_READER_TEXT:
201             {
202 #define SET_DATA( field, name ) else if( b_item == VLC_TRUE \
203                 && !strcmp( psz_elname, name ) ) \
204                 { \
205                     field = strdup( psz_text ); \
206                 }
207                 char *psz_text = xml_ReaderValue( p_xml_reader );
208                 /* item specific meta data */
209                 if( b_item == VLC_TRUE && !strcmp( psz_elname, "title" ) )
210                 {
211                     psz_item_name = strdup( psz_text );
212                 }
213                 else if( b_item == VLC_TRUE
214                          && ( !strcmp( psz_elname, "itunes:author" )
215                             ||!strcmp( psz_elname, "author" ) ) )
216                 { /* <author> isn't standard iTunes podcast stuff */
217                     psz_item_author = strdup( psz_text );
218                 }
219                 else if( b_item == VLC_TRUE
220                          && ( !strcmp( psz_elname, "itunes:summary" )
221                             ||!strcmp( psz_elname, "description" ) ) )
222                 { /* <description> isn't standard iTunes podcast stuff */
223                     psz_item_summary = strdup( psz_text );
224                 }
225                 SET_DATA( psz_item_date, "pubDate" )
226                 SET_DATA( psz_item_category, "itunes:category" )
227                 SET_DATA( psz_item_duration, "itunes:duration" )
228                 SET_DATA( psz_item_keywords, "itunes:keywords" )
229                 SET_DATA( psz_item_subtitle, "itunes:subtitle" )
230                 /* toplevel meta data */
231                 else if( b_item == VLC_FALSE && b_image == VLC_FALSE
232                          && !strcmp( psz_elname, "title" ) )
233                 {
234                     input_item_SetName( p_current_input, psz_text );
235                 }
236 #define ADD_GINFO( info, name ) \
237     else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
238     { \
239         input_ItemAddInfo( p_current_input, _("Podcast Info"), \
240                                 _( info ), "%s", psz_text ); \
241     }
242                 ADD_GINFO( "Podcast Link", "link" )
243                 ADD_GINFO( "Podcast Copyright", "copyright" )
244                 ADD_GINFO( "Podcast Category", "itunes:category" )
245                 ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
246                 ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
247 #undef ADD_GINFO
248                 else if( b_item == VLC_FALSE && b_image == VLC_FALSE
249                          && ( !strcmp( psz_elname, "itunes:summary" )
250                             ||!strcmp( psz_elname, "description" ) ) )
251                 { /* <description> isn't standard iTunes podcast stuff */
252                     input_ItemAddInfo( p_current_input,
253                              _( "Podcast Info" ), _( "Podcast Summary" ),
254                              "%s", psz_text );
255                 }
256                 else
257                 {
258                     msg_Dbg( p_demux, "unhandled text in element '%s'",
259                               psz_elname );
260                 }
261                 free( psz_text );
262                 break;
263             }
264             // End element
265             case XML_READER_ENDELEM:
266             {
267                 // Read the element name
268                 free( psz_elname );
269                 psz_elname = xml_ReaderName( p_xml_reader );
270                 if( !psz_elname ) return -1;
271                 if( !strcmp( psz_elname, "item" ) )
272                 {
273                     p_input = input_ItemNewExt( p_playlist, psz_item_mrl,
274                                                 psz_item_name, 0, NULL, -1 );
275                     if( p_input == NULL ) break;
276 #define ADD_INFO( info, field ) \
277     if( field ) { input_ItemAddInfo( p_input, \
278                             _( "Podcast Info" ),  _( info ), "%s", field ); }
279                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
280                     ADD_INFO( "Podcast Author", psz_item_author );
281                     ADD_INFO( "Podcast Subcategory", psz_item_category );
282                     ADD_INFO( "Podcast Duration", psz_item_duration );
283                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
284                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
285                     ADD_INFO( "Podcast Summary", psz_item_summary );
286                     ADD_INFO( "Podcast Type", psz_item_type );
287                     if( psz_item_size )
288                     {
289                         input_ItemAddInfo( p_input,
290                                                 _( "Podcast Info" ),
291                                                 _( "Podcast Size" ),
292                                                 "%s bytes",
293                                                 psz_item_size );
294                     }
295                     input_ItemAddSubItem( p_current_input, p_input );
296                     vlc_gc_decref( p_input );
297                     FREENULL( psz_item_name );
298                     FREENULL( psz_item_mrl );
299                     FREENULL( psz_item_size );
300                     FREENULL( psz_item_type );
301                     FREENULL( psz_item_date );
302                     FREENULL( psz_item_author );
303                     FREENULL( psz_item_category );
304                     FREENULL( psz_item_duration );
305                     FREENULL( psz_item_keywords );
306                     FREENULL( psz_item_subtitle );
307                     FREENULL( psz_item_summary );
308                     b_item = VLC_FALSE;
309                 }
310                 else if( !strcmp( psz_elname, "image" ) )
311                 {
312                     b_image = VLC_FALSE;
313                 }
314                 free( psz_elname );
315                 psz_elname = strdup("");
316
317                 break;
318             }
319         }
320     }
321
322     if( i_ret != 0 )
323     {
324         msg_Warn( p_demux, "error while parsing data" );
325     }
326
327     HANDLE_PLAY_AND_RELEASE;
328     return 0; /* Needed for correct operation of go back */
329 }
330
331 static int Control( demux_t *p_demux, int i_query, va_list args )
332 {
333     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
334     return VLC_EGENERIC;
335 }