用安全控件生成密码对话框

下列代码示例演示了如何使用安全控件来生成一个密码对话框。我们可以在资源文件中通过使用NUMSECRETED来定义一个数字安全控件,而通过使用SECRETED资源结构来定义一个字母安全控件。示例代码中含有两个SECRETED结构下的对话框。第一个包括密码和确认字段。第二个只是一个简单的文本框,可以输入密码字段。

 

lib:

 

LIBRARY     avkon.lib   //Avkon resources
 

资源文件

#define KMaxPasswordLength 10
 
RESOURCE DIALOG r_dialog_password_and_confirmation_query
{
flags = EGeneralQueryFlags;
 
buttons = R_AVKON_SOFTKEYS_OK_CANCEL;
items =
{
DLG_LINE
{
type = EAknCtMultilineQuery;
id = EMultilineFirstLine;
control = AVKON_DATA_QUERY
{
layout = EMultiDataFirstSecEd;
label = "Enter Password:";
control = SECRETED
{
num_letters = KMaxPasswordLength;
};
};
},
DLG_LINE
{
type = EAknCtMultilineQuery;
id = EMultilineSecondLine;
control = AVKON_DATA_QUERY
{
layout = EMultiDataSecondSecEd;
label = "Confirm Password:";
control = SECRETED
{
num_letters = KMaxPasswordLength;
};
};
}
};
}
 
 
RESOURCE DIALOG r_dialog_password_query
{
flags = EAknGeneralQueryFlags;
 
buttons = R_AVKON_SOFTKEYS_OK_CANCEL;
items =
{
DLG_LINE
{
type = EAknCtQuery;
id = EGeneralQuery;
control = AVKON_DATA_QUERY
{
layout = ECodeLayout;
label = "Enter Password:";
control = SECRETED
{
num_letters = KMaxPasswordLength;
};
};
}
};
}

头文件

#ifndef __PASSWORDAPPUI_H__
#define __PASSWORDAPPUI_H__
 
const TInt KMaxPasswordLength = 10;
//...
 
class CPasswordAppUi : public CAknAppUi
{
//...
private:
//...
TBool ShowPasswordAndConfirmationDialogL(TDes& aPassword);
TBool ShowPasswordDialogL(TDes& aPassword);
void UsePasswordDialogsL(); //a test method for dialogs
private:
//...
HBufC* iPassword;
};
 
#endif // __PASSWORDAPPUI_H__

源文件

#include <AknQueryDialog.h>
#include <Password_0xE0859401.rsg>
 
//...
CPasswordAppUi::CPasswordAppUi():iPassword(NULL)
{
}
 
CPasswordAppUi::~CPasswordAppUi()
{
//...
if (iPassword)
{
delete iPassword;
iPassword=NULL;
}
}
 
TBool CPasswordAppUi::ShowPasswordAndConfirmationDialogL(TDes& aPassword)
{
TBuf<KMaxPasswordLength> password;
TBuf<KMaxPasswordLength> confirmation;
 
CAknMultiLineDataQueryDialog* dlg =
CAknMultiLineDataQueryDialog::NewL(password, confirmation);
 
if (!dlg->ExecuteLD(R_DIALOG_PASSWORD_AND_CONFIRMATION_QUERY))
{
//Cancel key pressed
//do something...
return EFalse;
}
else
{
if(password.Compare(confirmation) == 0)
{
//password and confirmedPassword match.
//do something...
 
aPassword.Copy(password);
return ETrue;
}
else if(password.Length() != confirmation.Length())
{
//password buffer and confirmation buffer have different lengths
//do something...
 
return EFalse;
}
else
{
//password buffer and confirmation buffer have different contents
//do something...
 
return EFalse;
}
}
}
 
TBool CPasswordAppUi::ShowPasswordDialogL(TDes& aPassword)
{
TBuf<KMaxPasswordLength> password;
 
CAknTextQueryDialog* dlg =
new(ELeave) CAknTextQueryDialog(password, CAknQueryDialog::ENoTone );
 
if(!dlg->ExecuteLD(R_DIALOG_PASSWORD_QUERY))
{
//Cancel key pressed
//do something...
return EFalse;
}
else
{
//OK do something...
aPassword.Copy(password);
return ETrue;
}
}
 
 
void CPasswordAppUi::UsePasswordDialogsL()
{
//reset old password if exists
if(iPassword)
{
delete iPassword;
iPassword=NULL;
}
 
iPassword = HBufC::NewL(KMaxPasswordLength);
TPtr bufPtr = iPassword->Des();
 
//show password and confirmation dialog and store the given
//password in member variable iPassword if input values are ok
TBool ret = ShowPasswordAndConfirmationDialogL(bufPtr);
 
if(ret)
{
//create a temporary buffer to store the given password
HBufC* tmpPassword = HBufC::NewLC(KMaxPasswordLength);
TPtr tmpPtr = tmpPassword->Des();
 
//show a password dialog
TBool ret2 = ShowPasswordDialogL(tmpPtr);
 
if(ret2)
{
//compare the given password with member variable iPassword
if (tmpPassword->Compare(*iPassword) == 0)
{
//passwords match ok, do something....
}
else
{
//The given password is not correct, do something...
}
}
else
{
//the user pressed cancel, there is no password to compare
//do something...
}
 
CleanupStack::PopAndDestroy(); //tmpPassword
}
else
{
//giving a new password failed, do something...
delete iPassword;
iPassword=NULL;
}
}

FROM

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值