Index: gs2-extensions/open-office/trunk/perllib/plugins/OpenOfficeConverter.pm
===================================================================
--- gs2-extensions/open-office/trunk/perllib/plugins/OpenOfficeConverter.pm	(revision 22227)
+++ gs2-extensions/open-office/trunk/perllib/plugins/OpenOfficeConverter.pm	(revision 22227)
@@ -0,0 +1,214 @@
+###########################################################################
+#
+# OpenOfficeConverter - helper plugin that does office document conversion 
+#                       using jodconverter combined with OpenOffice
+#
+# A component of the Greenstone digital library software
+# from the New Zealand Digital Library Project at the 
+# University of Waikato, New Zealand.
+#
+# Copyright (C) 2010 New Zealand Digital Library Project
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+###########################################################################
+package OpenOfficeConverter;
+
+use BaseMediaConverter;
+
+use strict;
+no strict 'refs'; # allow filehandles to be variables and viceversa
+
+use gsprintf 'gsprintf';
+
+BEGIN {
+    @OpenOfficeConverter::ISA = ('BaseMediaConverter');
+}
+
+my $arguments = [ ];
+
+my $opt_arguments = [
+    { 'name' => "openoffice_scripting",
+      'desc' => "{OpenOfficeConverter.openoffice_scripting}",
+      'type' => "flag",
+      'reqd' => "no" },
+    { 'name' => "openoffice_port",
+      'desc' => "{OpenOfficeConverter.openoffice_scripting}",
+      'type' => "int",
+      'deft' => "8100",
+      'range' => "81,",
+      'reqd' => "no" },
+    ];
+
+my $options = { 'name' => "OpenOfficeConverter",
+		'desc' => "{OpenOfficeConverter.desc}",
+		'abstract' => "yes",
+		'inherits' => "yes",
+		'args' => $arguments };
+
+sub new {
+    my ($class) = shift (@_);
+    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
+    push(@$pluginlist, $class);
+
+
+    # Check that OpenOffice and jodconverter are installed and available on 
+    # the path 
+
+    my $openoffice_conversion_available = 1;
+    my $no_openoffice_conversion_reason = "";
+    
+    if (! defined $ENV{'GEXTOPENOFFICE'}) {
+	$openoffice_conversion_available = 0;
+	$no_openoffice_conversion_reason = "gextopenofficenotinstalled";
+    }
+    else {
+	my $gextoo_home = $ENV{'GEXTOPENOFFICE'};
+	my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
+
+	if (!-e $jodjar) {
+	    print STDERR "Failed to find $jodjar\n";
+	    $openoffice_conversion_available = 0;
+	    $no_openoffice_conversion_reason = "gextjodconverternotinstalled";
+	}
+	else {
+	    # test to see if soffice in in path
+	    my $cmd = "soffice -headless -help 2>&1";
+
+	    if (system($cmd)!=0) {
+		print STDERR "Failed to run: $cmd\n";
+		print STDERR "$!\n";
+		$openoffice_conversion_available = 0;
+		$no_openoffice_conversion_reason = "openofficenotinstalled";
+	    }
+	}
+    }
+
+    if ($openoffice_conversion_available) {
+	push(@$arguments,$opt_arguments);
+
+	# have necessary components => launch OpenOffice in headless mode
+	# listing to localhost port
+    }
+
+    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
+    push(@{$hashArgOptLists->{"OptList"}},$options);
+
+    my $self 
+	= new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
+
+    if ($openoffice_conversion_available) {
+
+	my $oo_port = $self->{'openoffice_port'};
+	my $launch_cmd = "soffice";
+	$launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
+	$launch_cmd .= " -headless";
+	
+	$self->{'openoffice_launch_cmd'} = $launch_cmd;	
+    }
+    else {       
+	$self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
+	$self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
+
+	my $outhandle = $self->{'outhandle'};
+	&gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
+    }  
+    
+    return bless $self, $class;
+
+}
+
+sub init {
+    my $self = shift(@_);
+
+    $self->{'ootmp_file_paths'} = ();
+}
+
+sub deinit {
+    my $self = shift(@_);
+
+    $self->clean_up_temporary_files();
+}
+	
+
+sub convert {
+    my $self = shift(@_);
+    my $source_file_path = shift(@_);
+    my $target_file_type = shift(@_);
+    my $convert_options  = shift(@_) || "";
+    my $convert_id       = shift(@_) || "";
+    my $cache_mode       = shift(@_) || "";
+
+    my $outhandle = $self->{'outhandle'};
+    my $verbosity = $self->{'verbosity'};
+
+    my $source_file_no_path = &File::Basename::basename($source_file_path);
+
+    # Determine the full name and path of the output file
+    my $target_file_path;
+    if ($self->{'enable_cache'}) {
+	my $cached_image_dir = $self->{'cached_dir'};
+	my $image_root = $self->{'cached_file_root'};
+	$image_root .= "_$convert_id" if ($convert_id ne "");
+	my $image_file = "$image_root.$target_file_type";
+	$target_file_path = &util::filename_cat($cached_image_dir,$image_file);
+    }
+    else {
+	$target_file_path = &util::get_tmp_filename($target_file_type);
+	push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
+    }
+
+    # Generate and run the convert command
+    my $convert_command = "oo2html $convert_options \"$source_file_path\" \"$target_file_path\"";
+
+    my $print_info = { 'message_prefix' => $convert_id,
+		       'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
+    $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
+
+    my ($regenerated,$result,$had_error) 
+	= $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
+
+    return ($result,$target_file_path);
+}
+
+sub convert_without_result {
+    my $self = shift(@_);
+
+    my $source_file_path = shift(@_);
+    my $target_file_type = shift(@_);
+    my $convert_options  = shift(@_) || "";
+    my $convert_id       = shift(@_) || "";
+
+    return $self->convert($source_file_path,$target_file_type,
+			  $convert_options,$convert_id,"without_result");
+}
+
+
+
+sub clean_up_temporary_files {
+    my $self = shift(@_);
+
+    foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
+	if (-e $ootmp_file_path) {
+	    &util::rm($ootmp_file_path);
+	}
+    }
+
+    $self->{'ootmp_file_paths'} = ();
+}
+
+
+
+1;	
