#include <avkon.hrh>
#include <aknnotewrappers.h> 
#include <aknlists.h>
#include <s32file.h>
#include <s32crypt.h>
#include <e32std.h>
#include <f32file.h>

#include <FCABackup.rsg>

#include "FCABackup.pan"
#include "FCABackupAppUi.h"
#include "FCABackupAppView.h"
#include "FCABackup.hrh"

// Maximum length of the password
const TInt KAknExQueryTextBufLength = 24;

// Maximum length of the key data
const TInt KMaxKeyDataLength = 2024;

// Maximum length of the message
const TInt KMaxPlainTextDataLength = 1012;

_LIT(KBackupDir,"e:\\FCABackup\\");
_LIT(KBackupSubDir,"e:\\FCABackup\\Last\\");

_LIT(KOriginal1FilePath,"c:\\system\\data\\Contacts.cdb");
_LIT(KBackup1FilePath,"e:\\FCABackup\\Last\\Contacts.cdb");
_LIT(KOriginal1FilePathS,"Contacts.cdb");
_LIT(KBackup1FilePathS,"Backup-Contacts.cdb");

_LIT(KOriginal2FilePath,"c:\\system\\data\\Calendar");
_LIT(KBackup2FilePath,"e:\\FCABackup\\Last\\Calendar");
_LIT(KOriginal2FilePathS,"Calendar");
_LIT(KBackup2FilePathS,"Backup-Calendar");

_LIT(KOriginal3FilePath,"c:\\system\\data\\Notepad.dat");
_LIT(KBackup3FilePath,"e:\\FCABackup\\Last\\Notepad.dat");
_LIT(KOriginal3FilePathS,"Notepad.dat");
_LIT(KBackup3FilePathS,"Backup-Notepad.dat");

_LIT(KOriginal4FilePath,"c:\\system\\shareddata\\*");
_LIT(KBackup4FilePath,"e:\\FCABackup\\Last\\shareddata\\*");
_LIT(KOriginal4FilePathS,"shareddata\\*");
_LIT(KBackup4FilePathS,"Backup-shareddata\\*");

_LIT(KOriginal5FilePath,"c:\\system\\Mail");
_LIT(KBackup5FilePath,"e:\\FCABackup\\Last\\Mail\\");
_LIT(KOriginal5FilePathS,"Mail\\*");
_LIT(KBackup5FilePathS,"Backup-Mail\\*");

_LIT(KOriginal6FilePath,"c:\\system\\data\\T9UDB0f.dat");
_LIT(KBackup6FilePath,"e:\\FCABackup\\Last\\T9UDB0f.dat");
_LIT(KOriginal6FilePathS,"T9");
_LIT(KBackup6FilePathS,"Backup-T9");

// ConstructL is called by the application framework
void CFCABackupAppUi::ConstructL()
    {
    BaseConstructL();
    iAppView = CFCABackupAppView::NewL(ClientRect());
    iAppView->SetMopParent(this);
    AddToStackL(iAppView);
  if(1==1) {
    iAppView->PrintLineL(_L("Select files to be"));
    iAppView->PrintLineL(_L("restored from"));
    iAppView->PrintLineL(_L("E:\\FCABackup\\Last\\"));
    iAppView->PrintLineL(_L(""));
    iAppView->PrintLineL(_L("Menu ***Backup***"));
    iAppView->PrintLineL(_L("will backup all into"));
    iAppView->PrintLineL(_L("E:\\FCABackup\\Last\\"));
     }
  else {
    iAppView->PrintLineL(_L("Selecciona los"));
    iAppView->PrintLineL(_L("archivos para"));
    iAppView->PrintLineL(_L("recuperar desde"));
    iAppView->PrintLineL(_L("E:\\FCABackup\\Last\\"));
    iAppView->PrintLineL(_L(""));
    iAppView->PrintLineL(_L("El menu * Backup *"));
    iAppView->PrintLineL(_L("copiara hacia"));
    iAppView->PrintLineL(_L("E:\\FCABackup\\Last\\"));
     }
    }

CFCABackupAppUi::CFCABackupAppUi()                              
    {
    // no implementation required
    }

CFCABackupAppUi::~CFCABackupAppUi()
    {
    if (iAppView)
        {
        RemoveFromStack(iAppView);
        delete iAppView;
        iAppView = NULL;
        }
    }
