]> git.sesse.net Git - vlc/blob - modules/video_filter/rss.c
rss.c: add 'support' for Atom feeds.
[vlc] / modules / video_filter / rss.c
1 /*****************************************************************************
2  * rss.c : rss/atom feed display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2006 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "vlc_filter.h"
34 #include "vlc_block.h"
35 #include "vlc_osd.h"
36
37 #include "vlc_block.h"
38 #include "vlc_stream.h"
39 #include "vlc_xml.h"
40 #include "charset.h"
41
42 #include "vlc_image.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int  CreateFilter ( vlc_object_t * );
48 static void DestroyFilter( vlc_object_t * );
49 static subpicture_t *Filter( filter_t *, mtime_t );
50
51 static int FetchRSS( filter_t * );
52 static void FreeRSS( filter_t * );
53
54 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
55                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
56                0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
57                0x00000080, 0x000000FF, 0x0000FFFF};
58 static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
59                N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
60                N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
61                N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
62                N_("Aqua") };
63
64 /*****************************************************************************
65  * filter_sys_t: rss filter descriptor
66  *****************************************************************************/
67
68 struct rss_item_t
69 {
70     char *psz_title;
71     char *psz_description;
72     char *psz_link;
73 };
74
75 struct rss_feed_t
76 {
77     char *psz_title;
78     char *psz_description;
79     char *psz_link;
80     char *psz_image;
81     picture_t *p_pic;
82
83     int i_items;
84     struct rss_item_t *p_items;
85 };
86
87 struct filter_sys_t
88 {
89     vlc_mutex_t lock;
90     vlc_mutex_t *p_lock;
91
92     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
93     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
94     int i_speed;
95     int i_length;
96
97     char *psz_marquee;    /* marquee string */
98
99     text_style_t *p_style; /* font control */
100
101     mtime_t last_date;
102
103     char *psz_urls;
104     int i_feeds;
105     struct rss_feed_t *p_feeds;
106
107     int i_ttl;
108     time_t t_last_update;
109     vlc_bool_t b_images;
110
111     int i_cur_feed;
112     int i_cur_item;
113     int i_cur_char;
114 };
115
116 #define MSG_TEXT N_("RSS/Atom feed URLs")
117 #define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) seperated URLs")
118 #define SPEED_TEXT N_("RSS/Atom feed speed")
119 #define SPEED_LONGTEXT N_("RSS/Atom feed speed (bigger is slower)")
120 #define LENGTH_TEXT N_("RSS/Atom feed max number of chars displayed")
121 #define LENGTH_LONGTEXT N_("RSS/Atom feed max number of chars displayed")
122 #define TTL_TEXT N_("Number of seconds between each forced refresh of the feeds")
123 #define TTL_LONGTEXT N_("Number of seconds between each forced refresh of the feeds. If 0, the feeds will never be updated.")
124 #define IMAGE_TEXT N_("Display feed images if available")
125 #define IMAGE_LONGTEXT N_("Display feed images if available")
126
127 #define POSX_TEXT N_("X offset, from left")
128 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
129 #define POSY_TEXT N_("Y offset, from the top")
130 #define POSY_LONGTEXT N_("Y offset, down from the top" )
131 #define OPACITY_TEXT N_("Opacity")
132 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
133     "overlay text. 0 = transparent, 255 = totally opaque. " )
134 #define SIZE_TEXT N_("Font size, pixels")
135 #define SIZE_LONGTEXT N_("Specify the font size, in pixels, " \
136     "with -1 = use freetype-fontsize" )
137
138 #define COLOR_TEXT N_("Text Default Color")
139 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, hexadecimal. " \
140     "#000000 = all colors off, " \
141     "0xFF0000 = just Red, 0xFFFFFF = all color on [White]" )
142
143 #define POS_TEXT N_("Marquee position")
144 #define POS_LONGTEXT N_( \
145   "You can enforce the marquee position on the video " \
146   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
147   "also use combinations of these values by adding them).")
148
149 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
150 static char *ppsz_pos_descriptions[] =
151      { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
152      N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
153
154 /*****************************************************************************
155  * Module descriptor
156  *****************************************************************************/
157 vlc_module_begin();
158     set_capability( "sub filter", 0 );
159     set_shortname( "RSS" );
160     set_callbacks( CreateFilter, DestroyFilter );
161     set_category( CAT_VIDEO );
162     set_subcategory( SUBCAT_VIDEO_SUBPIC );
163     add_string( "rss-urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
164
165     set_section( N_("Position"), NULL );
166     add_integer( "rss-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
167     add_integer( "rss-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
168     add_integer( "rss-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
169         change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
170
171     set_section( N_("Font"), NULL );
172     /* 5 sets the default to top [1] left [4] */
173     add_integer_with_range( "rss-opacity", 255, 0, 255, NULL,
174         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
175     add_integer( "rss-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
176                   VLC_FALSE );
177         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
178     add_integer( "rss-size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
179
180     set_section( N_("Misc"), NULL );
181     add_integer( "rss-speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT,
182                  VLC_FALSE );
183     add_integer( "rss-length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
184                  VLC_FALSE );
185     add_integer( "rss-ttl", 1800, NULL, TTL_TEXT, TTL_LONGTEXT, VLC_FALSE );
186     add_bool( "rss-images", 1, NULL, IMAGE_TEXT, IMAGE_LONGTEXT, VLC_FALSE );
187
188     set_description( _("RSS and Atom feed display") );
189     add_shortcut( "rss" );
190     add_shortcut( "atom" );
191 vlc_module_end();
192
193 /*****************************************************************************
194  * CreateFilter: allocates RSS video filter
195  *****************************************************************************/
196 static int CreateFilter( vlc_object_t *p_this )
197 {
198     filter_t *p_filter = (filter_t *)p_this;
199     filter_sys_t *p_sys;
200     int i_feed;
201
202     /* Allocate structure */
203     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
204     if( p_sys == NULL )
205     {
206         msg_Err( p_filter, "out of memory" );
207         return VLC_ENOMEM;
208     }
209
210     vlc_mutex_init( p_filter, &p_sys->lock );
211     vlc_mutex_lock( &p_sys->lock );
212
213     p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
214     p_sys->i_cur_feed = 0;
215     p_sys->i_cur_item = 0;
216     p_sys->i_cur_char = 0;
217     p_sys->i_feeds = 0;
218     p_sys->p_feeds = NULL;
219     p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
220     p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
221     p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, "rss-ttl" ) );
222     p_sys->b_images = var_CreateGetBool( p_filter, "rss-images" );
223     p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
224
225     p_sys->p_style = malloc( sizeof( text_style_t ));
226     memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ));
227
228     p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
229     p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
230     p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
231     p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, "rss-opacity" );
232     p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
233     p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
234
235     if( p_sys->b_images == VLC_TRUE && p_sys->p_style->i_font_size == -1 )
236     {
237         msg_Warn( p_filter, "rrs-size wasn't specified. Feed images will thus be displayed without being resized" );
238     }
239
240     if( FetchRSS( p_filter ) )
241     {
242         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
243         vlc_mutex_unlock( &p_sys->lock );
244         return VLC_EGENERIC;
245     }
246     p_sys->t_last_update = time( NULL );
247
248     if( p_sys->i_feeds == 0 )
249     {
250         vlc_mutex_unlock( &p_sys->lock );
251         return VLC_EGENERIC;
252     }
253     for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
254         if( p_sys->p_feeds[i_feed].i_items == 0 )
255         {
256             vlc_mutex_unlock( &p_sys->lock );
257             return VLC_EGENERIC;
258         }
259
260     /* Misc init */
261     p_filter->pf_sub_filter = Filter;
262     p_sys->last_date = (mtime_t)0;
263
264     vlc_mutex_unlock( &p_sys->lock );
265
266     return VLC_SUCCESS;
267 }
268 /*****************************************************************************
269  * DestroyFilter: destroy RSS video filter
270  *****************************************************************************/
271 static void DestroyFilter( vlc_object_t *p_this )
272 {
273     filter_t *p_filter = (filter_t *)p_this;
274     filter_sys_t *p_sys = p_filter->p_sys;
275
276     vlc_mutex_lock( &p_sys->lock );
277
278     if( p_sys->p_style ) free( p_sys->p_style );
279     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
280     free( p_sys->psz_urls );
281     FreeRSS( p_filter );
282     vlc_mutex_unlock( &p_sys->lock );
283     vlc_mutex_destroy( &p_sys->lock );
284     free( p_sys );
285
286     /* Delete the RSS variables */
287     var_Destroy( p_filter, "rss-urls" );
288     var_Destroy( p_filter, "rss-speed" );
289     var_Destroy( p_filter, "rss-length" );
290     var_Destroy( p_filter, "rss-ttl" );
291     var_Destroy( p_filter, "rss-images" );
292     var_Destroy( p_filter, "rss-x" );
293     var_Destroy( p_filter, "rss-y" );
294     var_Destroy( p_filter, "rss-position" );
295     var_Destroy( p_filter, "rss-color");
296     var_Destroy( p_filter, "rss-opacity");
297     var_Destroy( p_filter, "rss-size");
298 }
299
300 /****************************************************************************
301  * Filter: the whole thing
302  ****************************************************************************
303  * This function outputs subpictures at regular time intervals.
304  ****************************************************************************/
305 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
306 {
307     filter_sys_t *p_sys = p_filter->p_sys;
308     subpicture_t *p_spu;
309     video_format_t fmt = {0};
310
311     subpicture_region_t *p_region;
312
313     int i_feed, i_item;
314
315     struct rss_feed_t *p_feed;
316
317     vlc_mutex_lock( &p_sys->lock );
318
319     if( p_sys->last_date
320        + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 )
321            /* ( ... ? 5 : 1 ) means "wait more for the 1st char" */
322        * p_sys->i_speed > date )
323     {
324         vlc_mutex_unlock( &p_sys->lock );
325         return NULL;
326     }
327
328     /* Do we need to update the feeds ? */
329     if( p_sys->i_ttl
330         && time( NULL ) > p_sys->t_last_update + (time_t)p_sys->i_ttl )
331     {
332         msg_Dbg( p_filter, "Forcing update of all the RSS feeds" );
333         if( FetchRSS( p_filter ) )
334         {
335             msg_Err( p_filter, "failed while fetching RSS ... too bad" );
336             vlc_mutex_unlock( &p_sys->lock );
337             return NULL; /* FIXME : we most likely messed up all the data,
338                           * so we might need to do something about it */
339         }
340         p_sys->t_last_update = time( NULL );
341     }
342
343     p_sys->last_date = date;
344     p_sys->i_cur_char++;
345     if( p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 )
346     {
347         p_sys->i_cur_char = 0;
348         p_sys->i_cur_item++;
349         if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
350         {
351             p_sys->i_cur_item = 0;
352             p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
353         }
354     }
355
356     p_spu = p_filter->pf_sub_buffer_new( p_filter );
357     if( !p_spu )
358     {
359         vlc_mutex_unlock( &p_sys->lock );
360         return NULL;
361     }
362
363     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
364
365     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
366     if( !p_spu->p_region )
367     {
368         p_filter->pf_sub_buffer_del( p_filter, p_spu );
369         vlc_mutex_unlock( &p_sys->lock );
370         return NULL;
371     }
372
373     /* Generate the string that will be displayed. This string is supposed to
374        be p_sys->i_length characters long. */
375     i_item = p_sys->i_cur_item;
376     i_feed = p_sys->i_cur_feed;
377     p_feed = &p_sys->p_feeds[p_sys->i_cur_feed];
378
379     if( p_feed->p_pic )
380     {
381         /* Don't display the feed's title if we have an image */
382         snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
383                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title
384                   +p_sys->i_cur_char );
385     }
386     else
387     {
388         snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
389                   p_sys->p_feeds[i_feed].psz_title,
390                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title
391                   +p_sys->i_cur_char );
392     }
393
394     while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
395     {
396         i_item++;
397         if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
398         snprintf( strchr( p_sys->psz_marquee, 0 ),
399                   p_sys->i_length - strlen( p_sys->psz_marquee ),
400                   " - %s",
401                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
402     }
403
404     /* Calls to snprintf might split multibyte UTF8 chars ...
405      * which freetype doesn't like. */
406     {
407         char *a = strdup( p_sys->psz_marquee );
408         char *a2 = a;
409         char *b = p_sys->psz_marquee;
410         EnsureUTF8( p_sys->psz_marquee );
411         /* we want to use ' ' instead of '?' for erroneous chars */
412         while( *b != '\0' )
413         {
414             if( *b != *a ) *b = ' ';
415             b++;a++;
416         }
417         free( a2 );
418     }
419
420     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
421     p_spu->i_start = date;
422     p_spu->i_stop  = 0;
423     p_spu->b_ephemer = VLC_TRUE;
424
425     /*  where to locate the string: */
426     if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
427     {   /* set to one of the 9 relative locations */
428         p_spu->i_flags = p_sys->i_pos;
429         p_spu->i_x = 0;
430         p_spu->i_y = 0;
431         p_spu->b_absolute = VLC_FALSE;
432     }
433     else
434     {   /*  set to an absolute xy, referenced to upper left corner */
435         p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
436         p_spu->i_x = p_sys->i_xoff;
437         p_spu->i_y = p_sys->i_yoff;
438         p_spu->b_absolute = VLC_TRUE;
439     }
440
441     p_spu->i_height = 1;
442     p_spu->p_region->p_style = p_sys->p_style;
443
444     if( p_feed->p_pic )
445     {
446         /* Display the feed's image */
447         picture_t *p_pic = p_feed->p_pic;
448         video_format_t fmt_out = {0};
449
450         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
451         fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
452         fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
453         fmt_out.i_width =
454             fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
455         fmt_out.i_height =
456             fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
457
458         p_region = p_spu->pf_create_region( VLC_OBJECT( p_filter ), &fmt_out );
459         if( !p_region )
460         {
461             msg_Err( p_filter, "cannot allocate SPU region" );
462         }
463         else
464         {
465             vout_CopyPicture( p_filter, &p_region->picture, p_pic );
466             p_spu->p_region->p_next = p_region;
467         }
468
469         /* Offset text to display right next to the image */
470         p_spu->p_region->i_x = p_pic->p[Y_PLANE].i_visible_pitch;
471     }
472
473     vlc_mutex_unlock( &p_sys->lock );
474     return p_spu;
475 }
476
477 /****************************************************************************
478  * RSS related functions
479  ****************************************************************************
480  * You should always lock the p_filter mutex before using any of these
481  * functions
482  ***************************************************************************/
483
484 /****************************************************************************
485  * download and resize image located at psz_url
486  ***************************************************************************/
487 picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
488 {
489     filter_sys_t *p_sys = p_filter->p_sys;
490
491     video_format_t fmt_in={0}, fmt_out={0};
492     picture_t *p_orig, *p_pic=NULL;
493     image_handler_t *p_handler = image_HandlerCreate( p_filter );
494
495     char *psz_local;
496
497     psz_local = ToLocale( psz_url );
498     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
499     p_orig = image_ReadUrl( p_handler, psz_local, &fmt_in, &fmt_out );
500     LocaleFree( psz_local );
501
502     if( !p_orig )
503     {
504         msg_Warn( p_filter, "Unable to read image %s", psz_url );
505     }
506     else if( p_sys->p_style->i_font_size > 0 )
507     {
508
509         fmt_in.i_chroma = VLC_FOURCC('Y','U','V','A');
510         fmt_in.i_height = p_orig->p[Y_PLANE].i_visible_lines;
511         fmt_in.i_width = p_orig->p[Y_PLANE].i_visible_pitch;
512         fmt_out.i_width = p_orig->p[Y_PLANE].i_visible_pitch
513             *p_sys->p_style->i_font_size/p_orig->p[Y_PLANE].i_visible_lines;
514         fmt_out.i_height = p_sys->p_style->i_font_size;
515
516         p_pic = image_Convert( p_handler, p_orig, &fmt_in, &fmt_out );
517         p_orig->pf_release( p_orig );
518         if( !p_pic )
519         {
520             msg_Warn( p_filter, "Error while converting %s", psz_url );
521         }
522     }
523     else
524     {
525         p_pic = p_orig;
526     }
527
528     image_HandlerDelete( p_handler );
529
530     return p_pic;
531 }
532
533 /****************************************************************************
534  * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the
535  * string.
536  ***************************************************************************/
537 char *removeWhiteChars( char *psz_src )
538 {
539     char *psz_src2 = strdup( psz_src );
540     char *psz_clean = strdup( psz_src2 );
541     char *psz_clean2;
542     int i;
543     while( ( *psz_clean == ' ' || *psz_clean == '\t'
544            || *psz_clean == '\n' || *psz_clean == '\r' )
545            && *psz_clean != '\0' )
546     {
547         psz_clean++;
548     }
549     i = strlen( psz_clean );
550     while( --i > 0 &&
551          ( psz_clean[i] == ' ' || psz_clean[i] == '\t'
552         || psz_clean[i] == '\n' || psz_clean[i] == '\r' ) );
553     psz_clean[i+1] = '\0';
554     psz_clean2 = strdup( psz_clean );
555     free( psz_src2 );
556     return psz_clean2;
557 }
558
559 /****************************************************************************
560  * FetchRSS
561  ***************************************************************************/
562 static int FetchRSS( filter_t *p_filter)
563 {
564     filter_sys_t *p_sys = p_filter->p_sys;
565
566     stream_t *p_stream = NULL;
567     xml_t *p_xml = NULL;
568     xml_reader_t *p_xml_reader = NULL;
569
570     char *psz_eltname = NULL;
571     char *psz_eltvalue = NULL;
572     char *psz_feed = NULL;
573     char *psz_buffer = NULL;
574     char *psz_buffer_2 = NULL;
575
576     int i_feed;
577     int i_item;
578     vlc_bool_t b_is_item;
579     vlc_bool_t b_is_image;
580     int i_int;
581
582     FreeRSS( p_filter );
583     p_sys->i_feeds = 1;
584     i_int = 0;
585     while( p_sys->psz_urls[i_int] != 0 )
586         if( p_sys->psz_urls[i_int++] == '|' )
587             p_sys->i_feeds++;
588     p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
589                                 * sizeof( struct rss_feed_t ) );
590
591     p_xml = xml_Create( p_filter );
592     if( !p_xml )
593     {
594         msg_Err( p_filter, "Failed to open XML parser" );
595         return 1;
596     }
597
598     psz_buffer = strdup( p_sys->psz_urls );
599     psz_buffer_2 = psz_buffer; /* keep track so we can free it */
600     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
601     {
602         struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
603
604         if( psz_buffer == NULL ) break;
605         if( psz_buffer[0] == 0 ) psz_buffer++;
606         psz_feed = psz_buffer;
607         psz_buffer = strchr( psz_buffer, '|' );
608         if( psz_buffer != NULL ) psz_buffer[0] = 0;
609
610         p_feed->psz_title = NULL;
611         p_feed->psz_description = NULL;
612         p_feed->psz_link = NULL;
613         p_feed->psz_image = NULL;
614         p_feed->p_pic = NULL;
615         p_feed->i_items = 0;
616         p_feed->p_items = NULL;
617
618         msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
619
620         p_stream = stream_UrlNew( p_filter, psz_feed );
621         if( !p_stream )
622         {
623             msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
624             return 1;
625         }
626
627         p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
628         if( !p_xml_reader )
629         {
630             msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
631             return 1;
632         }
633
634         i_item = 0;
635         b_is_item = VLC_FALSE;
636         b_is_image = VLC_FALSE;
637
638         while( xml_ReaderRead( p_xml_reader ) == 1 )
639         {
640             switch( xml_ReaderNodeType( p_xml_reader ) )
641             {
642                 // Error
643                 case -1:
644                     return 1;
645
646                 case XML_READER_STARTELEM:
647                     if( psz_eltname )
648                     {
649                         free( psz_eltname );
650                         psz_eltname = NULL;
651                     }
652                     psz_eltname = xml_ReaderName( p_xml_reader );
653                     if( !psz_eltname )
654                     {
655                         return 1;
656                     }
657 #                   define RSS_DEBUG
658 #                   ifdef RSS_DEBUG
659                     msg_Dbg( p_filter, "element name : %s", psz_eltname );
660 #                   endif
661                     if( !strcmp( psz_eltname, "item" )
662                      || !strcmp( psz_eltname, "entry" ) )
663                     {
664                         b_is_item = VLC_TRUE;
665                         p_feed->i_items++;
666                         p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
667                         p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
668                         p_feed->p_items[p_feed->i_items-1].psz_description
669                                                                      = NULL;
670                         p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
671                     }
672                     else if( !strcmp( psz_eltname, "image" ) )
673                     {
674                         b_is_image = VLC_TRUE;
675                     }
676                     break;
677
678                 case XML_READER_ENDELEM:
679                     if( psz_eltname )
680                     {
681                         free( psz_eltname );
682                         psz_eltname = NULL;
683                     }
684                     psz_eltname = xml_ReaderName( p_xml_reader );
685                     if( !psz_eltname )
686                     {
687                         return 1;
688                     }
689 #                   ifdef RSS_DEBUG
690                     msg_Dbg( p_filter, "element end : %s", psz_eltname );
691 #                   endif
692                     if( !strcmp( psz_eltname, "item" )
693                      || !strcmp( psz_eltname, "entry" ) )
694                     {
695                         b_is_item = VLC_FALSE;
696                         i_item++;
697                     }
698                     else if( !strcmp( psz_eltname, "image" ) )
699                     {
700                         b_is_image = VLC_FALSE;
701                     }
702                     free( psz_eltname );
703                     psz_eltname = NULL;
704                     break;
705
706                 case XML_READER_TEXT:
707                     if( !psz_eltname ) break;
708                     psz_eltvalue = xml_ReaderValue( p_xml_reader );
709                     if( !psz_eltvalue )
710                     {
711                         return 1;
712                     }
713                     else
714                     {
715                         char *psz_clean;
716                         psz_clean = removeWhiteChars( psz_eltvalue );
717                         free( psz_eltvalue ); psz_eltvalue = psz_clean;
718                     }
719 #                   ifdef RSS_DEBUG
720                     msg_Dbg( p_filter, "  text : <%s>", psz_eltvalue );
721 #                   endif
722                     if( b_is_item == VLC_TRUE )
723                     {
724                         struct rss_item_t *p_item;
725                         p_item = p_feed->p_items+i_item;
726                         if( !strcmp( psz_eltname, "title" ) )
727                         {
728                             p_item->psz_title = psz_eltvalue;
729                         }
730                         else if( !strcmp( psz_eltname, "link" ) )
731                         {
732                             p_item->psz_link = psz_eltvalue;
733                         }
734                         else if( !strcmp( psz_eltname, "description" ) )
735                         {
736                             p_item->psz_description = psz_eltvalue;
737                         }
738                         else
739                         {
740                             free( psz_eltvalue );
741                             psz_eltvalue = NULL;
742                         }
743                     }
744                     else if( b_is_image == VLC_TRUE )
745                     {
746                         if( !strcmp( psz_eltname, "url" ) )
747                         {
748                             p_feed->psz_image = psz_eltvalue;
749                             if( p_sys->b_images == VLC_TRUE )
750                                 p_feed->p_pic = LoadImage( p_filter,
751                                                            p_feed->psz_image );
752                         }
753                         else
754                         {
755                             free( psz_eltvalue );
756                             psz_eltvalue = NULL;
757                         }
758                     }
759                     else
760                     {
761                         if( !strcmp( psz_eltname, "title" ) )
762                         {
763                             p_feed->psz_title = psz_eltvalue;
764                         }
765                         else if( !strcmp( psz_eltname, "link" ) )
766                         {
767                             p_feed->psz_link = psz_eltvalue;
768                         }
769                         else if( !strcmp( psz_eltname, "description" )
770                               || !strcmp( psz_eltname, "subtitle" ) )
771                         {
772                             p_feed->psz_description = psz_eltvalue;
773                         }
774                         else
775                         {
776                             free( psz_eltvalue );
777                             psz_eltvalue = NULL;
778                         }
779                     }
780                     break;
781             }
782         }
783
784         if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
785         if( p_stream ) stream_Delete( p_stream );
786         msg_Dbg( p_filter, "Done with %s RSS feed.", psz_feed );
787     }
788     free( psz_buffer_2 );
789     if( p_xml ) xml_Delete( p_xml );
790
791     return 0;
792 }
793
794 /****************************************************************************
795  * FreeRSS
796  ***************************************************************************/
797 static void FreeRSS( filter_t *p_filter)
798 {
799     filter_sys_t *p_sys = p_filter->p_sys;
800
801     struct rss_item_t *p_item;
802     struct rss_feed_t *p_feed;
803
804     int i_feed;
805     int i_item;
806
807     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
808     {
809         p_feed = p_sys->p_feeds+i_feed;
810         for( i_item = 0; i_item < p_feed->i_items; i_item++ )
811         {
812             p_item = p_feed->p_items+i_item;
813             free( p_item->psz_title );
814             free( p_item->psz_link );
815             free( p_item->psz_description );
816         }
817         free( p_feed->p_items );
818         free( p_feed->psz_title);
819         free( p_feed->psz_link );
820         free( p_feed->psz_description );
821         free( p_feed->psz_image );
822         if( p_feed->p_pic != NULL )
823             p_feed->p_pic->pf_release( p_feed->p_pic );
824     }
825     free( p_sys->p_feeds );
826     p_sys->i_feeds = 0;
827 }