]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/rss.c
rss.c: add some Atom stuff and some extra checks.
[vlc] / modules / video_filter / rss.c
index 2ae3371e5880734bc5441d582d470e94714b5d1e..04d05bbd36421b8020eda4759e0947fb1194f4c6 100644 (file)
@@ -1,10 +1,10 @@
 /*****************************************************************************
- * rss.c : rss feed display video plugin for vlc
+ * rss.c : rss/atom feed display video plugin for vlc
  *****************************************************************************
- * Copyright (C) 2003-2005 the VideoLAN team
+ * Copyright (C) 2003-2006 the VideoLAN team
  * $Id$
  *
- * Authors: Antoine Cellerier <dionoea@videolan.org>
+ * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Atom : http://www.ietf.org/rfc/rfc4287.txt
+ * RSS : http://www.rssboard.org/rss-specification
  *****************************************************************************/
 
 /*****************************************************************************
 
 #include "vlc_filter.h"
 #include "vlc_block.h"
-#include "osd.h"
+#include "vlc_osd.h"
 
 #include "vlc_block.h"
 #include "vlc_stream.h"
 #include "vlc_xml.h"
+#include "charset.h"
+
+#include "vlc_image.h"
 
 /*****************************************************************************
  * Local prototypes
@@ -46,6 +54,7 @@ static void DestroyFilter( vlc_object_t * );
 static subpicture_t *Filter( filter_t *, mtime_t );
 
 static int FetchRSS( filter_t * );
+static void FreeRSS( filter_t * );
 
 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
                0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
@@ -73,6 +82,8 @@ struct rss_feed_t
     char *psz_title;
     char *psz_description;
     char *psz_link;
+    char *psz_image;
+    picture_t *p_pic;
 
     int i_items;
     struct rss_item_t *p_items;
@@ -80,6 +91,9 @@ struct rss_feed_t
 
 struct filter_sys_t
 {
+    vlc_mutex_t lock;
+    vlc_mutex_t *p_lock;
+
     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
     int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
     int i_speed;
@@ -87,29 +101,33 @@ struct filter_sys_t
 
     char *psz_marquee;    /* marquee string */
 
-    int  i_font_color, i_font_opacity, i_font_size; /* font control */
+    text_style_t *p_style; /* font control */
 
     mtime_t last_date;
 
-    //vlc_bool_t b_need_update;
-
     char *psz_urls;
     int i_feeds;
     struct rss_feed_t *p_feeds;
 
+    int i_ttl;
+    time_t t_last_update;
+    vlc_bool_t b_images;
+
     int i_cur_feed;
     int i_cur_item;
     int i_cur_char;
 };
 
-// <TODO>
-#define MSG_TEXT N_("RSS feed URLs")
-#define MSG_LONGTEXT N_("RSS feed comma(TODO ?) seperated URLs")
-#define SPEED_TEXT N_("RSS feed speed")
-#define SPEED_LONGTEXT N_("RSS feed speed (bigger is slower)")
-#define LENGTH_TEXT N_("RSS feed max number of chars displayed")
-#define LENGTH_LONGTEXT N_("RSS feed max number of chars displayed")
-// </TODO>
+#define MSG_TEXT N_("RSS/Atom feed URLs")
+#define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) seperated URLs")
+#define SPEED_TEXT N_("RSS/Atom feed speed")
+#define SPEED_LONGTEXT N_("RSS/Atom feed speed (bigger is slower)")
+#define LENGTH_TEXT N_("RSS/Atom feed max number of chars displayed")
+#define LENGTH_LONGTEXT N_("RSS/Atom feed max number of chars displayed")
+#define TTL_TEXT N_("Number of seconds between each forced refresh of the feeds")
+#define TTL_LONGTEXT N_("Number of seconds between each forced refresh of the feeds. If 0, the feeds will never be updated.")
+#define IMAGE_TEXT N_("Display feed images if available")
+#define IMAGE_LONGTEXT N_("Display feed images if available")
 
 #define POSX_TEXT N_("X offset, from left")
 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
@@ -143,23 +161,24 @@ static char *ppsz_pos_descriptions[] =
  *****************************************************************************/
 vlc_module_begin();
     set_capability( "sub filter", 0 );
