/*
 * facts-directory.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-directory.c,v 1.13 2005/02/01 08:42:15 peter Exp $
 */

 
/*

File	: facts-directory.c
Autor	: Peter Juszack

Description
-----------------------------------------------------------------
directory menu

*/

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

#include <gphoto2/gphoto2.h>

#include "facts-directory.h"
#include "digicam.h"
#include "digicam-setup.h"
#include "digicam-logger.h"

cFactsheetDirectory::cFactsheetDirectory(cDirItem *file):
    cFactsheetBase( tr("Directory download"), file) {
    
    CameraList files;    
    CameraFileInfo fileInfo;
    unsigned long totalFileSize = 0;
    
    // Init class members    
    fileItem = file;
    fileCount = 0;
                
    if (digitalCamera.isAvailable() && fileItem != NULL ) {
	fileInfo.file.name[0] = 0;
#ifdef DEBUG
	printf("cFactsheetDirectory: directory = %s\n", fileItem->Path() );
#endif
	digitalCamera.setDirectory( "/", fileItem->Path() );

	// Get filelist
	if (digitalCamera.getFileList( &files ) != DIGICAM_NO_ERROR) {
	    cDigicamLogger::reportCameraError( digitalCamera.getErrNo() );
	    return;
	}
	
	fileCount = files.count;
	    	
	// walk through file list and get directory parameter (size, file count )
	for (int i=0; i<files.count; i++) {
#ifdef DEBUG
	    printf("Computing file %s\n", files.entry[i].name );
#endif
	    if (digitalCamera.getFileInformation( &fileInfo, files.entry[i].name ) !=
		DIGICAM_NO_ERROR) {
		cDigicamLogger::reportCameraError( digitalCamera.getErrNo() );
		return;		
	    }
	    
	    totalFileSize += fileInfo.file.size;
	}

	addAttribute( tr("Directory"), fileItem->Name );
	addAttribute( tr("File count"), files.count );
	addAttribute( tr("Size of all files"), ((double) totalFileSize) / 1024 / 1024, "mb" );
	addAttribute( tr("Store destination"), getCopyDestination() );
    }
    
    SetButtons();
}

void cFactsheetDirectory::SetButtons(void) {
    // Copy, delete only when files found
    //if (fileCount > 0)
	SetHelp( tr("Copy"), 0, tr("Delete"), tr("Cancel") );
    //else
	//SetHelp( 0, 0, 0, tr("Back") );
	
    Display();
}

// Download files(images) from the camera and save to disc
eOSState cFactsheetDirectory::SaveToDisc(void) {
    
    // If download shell be started as thread
    if (DigicamSetup.DownloadThread == 1) {
	// If no DirectoryLoader instance was created
	if (directoryLoader == NULL) {
    	    // create one
    	    directoryLoader = new cDirectoryLoader();
#ifdef DEBUG
	    printf("Created new cDirectoryLoader()\n");
#endif
	}

	// Download cannot be started when thread is active (another download in progress)
	if ( !directoryLoader->Active() ) {
#ifdef DEBUG
    	    printf("Thread not active => starting\n");
#endif
	    digitalCamera.release();
	    directoryLoader->Start( fileItem->Path() ); 	
	} else { // only for savety: should never happen because running copy thread is checked
    	    // before displaying browsing menu
    	    OSD_ErrorMsg( tr("Camera blocked") );
#ifdef DEBUG
    	    printf("Thread already active => ignoring\n");
#endif
	}
	
	return osEnd;
    } else { // Download not threaded
	cDirectoryLoader download;
	
	download.StartNoThread( &digitalCamera, fileItem->Path() );
	
	return osContinue;
    }    
}

eOSState cFactsheetDirectory::DeleteFromCamera(void) {
#ifdef DEBUG
    printf("Delete directory from camera\n" );
#endif
    eOSState state = osContinue;

    if (digitalCamera.isAvailable() && fileItem != NULL ) {
	// Confirmation by user
	if ( Interface->Confirm(tr("Delete directory?")) ) {
	    digitalCamera.setDirectory("/", fileItem->Subdir );
	    int result = digitalCamera.deleteFolder( fileItem->Name );

	    // Error: directory not empty	    
	    if (result == GP_ERROR_FILE_EXISTS) {
		// Confirm again to delete folder and all files
		if ( Interface->Confirm(tr("Not empty: Delete all?")) ) {
		    // delete folder and force deletion of all files
		    result = digitalCamera.deleteFolder( fileItem->Name, true );
		    if (result != 0)
		    	cDigicamLogger::reportCameraError( result );
		}
	    } else if (result != 0)
		cDigicamLogger::reportCameraError( result );
	    
	    // if no error occured close plugin because there is no way to refresh
	    // browsing list
	    if (result == 0) {
		OSD_StatusMsg( tr("Directory deleted") );
		state = osBack;
	    } else
		state = osContinue;
	} else
	    state = osContinue; // User said NO => do nothing
    }
    
    return state;
}
