#!/usr/bin/perl
#-----------------------------------------------------------------------------
# OpenOffice::OODoc test					2010-02-11
#-----------------------------------------------------------------------------

=head1	NAME

oodoc_test - OpenOffice::OODoc test document generation

=head1	DESCRIPTION

This utility checks the current OpenOffice::OODoc installation and generates
a test document from scratch. The generated document can then be found in the
current directory, and its name is "odftest.odt".

=cut

use strict;
use OpenOffice::OODoc;

#-----------------------------------------------------------------------------

sub	display_date
	{
	return localtime(odfTimelocal(shift));
	}

#-----------------------------------------------------------------------------

my $version	= $OpenOffice::OODoc::VERSION;
my $pkgdate	= display_date $OpenOffice::OODoc::BUILD_DATE;
my $instpath	= $OpenOffice::OODoc::INSTALLATION_PATH;

print	"OpenOffice::OODoc installation test\n  Version\t\t$version\n"	.
	"  Build date\t\t$pkgdate\n"		.
	"  Installation path\t$instpath\n";

my $generator	=	"OpenOffice::OODoc " . $OpenOffice::OODoc::VERSION .
				" installation test";
my $testfile    =       $ARGV[0]        ||
                        (
                        $OpenOffice::OODoc::File::DEFAULT_OFFICE_FORMAT == 2 ?
				"odftest.odt" : "ootest.sxw"
			);
my $class	=	"text";
my $image_file	=	"$instpath/data/image.png";
my $image_size	=	"60mm, 82mm";
my $test_date	=	odfLocaltime();

print "Generating $testfile file\n";

# Creating an empty new ODF file with the default template
# unlink $testfile;
my $archive = odfContainer($testfile, create => 'text');
unless ($archive)
	{
	die "# Unable to create the test file\n";
	}
	
#-----------------------------------------------------------------------------

my $notice	= 
"This document has been generated by the OpenOffice::OODoc " .
"installation test. If you can read this paragraph in blue letters with " .
"a yellow background, if you can see a centered image at the top of the " .
"page, and if the informations below make sense, " .
"your installation is probably OK.";

my $title	= "OpenOffice::OODoc test document";
my $description	= "Generated by $generator";

# Opening the content using OpenOffice::OODoc::Document
my $doc	= odfConnector
		(
		container	=> $archive,
		part		=> 'content',
		readable_XML	=> 'true'
		)
	or die "# Unable to find a regular document content\n";

my $styles = odfConnector
		(
		container	=> $archive,
		part		=> 'styles',
		readable_XML	=> 'true'
		)
	or die "# Unable to get the styles\n";

# Creating a graphic style
$styles->createImageStyle
	(
	'Centered Image',
	properties	=>
			{
			'horizontal-pos'	=> 'center',
			'vertical-pos'	        => 'from-top',
			'wrap'                  => 'none',
			'fo:margin-bottom'	=> '1cm'
			}
	);
# Inserting an image in the document
$doc->createImageElement
	(
		'Logo',
		style		=> 'Centered Image',
		page		=> 1,
		size		=> $image_size,
		import		=> $image_file
	);

# Appending a page footer
$styles->createStyle
	(
	'Centered Paragraph',
	family		=> 'paragraph',
	parent		=> 'Standard',
	properties	=>
		{
		'fo:text-align'		=> 'center'
		}
	);
$styles->styleProperties
	(
	'Centered Paragraph',
	area		=> 'text',
	'fo:font-size'	=> '60%'
	);
$styles->masterPageFooter
	(
	'Standard',
	$styles->createParagraph
			(
			"Created by OpenOffice::OODoc\n" . localtime(),
			'Centered Paragraph'
			)
	);
# Appending a level 1 heading
$doc->appendHeading
	(
	text	=> "Congratulations !",
	level	=> "1",
	style	=> "Heading_20_1"
	);

# Creating a coloured paragraph style (blue foreground, yellow background)
$styles->createStyle
	(
	"Colour",
	family		=> 'paragraph',
	parent		=> 'Standard',
	properties	=>
			{
			-area			=> 'paragraph',
			'fo:color'		=> odfColor(0,0,128),
			'fo:background-color'	=> odfColor("yellow"),
			'fo:text-align'		=> 'justify'
			}
	);
if ($doc->isOpenDocument)
	{
	$styles->styleProperties
		("Colour", -area => 'text', 'fo:color' => '#000080');
	}
# Appending another paragraph using the new style
my $pg = $doc->appendParagraph(text => $notice, style => "Colour" );

# Appending another level 1 heading
$doc->appendHeading
	(
	text	=> "Your environment",
	level	=> 1,
	style	=> "Heading_20_1"
	);
# Appending a table showing some environment details
my $table = $doc->appendTable("Environment", 6, 2);
$doc->cellValue($table, "A1", "Platform");
$doc->cellValue($table, "B1", $^O);
$doc->cellValue($table, "A2", "Perl version");
$doc->cellValue($table, "B2", $]);
$doc->cellValue($table, "A3", "Archive::Zip version");
$doc->cellValue($table, "B3", $Archive::Zip::VERSION);
$doc->cellValue($table, "A4", "XML::Twig version");
$doc->cellValue($table, "B4", $XML::Twig::VERSION);
$doc->cellValue($table, "A5", "OpenOffice::OODoc version");
$doc->cellValue($table, "B5", $OpenOffice::OODoc::VERSION);
$doc->cellValue($table, "A6", "OpenOffice::OODoc build");
$doc->cellValueType($table, "B6", 'date');
$doc->cellValue($table, "B6", $OpenOffice::OODoc::BUILD_DATE);

# Appending another level 1 heading
$doc->appendHeading
	(
	text	=> "Your installation choices",
	level	=> 1,
	style	=> "Heading_20_1"
	);

# Appending a table with the installation parameters
my $office_format = $OpenOffice::OODoc::File::DEFAULT_OFFICE_FORMAT == 2 ?
	"OASIS Open Document" : "OpenOffice.org 1.0";
my $color_map	= $OpenOffice::OODoc::Styles::COLORMAP || "<none>";
$table = $doc->appendTable("Choices", 4, 2);
$doc->cellValue($table, "A1", "Local character set");
$doc->cellValue($table, "B1", $OpenOffice::OODoc::XPath::LOCAL_CHARSET);
$doc->cellValue($table, "A2", "Working directory");
$doc->cellValue($table, "B2", $OpenOffice::OODoc::File::WORKING_DIRECTORY);
$doc->cellValue($table, "A3", "RGB color map");
$doc->cellValue($table, "B3", $color_map);
$doc->cellValue($table, "A4", "Default office document format");
$doc->cellValue($table, "B4", $office_format);

# Opening the metadata of the document
my $meta = odfMeta(container => $archive, readable_XML => 'on')
	or die "# Unable to find regular metadata\n";
# Writing some metadata elements
$meta->title($title);
$meta->creator($ENV{'USER'});
$meta->initial_creator($ENV{'USER'});
$meta->description($description);
$meta->generator($generator);
$meta->creation_date($test_date);
$meta->date($test_date);
$meta->removeUserProperties();
$meta->setUserProperty("Application", type => 'string', value => "oodoc_test");
$meta->setUserProperty("Test date", type => 'date', value => $test_date);

# Saving the $testfile file
print "Saving $testfile file\n";
$archive->save;

$doc->dispose;
$styles->dispose;
$meta->dispose;

exit 0;