<?php
/*
 * $RCSfile: DcrawToolkit.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * 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., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.5 $ $Date: 2005/08/23 03:49:39 $
 * @package Dcraw
 * @author Vallimar <vallimar@sexorcisto.net>
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * Load required classes
 */
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryToolkit.class');
GalleryCoreApi::relativeRequireOnce('modules/dcraw/classes/DcrawToolkitHelper.class');

/**
 * A Dcraw version of GalleryToolkit
 *
 * This class implements the GalleryToolkit API using Dcraw.
 *
 * @package Dcraw
 * @subpackage Classes
 */
class DcrawToolkit extends GalleryToolkit {

    /**
     * @see GalleryToolkit::getProperty()
     */
    function getProperty($mimeType, $propertyName, $sourceFilename) {
	global $gallery;

	if ($mimeType != 'image/x-dcraw') {
	    return array(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__),
			 null);
	}

	switch($propertyName) {
	case 'dimensions':
	    /*
	     * Look to see if there's another toolkit that supports dimensions for PPM.  If we
	     * find one, then convert the image to PPM and let that toolkit tell us its dimensions.
	     */
	    list ($ret, $toolkit) =
		GalleryCoreApi::getToolkitByProperty('image/x-portable-pixmap', 'dimensions');
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }

	    if (isset($toolkit)) {
		$platform = $gallery->getPlatform();
		$tempfile = $platform->tempnam($gallery->getConfig('data.gallery.tmp'), 'dcrw_');
		list ($ret, $outputMimeType, $context) = $this->performOperation(
		    'image/x-dcraw', 'convert-to-image/x-portable-pixmap', $sourceFilename,
		    $tempfile, array(), array());
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}

		/* We now have a PPM file, get its dimensions */
		list ($ret, $results) =
		    $toolkit->getProperty('image/x-portable-pixmap', 'dimensions', $tempfile);
		$platform->unlink($tempfile);
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
	    } else {
		/* We have no idea :-( */
		$results = array(0, 0);
	    }
	    break;

	default:
	    return array(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__),
			 null);
	}

	return array(GalleryStatus::success(), $results);
    }

    /**
     * @see GalleryToolkit::performOperation()
     */
    function performOperation($mimeType, $operationName, $sourceFilename,
			      $destFilename, $parameters, $context=array()) {
	global $gallery;

	if ($mimeType != 'image/x-dcraw') {
	    return array(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__),
			 null, null);
	}

	switch($operationName) {
	case 'convert-to-image/x-portable-pixmap':
	    list ($ret, $dcraw) = GalleryCoreApi::getPluginParameter('module', 'dcraw', 'path');
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null, null);
	    }

	    $tmpDir = $gallery->getConfig('data.gallery.tmp');
	    $platform = $gallery->getPlatform();
	    $tmpFilename = $platform->tempnam($tmpDir, 'dcraw_');
	    if (empty($tmpFilename)) {
		/* This can happen if the $tmpDir path is bad */
		return array(GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__),
			     null, null);
	    }

	    $cmd = array($dcraw, '-c', '-a', '-q', $sourceFilename, '>', $tmpFilename);
	    $gallery->guaranteeTimeLimit(90);
	    list ($success, $output) = $platform->exec(array($cmd));
	    if (!$success) {
		@$platform->unlink($tmpFilename);
		return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__),
			     null, null);
	    }

	    $success = $platform->rename($tmpFilename, $destFilename);
	    if (!$success) {
		@$platform->unlink($tmpFilename);
		return array(GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__,
						  "Failed renaming $tmpFilename -> $destFilename"),
			     null, null);
	    }

	    return array(GalleryStatus::success(), 'image/x-portable-pixmap', $context);

	default:
	    return array(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__),
			 null, null);
	}
    }
}
?>
