/*
 * facts-image.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *   
 *
 * This code 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 code 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 * $Id: facts-image.c,v 1.9 2005/02/01 08:42:15 peter Exp $
 */

 
/*

File	: facts-image.c
Autor	: Peter Juszack

Description
-----------------------------------------------------------------
image download menu

*/

#include <vdr/menuitems.h>
#include <vdr/status.h>
#include <vdr/interface.h>

#include <gphoto2/gphoto2.h>

#include "facts-image.h"
#include "digicam.h"
#include "digicam-logger.h"
#include "i18n.h"

cFactsheetImage::cFactsheetImage(cDirItem *file):cFactsheetBase(tr("Image download"), file) {
    char status[2][10];
    char *download = NULL;
        
    snprintf(status[0], 10, tr("yes") );
    snprintf(status[1], 10, tr("no") );
    
    CameraFileInfo fileInfo;
    
    if (digitalCamera.isAvailable() && fileItem != NULL ) {
	fileInfo.file.name[0] = 0;
#ifdef DEBUG
	printf("cFactsheetImage: file = %s\n", fileItem->Name );
#endif
	// Get file information
	if (digitalCamera.getFileInformation( &fileInfo, fileItem->Name ) != DIGICAM_NO_ERROR) {
	    cDigicamLogger::reportCameraError( digitalCamera.getErrNo() );
	    return;
	}

	if (fileInfo.file.status == GP_FILE_STATUS_DOWNLOADED)
	    download = status[0];
	else
	    download = status[1];
	
	addAttribute( tr("File"), fileInfo.file.name );
	addAttribute( tr("Size"), ((double) fileInfo.file.size) / 1024 / 1024, "mb" );
	addAttribute( tr("Downloaded"), download );
	addAttribute( tr("Store destination"), getCopyDestination() );
    }
    
    SetButtons();
}

// Download file(image) from the camera and save to disc
eOSState cFactsheetImage::SaveToDisc(void) {
    CameraFile *file = NULL;
    
    // Check camera instance and cDirItem
    if (digitalCamera.isAvailable() && fileItem != NULL) {
#ifdef DEBUG
    printf("Loading file '%s' from camera\n", fileItem->Name );
#endif
	// Start download
	gp_file_new(&file);

	// print status message
	OSD_StatusMsg( tr("Downloading file from camera...") );
	digitalCamera.getFile( fileItem->Name, file );

	// print status message
	OSD_StatusMsg( tr("Storing file...") );

	// Saving file to disc (testing with fix path)
	snprintf( buf, FACTSHEET_BUFLEN, "%s/%s", getCopyDestination(), fileItem->Name );

	digitalCamera.saveFileToDisc( file, buf );

	OSD_InfoMsg( tr("File stored") );	
	
	// release file object	
	gp_file_free(file);
	
	file = NULL;
    }

    return osContinue;
}

eOSState cFactsheetImage::DeleteFromCamera(void) {
#ifdef DEBUG
    printf("cFactsheetImage::DeleteFromCamera\n" );
#endif
    eOSState state = osBack;

    if (digitalCamera.isAvailable() && fileItem != NULL) {
	// Confirmation by user
	if (Interface->Confirm(tr("Delete image?")) ) {    
	    // Delete file
#ifdef DEBUG
	    printf("Delete image from camera\n" );
#endif
	    if ( digitalCamera.deleteFile( fileItem->Name ) == 0) {
		OSD_StatusMsg( tr("Image deleted") );    
		// close plugin because there is currently no way to refresh
		// browsing list (deleted file will still appear)
		state = osBack;
	    } else {
		cDigicamLogger::reportCameraError( digitalCamera.getErrNo() );
		state = osContinue;
	    }
	} else // User not confirmed => do nothing
	    state = osContinue;
    }

    return state;
}
