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