#/*
# *  Copyright 2007 hkrn <hikarin@users.sourceforge.jp>
# *
# *  Licensed under the Apache License, Version 2.0 (the "License");
# *  you may not use this file except in compliance with the License.
# *  You may obtain a copy of the License at
# *
# *      http://www.apache.org/licenses/LICENSE-2.0
# *
# *  Unless required by applicable law or agreed to in writing, software
# *  distributed under the License is distributed on an "AS IS" BASIS,
# *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# *  See the License for the specific language governing permissions and
# *  limitations under the License.
# */
#
# $Id: RSS.pm 1062 2007-09-05 14:01:20Z hikarin $
#

package Img0ch::Plugin::BBS::RSS;

use strict;
use Unicode::Japanese qw();
use XML::FeedPP qw();

sub generate_ver_10 {
    my ( $iApp, $plugin_id ) = @_;
    my $ctx = {};

    _get_info( $iApp, $plugin_id, $ctx );
    my $feed = XML::FeedPP::RDF->new();
    my $now  = $ctx->{now};
    my $path = $ctx->{path} . '.rdf';
    -r $path and $feed->merge($path);

    $feed->title( $ctx->{'c:title'} );
    $feed->description( $ctx->{'c:desc'} );
    $feed->link( $ctx->{'c:url'} );
    $feed->language('ja');
    $feed->pubDate($now);

    my $item = $feed->add_item( $ctx->{'i:resource'} );
    $item->title( $ctx->{'i:title'} );
    $item->pubDate($now);
    $item->description( $ctx->{'i:desc'} );
    $item->author( $ctx->{'i:author'} );

    $feed->limit_item( $ctx->{limit} );
    $feed->normalize();
    $feed->to_file($path);
    return 1;
}

sub generate_ver_20 {
    my ( $iApp, $plugin_id ) = @_;
    my $ctx = {};

    _get_info( $iApp, $plugin_id, $ctx );
    my $feed = XML::FeedPP::RSS->new();
    my $now  = $ctx->{now};
    my $path = $ctx->{path} . '.xml';
    -r $path and $feed->merge($path);

    $feed->title( $ctx->{'c:title'} );
    $feed->description( $ctx->{'c:desc'} );
    $feed->link( $ctx->{'c:url'} );
    $feed->language('ja');
    $feed->pubDate($now);

    my $item = $feed->add_item( $ctx->{'i:resource'} );
    $item->title( $ctx->{'i:title'} );
    $item->pubDate($now);
    $item->description( $ctx->{'i:desc'} );
    $item->author( $ctx->{'i:author'} );

    $feed->limit_item( $ctx->{limit} );
    $feed->normalize();
    $feed->to_file($path);
    return 1;
}

sub _get_info {
    my ( $iApp, $plugin_id, $ctx ) = @_;
    my $iBBS     = $iApp->bbs();
    my $iKernel  = $iApp->kernel();
    my $iConfig  = $iKernel->get_config();
    my $iRequest = $iApp->request();
    my $iSetting = $iApp->setting();
    my $iThread  = $iApp->thread();
    my $encoding = $iKernel->get_encoding(1);
    my $unijp    = Unicode::Japanese->new();

    $ctx->{'c:url'} = $iBBS->url() . '/';
    $ctx->{'c:title'}
        = $unijp->set( $iSetting->get('BBS_TITLE'), $encoding )->get();
    $ctx->{'c:desc'}
        = $unijp->set( $iSetting->get('BBS_SUBTITLE'), $encoding )->get();
    $ctx->{'i:resource'} = join '/', $iRequest->get_app_uri(), 'read.cgi',
        $iBBS->get_name(), $iThread->get_key(), ( $iThread->count + 1 );
    $ctx->{path} = $iBBS->path('index');
    $ctx->{'i:author'} = $unijp->set( $iApp->get_name(), $encoding )->get();
    $ctx->{'i:desc'} = $unijp->set( $iApp->get_comment(), $encoding )->get();
    $ctx->{'i:title'}
        = $unijp->set( $iThread->get_subject(), $encoding )->get();
    $ctx->{limit} = -1 * (
        Img0ch::Kernel::intval(
            shift @{ $iApp->get_plugin_config( $plugin_id, 'max', 1 ) }
            )
            || 10
    );
    $ctx->{now} = $iRequest->now();
    return;
}

1;
__END__
