package XXV::OUTPUT::NEWS::RSS;
use strict;

use POSIX;
use POSIX qw(locale_h);
use Tools;
use Locale::gettext;
use XML::RSS;

# News Modules have only three methods
# init - for intervall or others
# send - send the informations
# read - read the news and parse it

# This module method must exist for XXV
# ------------------
sub module {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $args = {
        Name => 'NEWS::RSS',
        Description => gettext('This NEWS module generate an RSS Newsfeed for your rss reader.'),
        Version => '0.01',
        Date => '31.09.2005',
        Author => 'xpix',
        Preferences => {
            active => {
                description => gettext('Activate this service'),
                default     => 'y',
                type        => 'confirm',
                required    => gettext('This is required!'),
            },
        },
    };
    return $args;
}

# ------------------
sub new {
# ------------------
	my($class, %attr) = @_;
	my $self = {};
	bless($self, $class);

    # paths
    $self->{paths} = delete $attr{'-paths'};

    # host
    $self->{host} = delete $attr{'-host'};

	# who am I
    $self->{MOD} = $self->module;

    # all configvalues to $self without parents (important for ConfigModule)
    map {
        $self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_} || $self->{MOD}->{Preferences}->{$_}->{default}
    } keys %{$self->{MOD}->{Preferences}};


    # Repair later Data ...
    main::after(sub{
        # The Initprocess
        my $erg = $self->init
            or return error('Problem to initialze News Module');
    }, "NEWS::RSS: Start initiate the RSS Feed ...");

    $self->{TYP} = 'application/xhtml+xml';

	return $self;
}

# ------------------
sub init {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $url = sprintf("http://%s:%s", $obj->{host}, main::getModule('HTTPD')->{Port});


    $obj->{RSS} = XML::RSS->new(
        version => '1.0',
    ) || return error('Problem to create an RSS Object');

    my $account = sprintf("%s@%s", $ENV{USER}, main::getModule('STATUS')->name);

    $obj->{RSS}->channel(
        title        => gettext("XXV RSS NewsFeader"),
        link         => $url,
        description  => gettext("Very important messages from your vdr/xxv"),
        dc => {
            date       => scalar localtime,
            subject    => gettext("XXV Messages"),
            creator    => $account,
            language   => setlocale(POSIX::LC_MESSAGES),
        },
        syn => {
            updatePeriod     => "hourly",
            updateFrequency  => "1",
            updateBase       => "1901-01-01T00:00+00:00",
        },
    );

    $obj->{RSS}->image(
        title  => "XXV",
        url    => sprintf("%s/images/logo.gif", $url),
        link   => $url,
    );

    1;
}

# ------------------
sub send {
# ------------------
    my $obj = shift  || return error ('No Object!' );
    my $vars = shift || return error ('No Vars!' );
    my $url = sprintf("http://%s:%s", $obj->{host}, main::getModule('HTTPD')->{Port});

    ++$obj->{COUNT};

    $obj->{RSS}->add_item(
        title       => $vars->{Title},
        link        => sprintf("%s/?cmd=%s&data=%s", $url, $vars->{Cmd}, $vars->{Id}),
        description => $vars->{Text},
        date        => scalar localtime,
    );

    lg sprintf('News RSS with nr. %d successfully send at %s', $obj->{COUNT}, scalar localtime);
    1;
}

# ------------------
sub read {
# ------------------
    my $obj = shift  || return error ('No Object!' );
    my $vars = shift || return error ('No News!' );
    my $output = '';

    return undef, lg('This function is deactivated!')
        if($obj->{active} ne 'y');


    $vars->{count} = ++$obj->{NEWSCOUNT};
    $vars->{host}  = $obj->{host};
    $vars->{port}  = main::getModule('HTTPD')->{Port};

    $obj->send($vars);

    return $output;
}

# ------------------
sub req {
# ------------------
    my $obj = shift  || return error ('No Object!' );

    return $obj->{RSS}->as_string;
}


1;