-    set_shortname( N_("RSS" ));
+    set_shortname( "RSS / Atom" );
     set_callbacks( CreateFilter, DestroyFilter );
     set_category( CAT_VIDEO );
     set_subcategory( SUBCAT_VIDEO_SUBPIC );
     add_string( "rss-urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
 
     set_section( N_("Position"), NULL );
-    add_integer( "rss-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
-    add_integer( "rss-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
-    add_integer( "rss-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
+    add_integer( "rss-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
+    add_integer( "rss-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
+    add_integer( "rss-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
+        change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
 
     set_section( N_("Font"), NULL );
     /* 5 sets the default to top [1] left [4] */
-    change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
     add_integer_with_range( "rss-opacity", 255, 0, 255, NULL,
         OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
-    add_integer( "rss-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT, VLC_TRUE );
+    add_integer( "rss-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
+                  VLC_FALSE );
         change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
     add_integer( "rss-size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
 
@@ -168,9 +187,12 @@ vlc_module_begin();
                  VLC_FALSE );
     add_integer( "rss-length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
                  VLC_FALSE );
+    add_integer( "rss-ttl", 1800, NULL, TTL_TEXT, TTL_LONGTEXT, VLC_FALSE );
+    add_bool( "rss-images", 1, NULL, IMAGE_TEXT, IMAGE_LONGTEXT, VLC_FALSE );
 
-    set_description( _("RSS feed display sub filter") );
+    set_description( _("RSS and Atom feed display") );
     add_shortcut( "rss" );
+    add_shortcut( "atom" );
 vlc_module_end();
 
 /*****************************************************************************
@@ -190,37 +212,61 @@ static int CreateFilter( vlc_object_t *p_this )
         return VLC_ENOMEM;
     }
 
+    vlc_mutex_init( p_filter, &p_sys->lock );
+    vlc_mutex_lock( &p_sys->lock );
+
     p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
     p_sys->i_cur_feed = 0;
     p_sys->i_cur_item = 0;
     p_sys->i_cur_char = 0;
+    p_sys->i_feeds = 0;
+    p_sys->p_feeds = NULL;
     p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
     p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
+    p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, "rss-ttl" ) );
+    p_sys->b_images = var_CreateGetBool( p_filter, "rss-images" );
     p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
 
+    p_sys->p_style = malloc( sizeof( text_style_t ));
+    memcpy( p_sys->p_style, &default_text_style, sizeof( text_style_t ));
+
     p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
     p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
     p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
-    var_Create( p_filter, "rss-opacity", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
-    p_sys->i_font_opacity = var_CreateGetInteger( p_filter, "rss-opacity" );
-    p_sys->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
-    p_sys->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
+    p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, "rss-opacity" );
+    p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
+    p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
+
+    if( p_sys->b_images == VLC_TRUE && p_sys->p_style->i_font_size == -1 )
+    {
+        msg_Warn( p_filter, "rrs-size wasn't specified. Feed images will thus be displayed without being resized" );
+    }
 
     if( FetchRSS( p_filter ) )
     {
         msg_Err( p_filter, "failed while fetching RSS ... too bad" );
+        vlc_mutex_unlock( &p_sys->lock );
         return VLC_EGENERIC;
     }
+    p_sys->t_last_update = time( NULL );
 
-    if( p_sys->i_feeds == 0 ) return VLC_EGENERIC;
+    if( p_sys->i_feeds == 0 )
+    {
+        vlc_mutex_unlock( &p_sys->lock );
+        return VLC_EGENERIC;
+    }
     for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
         if( p_sys->p_feeds[i_feed].i_items == 0 )
+        {
+            vlc_mutex_unlock( &p_sys->lock );
             return VLC_EGENERIC;
+        }
 
     /* Misc init */
     p_filter->pf_sub_filter = Filter;
     p_sys->last_date = (mtime_t)0;
-    //p_sys->b_need_update = VLC_TRUE;
+
+    vlc_mutex_unlock( &p_sys->lock );
 
     return VLC_SUCCESS;
 }
@@ -232,15 +278,22 @@ static void DestroyFilter( vlc_object_t *p_this )
     filter_t *p_filter = (filter_t *)p_this;
     filter_sys_t *p_sys = p_filter->p_sys;
 
+    vlc_mutex_lock( &p_sys->lock );
+
+    if( p_sys->p_style ) free( p_sys->p_style );
     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
     free( p_sys->psz_urls );
-    /* TODO : free RSS feeds stuff */
+    FreeRSS( p_filter );
+    vlc_mutex_unlock( &p_sys->lock );
+    vlc_mutex_destroy( &p_sys->lock );
     free( p_sys );
 
     /* Delete the RSS variables */
     var_Destroy( p_filter, "rss-urls" );
     var_Destroy( p_filter, "rss-speed" );
     var_Destroy( p_filter, "rss-length" );
+    var_Destroy( p_filter, "rss-ttl" );
+    var_Destroy( p_filter, "rss-images" );
     var_Destroy( p_filter, "rss-x" );
     var_Destroy( p_filter, "rss-y" );
     var_Destroy( p_filter, "rss-position" );
@@ -258,12 +311,39 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
     subpicture_t *p_spu;
-    video_format_t fmt;
+    video_format_t fmt = {0};
+
+    subpicture_region_t *p_region;
 
     int i_feed, i_item;
 
-    /* wait more for the 1st char */
-    if( p_sys->last_date + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 ) * p_sys->i_speed > date ) return NULL;
+    struct rss_feed_t *p_feed;
+
+    vlc_mutex_lock( &p_sys->lock );
+
+    if( p_sys->last_date
+       + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 )
+           /* ( ... ? 5 : 1 ) means "wait more for the 1st char" */
+       * p_sys->i_speed > date )
+    {
+        vlc_mutex_unlock( &p_sys->lock );
+        return NULL;
+    }
+
+    /* Do we need to update the feeds ? */
+    if( p_sys->i_ttl
+        && time( NULL ) > p_sys->t_last_update + (time_t)p_sys->i_ttl )
+    {
+        msg_Dbg( p_filter, "Forcing update of all the RSS feeds" );
+        if( FetchRSS( p_filter ) )
+        {
+            msg_Err( p_filter, "failed while fetching RSS ... too bad" );
+            vlc_mutex_unlock( &p_sys->lock );
+            return NULL; /* FIXME : we most likely messed up all the data,
+                          * so we might need to do something about it */
+        }
+        p_sys->t_last_update = time( NULL );
+    }
 
     p_sys->last_date = date;
     p_sys->i_cur_char++;
@@ -278,38 +358,68 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         }
     }
 
-
-/* We always need an update
-    if( p_sys->b_need_update == VLC_FALSE )
+    p_spu = p_filter->pf_sub_buffer_new( p_filter );
+    if( !p_spu )
     {
+        vlc_mutex_unlock( &p_sys->lock );
         return NULL;
-    }*/
-
-    p_spu = p_filter->pf_sub_buffer_new( p_filter );
-    if( !p_spu ) return NULL;
+    }
 
-    memset( &fmt, 0, sizeof(video_format_t) );
     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
-    fmt.i_aspect = 0;
-    fmt.i_width = 0;
-    fmt.i_height = 0;
-    fmt.i_x_offset = 0;
-    fmt.i_y_offset = 0;
+
     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
     if( !p_spu->p_region )
     {
         p_filter->pf_sub_buffer_del( p_filter, p_spu );
+        vlc_mutex_unlock( &p_sys->lock );
         return NULL;
     }
 
+    /* Generate the string that will be displayed. This string is supposed to
+       be p_sys->i_length characters long. */
     i_item = p_sys->i_cur_item;
     i_feed = p_sys->i_cur_feed;
-    snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s", p_sys->p_feeds[i_feed].psz_title, p_sys->p_feeds[i_feed].p_items[i_item].psz_title+p_sys->i_cur_char );
+    p_feed = &p_sys->p_feeds[p_sys->i_cur_feed];
+
+    if( p_feed->p_pic )
+    {
+        /* Don't display the feed's title if we have an image */
+        snprintf( p_sys->psz_marquee, p_sys->i_length, "%s",
+                  p_sys->p_feeds[i_feed].p_items[i_item].psz_title
+                  +p_sys->i_cur_char );
+    }
+    else
+    {
+        snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s",
+                  p_sys->p_feeds[i_feed].psz_title,
+                  p_sys->p_feeds[i_feed].p_items[i_item].psz_title
+                  +p_sys->i_cur_char );
+    }
+
     while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
     {
         i_item++;
         if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
-        snprintf( strchr( p_sys->psz_marquee, 0 ), p_sys->i_length - strlen( p_sys->psz_marquee ), " - %s", p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
+        snprintf( strchr( p_sys->psz_marquee, 0 ),
+                  p_sys->i_length - strlen( p_sys->psz_marquee ),
+                  " - %s",
+                  p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
+    }
+
+    /* Calls to snprintf might split multibyte UTF8 chars ...
+     * which freetype doesn't like. */
+    {
+        char *a = strdup( p_sys->psz_marquee );
+        char *a2 = a;
+        char *b = p_sys->psz_marquee;
+        EnsureUTF8( p_sys->psz_marquee );
+        /* we want to use ' ' instead of '?' for erroneous chars */
+        while( *b != '\0' )
+        {
+            if( *b != *a ) *b = ' ';
+            b++;a++;
+        }
+        free( a2 );
     }
 
     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
@@ -332,16 +442,130 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
         p_spu->i_y = p_sys->i_yoff;
         p_spu->b_absolute = VLC_TRUE;
     }
+
     p_spu->i_height = 1;
-    p_spu->p_region->i_text_color = p_sys->i_font_color;
-    p_spu->p_region->i_text_alpha = 255 - p_sys->i_font_opacity;
-    p_spu->p_region->i_text_size = p_sys->i_font_size;
+    p_spu->p_region->p_style = p_sys->p_style;
 
+    if( p_feed->p_pic )
+    {
+        /* Display the feed's image */
+        picture_t *p_pic = p_feed->p_pic;
+        video_format_t fmt_out = {0};
+
+        fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
+        fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
+        fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
+        fmt_out.i_width =
+            fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
+        fmt_out.i_height =
+            fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
+
+        p_region = p_spu->pf_create_region( VLC_OBJECT( p_filter ), &fmt_out );
+        if( !p_region )
+        {
+            msg_Err( p_filter, "cannot allocate SPU region" );
+        }
+        else
+        {
+            vout_CopyPicture( p_filter, &p_region->picture, p_pic );
+            p_spu->p_region->p_next = p_region;
+        }
 
-    //p_sys->b_need_update = VLC_FALSE;
+        /* Offset text to display right next to the image */
+        p_spu->p_region->i_x = p_pic->p[Y_PLANE].i_visible_pitch;
+    }
+
+    vlc_mutex_unlock( &p_sys->lock );
     return p_spu;
 }
 
+/****************************************************************************
+ * RSS related functions
+ ****************************************************************************
+ * You should always lock the p_filter mutex before using any of these
+ * functions
+ ***************************************************************************/
+
+#undef LoadImage /* do not conflict with Win32 API */
+
+/****************************************************************************
+ * download and resize image located at psz_url
+ ***************************************************************************/
+picture_t *LoadImage( filter_t *p_filter, const char *psz_url )
+{
+    filter_sys_t *p_sys = p_filter->p_sys;
+
+    video_format_t fmt_in={0}, fmt_out={0};
+    picture_t *p_orig, *p_pic=NULL;
+    image_handler_t *p_handler = image_HandlerCreate( p_filter );
+
+    char *psz_local;
+
+    psz_local = ToLocale( psz_url );
+    fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
+    p_orig = image_ReadUrl( p_handler, psz_local, &fmt_in, &fmt_out );
+    LocaleFree( psz_local );
+
+    if( !p_orig )
+    {
+        msg_Warn( p_filter, "Unable to read image %s", psz_url );
+    }
+    else if( p_sys->p_style->i_font_size > 0 )
+    {
+
+        fmt_in.i_chroma = VLC_FOURCC('Y','U','V','A');
+        fmt_in.i_height = p_orig->p[Y_PLANE].i_visible_lines;
+        fmt_in.i_width = p_orig->p[Y_PLANE].i_visible_pitch;
+        fmt_out.i_width = p_orig->p[Y_PLANE].i_visible_pitch
+            *p_sys->p_style->i_font_size/p_orig->p[Y_PLANE].i_visible_lines;
+        fmt_out.i_height = p_sys->p_style->i_font_size;
+
+        p_pic = image_Convert( p_handler, p_orig, &fmt_in, &fmt_out );
+        p_orig->pf_release( p_orig );
+        if( !p_pic )
+        {
+            msg_Warn( p_filter, "Error while converting %s", psz_url );
+        }
+    }
+    else
+    {
+        p_pic = p_orig;
+    }
+
+    image_HandlerDelete( p_handler );
+
+    return p_pic;
+}
+
+/****************************************************************************
+ * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the
+ * string.
+ ***************************************************************************/
+char *removeWhiteChars( char *psz_src )
+{
+    char *psz_src2 = strdup( psz_src );
+    char *psz_clean = strdup( psz_src2 );
+    char *psz_clean2;
+    int i;
+    while( ( *psz_clean == ' ' || *psz_clean == '\t'
+           || *psz_clean == '\n' || *psz_clean == '\r' )
+           && *psz_clean != '\0' )
+    {
+        psz_clean++;
+    }
+    i = strlen( psz_clean );
+    while( --i > 0 &&
+         ( psz_clean[i] == ' ' || psz_clean[i] == '\t'
+        || psz_clean[i] == '\n' || psz_clean[i] == '\r' ) );
+    psz_clean[i+1] = '\0';
+    psz_clean2 = strdup( psz_clean );
+    free( psz_src2 );
+    return psz_clean2;
+}
+
+/****************************************************************************
+ * FetchRSS (or Atom) feeds
+ ***************************************************************************/
 static int FetchRSS( filter_t *p_filter)
 {
     filter_sys_t *p_sys = p_filter->p_sys;
@@ -354,16 +578,19 @@ static int FetchRSS( filter_t *p_filter)
     char *psz_eltvalue = NULL;
     char *psz_feed = NULL;
     char *psz_buffer = NULL;
+    char *psz_buffer_2 = NULL;
 
     int i_feed;
     int i_item;
-    int i_is_item;
+    vlc_bool_t b_is_item;
+    vlc_bool_t b_is_image;
     int i_int;
 
+    FreeRSS( p_filter );
     p_sys->i_feeds = 1;
     i_int = 0;
     while( p_sys->psz_urls[i_int] != 0 )
-        if( p_sys->psz_urls[i_int++] == ',' )
+        if( p_sys->psz_urls[i_int++] == '|' )
             p_sys->i_feeds++;
     p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
                                 * sizeof( struct rss_feed_t ) );
@@ -376,6 +603,7 @@ static int FetchRSS( filter_t *p_filter)
     }
 
     psz_buffer = strdup( p_sys->psz_urls );
+    psz_buffer_2 = psz_buffer; /* keep track so we can free it */
     for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
     {
         struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
@@ -383,16 +611,18 @@ static int FetchRSS( filter_t *p_filter)
         if( psz_buffer == NULL ) break;
         if( psz_buffer[0] == 0 ) psz_buffer++;
         psz_feed = psz_buffer;
-        psz_buffer = strchr( psz_buffer, ',' );
+        psz_buffer = strchr( psz_buffer, '|' );
         if( psz_buffer != NULL ) psz_buffer[0] = 0;
 
         p_feed->psz_title = NULL;
         p_feed->psz_description = NULL;
         p_feed->psz_link = NULL;
+        p_feed->psz_image = NULL;
+        p_feed->p_pic = NULL;
         p_feed->i_items = 0;
         p_feed->p_items = NULL;
 
-        msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
+        msg_Dbg( p_filter, "Opening %s RSS/Atom feed ...", psz_feed );
 
         p_stream = stream_UrlNew( p_filter, psz_feed );
         if( !p_stream )
@@ -409,7 +639,8 @@ static int FetchRSS( filter_t *p_filter)
         }
 
         i_item = 0;
-        i_is_item = VLC_FALSE;
+        b_is_item = VLC_FALSE;
+        b_is_image = VLC_FALSE;
 
         while( xml_ReaderRead( p_xml_reader ) == 1 )
         {
@@ -420,92 +651,252 @@ static int FetchRSS( filter_t *p_filter)
                     return 1;
 
                 case XML_READER_STARTELEM:
-                    if( psz_eltname ) free( psz_eltname );
+                    if( psz_eltname )
+                    {
+                        free( psz_eltname );
+                        psz_eltname = NULL;
+                    }
                     psz_eltname = xml_ReaderName( p_xml_reader );
                     if( !psz_eltname )
                     {
                         return 1;
                     }
+#                   ifdef RSS_DEBUG
                     msg_Dbg( p_filter, "element name : %s", psz_eltname );
-                    if( !strcmp( psz_eltname, "item" ) )
+#                   endif
+                    if( !strcmp( psz_eltname, "item" ) /* rss */
+                     || !strcmp( psz_eltname, "entry" ) ) /* atom */
                     {
-                        i_is_item = VLC_TRUE;
+                        b_is_item = VLC_TRUE;
                         p_feed->i_items++;
                         p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
+                        p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
+                        p_feed->p_items[p_feed->i_items-1].psz_description
+                                                                     = NULL;
+                        p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
+                    }
+                    else if( !strcmp( psz_eltname, "image" ) ) /* rss */
+                    {
+                        b_is_image = VLC_TRUE;
+                    }
+                    else if( !strcmp( psz_eltname, "link" ) ) /* atom */
+                    {
+                        char *psz_href = NULL;
+                        char *psz_rel = NULL;
+                        while( xml_ReaderNextAttr( p_xml_reader )
+                               == VLC_SUCCESS )
+                        {
+                            char *psz_name = xml_ReaderName( p_xml_reader );
+                            char *psz_value = xml_ReaderValue( p_xml_reader );
+                            if( !strcmp( psz_name, "rel" ) )
+                            {
+                                psz_rel = psz_value;
+                            }
+                            else if( !strcmp( psz_name, "href" ) )
+                            {
+                                psz_href = psz_value;
+                            }
+                            else
+                            {
+                                free( psz_value );
+                            }
+                            free( psz_name );
+                        }
+                        if( psz_rel && psz_href )
+                        {
+                            if( !strcmp( psz_rel, "alternate" )
+                                && b_is_item == VLC_FALSE
+                                && b_is_image == VLC_FALSE
+                                && !p_feed->psz_link )
+                            {
+                                p_feed->psz_link = psz_href;
+                            }
+                            /* this isn't in the rfc but i found some ... */
+                            else if( ( !strcmp( psz_rel, "logo" )
+                                    || !strcmp( psz_rel, "icon" ) )
+                                    && b_is_item == VLC_FALSE
+                                    && b_is_image == VLC_FALSE
+                                    && !p_feed->psz_image )
+                            {
+                                p_feed->psz_image = psz_href;
+                            }
+                            else
+                            {
+                                free( psz_href );
+                            }
+                        }
+                        else
+                        {
+                            if( psz_href ) free( psz_href );
+                        }
+                        if( psz_rel ) free( psz_rel );
                     }
                     break;
 
                 case XML_READER_ENDELEM:
-                    if( psz_eltname ) free( psz_eltname );
+                    if( psz_eltname )
+                    {
+                        free( psz_eltname );
+                        psz_eltname = NULL;
+                    }
                     psz_eltname = xml_ReaderName( p_xml_reader );
                     if( !psz_eltname )
                     {
                         return 1;
                     }
+#                   ifdef RSS_DEBUG
                     msg_Dbg( p_filter, "element end : %s", psz_eltname );
-                    if( !strcmp( psz_eltname, "item" ) )
+#                   endif
+                    if( !strcmp( psz_eltname, "item" ) /* rss */
+                     || !strcmp( psz_eltname, "entry" ) ) /* atom */
                     {
-                        i_is_item = VLC_FALSE;
+                        b_is_item = VLC_FALSE;
                         i_item++;
                     }
-                    free( psz_eltname ); psz_eltname = NULL;
+                    else if( !strcmp( psz_eltname, "image" ) ) /* rss */
+                    {
+                        b_is_image = VLC_FALSE;
+                    }
+                    free( psz_eltname );
+                    psz_eltname = NULL;
                     break;
 
                 case XML_READER_TEXT:
+                    if( !psz_eltname ) break;
                     psz_eltvalue = xml_ReaderValue( p_xml_reader );
                     if( !psz_eltvalue )
                     {
                         return 1;
                     }
-                    msg_Dbg( p_filter, "  text : %s", psz_eltvalue );
-                    if( i_is_item == VLC_FALSE )
+                    else
+                    {
+                        char *psz_clean;
+                        psz_clean = removeWhiteChars( psz_eltvalue );
+                        free( psz_eltvalue ); psz_eltvalue = psz_clean;
+                    }
+#                   ifdef RSS_DEBUG
+                    msg_Dbg( p_filter, "  text : <%s>", psz_eltvalue );
+#                   endif
+                    if( b_is_item == VLC_TRUE )
                     {
-                        if( !strcmp( psz_eltname, "title" ) )
+                        struct rss_item_t *p_item;
+                        p_item = p_feed->p_items+i_item;
+                        if( !strcmp( psz_eltname, "title" ) /* rss/atom */
+                            && !p_item->psz_title )
                         {
-                            p_feed->psz_title = psz_eltvalue;
+                            p_item->psz_title = psz_eltvalue;
                         }
-                        else if( !strcmp( psz_eltname, "link" ) )
+                        else if( !strcmp( psz_eltname, "link" ) /* rss */
+                                 && !p_item->psz_link )
                         {
-                            p_feed->psz_link = psz_eltvalue;
+                            p_item->psz_link = psz_eltvalue;
                         }
-                        else if( !strcmp( psz_eltname, "description" ) )
+                        else if((!strcmp( psz_eltname, "description" ) /* rss */
+                              || !strcmp( psz_eltname, "summary" ) ) /* atom */
+                              && !p_item->psz_description )
                         {
-                            p_feed->psz_description = psz_eltvalue;
+                            p_item->psz_description = psz_eltvalue;
+                        }
+                        else
+                        {
+                            free( psz_eltvalue );
+                            psz_eltvalue = NULL;
+                        }
+                    }
+                    else if( b_is_image == VLC_TRUE )
+                    {
+                        if( !strcmp( psz_eltname, "url" ) /* rss */
+                            && !p_feed->psz_image )
+                        {
+                            p_feed->psz_image = psz_eltvalue;
                         }
                         else
                         {
                             free( psz_eltvalue );
+                            psz_eltvalue = NULL;
                         }
                     }
                     else
                     {
-                        struct rss_item_t *p_item;
-                        p_item = p_feed->p_items+i_item;
-                        if( !strcmp( psz_eltname, "title" ) )
+                        if( !strcmp( psz_eltname, "title" ) /* rss/atom */
+                            && !p_feed->psz_title )
                         {
-                            p_item->psz_title = psz_eltvalue;
+                            p_feed->psz_title = psz_eltvalue;
                         }
-                        else if( !strcmp( psz_eltname, "link" ) )
+                        else if( !strcmp( psz_eltname, "link" ) /* rss */
+                                 && !p_feed->psz_link )
                         {
-                            p_item->psz_link = psz_eltvalue;
+                            p_feed->psz_link = psz_eltvalue;
                         }
-                        else if( !strcmp( psz_eltname, "description" ) )
+                        else if((!strcmp( psz_eltname, "description" ) /* rss */
+                              || !strcmp( psz_eltname, "subtitle" ) ) /* atom */
+                              && !p_feed->psz_description )
                         {
-                            p_item->psz_description = psz_eltvalue;
+                            p_feed->psz_description = psz_eltvalue;
+                        }
+                        else if( ( !strcmp( psz_eltname, "logo" ) /* atom */
+                              || !strcmp( psz_eltname, "icon" ) ) /* atom */
+                              && !p_feed->psz_image )
+                        {
+                            p_feed->psz_image = psz_eltvalue;
                         }
                         else
                         {
                             free( psz_eltvalue );
+                            psz_eltvalue = NULL;
                         }
                     }
                     break;
             }
         }
 
