// EIKPWORD.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <eikpword.h>
#include <eikenv.h>
#include <eikpriv.hrh>
#include <eikon.rsg>

//
// class CEikSetPasswordDialog
//

EXPORT_C CEikSetPasswordDialog::CEikSetPasswordDialog(CSecurityBase& aSecurity,TPassword& aOldPassword)
// Set a password. aPassword comes in as the old password, if its length is
// zero this means no password is set. (The new password is not given back)
//
	: iSecurity(aSecurity),iOldPassword(aOldPassword)
	{}

void CEikSetPasswordDialog::PreLayoutDynInitL()
	{
	if (iSecurity.IsEnabled())
		// changing password
		SetTitleL(R_EIK_TBUF_CHANGE_PASSWORD);
	}

TBool CEikSetPasswordDialog::OkToExitL(TInt /*aButtonId*/)
	{
	TPassword password;
	GetSecretEditorText(password,EEikCidPassword);
	TPassword confirm;
    GetSecretEditorText(confirm,EEikCidPasswordConfirm);
	if (password.CompareF(confirm)==KErrNone)
		{
		password.Fold();
		const TBool setEnabled=(password.Length()!=0);  // check for null password, ie no password
		iSecurity.SetL(iOldPassword,password);
		iSecurity.SetEnabledL(password,setEnabled);
		return ETrue;
		}
	iEikonEnv->InfoMsg(R_EIK_TBUF_PASSWORD_NOT_CONFIRMED);
	ResetSecretEditor(EEikCidPasswordConfirm);
	ResetSecretEditor(EEikCidPassword);
	TryChangeFocusToL(EEikCidPassword);
    return EFalse;
	}

//
// class CEikVerifyPasswordDialog
//

EXPORT_C CEikVerifyPasswordDialog::CEikVerifyPasswordDialog(TBool& aPasswordCorrect,const CSecurityBase& aSecurity,TPassword& aPassword)
// aSecurity is the already primed security object.
// The user entered password is check to see if it generates the correct security data.
// If the user inputs the correct password then aPassword is set to this (non-encrypted)
//
	:iPasswordCorrect(aPasswordCorrect),iSecurity(aSecurity),iPassword(aPassword)
	{
	iPasswordCorrect=EFalse;
	}

TBool CEikVerifyPasswordDialog::OkToExitL(TInt /*aButtonId*/)
	{
    TPassword password;
    GetSecretEditorText(password,EEikCidPassword);
	TInt err=KErrNone;
	if (password.Length()==0)
		{
		iEikonEnv->InfoMsg(R_EIK_ENTER_PASSWORD);
		return EFalse;
		}
	else
		{
		// check that what the user types encrypts to the security data
		TRAP(err,CONST_CAST(CSecurityBase&,iSecurity).PrepareL(password));
		if (err!=KErrNone)
			{
			password.Fold();
			TRAP(err,CONST_CAST(CSecurityBase&,iSecurity).PrepareL(password));
			}
		if (err==KErrNone)
			{// user typed the correct password, so set iPassword (as return value)
			iPassword=password;
			iPasswordCorrect=ETrue;
			return ETrue;
			}
		else if (err==KErrGeneral)
			{
			iEikonEnv->InfoMsg(R_EIK_TBUF_INCORRECT_PASSWORD);
			ResetSecretEditor(EEikCidPassword);
			TryChangeFocusToL(EEikCidPassword);
			return EFalse;
			}
		}
	User::Leave(err);
	return EFalse;
	}
