package Plugins::BbcNews;


# BBCNews Ticker v0.3
# (c) Gordon Johnston (gordonj@newswall.org.uk)

# Contains code from SliMP3 Server which is: Copyright (C) 2001 Sean Adams, Slim Devices Inc.

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License, 
# version 2.

use strict;
use File::Spec::Functions qw(:ALL);
use File::Spec::Functions qw(updir);
use SliMP3::Buttons::Common;
use SliMP3::Misc;
use SliMP3::RemoteStream;

use SliMP3::Strings qw (string);
use Socket;

# constants

# wrap around, set to 0 (zero) if you would like the ticker to stop when it reaches
# the last news catagory 

my $wrap = 1;

# specify the locaiton of the bbc news ticker on-line

my $bbcnews_location = 'http://tickers.bbc.co.uk/tickerdata/story2.dat';

# The BBC News Ticker comes in 8 news catagories.
# Specify the catagories you want and their ordering here:

# show all
#my @display_stack=(2,3,4,5,6,7,8);
#my @display_stack_left=();
#my $current_display_catagory=1;

# no travel
# we hide travel by default as it doesn't display anything useful
my @display_stack=(2,3,4,5,6,8);
my @display_stack_left=();
my $display_current=1;

# Catagories are:

my @news_description=(
	'NEWS DESCRIPTIONS',	#Catagory Numbers
	'WORLD NEWS',		#1
	'UK NEWS',		#2
	'SPORTS NEWS',		#3
	'BUSINESS NEWS',	#4
	'SCI-TECH NEWS',	#5
	'WEATHER',		#6
	'TRAVEL NEWS',		#7
	'FINANCE'		#8
);

# Plugin descriptions

sub getDisplayName() {return string('PLUGIN_BBCNEWS')}

sub strings() { return '
PLUGIN_BBCNEWS
	EN	BBC News Ticker
	
PLUGIN_BBCNEWS_WAITING
	EN	Please wait...

PLUGIN_BBCNEWS_ERROR
	EN	Failed to connect - Press Record
'};

my @thenews ;

my $state = "wait";

# button functions

# These functions are run when the respective button is pressed on the remote

my %functions = (
	'left' => sub {
		#Return to previous menu
		my $client=shift;
		SliMP3::Buttons::Common::popModeRight($client);
		return;
},
	'up' => sub {
		#if there are no catagories left then wrap around if selected
		if((!@display_stack)&&($wrap))
		{
			@display_stack=@display_stack_left;
			@display_stack_left=();
		}
		#Move up the list of catagories
		if(@display_stack){
			push @display_stack_left, ($display_current);
			$display_current=shift @display_stack;
			my $client = shift;

			$client->lines(\&lines);
			SliMP3::Display::update($client);;

			return;
		}
		return;
},

	'down' => sub {
		#if there are no catagories left then wrap around if selected
		if((!@display_stack_left)&&($wrap))
		{
			@display_stack_left=@display_stack;
			@display_stack=();
		}
		#Move down the list of catagories
		if(@display_stack_left){

			unshift @display_stack, ($display_current);
			$display_current=pop @display_stack_left;
			my $client = shift;

			$client->lines(\&lines);
			SliMP3::Display::update($client);;

			return;
		}
		return;
},
	'rec' => sub {
		#Refresh the news
	        my $client = shift;
                my @oldlines = SliMP3::Display::curLines($client);
		$state='wait';
		$client->lines(\&lines);
                SliMP3::Display::update($client);

		&retrieveNews($client);
		return;
}
);



sub getFunctions {
	#export the functions to the system
	return \%functions;
}

sub retrieveNews {

	my $client = shift;
        my $sock = SliMP3::RemoteStream::openRemoteStream($bbcnews_location, $client);

	if ($sock)
	{
		#import the news from the socket 
	        my $cur_story=1;
		@thenews=();
	        while(<$sock>)
		{
			if (/^STORY (.)/)
			{
				$cur_story=$1;
			}
			elsif (/^HEADLINE (.*)/)
			{
	                        $thenews[$cur_story].=$1." --- ";
			}
		}
		$sock->close;;
	};
        $state='';

}

sub setMode {
	#This is executed each time the Ticker is selected from the plugins/extras menu.
	my $client = shift;
        $state= 'wait';
        $client->lines(\&lines);

	&retrieveNews($client);

}


sub lines {
	#This returns the 2 lines to display on the unit 
	my $client = shift;
	my ($line1, $line2);


        $line1 = "BBC News - ".$news_description[$display_current];

	if ($state eq 'wait')
	{
		$line2 = string('PLUGIN_BBCNEWS_WAIT');
	} 
	elsif (exists($thenews[$display_current]))
	{
                $line2 = $thenews[$display_current];
        }
	else
	{
                $line2 = string('PLUGIN_BBCNEWS_ERROR');
        }

	return ($line1, $line2);
}	

1;

__END__
