# Album Plugin: extra/media_rss
# For info: 'album -plugin_info extra/media_rss'
# For usage:  'album -plugin_usage extra/media_rss'
# The plugin should have been written using the XML::RSS module combined with 
# http://search.yahoo.com/mrss namespace. But, outputting the stream to a file
# did not work, I had to do it manually.
use strict; # Not required, but recommended

my $DESCRIPTION = << 'DESCRIPTION';
Generates a Media RSS feed containing the album images. If the medium option is used,
an additional media stream containing the medium resolution images is made.
A link to the PicLens plugin and a link to launch a slide show using the PicLens plugin
is added to the album header.
DESCRIPTION
 

my $feedFileName = "photos.xml";
my $LRfeedFileName = "photosLR.xml";

sub start_plugin {
   
	my ($opt) = @_;

	my $ret = {
		author      => 'Reidar D. Midtun',
		href        => 'reidar@midtun.dyndns.org',
		version     => '1.0',
		description => $DESCRIPTION,
	};


	# Options
    ## Just use the relative URL...
        album::add_option(1,
                          'clean_up',
                          album::OPTION_BOOL, 
                          default=>0, 
                          usage=>"Removes generated xml files");
      
 
	# Setup the hooks
        album::hook($opt, 'write_index', \&setup_header);
 	album::hook($opt, 'end_album', \&end_album);
	
	return $ret;
}


sub get_options {
        my ($opt, $dir) = @_;
	# Get options
 	
        my @fields = split('/', $dir); 
        my $n = @fields;
        my $albumName = @fields[$n-1]; 
   
        my $medium = album::option($opt, 'medium');
        my $clean_up = album::option($opt, 'clean_up');

        return ($medium, $albumName, $clean_up);
}

# Sets up the meta data in the html header
# and creates the media rss file
sub end_album {
        my ($opt, $data, $hook,  $dir, $album) = @_;
        my $nPicks = $#{$data->{pics}};
        return unless $nPicks > 0;
	my ($medium, $albumName, $clean_up) = get_options($opt, $dir);	        
	create_image_feed($opt,$data, $albumName, $medium, $clean_up);
}

sub create_image_feed {
        my ($opt, $data, $albumName, $medium, $clean_up) = @_;

        my $dir = $data->{paths}{dir};
        my $feedFile = "$dir/$feedFileName";
        if ($clean_up) {
               print "Deleting file: $feedFile\n";
               unlink($feedFile);  
               $feedFile = "$dir/$LRfeedFileName";
               print "Deleting file: $feedFile\n";
               unlink($feedFile);
        }
        else {
	        my $rss_header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" .
	                         "<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss\">\n" .
	                         "<channel>\n" .
	                         "<title>$albumName</title>\n" .
	                         "<link>.</link>\n";
		
	        my $rss_footer = "</channel>\n" . 
	                         "</rss>\n";
	
	      
		open FEED, ">$feedFile" or die $!;
		print FEED $rss_header;	
		write_image_items($opt,$data, $dir, 0);	
		print FEED $rss_footer;
		close FEED;
                if ($medium) {
                     $feedFile = "$dir/$LRfeedFileName";
                     open FEED, ">$feedFile" or die $!;
		     print FEED $rss_header;	
		     write_image_items($opt, $data, $dir, 1);	
		     print FEED $rss_footer;
		     close FEED;
                }
        }
}


sub write_image_items {
	my ($opt, $data, $dir, $medium) = @_;

	foreach my $pic ( @{$data->{pics}} ) {	
	       my $obj = $data->{obj}{$pic};
               next unless $obj->{is_image};
               next unless !$obj->{is_movie};

	       my $thumb = $obj->{URL}{album_page}{thumb};
               $thumb =~ s/^'(.*)'$/$1/;	# Remove surrounding quotes
               ## Just use the relative URL...
               #my $picRef = "$dir/$pic";
               my $picRef = $pic;
	       if ($medium) {
	          # Unfortunately we don't have an $obj->{URL}{album_page}{image_src}
	          # (never needed it..)
	          $picRef = $obj->{URL}{image_page}{image_src};
	          $picRef =~ s/^'(.*)'$/$1/;
	          $picRef = album::option($opt,'dir')."/".$picRef;
	       }
               
               my $item = 
                  "   <item>\n" .
                  "      <title>$pic</title>\n" .
                  "      <link>$picRef</link>\n" .
                  ##"      <media:thumbnail url=\"$dir/$thumb\" />\n" .
                  "      <media:thumbnail url=\"$thumb\" />\n" .
                  "      <media:content url=\"$picRef\" type=\"image/jpeg\" />\n" .
                  "   </item>\n";
               
	       print FEED $item;
		
	}
}


# Makes a header file containing a ref to start the slide show
sub setup_header {
    my ($opt, $data, $hook,  $dir, $album) = @_;

    my $nPicks = $#{$data->{pics}};
    return unless $nPicks > 0;

    my ($medium, $albumName, $clean_up) = get_options($opt, $dir);
   
	  # Updates the html header with a link to the rss feed and definition
	  # of the script launching the slide show.  
	  my $meta = "                <script type=\"text/javascript\" src=\"http://lite.piclens.com/current/piclens.js\"></script>\n";
	  album::add_head($opt,$data,$meta);
          my $launchText =  "   <a href=\"javascript:PicLensLite.start({feedUrl:'$feedFileName'});\">\n" . 
                            "      <img src=\"http://lite.piclens.com/images/PicLensButton.png\"\n" .
                            "      width=\"20\" height=\"15\" border=\"0\" align=\"absmiddle\"></a>\n";
          if ($medium) {
              $launchText = "   <a href=\"javascript:PicLensLite.start({feedUrl:'$feedFileName'});\">\n" .
                            "      High Resolution <img src=\"http://lite.piclens.com/images/PicLensButton.png\"\n" .
                            "      width=\"20\" height=\"15\" border=\"0\" align=\"absmiddle\"></a> or \n" .
                            "   <a href=\"javascript:PicLensLite.start({feedUrl:'$LRfeedFileName'});\">\n" .
                            "      Low Resolution <img src=\"http://lite.piclens.com/images/PicLensButton.png\"\n" .
                            "      width=\"20\" height=\"15\" border=\"0\" align=\"absmiddle\"> </a>\n";

          }
	
	  # The html body header is updated with the content of the header.txt file.
	  # We make a link to the PicLens plugin site and a link to launch the slide show.
	  my $header = "<div align=\"center\">\n" .
                       "   <font size='-1' face='Verdana'>\n" .
                       "   Use the <a href=\"http://www.piclens.com/site/firefox/\">PicLens</a> plugin to\n" .  
                       "   start a slide show\n" .                              
                       "   $launchText" . 
                       "</div>\n";

	  album::add_header($opt,$data,$header);
	  
    return 0;
}


# Plugins always end with:
1;
