]> git.sesse.net Git - vlc/blob - modules/video_filter/rss.c
LoadImage is a macro on Win32 API - you can't use as a function name as is
[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 / Atom" );
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 #undef LoadImage /* do not conflict with Win32 API */
485
486 /****************************************************************************
487  * download and resize image located at psz_url
488  ***************************************************************************/
489 picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
490 {
491     filter_sys_t *p_sys = p_filter->p_sys;
492
493     video_format_t fmt_in={0}, fmt_out={0};
494     picture_t *p_orig, *p_pic=NULL;
495     image_handler_t *p_handler = image_HandlerCreate( p_filter );
496
497     char *psz_local;
498
499     psz_local = ToLocale( psz_url );
500     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
501     p_orig = image_ReadUrl( p_handler, psz_local, &fmt_in, &fmt_out );
502     LocaleFree( psz_local );
503
504     if( !p_orig )
505     {
506         msg_Warn( p_filter, "Unable to read image %s", psz_url );
507     }
508     else if( p_sys->p_style->i_font_size > 0 )
509     {
510
511         fmt_in.i_chroma = VLC_FOURCC('Y','U','V','A');
512         fmt_in.i_height = p_orig->p[Y_PLANE].i_visible_lines;
513         fmt_in.i_width = p_orig->p[Y_PLANE].i_visible_pitch;
514         fmt_out.i_width = p_orig->p[Y_PLANE].i_visible_pitch
515             *p_sys->p_style->i_font_size/p_orig->p[Y_PLANE].i_visible_lines;
516         fmt_out.i_height = p_sys->p_style->i_font_size;
517
518         p_pic = image_Convert( p_handler, p_orig, &fmt_in, &fmt_out );
519         p_orig->pf_release( p_orig );
520         if( !p_pic )
521         {
522             msg_Warn( p_filter, "Error while converting %s", psz_url );
523         }
524     }
525     else
526     {
527         p_pic = p_orig;
528     }
529
530     image_HandlerDelete( p_handler );
531
532     return p_pic;
533 }
534
535 /****************************************************************************
536  * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the
537  * string.
538  ***************************************************************************/
539 char *removeWhiteChars( char *psz_src )
540 {
541     char *psz_src2 = strdup( psz_src );
542     char *psz_clean = strdup( psz_src2 );
543     char *psz_clean2;
544     int i;
545     while( ( *psz_clean == ' ' || *psz_clean == '\t'
546            || *psz_clean == '\n' || *psz_clean == '\r' )
547            && *psz_clean != '\0' )
548     {
549         psz_clean++;
550     }
551     i = strlen( psz_clean );
552     while( --i > 0 &&
553          ( psz_clean[i] == ' ' || psz_clean[i] == '\t'
554         || psz_clean[i] == '\n' || psz_clean[i] == '\r' ) );
555     psz_clean[i+1] = '\0';
556     psz_clean2 = strdup( psz_clean );
557     free( psz_src2 );
558     return psz_clean2;
559 }
560
561 /****************************************************************************
562  * FetchRSS
563  ***************************************************************************/
564 static int FetchRSS( filter_t *p_filter)
565 {
566     filter_sys_t *p_sys = p_filter->p_sys;
567
568     stream_t *p_stream = NULL;
569     xml_t *p_xml = NULL;
570     xml_reader_t *p_xml_reader = NULL;
571
572     char *psz_eltname = NULL;
573     char *psz_eltvalue = NULL;
574     char *psz_feed = NULL;
575     char *psz_buffer = NULL;
576     char *psz_buffer_2 = NULL;
577
578     int i_feed;
579     int i_item;
580     vlc_bool_t b_is_item;
581     vlc_bool_t b_is_image;
582     int i_int;
583
584     FreeRSS( p_filter );
585     p_sys->i_feeds = 1;
586     i_int = 0;
587     while( p_sys->psz_urls[i_int] != 0 )
588         if( p_sys->psz_urls[i_int++] == '|' )
589             p_sys->i_feeds++;
590     p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
591                                 * sizeof( struct rss_feed_t ) );
592
593     p_xml = xml_Create( p_filter );
594     if( !p_xml )
595     {
596         msg_Err( p_filter, "Failed to open XML parser" );
597         return 1;
598     }
599
600     psz_buffer = strdup( p_sys->psz_urls );
601     psz_buffer_2 = psz_buffer; /* keep track so we can free it */
602     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
603     {
604         struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
605
606         if( psz_buffer == NULL ) break;
607         if( psz_buffer[0] == 0 ) psz_buffer++;
608         psz_feed = psz_buffer;
609         psz_buffer = strchr( psz_buffer, '|' );
610         if( psz_buffer != NULL ) psz_buffer[0] = 0;
611
612         p_feed->psz_title = NULL;
613         p_feed->psz_description = NULL;
614         p_feed->psz_link = NULL;
615         p_feed->psz_image = NULL;
616         p_feed->p_pic = NULL;
617         p_feed->i_items = 0;
618         p_feed->p_items = NULL;
619
620         msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
621
622         p_stream = stream_UrlNew( p_filter, psz_feed );
623         if( !p_stream )
624         {
625             msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
626             return 1;
627         }
628
629         p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
630         if( !p_xml_reader )
631         {
632             msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
633             return 1;
634         }
635
636         i_item = 0;
637         b_is_item = VLC_FALSE;
638         b_is_image = VLC_FALSE;
639
640         while( xml_ReaderRead( p_xml_reader ) == 1 )
641         {
642             switch( xml_ReaderNodeType( p_xml_reader ) )
643             {
644                 // Error
645                 case -1:
646                     return 1;
647
648                 case XML_READER_STARTELEM:
649                     if( psz_eltname )
650                     {
651                         free( psz_eltname );
652                         psz_eltname = NULL;
653                     }
654                     psz_eltname = xml_ReaderName( p_xml_reader );
655                     if( !psz_eltname )
656                     {
657                         return 1;
658                     }
659 #                   define RSS_DEBUG
660 #                   ifdef RSS_DEBUG
661                     msg_Dbg( p_filter, "element name : %s", psz_eltname );
662 #                   endif
663                     if( !strcmp( psz_eltname, "item" )
664                      || !strcmp( psz_eltname, "entry" ) )
665                     {
666                         b_is_item = VLC_TRUE;
667                         p_feed->i_items++;
668                         p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
669                         p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
670                         p_feed->p_items[p_feed->i_items-1].psz_description
671                                                                      = NULL;
672                         p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
673                     }
674                     else if( !strcmp( psz_eltname, "image" ) )
675                     {
676                         b_is_image = VLC_TRUE;
677                     }
678                     break;
679
680                 case XML_READER_ENDELEM:
681                     if( psz_eltname )
682                     {
683                         free( psz_eltname );
684                         psz_eltname = NULL;
685                     }
686                     psz_eltname = xml_ReaderName( p_xml_reader );
687                     if( !psz_eltname )
688                     {
689                         return 1;
690                     }
691 #                   ifdef RSS_DEBUG
692                     msg_Dbg( p_filter, "element end : %s", psz_eltname );
693 #                   endif
694                     if( !strcmp( psz_eltname, "item" )
695                      || !strcmp( psz_eltname, "entry" ) )
696                     {
697                         b_is_item = VLC_FALSE;
698                         i_item++;
699                     }
700                     else if( !strcmp( psz_eltname, "image" ) )
701                     {
702                         b_is_image = VLC_FALSE;
703                     }
704                     free( psz_eltname );
705                     psz_eltname = NULL;
706                     break;
707
708                 case XML_READER_TEXT:
709                     if( !psz_eltname ) break;
710                     psz_eltvalue = xml_ReaderValue( p_xml_reader );
711                     if( !psz_eltvalue )
712                     {
713                         return 1;
714                     }
715                     else
716                     {
717                         char *psz_clean;
718                         psz_clean = removeWhiteChars( psz_eltvalue );
719                         free( psz_eltvalue ); psz_eltvalue = psz_clean;
720                     }
721 #                   ifdef RSS_DEBUG
722                     msg_Dbg( p_filter, "  text : <%s>", psz_eltvalue );
723 #                   endif
724                     if( b_is_item == VLC_TRUE )
725                     {
726                         struct rss_item_t *p_item;
727                         p_item = p_feed->p_items+i_item;
728                         if( !strcmp( psz_eltname, "title" ) )
729                         {
730                             p_item->psz_title = psz_eltvalue;
731                         }
732                         else if( !strcmp( psz_eltname, "link" ) )
733                         {
734                             p_item->psz_link = psz_eltvalue;
735                         }
736                         else if( !strcmp( psz_eltname, "description" ) )
737                         {
738                             p_item->psz_description = psz_eltvalue;
739                         }
740                         else
741                         {
742                             free( psz_eltvalue );
743                             psz_eltvalue = NULL;
744                         }
745                     }
746                     else if( b_is_image == VLC_TRUE )
747                     {
748                         if( !strcmp( psz_eltname, "url" ) )
749                         {
750                             p_feed->psz_image = psz_eltvalue;
751                             if( p_sys->b_images == VLC_TRUE )
752                                 p_feed->p_pic = LoadImage( p_filter,
753                                                            p_feed->psz_image );
754                         }
755                         else
756                         {
757                             free( psz_eltvalue );
758                             psz_eltvalue = NULL;
759                         }
760                     }
761                     else
762                     {
763                         if( !strcmp( psz_eltname, "title" ) )
764                         {
765                             p_feed->psz_title = psz_eltvalue;
766                         }
767                         else if( !strcmp( psz_eltname, "link" ) )
768                         {
769                             p_feed->psz_link = psz_eltvalue;
770                         }
771                         else if( !strcmp( psz_eltname, "description" )
772                               || !strcmp( psz_eltname, "subtitle" ) )
773                         {
774                             p_feed->psz_description = psz_eltvalue;
775                         }
776                         else
777                         {
778                             free( psz_eltvalue );
779                             psz_eltvalue = NULL;
780                         }
781                     }
782                     break;
783             }
784         }
785
786         if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
787         if( p_stream ) stream_Delete( p_stream );
788         msg_Dbg( p_filter, "Done with %s RSS feed.", psz_feed );
789     }
790     free( psz_buffer_2 );
791     if( p_xml ) xml_Delete( p_xml );
792
793     return 0;
794 }
795
796 /****************************************************************************
797  * FreeRSS
798  ***************************************************************************/
799 static void FreeRSS( filter_t *p_filter)
800 {
801     filter_sys_t *p_sys = p_filter->p_sys;
802
803     struct rss_item_t *p_item;
804     struct rss_feed_t *p_feed;
805
806     int i_feed;
807     int i_item;
808
809     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
810     {
811         p_feed = p_sys->p_feeds+i_feed;
812         for( i_item = 0; i_item < p_feed->i_items; i_item++ )
813         {
814             p_item = p_feed->p_items+i_item;
815             free( p_item->psz_title );
816             free( p_item->psz_link );
817             free( p_item->psz_description );
818         }
819         free( p_feed->p_items );
820         free( p_feed->psz_title);
821         free( p_feed->psz_link );
822         free( p_feed->psz_description );
823         free( p_feed->psz_image );
824         if( p_feed->p_pic != NULL )
825             p_feed->p_pic->pf_release( p_feed->p_pic );
826     }
827     free( p_sys->p_feeds );
828     p_sys->i_feeds = 0;
829 }