]> git.sesse.net Git - vlc/blob - modules/video_filter/rss.c
rss.c: add suport for feed images (default enabled).
[vlc] / modules / video_filter / rss.c
1 /*****************************************************************************
2  * rss.c : rss 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 feed URLs")
117 #define MSG_LONGTEXT N_("RSS feed '|' (pipe) seperated URLs")
118 #define SPEED_TEXT N_("RSS feed speed")
119 #define SPEED_LONGTEXT N_("RSS feed speed (bigger is slower)")
120 #define LENGTH_TEXT N_("RSS feed max number of chars displayed")
121 #define LENGTH_LONGTEXT N_("RSS 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 feed display") );
189     add_shortcut( "rss" );
190 vlc_module_end();
191
192 /*****************************************************************************
193  * CreateFilter: allocates RSS video filter
194  *****************************************************************************/
195 static int CreateFilter( vlc_object_t *p_this )
196 {
197     filter_t *p_filter = (filter_t *)p_this;
198     filter_sys_t *p_sys;
199     int i_feed;
200
201     /* Allocate structure */
202     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
203     if( p_sys == NULL )
204     {
205         msg_Err( p_filter, "out of memory" );
206         return VLC_ENOMEM;
207     }
208
209     vlc_mutex_init( p_filter, &p_sys->lock );
210     vlc_mutex_lock( &p_sys->lock );
211
212     p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
213     p_sys->i_cur_feed = 0;
214     p_sys->i_cur_item = 0;
215     p_sys->i_cur_char = 0;
216     p_sys->i_feeds = 0;
217     p_sys->p_feeds = NULL;
218     p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
219     p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
220     p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, "rss-ttl" ) );
221     p_sys->b_images = var_CreateGetBool( p_filter, "rss-images" );
222     p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
223
224     p_sys->p_style = malloc( sizeof( text_style_t ));
225     memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ));
226
227     p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
228     p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
229     p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
230     p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, "rss-opacity" );
231     p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
232     p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
233
234     if( p_sys->b_images == VLC_TRUE && p_sys->p_style->i_font_size == -1 )
235     {
236         msg_Warn( p_filter, "rrs-size wasn't specified. Feed images will thus be displayed without being resized" );
237     }
238
239     if( FetchRSS( p_filter ) )
240     {
241         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
242         vlc_mutex_unlock( &p_sys->lock );
243         return VLC_EGENERIC;
244     }
245     p_sys->t_last_update = time( NULL );
246
247     if( p_sys->i_feeds == 0 )
248     {
249         vlc_mutex_unlock( &p_sys->lock );
250         return VLC_EGENERIC;
251     }
252     for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
253         if( p_sys->p_feeds[i_feed].i_items == 0 )
254         {
255             vlc_mutex_unlock( &p_sys->lock );
256             return VLC_EGENERIC;
257         }
258
259     /* Misc init */
260     p_filter->pf_sub_filter = Filter;
261     p_sys->last_date = (mtime_t)0;
262
263     vlc_mutex_unlock( &p_sys->lock );
264
265     return VLC_SUCCESS;
266 }
267 /*****************************************************************************
268  * DestroyFilter: destroy RSS video filter
269  *****************************************************************************/
270 static void DestroyFilter( vlc_object_t *p_this )
271 {
272     filter_t *p_filter = (filter_t *)p_this;
273     filter_sys_t *p_sys = p_filter->p_sys;
274
275     vlc_mutex_lock( &p_sys->lock );
276
277     if( p_sys->p_style ) free( p_sys->p_style );
278     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
279     free( p_sys->psz_urls );
280     FreeRSS( p_filter );
281     vlc_mutex_unlock( &p_sys->lock );
282     vlc_mutex_destroy( &p_sys->lock );
283     free( p_sys );
284
285     /* Delete the RSS variables */
286     var_Destroy( p_filter, "rss-urls" );
287     var_Destroy( p_filter, "rss-speed" );
288     var_Destroy( p_filter, "rss-length" );
289     var_Destroy( p_filter, "rss-ttl" );
290     var_Destroy( p_filter, "rss-images" );
291     var_Destroy( p_filter, "rss-x" );
292     var_Destroy( p_filter, "rss-y" );
293     var_Destroy( p_filter, "rss-position" );
294     var_Destroy( p_filter, "rss-color");
295     var_Destroy( p_filter, "rss-opacity");
296     var_Destroy( p_filter, "rss-size");
297 }
298
299 /****************************************************************************
300  * Filter: the whole thing
301  ****************************************************************************
302  * This function outputs subpictures at regular time intervals.
303  ****************************************************************************/
304 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
305 {
306     filter_sys_t *p_sys = p_filter->p_sys;
307     subpicture_t *p_spu;
308     video_format_t fmt = {0};
309
310     subpicture_region_t *p_region;
311
312     int i_feed, i_item;
313
314     struct rss_feed_t *p_feed;
315
316     vlc_mutex_lock( &p_sys->lock );
317
318     if( p_sys->last_date
319        + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 )
320            /* ( ... ? 5 : 1 ) means "wait more for the 1st char" */
321        * p_sys->i_speed > date )
322     {
323         vlc_mutex_unlock( &p_sys->lock );
324         return NULL;
325     }
326
327     /* Do we need to update the feeds ? */
328     if( p_sys->i_ttl
329         && time( NULL ) > p_sys->t_last_update + (time_t)p_sys->i_ttl )
330     {
331         msg_Dbg( p_filter, "Forcing update of all the RSS feeds" );
332         if( FetchRSS( p_filter ) )
333         {
334             msg_Err( p_filter, "failed while fetching RSS ... too bad" );
335             vlc_mutex_unlock( &p_sys->lock );
336             return NULL; /* FIXME : we most likely messed up all the data,
337                           * so we might need to do something about it */
338         }
339         p_sys->t_last_update = time( NULL );
340     }
341
342     p_sys->last_date = date;
343     p_sys->i_cur_char++;
344     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 )
345     {
346         p_sys->i_cur_char = 0;
347         p_sys->i_cur_item++;
348         if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
349         {
350             p_sys->i_cur_item = 0;
351             p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
352         }
353     }
354
355     p_spu = p_filter->pf_sub_buffer_new( p_filter );
356     if( !p_spu )
357     {
358         vlc_mutex_unlock( &p_sys->lock );
359         return NULL;
360     }
361
362     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
363
364     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
365     if( !p_spu->p_region )
366     {
367         p_filter->pf_sub_buffer_del( p_filter, p_spu );
368         vlc_mutex_unlock( &p_sys->lock );
369         return NULL;
370     }
371
372     /* Generate the string that will be displayed. This string is supposed to
373        be p_sys->i_length characters long. */
374     i_item = p_sys->i_cur_item;
375     i_feed = p_sys->i_cur_feed;
376     p_feed = &p_sys->p_feeds[p_sys->i_cur_feed];
377
378     if( p_feed->p_pic )
379     {
380         /* Don't display the feed's title if we have an image */
381         snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
382                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title
383                   +p_sys->i_cur_char );
384     }
385     else
386     {
387         snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
388                   p_sys->p_feeds[i_feed].psz_title,
389                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title
390                   +p_sys->i_cur_char );
391     }
392
393     while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
394     {
395         i_item++;
396         if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
397         snprintf( strchr( p_sys->psz_marquee, 0 ),
398                   p_sys->i_length - strlen( p_sys->psz_marquee ),
399                   " - %s",
400                   p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
401     }
402
403     /* Calls to snprintf might split multibyte UTF8 chars ...
404      * which freetype doesn't like. */
405     {
406         char *a = strdup( p_sys->psz_marquee );
407         char *a2 = a;
408         char *b = p_sys->psz_marquee;
409         EnsureUTF8( p_sys->psz_marquee );
410         /* we want to use ' ' instead of '?' for erroneous chars */
411         while( *b != '\0' )
412         {
413             if( *b != *a ) *b = ' ';
414             b++;a++;
415         }
416         free( a2 );
417     }
418
419     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
420     p_spu->i_start = date;
421     p_spu->i_stop  = 0;
422     p_spu->b_ephemer = VLC_TRUE;
423
424     /*  where to locate the string: */
425     if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
426     {   /* set to one of the 9 relative locations */
427         p_spu->i_flags = p_sys->i_pos;
428         p_spu->i_x = 0;
429         p_spu->i_y = 0;
430         p_spu->b_absolute = VLC_FALSE;
431     }
432     else
433     {   /*  set to an absolute xy, referenced to upper left corner */
434         p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
435         p_spu->i_x = p_sys->i_xoff;
436         p_spu->i_y = p_sys->i_yoff;
437         p_spu->b_absolute = VLC_TRUE;
438     }
439
440     p_spu->i_height = 1;
441     p_spu->p_region->p_style = p_sys->p_style;
442
443     if( p_feed->p_pic )
444     {
445         /* Display the feed's image */
446         picture_t *p_pic = p_feed->p_pic;
447         video_format_t fmt_out = {0};
448
449         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
450         fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
451         fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
452         fmt_out.i_width =
453             fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
454         fmt_out.i_height =
455             fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
456
457         p_region = p_spu->pf_create_region( VLC_OBJECT( p_filter ), &fmt_out );
458         if( !p_region )
459         {
460             msg_Err( p_filter, "cannot allocate SPU region" );
461         }
462         else
463         {
464             vout_CopyPicture( p_filter, &p_region->picture, p_pic );
465             p_spu->p_region->p_next = p_region;
466         }
467
468         /* Offset text to display right next to the image */
469         p_spu->p_region->i_x = p_pic->p[Y_PLANE].i_visible_pitch;
470     }
471
472     vlc_mutex_unlock( &p_sys->lock );
473     return p_spu;
474 }
475
476 /****************************************************************************
477  * RSS related functions
478  ****************************************************************************
479  * You should always lock the p_filter mutex before using any of these
480  * functions
481  ***************************************************************************/
482
483 /****************************************************************************
484  * download and resize image located at psz_url
485  ***************************************************************************/
486 picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
487 {
488     filter_sys_t *p_sys = p_filter->p_sys;
489
490     video_format_t fmt_in={0}, fmt_out={0};
491     picture_t *p_orig, *p_pic=NULL;
492     image_handler_t *p_handler = image_HandlerCreate( p_filter );
493
494     char *psz_local;
495
496     psz_local = ToLocale( psz_url );
497     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
498     p_orig = image_ReadUrl( p_handler, psz_local, &fmt_in, &fmt_out );
499     LocaleFree( psz_local );
500
501     if( !p_orig )
502     {
503         msg_Warn( p_filter, "Unable to read image %s", psz_url );
504     }
505     else if( p_sys->p_style->i_font_size > 0 )
506     {
507
508         fmt_in.i_chroma = VLC_FOURCC('Y','U','V','A');
509         fmt_in.i_height = p_orig->p[Y_PLANE].i_visible_lines;
510         fmt_in.i_width = p_orig->p[Y_PLANE].i_visible_pitch;
511         fmt_out.i_width = p_orig->p[Y_PLANE].i_visible_pitch
512             *p_sys->p_style->i_font_size/p_orig->p[Y_PLANE].i_visible_lines;
513         fmt_out.i_height = p_sys->p_style->i_font_size;
514
515         p_pic = image_Convert( p_handler, p_orig, &fmt_in, &fmt_out );
516         p_orig->pf_release( p_orig );
517         if( !p_pic )
518         {
519             msg_Warn( p_filter, "Error while converting %s", psz_url );
520         }
521     }
522     else
523     {
524         p_pic = p_orig;
525     }
526
527     image_HandlerDelete( p_handler );
528
529     return p_pic;
530 }
531
532 /****************************************************************************
533  * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the
534  * string.
535  ***************************************************************************/
536 char *removeWhiteChars( char *psz_src )
537 {
538     char *psz_src2 = strdup( psz_src );
539     char *psz_clean = strdup( psz_src2 );
540     char *psz_clean2;
541     int i;
542     while( ( *psz_clean == ' ' || *psz_clean == '\t'
543            || *psz_clean == '\n' || *psz_clean == '\r' )
544            && *psz_clean != '\0' )
545     {
546         psz_clean++;
547     }
548     i = strlen( psz_clean );
549     while( --i > 0 &&
550          ( psz_clean[i] == ' ' || psz_clean[i] == '\t'
551         || psz_clean[i] == '\n' || psz_clean[i] == '\r' ) );
552     psz_clean[i+1] = '\0';
553     psz_clean2 = strdup( psz_clean );
554     free( psz_src2 );
555     return psz_clean2;
556 }
557
558 /****************************************************************************
559  * FetchRSS
560  ***************************************************************************/
561 static int FetchRSS( filter_t *p_filter)
562 {
563     filter_sys_t *p_sys = p_filter->p_sys;
564
565     stream_t *p_stream = NULL;
566     xml_t *p_xml = NULL;
567     xml_reader_t *p_xml_reader = NULL;
568
569     char *psz_eltname = NULL;
570     char *psz_eltvalue = NULL;
571     char *psz_feed = NULL;
572     char *psz_buffer = NULL;
573     char *psz_buffer_2 = NULL;
574
575     int i_feed;
576     int i_item;
577     vlc_bool_t b_is_item;
578     vlc_bool_t b_is_image;
579     int i_int;
580
581     FreeRSS( p_filter );
582     p_sys->i_feeds = 1;
583     i_int = 0;
584     while( p_sys->psz_urls[i_int] != 0 )
585         if( p_sys->psz_urls[i_int++] == '|' )
586             p_sys->i_feeds++;
587     p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
588                                 * sizeof( struct rss_feed_t ) );
589
590     p_xml = xml_Create( p_filter );
591     if( !p_xml )
592     {
593         msg_Err( p_filter, "Failed to open XML parser" );
594         return 1;
595     }
596
597     psz_buffer = strdup( p_sys->psz_urls );
598     psz_buffer_2 = psz_buffer; /* keep track so we can free it */
599     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
600     {
601         struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
602
603         if( psz_buffer == NULL ) break;
604         if( psz_buffer[0] == 0 ) psz_buffer++;
605         psz_feed = psz_buffer;
606         psz_buffer = strchr( psz_buffer, '|' );
607         if( psz_buffer != NULL ) psz_buffer[0] = 0;
608
609         p_feed->psz_title = NULL;
610         p_feed->psz_description = NULL;
611         p_feed->psz_link = NULL;
612         p_feed->psz_image = NULL;
613         p_feed->p_pic = NULL;
614         p_feed->i_items = 0;
615         p_feed->p_items = NULL;
616
617         msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
618
619         p_stream = stream_UrlNew( p_filter, psz_feed );
620         if( !p_stream )
621         {
622             msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
623             return 1;
624         }
625
626         p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
627         if( !p_xml_reader )
628         {
629             msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
630             return 1;
631         }
632
633         i_item = 0;
634         b_is_item = VLC_FALSE;
635         b_is_image = VLC_FALSE;
636
637         while( xml_ReaderRead( p_xml_reader ) == 1 )
638         {
639             switch( xml_ReaderNodeType( p_xml_reader ) )
640             {
641                 // Error
642                 case -1:
643                     return 1;
644
645                 case XML_READER_STARTELEM:
646                     if( psz_eltname )
647                     {
648                         free( psz_eltname );
649                         psz_eltname = NULL;
650                     }
651                     psz_eltname = xml_ReaderName( p_xml_reader );
652                     if( !psz_eltname )
653                     {
654                         return 1;
655                     }
656 #                   ifdef RSS_DEBUG
657                     msg_Dbg( p_filter, "element name : %s", psz_eltname );
658 #                   endif
659                     if( !strcmp( psz_eltname, "item" ) )
660                     {
661                         b_is_item = VLC_TRUE;
662                         p_feed->i_items++;
663                         p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
664                         p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
665                         p_feed->p_items[p_feed->i_items-1].psz_description
666                                                                      = NULL;
667                         p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
668                     }
669                     else if( !strcmp( psz_eltname, "image" ) )
670                     {
671                         b_is_image = VLC_TRUE;
672                     }
673                     break;
674
675                 case XML_READER_ENDELEM:
676                     if( psz_eltname )
677                     {
678                         free( psz_eltname );
679                         psz_eltname = NULL;
680                     }
681                     psz_eltname = xml_ReaderName( p_xml_reader );
682                     if( !psz_eltname )
683                     {
684                         return 1;
685                     }
686 #                   ifdef RSS_DEBUG
687                     msg_Dbg( p_filter, "element end : %s", psz_eltname );
688 #                   endif
689                     if( !strcmp( psz_eltname, "item" ) )
690                     {
691                         b_is_item = VLC_FALSE;
692                         i_item++;
693                     }
694                     else if( !strcmp( psz_eltname, "image" ) )
695                     {
696                         b_is_image = VLC_FALSE;
697                     }
698                     free( psz_eltname );
699                     psz_eltname = NULL;
700                     break;
701
702                 case XML_READER_TEXT:
703                     psz_eltvalue = xml_ReaderValue( p_xml_reader );
704                     if( !psz_eltvalue )
705                     {
706                         return 1;
707                     }
708                     else
709                     {
710                         char *psz_clean;
711                         psz_clean = removeWhiteChars( psz_eltvalue );
712                         free( psz_eltvalue ); psz_eltvalue = psz_clean;
713                     }
714 #                   ifdef RSS_DEBUG
715                     msg_Dbg( p_filter, "  text : <%s>", psz_eltvalue );
716 #                   endif
717                     if( b_is_item == VLC_TRUE )
718                     {
719                         struct rss_item_t *p_item;
720                         p_item = p_feed->p_items+i_item;
721                         if( !strcmp( psz_eltname, "title" ) )
722                         {
723                             p_item->psz_title = psz_eltvalue;
724                         }
725                         else if( !strcmp( psz_eltname, "link" ) )
726                         {
727                             p_item->psz_link = psz_eltvalue;
728                         }
729                         else if( !strcmp( psz_eltname, "description" ) )
730                         {
731                             p_item->psz_description = psz_eltvalue;
732                         }
733                         else
734                         {
735                             free( psz_eltvalue );
736                             psz_eltvalue = NULL;
737                         }
738                     }
739                     else if( b_is_image == VLC_TRUE )
740                     {
741                         if( !strcmp( psz_eltname, "url" ) )
742                         {
743                             p_feed->psz_image = psz_eltvalue;
744                             if( p_sys->b_images == VLC_TRUE )
745                                 p_feed->p_pic = LoadImage( p_filter,
746                                                            p_feed->psz_image );
747                         }
748                         else
749                         {
750                             free( psz_eltvalue );
751                             psz_eltvalue = NULL;
752                         }
753                     }
754                     else
755                     {
756                         if( !strcmp( psz_eltname, "title" ) )
757                         {
758                             p_feed->psz_title = psz_eltvalue;
759                         }
760                         else if( !strcmp( psz_eltname, "link" ) )
761                         {
762                             p_feed->psz_link = psz_eltvalue;
763                         }
764                         else if( !strcmp( psz_eltname, "description" ) )
765                         {
766                             p_feed->psz_description = psz_eltvalue;
767                         }
768                         else
769                         {
770                             free( psz_eltvalue );
771                             psz_eltvalue = NULL;
772                         }
773                     }
774                     break;
775             }
776         }
777
778         if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
779         if( p_stream ) stream_Delete( p_stream );
780         msg_Dbg( p_filter, "Done with %s RSS feed.", psz_feed );
781     }
782     free( psz_buffer_2 );
783     if( p_xml ) xml_Delete( p_xml );
784
785     return 0;
786 }
787
788 /****************************************************************************
789  * FreeRSS
790  ***************************************************************************/
791 static void FreeRSS( filter_t *p_filter)
792 {
793     filter_sys_t *p_sys = p_filter->p_sys;
794
795     struct rss_item_t *p_item;
796     struct rss_feed_t *p_feed;
797
798     int i_feed;
799     int i_item;
800
801     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
802     {
803         p_feed = p_sys->p_feeds+i_feed;
804         for( i_item = 0; i_item < p_feed->i_items; i_item++ )
805         {
806             p_item = p_feed->p_items+i_item;
807             free( p_item->psz_title );
808             free( p_item->psz_link );
809             free( p_item->psz_description );
810         }
811         free( p_feed->p_items );
812         free( p_feed->psz_title);
813         free( p_feed->psz_link );
814         free( p_feed->psz_description );
815         free( p_feed->psz_image );
816         if( p_feed->p_pic != NULL )
817             p_feed->p_pic->pf_release( p_feed->p_pic );
818     }
819     free( p_sys->p_feeds );
820     p_sys->i_feeds = 0;
821 }