+        if( p_sys->b_images == VLC_TRUE
+            && p_feed->psz_image && !p_feed->p_pic )
+        {
+            p_feed->p_pic = LoadImage( p_filter, p_feed->psz_image );
+        }
+
         if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
         if( p_stream ) stream_Delete( p_stream );
+        msg_Dbg( p_filter, "Done with %s RSS/Atom feed.", psz_feed );
     }
+    free( psz_buffer_2 );
     if( p_xml ) xml_Delete( p_xml );
 
     return 0;
 }
+
+/****************************************************************************
+ * FreeRSS
+ ***************************************************************************/
+static void FreeRSS( filter_t *p_filter)
+{
+    filter_sys_t *p_sys = p_filter->p_sys;
+
+    struct rss_item_t *p_item;
+    struct rss_feed_t *p_feed;
+
+    int i_feed;
+    int i_item;
+
+    for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
+    {
+        p_feed = p_sys->p_feeds+i_feed;
+        for( i_item = 0; i_item < p_feed->i_items; i_item++ )
+        {
+            p_item = p_feed->p_items+i_item;
+            free( p_item->psz_title );
+            free( p_item->psz_link );
+            free( p_item->psz_description );
+        }
+        free( p_feed->p_items );
+        free( p_feed->psz_title);
+        free( p_feed->psz_link );
+        free( p_feed->psz_description );
+        free( p_feed->psz_image );
+        if( p_feed->p_pic != NULL )
+            p_feed->p_pic->pf_release( p_feed->p_pic );
+    }
+    free( p_sys->p_feeds );
+    p_sys->i_feeds = 0;
+}