X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fvideo_filter%2Frss.c;h=885bfc9154a9f7314484a60af6eea545981e97ef;hb=396236ce7c5034845c15559d420ca82c3f24d56f;hp=5c6e4c8192573b6b054c9598caa44855ae0244b2;hpb=74e5a0727b83ea8bd5ee87dbce7596b990ef14c1;p=vlc diff --git a/modules/video_filter/rss.c b/modules/video_filter/rss.c index 5c6e4c8192..885bfc9154 100644 --- a/modules/video_filter/rss.c +++ b/modules/video_filter/rss.c @@ -1,24 +1,25 @@ /***************************************************************************** * rss.c : rss/atom feed display video plugin for vlc ***************************************************************************** - * Copyright (C) 2003-2006 the VideoLAN team + * Copyright (C) 2003-2006 VLC authors and VideoLAN * $Id$ * * Authors: Antoine Cellerier + * Rémi Duraffort * - * 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 - * the Free Software Foundation; either version 2 of the License, or + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** @@ -37,16 +38,14 @@ #include #include -#include "vlc_filter.h" -#include "vlc_block.h" -#include "vlc_osd.h" +#include +#include -#include "vlc_block.h" -#include "vlc_stream.h" -#include "vlc_xml.h" +#include +#include #include -#include "vlc_image.h" +#include #include @@ -57,8 +56,11 @@ static int CreateFilter ( vlc_object_t * ); 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 struct rss_feed_t *FetchRSS( filter_t * ); +static void FreeRSS( struct rss_feed_t *, int ); +static int ParseUrls( filter_t *, char * ); + +static void Fetch( void * ); static const int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0, @@ -76,15 +78,16 @@ static const char *const ppsz_color_descriptions[] = { * filter_sys_t: rss filter descriptor *****************************************************************************/ -struct rss_item_t +typedef struct rss_item_t { char *psz_title; char *psz_description; char *psz_link; -}; +} rss_item_t; -struct rss_feed_t +typedef struct rss_feed_t { + char *psz_url; char *psz_title; char *psz_description; char *psz_link; @@ -92,13 +95,14 @@ struct rss_feed_t picture_t *p_pic; int i_items; - struct rss_item_t *p_items; -}; + rss_item_t *p_items; +} rss_feed_t; struct filter_sys_t { vlc_mutex_t lock; - vlc_mutex_t *p_lock; + vlc_timer_t timer; /* Timer to refresh the rss feeds */ + bool b_fetched; 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) */ @@ -111,12 +115,9 @@ struct filter_sys_t mtime_t last_date; - char *psz_urls; int i_feeds; - struct rss_feed_t *p_feeds; + rss_feed_t *p_feeds; - int i_ttl; - time_t t_last_update; bool b_images; int i_title; @@ -126,7 +127,7 @@ struct filter_sys_t }; #define MSG_TEXT N_("Feed URLs") -#define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) seperated URLs.") +#define MSG_LONGTEXT N_("RSS/Atom feed '|' (pipe) separated URLs.") #define SPEED_TEXT N_("Speed of feeds") #define SPEED_LONGTEXT N_("Speed of the RSS/Atom feeds in microseconds (bigger is slower).") #define LENGTH_TEXT N_("Max length") @@ -165,6 +166,8 @@ struct filter_sys_t #define TITLE_TEXT N_("Title display mode") #define TITLE_LONGTEXT N_("Title display mode. Default is 0 (hidden) if the feed has an image and feed images are enabled, 1 otherwise.") +#define RSS_HELP N_("Display a RSS or ATOM Feed on your video") + static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 }; static const char *const ppsz_pos_descriptions[] = { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"), @@ -186,45 +189,47 @@ static const char *const ppsz_title_modes[] = * Module descriptor *****************************************************************************/ vlc_module_begin () - set_capability( "sub filter", 1 ) - set_shortname( "RSS / Atom" ) + set_capability( "sub source", 1 ) + set_shortname( N_("RSS / Atom") ) + set_help(RSS_HELP) set_callbacks( CreateFilter, DestroyFilter ) set_category( CAT_VIDEO ) set_subcategory( SUBCAT_VIDEO_SUBPIC ) - add_string( CFG_PREFIX "urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, false ) + add_string( CFG_PREFIX "urls", NULL, MSG_TEXT, MSG_LONGTEXT, false ) set_section( N_("Position"), NULL ) - add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, true ) - add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, true ) - add_integer( CFG_PREFIX "position", -1, NULL, POS_TEXT, POS_LONGTEXT, false ) - change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL ) + add_integer( CFG_PREFIX "x", 0, POSX_TEXT, POSX_LONGTEXT, true ) + add_integer( CFG_PREFIX "y", 0, POSY_TEXT, POSY_LONGTEXT, true ) + add_integer( CFG_PREFIX "position", -1, POS_TEXT, POS_LONGTEXT, false ) + change_integer_list( pi_pos_values, ppsz_pos_descriptions ) set_section( N_("Font"), NULL ) /* 5 sets the default to top [1] left [4] */ - add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255, NULL, + add_integer_with_range( CFG_PREFIX "opacity", 255, 0, 255, OPACITY_TEXT, OPACITY_LONGTEXT, false ) - add_integer( CFG_PREFIX "color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT, + add_rgb( CFG_PREFIX "color", 0xFFFFFF, COLOR_TEXT, COLOR_LONGTEXT, false ) - change_integer_list( pi_color_values, ppsz_color_descriptions, NULL ) - add_integer( CFG_PREFIX "size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, false ) + change_integer_list( pi_color_values, ppsz_color_descriptions ) + add_integer( CFG_PREFIX "size", -1, SIZE_TEXT, SIZE_LONGTEXT, false ) + change_integer_range( -1, 4096) set_section( N_("Misc"), NULL ) - add_integer( CFG_PREFIX "speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT, + add_integer( CFG_PREFIX "speed", 100000, SPEED_TEXT, SPEED_LONGTEXT, false ) - add_integer( CFG_PREFIX "length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT, + add_integer( CFG_PREFIX "length", 60, LENGTH_TEXT, LENGTH_LONGTEXT, false ) - add_integer( CFG_PREFIX "ttl", 1800, NULL, TTL_TEXT, TTL_LONGTEXT, false ) - add_bool( CFG_PREFIX "images", 1, NULL, IMAGE_TEXT, IMAGE_LONGTEXT, false ) - add_integer( CFG_PREFIX "title", default_title, NULL, TITLE_TEXT, TITLE_LONGTEXT, false ) - change_integer_list( pi_title_modes, ppsz_title_modes, NULL ) + add_integer( CFG_PREFIX "ttl", 1800, TTL_TEXT, TTL_LONGTEXT, false ) + add_bool( CFG_PREFIX "images", true, IMAGE_TEXT, IMAGE_LONGTEXT, false ) + add_integer( CFG_PREFIX "title", default_title, TITLE_TEXT, TITLE_LONGTEXT, + false ) + change_integer_list( pi_title_modes, ppsz_title_modes ) set_description( N_("RSS and Atom feed display") ) - add_shortcut( "rss" ) - add_shortcut( "atom" ) + add_shortcut( "rss", "atom" ) vlc_module_end () static const char *const ppsz_filter_options[] = { - "urls", "x", "y", "position", "color", "size", "speed", "length", + "urls", "x", "y", "position", "opacity", "color", "size", "speed", "length", "ttl", "images", "title", NULL }; @@ -235,20 +240,27 @@ static int CreateFilter( vlc_object_t *p_this ) { filter_t *p_filter = (filter_t *)p_this; filter_sys_t *p_sys; - int i_feed; + char *psz_urls; + int i_ttl; /* Allocate structure */ p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) ); if( p_sys == NULL ) return VLC_ENOMEM; - vlc_mutex_init( &p_sys->lock ); - vlc_mutex_lock( &p_sys->lock ); - config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options, p_filter->p_cfg ); - p_sys->psz_urls = var_CreateGetString( p_filter, CFG_PREFIX "urls" ); + /* Get the urls to parse: must be non empty */ + psz_urls = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "urls" ); + if( !psz_urls ) + { + msg_Err( p_filter, "The list of urls must not be empty" ); + free( p_sys ); + return VLC_EGENERIC; + } + + /* Fill the p_sys structure with the configuration */ p_sys->i_title = var_CreateGetInteger( p_filter, CFG_PREFIX "title" ); p_sys->i_cur_feed = 0; p_sys->i_cur_item = p_sys->i_title == scroll_title ? -1 : 0; @@ -257,15 +269,14 @@ static int CreateFilter( vlc_object_t *p_this ) p_sys->p_feeds = NULL; p_sys->i_speed = var_CreateGetInteger( p_filter, CFG_PREFIX "speed" ); p_sys->i_length = var_CreateGetInteger( p_filter, CFG_PREFIX "length" ); - p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, CFG_PREFIX "ttl" ) ); p_sys->b_images = var_CreateGetBool( p_filter, CFG_PREFIX "images" ); - p_sys->psz_marquee = (char *)malloc( p_sys->i_length + 1 ); + i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, CFG_PREFIX "ttl" ) ); + + p_sys->psz_marquee = malloc( p_sys->i_length + 1 ); if( p_sys->psz_marquee == NULL ) { - vlc_mutex_unlock( &p_sys->lock ); - vlc_mutex_destroy( &p_sys->lock ); - free( p_sys->psz_urls ); + free( psz_urls ); free( p_sys ); return VLC_ENOMEM; } @@ -273,71 +284,50 @@ static int CreateFilter( vlc_object_t *p_this ) p_sys->p_style = text_style_New(); if( p_sys->p_style == NULL ) - { - free( p_sys->psz_marquee ); - vlc_mutex_unlock( &p_sys->lock ); - vlc_mutex_destroy( &p_sys->lock ); - free( p_sys->psz_urls ); - free( p_sys ); - return VLC_ENOMEM; - } + goto error; p_sys->i_xoff = var_CreateGetInteger( p_filter, CFG_PREFIX "x" ); p_sys->i_yoff = var_CreateGetInteger( p_filter, CFG_PREFIX "y" ); p_sys->i_pos = var_CreateGetInteger( p_filter, CFG_PREFIX "position" ); - p_sys->p_style->i_font_alpha = 255 - var_CreateGetInteger( p_filter, CFG_PREFIX "opacity" ); + p_sys->p_style->i_font_alpha = var_CreateGetInteger( p_filter, CFG_PREFIX "opacity" ); p_sys->p_style->i_font_color = var_CreateGetInteger( p_filter, CFG_PREFIX "color" ); p_sys->p_style->i_font_size = var_CreateGetInteger( p_filter, CFG_PREFIX "size" ); - if( p_sys->b_images == true && p_sys->p_style->i_font_size == -1 ) + if( p_sys->b_images && p_sys->p_style->i_font_size == -1 ) { - msg_Warn( p_filter, "rss-size wasn't specified. Feed images will thus be displayed without being resized" ); + msg_Warn( p_filter, "rss-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" ); - text_style_Delete( p_sys->p_style ); - free( p_sys->psz_marquee ); - vlc_mutex_unlock( &p_sys->lock ); - vlc_mutex_destroy( &p_sys->lock ); - free( p_sys->psz_urls ); - free( p_sys ); - return VLC_EGENERIC; - } - p_sys->t_last_update = time( NULL ); + /* Parse the urls */ + if( ParseUrls( p_filter, psz_urls ) ) + goto error; - if( p_sys->i_feeds == 0 ) - { - text_style_Delete( p_sys->p_style ); - free( p_sys->psz_marquee ); - vlc_mutex_unlock( &p_sys->lock ); - vlc_mutex_destroy( &p_sys->lock ); - free( p_sys->psz_urls ); - free( p_sys ); - 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 ) - { - text_style_Delete( p_sys->p_style ); - free( p_sys->psz_marquee ); - FreeRSS( p_filter ); - vlc_mutex_unlock( &p_sys->lock ); - vlc_mutex_destroy( &p_sys->lock ); - free( p_sys->psz_urls ); - free( p_sys ); - return VLC_EGENERIC; - } - } /* Misc init */ - p_filter->pf_sub_filter = Filter; + vlc_mutex_init( &p_sys->lock ); + p_filter->pf_sub_source = Filter; p_sys->last_date = (mtime_t)0; + p_sys->b_fetched = false; - vlc_mutex_unlock( &p_sys->lock ); + /* Create and arm the timer */ + if( vlc_timer_create( &p_sys->timer, Fetch, p_filter ) ) + { + vlc_mutex_destroy( &p_sys->lock ); + goto error; + } + vlc_timer_schedule( p_sys->timer, false, 1, + (mtime_t)(i_ttl)*1000000 ); + free( psz_urls ); return VLC_SUCCESS; + +error: + if( p_sys->p_style ) + text_style_Delete( p_sys->p_style ); + free( p_sys->psz_marquee ); + free( psz_urls ); + free( p_sys ); + return VLC_ENOMEM; } /***************************************************************************** * DestroyFilter: destroy RSS video filter @@ -347,29 +337,13 @@ 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 ); + vlc_timer_destroy( p_sys->timer ); + vlc_mutex_destroy( &p_sys->lock ); text_style_Delete( p_sys->p_style ); free( p_sys->psz_marquee ); - free( p_sys->psz_urls ); - FreeRSS( p_filter ); - vlc_mutex_unlock( &p_sys->lock ); - vlc_mutex_destroy( &p_sys->lock ); + FreeRSS( p_sys->p_feeds, p_sys->i_feeds ); free( p_sys ); - - /* Delete the RSS variables */ - var_Destroy( p_filter, CFG_PREFIX "urls" ); - var_Destroy( p_filter, CFG_PREFIX "speed" ); - var_Destroy( p_filter, CFG_PREFIX "length" ); - var_Destroy( p_filter, CFG_PREFIX "ttl" ); - var_Destroy( p_filter, CFG_PREFIX "images" ); - var_Destroy( p_filter, CFG_PREFIX "x" ); - var_Destroy( p_filter, CFG_PREFIX "y" ); - var_Destroy( p_filter, CFG_PREFIX "position" ); - var_Destroy( p_filter, CFG_PREFIX "color"); - var_Destroy( p_filter, CFG_PREFIX "opacity"); - var_Destroy( p_filter, CFG_PREFIX "size"); - var_Destroy( p_filter, CFG_PREFIX "title" ); } /**************************************************************************** @@ -385,40 +359,36 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) subpicture_region_t *p_region; int i_feed, i_item; - - struct rss_feed_t *p_feed; + rss_feed_t *p_feed; memset( &fmt, 0, sizeof(video_format_t) ); vlc_mutex_lock( &p_sys->lock ); - if( p_sys->last_date - + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == ( p_sys->i_title == scroll_title ? -1 : 0 ) ? 5 : 1 ) - /* ( ... ? 5 : 1 ) means "wait 5 times more for the 1st char" */ - * p_sys->i_speed > date ) + /* Check if the feeds have been fetched and that we have some feeds */ + /* TODO: check that we have items for each feeds */ + if( !p_sys->b_fetched && p_sys->i_feeds > 0 ) { 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 ) + if( p_sys->last_date + + ( p_sys->i_cur_char == 0 && + p_sys->i_cur_item == ( p_sys->i_title == scroll_title ? -1 : 0 ) ? 5 : 1 ) + /* ( ... ? 5 : 1 ) means "wait 5 times more for the 1st char" */ + * p_sys->i_speed > date ) { - 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 ); + vlc_mutex_unlock( &p_sys->lock ); + return NULL; } p_sys->last_date = date; p_sys->i_cur_char++; - if( p_sys->i_cur_item == -1 ? p_sys->p_feeds[p_sys->i_cur_feed].psz_title[p_sys->i_cur_char] == 0 : p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 ) + + if( p_sys->i_cur_item == -1 ? + p_sys->p_feeds[p_sys->i_cur_feed].psz_title[p_sys->i_cur_char] == 0 : + p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 ) { p_sys->i_cur_char = 0; p_sys->i_cur_item++; @@ -444,7 +414,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) p_spu->p_region = subpicture_region_New( &fmt ); if( !p_spu->p_region ) { - p_filter->pf_sub_buffer_del( p_filter, p_spu ); + subpicture_Delete( p_spu ); vlc_mutex_unlock( &p_sys->lock ); return NULL; } @@ -519,7 +489,7 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) /* where to locate the string: */ if( p_sys->i_pos < 0 ) { /* set to an absolute xy */ - p_spu->p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP; + p_spu->p_region->i_align = SUBPICTURE_ALIGN_LEFT | SUBPICTURE_ALIGN_TOP; p_spu->b_absolute = true; } else @@ -527,6 +497,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) p_spu->p_region->i_align = p_sys->i_pos; p_spu->b_absolute = false; } + p_spu->p_region->i_x = p_sys->i_xoff; + p_spu->p_region->i_y = p_sys->i_yoff; p_spu->p_region->p_style = text_style_Duplicate( p_sys->p_style ); @@ -539,7 +511,6 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) memset( &fmt_out, 0, sizeof(video_format_t) ); fmt_out.i_chroma = VLC_CODEC_YUVA; - 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; @@ -553,15 +524,15 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) } else { - p_region->i_x = p_sys->i_xoff; - p_region->i_y = p_sys->i_yoff; + p_region->i_x = p_spu->p_region->i_x; + p_region->i_y = p_spu->p_region->i_y; /* FIXME the copy is probably not needed anymore */ picture_Copy( p_region->p_picture, p_pic ); p_spu->p_region->p_next = p_region; - } - /* Offset text to display right next to the image */ - p_spu->p_region->i_x = p_pic->p[Y_PLANE].i_visible_pitch; + /* Offset text to display right next to the image */ + p_spu->p_region->i_x += fmt_out.i_visible_width; + } } vlc_mutex_unlock( &p_sys->lock ); @@ -630,12 +601,12 @@ static picture_t *LoadImage( filter_t *p_filter, const char *psz_url ) * remove all ' ' '\t' '\n' '\r' characters from the begining and end of the * string. ***************************************************************************/ -static char *removeWhiteChars( char *psz_src ) +static char *removeWhiteChars( const char *psz_src ) { - char *psz_src2 = strdup( psz_src ); - char *psz_clean = strdup( psz_src2 ); - char *psz_clean2; + char *psz_src2,*psz_clean, *psz_clean2; + psz_src2 = psz_clean = strdup( psz_src ); int i; + while( ( *psz_clean == ' ' || *psz_clean == '\t' || *psz_clean == '\n' || *psz_clean == '\r' ) && *psz_clean != '\0' ) @@ -652,331 +623,368 @@ static char *removeWhiteChars( char *psz_src ) return psz_clean2; } + /**************************************************************************** - * FetchRSS (or Atom) feeds + * Parse url list, psz_urls must be non empty (TODO: check it !) ***************************************************************************/ -static int FetchRSS( filter_t *p_filter) +static int ParseUrls( filter_t *p_filter, char *psz_urls ) { filter_sys_t *p_sys = p_filter->p_sys; + char *psz_urls2 = psz_urls; - stream_t *p_stream = NULL; - xml_t *p_xml = NULL; - xml_reader_t *p_xml_reader = NULL; - - char *psz_eltname = NULL; - char *psz_eltvalue = NULL; - char *psz_feed = NULL; - char *psz_buffer = NULL; - char *psz_buffer_2 = NULL; - - int i_feed; - int i_item; - bool b_is_item; - bool 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++] == '|' ) - p_sys->i_feeds++; - p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds - * sizeof( struct rss_feed_t ) ); - p_xml = xml_Create( p_filter ); - if( !p_xml ) + /* Count the number of feeds */ + while( *psz_urls ) { - msg_Err( p_filter, "Failed to open XML parser" ); - return 1; + if( *psz_urls == '|' ) + p_sys->i_feeds++; + psz_urls++; } - 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++ ) + /* Allocate the structure */ + p_sys->p_feeds = malloc( p_sys->i_feeds * sizeof( rss_feed_t ) ); + if( !p_sys->p_feeds ) + return VLC_ENOMEM; + + /* Loop on all urls and fill in the struct */ + psz_urls = psz_urls2; + for( int i = 0; i < p_sys->i_feeds; i++ ) { - struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed; + rss_feed_t* p_feed = p_sys->p_feeds + i; + char *psz_end; - if( psz_buffer == NULL ) break; - if( psz_buffer[0] == 0 ) psz_buffer++; - psz_feed = psz_buffer; - psz_buffer = strchr( psz_buffer, '|' ); - if( psz_buffer != NULL ) psz_buffer[0] = 0; + if( i < p_sys->i_feeds - 1 ) + { + psz_end = strchr( psz_urls, '|' ); + *psz_end = '\0'; + } + else + psz_end = psz_urls; + p_feed->i_items = 0; + p_feed->p_items = NULL; p_feed->psz_title = NULL; - p_feed->psz_description = NULL; p_feed->psz_link = NULL; + p_feed->psz_description = NULL; p_feed->psz_image = NULL; p_feed->p_pic = NULL; - p_feed->i_items = 0; - p_feed->p_items = NULL; + p_feed->psz_url = strdup( psz_urls ); - msg_Dbg( p_filter, "opening %s RSS/Atom feed ...", psz_feed ); + psz_urls = psz_end + 1; + } - p_stream = stream_UrlNew( p_filter, psz_feed ); - if( !p_stream ) - { - msg_Err( p_filter, "Failed to open %s for reading", psz_feed ); - return 1; - } + return VLC_SUCCESS; +} - p_xml_reader = xml_ReaderCreate( p_xml, p_stream ); - if( !p_xml_reader ) - { - msg_Err( p_filter, "Failed to open %s for parsing", psz_feed ); - return 1; - } - i_item = 0; - b_is_item = false; - b_is_image = false; - while( xml_ReaderRead( p_xml_reader ) == 1 ) +/**************************************************************************** + * Parse the rss feed + ***************************************************************************/ +static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader, + rss_feed_t *p_feed ) +{ + VLC_UNUSED(p_filter); + const char *node; + char *psz_eltname = NULL; + + bool b_is_item = false; + bool b_is_image = false; + + int i_item = 0; + int type; + + while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 ) + { + switch( type ) { - switch( xml_ReaderNodeType( p_xml_reader ) ) + case XML_READER_STARTELEM: +#ifdef RSS_DEBUG + msg_Dbg( p_filter, "element <%s>", node ); +#endif + psz_eltname = strdup( node ); + if( unlikely(!psz_eltname) ) + goto end; + + /* rss or atom */ + if( !strcmp( node, "item" ) || !strcmp( node, "entry" ) ) { - // Error - case -1: - return 1; - - case XML_READER_STARTELEM: - free( psz_eltname ); - psz_eltname = xml_ReaderName( p_xml_reader ); - if( !psz_eltname ) - { - return 1; - } -# ifdef RSS_DEBUG - msg_Dbg( p_filter, "element name: %s", psz_eltname ); -# endif - if( !strcmp( psz_eltname, "item" ) /* rss */ - || !strcmp( psz_eltname, "entry" ) ) /* atom */ - { - b_is_item = 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 = 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" ) ) - { - if( psz_rel ) - { - msg_Dbg( p_filter, "\"rel\" attribute of link atom duplicated (last value: %s)", psz_value ); - free( psz_rel ); - } - psz_rel = psz_value; - } - else if( !strcmp( psz_name, "href" ) ) - { - if( psz_href ) - { - msg_Dbg( p_filter, "\"href\" attribute of link atom duplicated (last value: %s)", psz_href ); - free( psz_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 == false - && b_is_image == 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 == false - && b_is_image == false - && !p_feed->psz_image ) - { - p_feed->psz_image = psz_href; - } - else - { - free( psz_href ); - } - } - else - { - free( psz_href ); - } - free( psz_rel ); - } - break; + b_is_item = true; + p_feed->i_items++; + p_feed->p_items = xrealloc( p_feed->p_items, + p_feed->i_items * sizeof( 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; + } + /* rss */ + else if( !strcmp( node, "image" ) ) + { + b_is_image = true; + } + /* atom */ + else if( !strcmp( node, "link" ) ) + { + const char *name, *value; + char *psz_href = NULL; + char *psz_rel = NULL; - case XML_READER_ENDELEM: - 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 ); -# endif - if( !strcmp( psz_eltname, "item" ) /* rss */ - || !strcmp( psz_eltname, "entry" ) ) /* atom */ - { - b_is_item = false; - i_item++; - } - else if( !strcmp( psz_eltname, "image" ) ) /* rss */ - { - b_is_image = 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 ) + while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL ) + { + if( !strcmp( name, "rel" ) ) { - return 1; + free( psz_rel ); + psz_rel = strdup( value ); } - else + else if( !strcmp( name, "href" ) ) { - char *psz_clean; - psz_clean = removeWhiteChars( psz_eltvalue ); - free( psz_eltvalue ); psz_eltvalue = psz_clean; + free( psz_href ); + psz_href = strdup( value ); } -# ifdef RSS_DEBUG - msg_Dbg( p_filter, " text : <%s>", psz_eltvalue ); -# endif - if( b_is_item == true ) + } + + /* "rel" and "href" must be defined */ + if( psz_rel && psz_href ) + { + if( !strcmp( psz_rel, "alternate" ) && !b_is_item && + !b_is_image && !p_feed->psz_link ) { - 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_item->psz_title = psz_eltvalue; - } - else if( !strcmp( psz_eltname, "link" ) /* rss */ - && !p_item->psz_link ) - { - p_item->psz_link = psz_eltvalue; - } - else if((!strcmp( psz_eltname, "description" ) /* rss */ - || !strcmp( psz_eltname, "summary" ) ) /* atom */ - && !p_item->psz_description ) - { - p_item->psz_description = psz_eltvalue; - } - else - { - free( psz_eltvalue ); - psz_eltvalue = NULL; - } + p_feed->psz_link = psz_href; } - else if( b_is_image == true ) + /* this isn't in the rfc but i found some ... */ + else if( ( !strcmp( psz_rel, "logo" ) || + !strcmp( psz_rel, "icon" ) ) + && !b_is_item && !b_is_image + && !p_feed->psz_image ) { - if( !strcmp( psz_eltname, "url" ) /* rss */ - && !p_feed->psz_image ) - { - p_feed->psz_image = psz_eltvalue; - } - else - { - free( psz_eltvalue ); - psz_eltvalue = NULL; - } + p_feed->psz_image = psz_href; } else { - if( !strcmp( psz_eltname, "title" ) /* rss/atom */ - && !p_feed->psz_title ) - { - p_feed->psz_title = psz_eltvalue; - } - else if( !strcmp( psz_eltname, "link" ) /* rss */ - && !p_feed->psz_link ) - { - p_feed->psz_link = psz_eltvalue; - } - else if((!strcmp( psz_eltname, "description" ) /* rss */ - || !strcmp( psz_eltname, "subtitle" ) ) /* atom */ - && !p_feed->psz_description ) - { - 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; - } + free( psz_href ); } - break; + } + else + { + free( psz_href ); + } + free( psz_rel ); } - } + break; - if( p_sys->b_images == true - && p_feed->psz_image && !p_feed->p_pic ) + case XML_READER_ENDELEM: + FREENULL( psz_eltname ); +#ifdef RSS_DEBUG + msg_Dbg( p_filter, "element end ", node ); +#endif + /* rss or atom */ + if( !strcmp( node, "item" ) || !strcmp( node, "entry" ) ) + { + b_is_item = false; + i_item++; + } + /* rss */ + else if( !strcmp( node, "image" ) ) + { + b_is_image = false; + } + break; + + case XML_READER_TEXT: { - p_feed->p_pic = LoadImage( p_filter, p_feed->psz_image ); - } + if( !psz_eltname ) + break; + + char *psz_eltvalue = removeWhiteChars( node ); - 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 ); +#ifdef RSS_DEBUG + msg_Dbg( p_filter, " text : \"%s\"", psz_eltvalue ); +#endif + /* Is it an item ? */ + if( b_is_item ) + { + rss_item_t *p_item = p_feed->p_items+i_item; + /* rss/atom */ + if( !strcmp( psz_eltname, "title" ) && !p_item->psz_title ) + { + p_item->psz_title = psz_eltvalue; + } + else if( !strcmp( psz_eltname, "link" ) /* rss */ + && !p_item->psz_link ) + { + p_item->psz_link = psz_eltvalue; + } + /* rss/atom */ + else if( ( !strcmp( psz_eltname, "description" ) || + !strcmp( psz_eltname, "summary" ) ) + && !p_item->psz_description ) + { + p_item->psz_description = psz_eltvalue; + } + else + { + free( psz_eltvalue ); + } + } + /* Is it an image ? */ + else if( b_is_image ) + { + if( !strcmp( psz_eltname, "url" ) && !p_feed->psz_image ) + p_feed->psz_image = psz_eltvalue; + else + free( psz_eltvalue ); + } + else + { + /* rss/atom */ + if( !strcmp( psz_eltname, "title" ) && !p_feed->psz_title ) + { + p_feed->psz_title = psz_eltvalue; + } + /* rss */ + else if( !strcmp( psz_eltname, "link" ) && !p_feed->psz_link ) + { + p_feed->psz_link = psz_eltvalue; + } + /* rss ad atom */ + else if( ( !strcmp( psz_eltname, "description" ) || + !strcmp( psz_eltname, "subtitle" ) ) + && !p_feed->psz_description ) + { + p_feed->psz_description = psz_eltvalue; + } + /* rss */ + else if( ( !strcmp( psz_eltname, "logo" ) || + !strcmp( psz_eltname, "icon" ) ) + && !p_feed->psz_image ) + { + p_feed->psz_image = psz_eltvalue; + } + else + { + free( psz_eltvalue ); + } + } + break; + } + } } - free( psz_buffer_2 ); - if( p_xml ) xml_Delete( p_xml ); - return 0; + free( psz_eltname ); + return true; + +end: + return false; } + /**************************************************************************** - * FreeRSS + * FetchRSS (or Atom) feeds ***************************************************************************/ -static void FreeRSS( filter_t *p_filter) +static rss_feed_t* FetchRSS( 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; - + stream_t *p_stream; + xml_t *p_xml; + xml_reader_t *p_xml_reader; int i_feed; - int i_item; - for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ ) + /* These data are not modified after the creation of the module so we don't + need to hold the lock */ + int i_feeds = p_sys->i_feeds; + bool b_images = p_sys->b_images; + + /* Allocate a new structure */ + rss_feed_t *p_feeds = malloc( i_feeds * sizeof( rss_feed_t ) ); + if( !p_feeds ) + return NULL; + + p_xml = xml_Create( p_filter ); + if( !p_xml ) { - p_feed = p_sys->p_feeds+i_feed; - for( i_item = 0; i_item < p_feed->i_items; i_item++ ) + msg_Err( p_filter, "Failed to open XML parser" ); + free( p_feeds ); + return NULL; + } + + /* Fetch all feeds and parse them */ + for( i_feed = 0; i_feed < i_feeds; i_feed++ ) + { + rss_feed_t *p_feed = p_feeds + i_feed; + rss_feed_t *p_old_feed = p_sys->p_feeds + i_feed; + + /* Initialize the structure */ + 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; + + p_feed->psz_url = strdup( p_old_feed->psz_url ); + + /* Fetch the feed */ + msg_Dbg( p_filter, "opening %s RSS/Atom feed ...", p_feed->psz_url ); + + p_stream = stream_UrlNew( p_filter, p_feed->psz_url ); + if( !p_stream ) + { + msg_Err( p_filter, "Failed to open %s for reading", p_feed->psz_url ); + p_xml_reader = NULL; + goto error; + } + + p_xml_reader = xml_ReaderCreate( p_xml, p_stream ); + if( !p_xml_reader ) + { + msg_Err( p_filter, "Failed to open %s for parsing", p_feed->psz_url ); + goto error; + } + + /* Parse the feed */ + if( !ParseFeed( p_filter, p_xml_reader, p_feed ) ) + goto error; + + /* If we have a image: load it if requiere */ + if( b_images && p_feed->psz_image && !p_feed->p_pic ) + { + p_feed->p_pic = LoadImage( p_filter, p_feed->psz_image ); + } + + msg_Dbg( p_filter, "done with %s RSS/Atom feed", p_feed->psz_url ); + xml_ReaderDelete( p_xml_reader ); + stream_Delete( p_stream ); + } + + xml_Delete( p_xml ); + return p_feeds; + +error: + FreeRSS( p_feeds, i_feed + 1 ); + if( p_xml_reader ) + xml_ReaderDelete( p_xml_reader ); + if( p_stream ) + stream_Delete( p_stream ); + if( p_xml ) + xml_Delete( p_xml ); + + return NULL; +} + +/**************************************************************************** + * FreeRSS + ***************************************************************************/ +static void FreeRSS( rss_feed_t *p_feeds, int i_feeds ) +{ + for( int i_feed = 0; i_feed < i_feeds; i_feed++ ) + { + rss_feed_t *p_feed = p_feeds+i_feed; + for( int i_item = 0; i_item < p_feed->i_items; i_item++ ) { - p_item = p_feed->p_items+i_item; + rss_item_t *p_item = p_feed->p_items+i_item; free( p_item->psz_title ); free( p_item->psz_link ); free( p_item->psz_description ); @@ -988,7 +996,36 @@ static void FreeRSS( filter_t *p_filter) free( p_feed->psz_image ); if( p_feed->p_pic != NULL ) picture_Release( p_feed->p_pic ); + free( p_feed->psz_url ); } - free( p_sys->p_feeds ); - p_sys->i_feeds = 0; + free( p_feeds ); +} + +static void Fetch( void *p_data ) +{ + filter_t *p_filter = p_data; + filter_sys_t *p_sys = p_filter->p_sys; + + msg_Dbg( p_filter, "Updating the rss feeds" ); + rss_feed_t *p_feeds = FetchRSS( p_filter ); + if( !p_feeds ) + { + msg_Err( p_filter, "Unable to fetch the feeds" ); + return; + } + + rss_feed_t *p_old_feeds = p_sys->p_feeds; + + vlc_mutex_lock( &p_sys->lock ); + /* Update the feeds */ + p_sys->p_feeds = p_feeds; + p_sys->b_fetched = true; + /* Set all current info to the original values */ + p_sys->i_cur_feed = 0; + p_sys->i_cur_item = p_sys->i_title == scroll_title ? -1 : 0; + p_sys->i_cur_char = 0; + vlc_mutex_unlock( &p_sys->lock ); + + if( p_old_feeds ) + FreeRSS( p_old_feeds, p_sys->i_feeds ); }