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

 
/*

File	: menu-destbrowse.c
Autor	: Peter Juszack

Description
-----------------------------------------------------------------
File browser for store destination

*/

#include <vdr/osd.h>

#include "menu-camera.h"
#include "data.h"
#include "digicam.h"
#include "menu-destbrowse.h"
#include "facts-base.h"
#include "i18n.h"
#include "menu-mkdir.h"

#include <string.h>

cMenuDestinationBrowse::cMenuDestinationBrowse(void):cMenuBrowse(CopyDestinations.GetSource(), 
    new cDirList(), true, tr("Store destination browser") ) {
    
    sourcing = false;
    
    SetButtons();
}

cMenuDestinationBrowse::~cMenuDestinationBrowse() {
}

void cMenuDestinationBrowse::SetButtons(void) {
    SetHelp( tr("Select"), tr("Create folder"), tr("Store destination"), currentdir ? tr("Parent") : 0 );
    Display();
}

eOSState cMenuDestinationBrowse::Source(bool second) {
    //if ( HasSubMenu() )
	//return osContinue;

#ifdef DEBUG
    printf("Source called\n");
#endif
	
    if (!second) {
	sourcing = true;
	return AddSubMenu(new cMenuSource(&CopyDestinations, tr("Store destination") ) );
    }
    
    sourcing = false;
    cFileSource* src = cMenuSource::GetSelected();
    
    if (src != NULL) {
	cFactsheetBase::setCopyDestination(src->BaseDir() );
	CopyDestinations.SetSource(src);
	SetSource(src);
	NewDir(0);
    }
    
    return osContinue;
}

eOSState cMenuDestinationBrowse::Select( bool isred ) {
    cDirItem *item = CurrentItem();
    
    if ( item == NULL || !isred || item->Type == itParent )
	return cMenuBrowse::Select( isred );

    if ( isred ) {
	
	switch (item->Type) {
	    case itFile:
#ifdef DEBUG
		printf("File selected\n");
#endif
	    OSD_ErrorMsg( tr("Please select a directory") );
	    break;
	    case itDir:
#ifdef DEBUG
		printf("New destination: %s\n", item->Path() );
#endif
		// set static copy destination path for all fact menues
	    	cFactsheetBase::setCopyDestination( source->BuildName(item->Path()) );
#ifdef DEBUG
		printf("Destination set to: %s\n", cFactsheetBase::getCopyDestination() );
#endif
	    	return osBack;
	    break;
	    
	    default: break;
	}
    }
    
    return osContinue;
}

// Process key or "button" event
eOSState cMenuDestinationBrowse::ProcessKey(eKeys Key) {
    eOSState state = osUnknown;

    switch(Key) {
        case kYellow: // Show source selection
#ifdef DEBUG
	    printf("Destination selection\n");
#endif
	    state = Source(false);
	    //    state = AddSubMenu(new cMenuSource(&CopyDestinations, "Copy destination" ) );
#ifdef DEBUG
	    printf("AddSubMenu returned: %d\n", state );
#endif
	    break;
	case kGreen: {
		cDirItem *item = CurrentItem();
		if (item != NULL) {
		    if (item->Subdir != NULL)
			AddSubMenu( new cMenuMkDir( source->BuildName(item->Subdir) ));
		    else
			AddSubMenu( new cMenuMkDir( source->BaseDir()) );
			
		    refreshMenu = true;
		}
		
		state = osContinue;
	    }
	    break;
	    
	default: break;	
    }    

    if (state == osUnknown)
	// pass event to base class
        state = cMenuBrowse::ProcessKey( Key );

    if (!HasSubMenu() && state == osContinue) {
	if (sourcing)
	    return Source(true);
    }
    	
    return state;
}
