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