]> git.sesse.net Git - vlc/blob - modules/demux/playlist/podcast.c
Fix potential memleak.
[vlc] / 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_common.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     xml_t *p_xml;
41     xml_reader_t *p_xml_reader;
42 };
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int Demux( demux_t *p_demux);
48 static int Control( demux_t *p_demux, int i_query, va_list args );
49
50 /*****************************************************************************
51  * Import_podcast: main import function
52  *****************************************************************************/
53 int Import_podcast( vlc_object_t *p_this )
54 {
55     demux_t *p_demux = (demux_t *)p_this;
56
57     if( !demux_IsForced( p_demux, "podcast" ) )
58         return VLC_EGENERIC;
59
60     STANDARD_DEMUX_INIT_MSG( "using podcast reader" );
61     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
62     p_demux->p_sys->p_xml = NULL;
63     p_demux->p_sys->p_xml_reader = NULL;
64
65     return VLC_SUCCESS;
66 }
67
68 /*****************************************************************************
69  * Deactivate: frees unused data
70  *****************************************************************************/
71 void Close_podcast( vlc_object_t *p_this )
72 {
73     demux_t *p_demux = (demux_t *)p_this;
74     demux_sys_t *p_sys = p_demux->p_sys;
75
76     free( p_sys->psz_prefix );
77     if( p_sys->p_xml_reader ) xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
78     if( p_sys->p_xml ) xml_Delete( p_sys->p_xml );
79     free( p_sys );
80 }
81
82 /* "specs" : http://phobos.apple.com/static/iTunesRSS.html */
83 static int Demux( demux_t *p_demux )
84 {
85     demux_sys_t *p_sys = p_demux->p_sys;
86
87     bool b_item = false;
88     bool b_image = false;
89     int i_ret;
90
91     xml_t *p_xml;
92     xml_reader_t *p_xml_reader;
93     char *psz_elname = NULL;
94     char *psz_item_mrl = NULL;
95     char *psz_item_size = NULL;
96     char *psz_item_type = NULL;
97     char *psz_item_name = NULL;
98     char *psz_item_date = NULL;
99     char *psz_item_author = NULL;
100     char *psz_item_category = NULL;
101     char *psz_item_duration = NULL;
102     char *psz_item_keywords = NULL;
103     char *psz_item_subtitle = NULL;
104     char *psz_item_summary = NULL;
105     int i_type;
106     input_item_t *p_input;
107
108     INIT_PLAYLIST_STUFF;
109
110     p_xml = p_sys->p_xml = xml_Create( p_demux );
111     if( !p_xml ) return -1;
112
113 /*    psz_elname = stream_ReadLine( p_demux->s );
114     if( psz_elname ) free( psz_elname );
115     psz_elname = 0;*/
116
117     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
118     if( !p_xml_reader ) return -1;
119     p_sys->p_xml_reader = p_xml_reader;
120
121     /* xml */
122     /* check root node */
123     if( xml_ReaderRead( p_xml_reader ) != 1 )
124     {
125         msg_Err( p_demux, "invalid file (no root node)" );
126         return -1;
127     }
128     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
129         ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
130         strcmp( psz_elname, "rss" ) )
131     {
132         msg_Err( p_demux, "invalid root node %i, %s",
133                  xml_ReaderNodeType( p_xml_reader ), psz_elname );
134         free( psz_elname );
135         return -1;
136     }
137     free( psz_elname ); psz_elname = NULL;
138
139     while( (i_ret = xml_ReaderRead( p_xml_reader )) == 1 )
140     {
141         // Get the node type
142         i_type = xml_ReaderNodeType( p_xml_reader );
143         switch( i_type )
144         {
145             // Error
146             case -1:
147                 return -1;
148                 break;
149
150             case XML_READER_STARTELEM:
151             {
152                 // Read the element name
153                 free( psz_elname );
154                 psz_elname = xml_ReaderName( p_xml_reader );
155                 if( !psz_elname ) return -1;
156
157                 if( !strcmp( psz_elname, "item" ) )
158                 {
159                     b_item = true;
160                 }
161                 else if( !strcmp( psz_elname, "image" ) )
162                 {
163                     b_item = true;
164                 }
165
166                 // Read the attributes
167                 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
168                 {
169                     char *psz_name = xml_ReaderName( p_xml_reader );
170                     char *psz_value = xml_ReaderValue( p_xml_reader );
171                     if( !psz_name || !psz_value )
172                     {
173                         free( psz_name );
174                         free( psz_value );
175                         return -1;
176                     }
177                     if( !strcmp( psz_elname, "enclosure" ) &&
178                         !strcmp( psz_name, "url" ) )
179                     {
180                         psz_item_mrl = strdup( psz_value );
181                     }
182                     else if( !strcmp( psz_elname, "enclosure" ) &&
183                         !strcmp( psz_name, "length" ) )
184                     {
185                         psz_item_size = strdup( psz_value );
186                     }
187                     else if( !strcmp( psz_elname, "enclosure" ) &&
188                         !strcmp( psz_name, "type" ) )
189                     {
190                         psz_item_type = strdup( psz_value );
191                     }
192                     else
193                     {
194                         msg_Dbg( p_demux,"unhandled attribure %s in element %s",
195                                   psz_name, psz_elname );
196                     }
197                     free( psz_name );
198                     free( psz_value );
199                 }
200                 break;
201             }
202             case XML_READER_TEXT:
203             {
204 #define SET_DATA( field, name ) else if( b_item == true \
205                 && !strcmp( psz_elname, name ) ) \
206                 { \
207                     field = strdup( psz_text ); \
208                 }
209                 char *psz_text = xml_ReaderValue( p_xml_reader );
210                 /* item specific meta data */
211                 if( b_item == true && !strcmp( psz_elname, "title" ) )
212                 {
213                     psz_item_name = strdup( psz_text );
214                 }
215                 else if( b_item == true
216                          && ( !strcmp( psz_elname, "itunes:author" )
217                             ||!strcmp( psz_elname, "author" ) ) )
218                 { /* <author> isn't standard iTunes podcast stuff */
219                     psz_item_author = strdup( psz_text );
220                 }
221                 else if( b_item == true
222                          && ( !strcmp( psz_elname, "itunes:summary" )
223                             ||!strcmp( psz_elname, "description" ) ) )
224                 { /* <description> isn't standard iTunes podcast stuff */
225                     psz_item_summary = strdup( psz_text );
226                 }
227                 SET_DATA( psz_item_date, "pubDate" )
228                 SET_DATA( psz_item_category, "itunes:category" )
229                 SET_DATA( psz_item_duration, "itunes:duration" )
230                 SET_DATA( psz_item_keywords, "itunes:keywords" )
231                 SET_DATA( psz_item_subtitle, "itunes:subtitle" )
232                 /* toplevel meta data */
233                 else if( b_item == false && b_image == false
234                          && !strcmp( psz_elname, "title" ) )
235                 {
236                     input_item_SetName( p_current_input, psz_text );
237                 }
238 #define ADD_GINFO( info, name ) \
239     else if( !b_item && !b_image && !strcmp( psz_elname, name ) ) \
240     { \
241         input_item_AddInfo( p_current_input, _("Podcast Info"), \
242                                 _( info ), "%s", psz_text ); \
243     }
244                 ADD_GINFO( "Podcast Link", "link" )
245                 ADD_GINFO( "Podcast Copyright", "copyright" )
246                 ADD_GINFO( "Podcast Category", "itunes:category" )
247                 ADD_GINFO( "Podcast Keywords", "itunes:keywords" )
248                 ADD_GINFO( "Podcast Subtitle", "itunes:subtitle" )
249 #undef ADD_GINFO
250                 else if( b_item == false && b_image == false
251                          && ( !strcmp( psz_elname, "itunes:summary" )
252                             ||!strcmp( psz_elname, "description" ) ) )
253                 { /* <description> isn't standard iTunes podcast stuff */
254                     input_item_AddInfo( p_current_input,
255                              _( "Podcast Info" ), _( "Podcast Summary" ),
256                              "%s", psz_text );
257                 }
258                 else
259                 {
260                     msg_Dbg( p_demux, "unhandled text in element '%s'",
261                               psz_elname );
262                 }
263                 free( psz_text );
264                 break;
265             }
266             // End element
267             case XML_READER_ENDELEM:
268             {
269                 // Read the element name
270                 free( psz_elname );
271                 psz_elname = xml_ReaderName( p_xml_reader );
272                 if( !psz_elname ) return -1;
273                 if( !strcmp( psz_elname, "item" ) )
274                 {
275                     if( psz_item_mrl == NULL )
276                     {
277                         msg_Err( p_demux, "invalid XML (no enclosure markup)" );
278                         return -1;
279                     }
280                     p_input = input_item_NewExt( p_demux, psz_item_mrl,
281                                                 psz_item_name, 0, NULL, -1 );
282                     if( p_input == NULL ) break;
283 #define ADD_INFO( info, field ) \
284     if( field ) { input_item_AddInfo( p_input, \
285                             _( "Podcast Info" ),  _( info ), "%s", field ); }
286                     ADD_INFO( "Podcast Publication Date", psz_item_date  );
287                     ADD_INFO( "Podcast Author", psz_item_author );
288                     ADD_INFO( "Podcast Subcategory", psz_item_category );
289                     ADD_INFO( "Podcast Duration", psz_item_duration );
290                     ADD_INFO( "Podcast Keywords", psz_item_keywords );
291                     ADD_INFO( "Podcast Subtitle", psz_item_subtitle );
292                     ADD_INFO( "Podcast Summary", psz_item_summary );
293                     ADD_INFO( "Podcast Type", psz_item_type );
294                     if( psz_item_size )
295                     {
296                         input_item_AddInfo( p_input,
297                                                 _( "Podcast Info" ),
298                                                 _( "Podcast Size" ),
299                                                 "%s bytes",
300                                                 psz_item_size );
301                     }
302                     input_item_AddSubItem( p_current_input, p_input );
303                     vlc_gc_decref( p_input );
304                     FREENULL( psz_item_name );
305                     FREENULL( psz_item_mrl );
306                     FREENULL( psz_item_size );
307                     FREENULL( psz_item_type );
308                     FREENULL( psz_item_date );
309                     FREENULL( psz_item_author );
310                     FREENULL( psz_item_category );
311                     FREENULL( psz_item_duration );
312                     FREENULL( psz_item_keywords );
313                     FREENULL( psz_item_subtitle );
314                     FREENULL( psz_item_summary );
315                     b_item = false;
316                 }
317                 else if( !strcmp( psz_elname, "image" ) )
318                 {
319                     b_image = false;
320                 }
321                 free( psz_elname );
322                 psz_elname = strdup("");
323
324                 break;
325             }
326         }
327     }
328
329     if( i_ret != 0 )
330     {
331         msg_Warn( p_demux, "error while parsing data" );
332     }
333
334     HANDLE_PLAY_AND_RELEASE;
335     return 0; /* Needed for correct operation of go back */
336 }
337
338 static int Control( demux_t *p_demux, int i_query, va_list args )
339 {
340     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
341     return VLC_EGENERIC;
342 }