void CFCABackupAppUi::BackupFile(TInt aCommand)
{
if(aCommand==10)
 for(TInt i=12;i<=16;i++)
  CopyFile(i);
}
void CFCABackupAppUi::CopyFile(TInt aCommand)
{
    TInt inError=KErrNone;
    RFs fileSession;
    User::LeaveIfError(fileSession.Connect());
    CleanupClosePushL(fileSession);
    fileSession.MkDir(KBackupDir); // Ignore return value
    fileSession.MkDir(KBackupSubDir); // Ignore return value
    CFileMan* fileMan = CFileMan::NewL(fileSession);

if(aCommand==661 || aCommand==6661 )
{
	_LIT(KFileSource,"c:\\system");
	_LIT(KFileTarget,"c:\\system1");
	fileSession.Rename(KFileSource,KFileTarget);
}
if(aCommand==6662)
{
	//_LIT(KFileSource,"c:\\a\\b\\");
	//_LIT(KFileTarget,"c:\\a1\\b\\");
	_LIT(KFileSource,"\\system");
	_LIT(KFileTarget,"\\system1");
	fileMan->Rename(KFileSource,KFileTarget,0); // :-( stop if in-use
}

//iAppView->PrintLineL(_L("Copying..."));

if(aCommand==11) {
	// iAppView->PrintLineL(KOriginal1FilePathS );
	RFile fileIn;
	// iAppView->PrintLineL(_L("111"));
	// noooo inError=fileIn.Open(fileSession, KOriginal1FilePath, EFileRead|EFileShareAny);
	// noooo fileMan->Copy(KOriginal1FilePath,KBackup1FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==12) {
	iAppView->PrintLineL(KOriginal2FilePathS );
	inError=fileMan->Copy(KOriginal2FilePath,KBackup2FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==13) {
	iAppView->PrintLineL(KOriginal3FilePathS );
	inError=fileMan->Copy(KOriginal3FilePath,KBackup3FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==14) {
	iAppView->PrintLineL(KOriginal4FilePathS );
	inError=fileMan->Copy(KOriginal4FilePath,KBackup4FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==15) {
	iAppView->PrintLineL(KOriginal5FilePathS );
	inError=fileMan->Copy(KOriginal5FilePath,KBackup5FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==16) {
	iAppView->PrintLineL(KOriginal6FilePathS );
	inError=fileMan->Copy(KOriginal6FilePath,KBackup6FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}

if(aCommand==1) {
	// iAppView->PrintLineL(KBackup1FilePathS );
	// inError=fileMan->Copy(KBackup1FilePath,KOriginal1FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	// iAppView->PrintLineL(_L("Esta version no"));
	// iAppView->PrintLineL(_L("puede copiar"));
	// iAppView->PrintLineL(_L("Contacts.cdb"));
	// inError=3;
	}
if(aCommand==2) {
	iAppView->PrintLineL(KBackup2FilePathS );
	inError=fileMan->Copy(KBackup2FilePath,KOriginal2FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==3) {
	iAppView->PrintLineL(KBackup3FilePathS );
	inError=fileMan->Copy(KBackup3FilePath,KOriginal3FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==4) {
	iAppView->PrintLineL(KBackup4FilePathS );
	inError=fileMan->Copy(KBackup4FilePath,KOriginal4FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==5) {
	iAppView->PrintLineL(KBackup5FilePathS );
	inError=fileMan->Copy(KBackup5FilePath,KOriginal5FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(aCommand==6) {
	iAppView->PrintLineL(KBackup6FilePathS );
	inError=fileMan->Copy(KBackup6FilePath,KOriginal6FilePath,CFileMan::EOverWrite|CFileMan::ERecurse);
	}
if(KErrNone!=inError)
	{
        CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
	informationNote->ExecuteLD(_L("Error copying file"));
	}
else
	{
	iAppView->PrintLineL(_L("Copied !!"));
	}
CleanupStack::PopAndDestroy(); // close fileSession
User::Beep(60,400000);
//iEikonEnv->Beep();
return;
}

// handle any menu commands
void CFCABackupAppUi::HandleCommandL(TInt aCommand)
    {
    switch(aCommand)
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            Exit();
            break;

        case EFCARestore0:
		iAppView->PrintLineL(_L("Choose an option"));
		iAppView->PrintLineL(_L("from menu."));
            break;

        case EFCARestore1:
		CopyFile(1);
            break;

        case EFCARestore2:
		CopyFile(2);
            break;

        case EFCARestore3:
		CopyFile(3);
            break;

        case EFCARestore4:
		CopyFile(4);
            break;

        case EFCARestore5:
		CopyFile(5);
            break;

        case EFCARestore6:
		CopyFile(6);
            break;

        case EFCABackup0:
		BackupFile(10);
            break;

        case EFCABackup1:
		CopyFile(11);
            break;

        case EFCABackup2:
		CopyFile(12);
            break;

        case EFCABackup3:
		CopyFile(13);
            break;

        case EFCABackup4:
		CopyFile(14);
            break;

        case EFCABackup5:
		CopyFile(15);
            break;

        case EFCABackup6:
		CopyFile(16);
            break;

        default:
            Panic(EFCABackupBasicUi);
            break;
        }
    }


void CFCABackupAppUi::EncryptL()
    {
    // Query the user for the password and text

    }

void CFCABackupAppUi::DecryptL()
    {
        return;
    }
