/*
 * menu-camera.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *   
 * based on th image plugin
 *
 * 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: menu-camera.c,v 1.11 2005/02/01 08:42:15 peter Exp $
 */

 
/*

File	: menu-camera.c
Autor	: Peter Juszack

Description
-----------------------------------------------------------------
Camera file browser

*/

#include <vdr/osd.h>

#include "menu-camera.h"
#include "data.h"
#include "cameralist.h"
#include "digitalcamera.h"
#include "camera-info.h"
#include "digicam.h"
#include "facts-image.h"
#include "facts-directory.h"
#include "menu-destbrowse.h"
#include "digicam-logger.h"

#include "i18n.h"

cMenuCamera::cMenuCamera(void):cMenuBrowse(CameraSources.GetSource(), 
    new cCameraList(), true, tr("Camera browser") ) {
    
    // set camera wrapper reference
    dCam = &digitalCamera;

    // If camera error occured during listing
    if (dCam != NULL && dCam->getErrNo()) {
	ErrorOccured();
    } else // set buttons
        SetButtons();
}

cMenuCamera::~cMenuCamera() {
#ifdef DEBUG
    printf("cMenuCamera:~cMenuCamera\n");
#endif
    // release gphoto2 camera
    dCam->release();	
	dCam = NULL;
}

void cMenuCamera::ErrorOccured(void) {
    // no buttons available
    SetHelp( 0,0,0,0 );
    // remove all menu items ( [..] )
    Clear();
    Display();
    // report message to user
    cDigicamLogger::reportCameraError( dCam->getErrNo() );
}

void cMenuCamera::SetButtons(void) {
    /* set buttons
    - Select		- show directory/image-data for copy
    - Camera		- show camera information
    - Destination	- open browse menu to choose copy destination
    - Parent Dir	- goto parent dir
    */
    SetHelp( tr("Select"), tr("Camera info"), tr("Store destination"), currentdir ? tr("Parent") : 0 );
    Display();
}

eOSState cMenuCamera::Select( bool isred ) {
    cDirItem *item = CurrentItem();
    eOSState state = osContinue;
    
#ifdef DEBUG9
    printf("cMenuCamera::Select()");
#endif
    /* call base class implementation if ...    
	- nothing is selected
	- parent entry (..) is selected
	- OK button pressed and directory selected
    */
    if ( item == NULL || (!isred && item->Type == itDir) || item->Type == itParent ) {
	state = cMenuBrowse::Select( isred );
	
	// If camera error occured when getting new list
	if (dCam != NULL && dCam->getErrNo() != DIGICAM_NO_ERROR )
	    ErrorOccured();    

	return state;
    }

    // directory or file ?
    switch (item->Type) {
        case itFile:
#ifdef DEBUG10
	    printf("File selected\n");
#endif
	    state = AddSubMenu( new cFactsheetImage(item) );
	    refreshMenu = true;
	    break;
	case itDir:
#ifdef DEBUG10
	    printf("Directory selected\n");
#endif
	    state = AddSubMenu( new cFactsheetDirectory(item) );
	    refreshMenu = true;
	break;
	    
	default: state = osContinue; break;
    }
    
    return state;
}

// Process key or "button" event
eOSState cMenuCamera::ProcessKey(eKeys Key) {
    // if error occured: exit plugin
    if (dCam->getErrNo() != DIGICAM_NO_ERROR)
	return osEnd;

    // pass event to base class
    eOSState state = cMenuBrowse::ProcessKey( Key );
    
    // when base class was not able to handle the event
    if (state != osUnknown && state != osBack )
	return state;
    
    // validate pressed key
    switch(Key) {
        case kGreen: // Show camera info		
	    return AddSubMenu( new cCameraInfoMenu() );
	break;
	
	case kYellow: // Show copy destination menu
	    state = AddSubMenu( new cMenuDestinationBrowse() );
	    
#ifdef DEBUG
	    printf("CameraMenu: Source |%s|\n", cFactsheetBase::getCopyDestination() );
#endif
	break;
	default: 
	    if (state == osUnknown)
		state = osContinue;
    }
    
    return state;
}
