<?php
/*
 * $RCSfile: ThumbnailToolkit.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.9 $ $Date: 2005/08/23 03:49:56 $
 * @package Thumbnail
 * @author Alan Harder <alan.harder@sun.com>
 */

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

/**
 * A version of GalleryToolkit to supply default thumbnail images for
 * non-image mime types.
 *
 * @package Thumbnail
 * @subpackage Classes
 */
class ThumbnailToolkit extends GalleryToolkit {

    /**
     * @see GalleryToolkit::getProperty()
     */
    function getProperty($mimeType, $propertyName, $sourceFilename) {
	return array(GalleryStatus::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__), null);
    }

    /**
     * @see GalleryToolkit::performOperation()
     */
    function performOperation($mimeType, $operationName, $sourceFilename,
			      $destFilename, $parameters, $context=array()) {
	GalleryCoreApi::relativeRequireOnce('modules/thumbnail/classes/ThumbnailHelper.class');
	global $gallery;
	$platform = $gallery->getPlatform();

	list ($ret, $mimeTypeMap) = ThumbnailHelper::fetchMimeTypeMap();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}
	if (!isset($mimeTypeMap[$mimeType]) || $operationName != 'convert-to-image/jpeg') {
	    return array(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
					      "$operationName $mimeType"), null, null);
	}
	list ($ret, $thumbImage) = GalleryCoreApi::loadEntitiesById($mimeTypeMap[$mimeType]);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}
	list ($ret, $thumbPath) = $thumbImage->fetchPath();
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}

	$outputMimeType = 'image/jpeg';
	$success = $platform->copy($thumbPath, $destFilename);
	if (!$success) {
	    return array(GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__,
					"Failed copying $thumbPath -> $destFilename"), null, null);
	}

	if (isset($context['width'])) {
	    $context['width'] = $thumbImage->getWidth();
	    $context['height'] = $thumbImage->getHeight();
	}

	return array(GalleryStatus::success(), $outputMimeType, $context);
    }
}
?>
