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

 
/*

File	: menu-mkdir.c
Autor	: Peter Juszack

Description
-----------------------------------------------------------------
Create directories on disc

*/


#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "menu-mkdir.h"
#include <vdr/menuitems.h>

void cMenuMkDir::Display(void) {
    SetHelp( tr("OK"), 0, 0, tr( "Cancel") );
    
    cOsdMenu::Display();
}

bool cMenuMkDir::mkDir() {
    if (strlen(name) > 0) {
	char dir[CAMERA_PATH_MAXLENGTH];
	snprintf(dir, CAMERA_PATH_MAXLENGTH, "%s/%s", path, name);
	
	if ( mkdir(dir, 0755) == 0 ) {
	    isyslog("Digicam: created directory '%s'", dir);
	    return true;
	} else
	    isyslog("Digicam: creating directory '%s' failed: %d - %s", dir, errno, strerror(errno) );
	    
    } else {
	OSD_ErrorMsg( tr("Please enter a name") );
    }
    
    return false;
}

cMenuMkDir::cMenuMkDir(const char* basePath):cOsdMenu( tr("Create folder"), 17) {
    name[0] = 0;
    strncpy( path, basePath, CAMERA_PATH_MAXLENGTH);
    
    Add( new cMenuEditStrItem( tr("Folder"), name, CAMERA_PATH_MAXLENGTH,
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_" ));

    Display();
}

eOSState cMenuMkDir::ProcessKey(eKeys Key) {
    eOSState state = osUnknown;

    switch(Key) {
	case kRed: 
	    if ( mkDir() )
		state = osBack;
	    else {
		state = osContinue;
		OSD_ErrorMsg( tr("Could not create folder") );
	    }
		
	    break;
	case kBlue: 
	    state = osBack; break;
	default: break;
    }
    
    if (state == osUnknown) {
	state = cOsdMenu::ProcessKey(Key);
	
	if (state != osUnknown)
	    Display();
    }
	
    if (state == osUnknown)
	state = osContinue;
    
    return state;
}
