Click here to Skip to main content
15,885,186 members
Articles / Desktop Programming / MFC

Exile 1.8 - The Password Manager

Rate me:
Please Sign up or sign in to vote.
4.57/5 (51 votes)
6 Mar 20058 min read 255.1K   7.4K   111  
Yet another password manager.
// NewDialog.cpp : implementation file
//

#include "stdafx.h"
#include "Exile.h"
#include "NewDialog.h"
#include "PasswordGeneratorDialog.h"
#include "security.h"
#include "messagebox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNewDialog dialog


CNewDialog::CNewDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CNewDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNewDialog)
	m_strMasterPassword = _T("");
	m_nRounds = 0;
	m_strUserName = _T("");
	//}}AFX_DATA_INIT
	m_bPassword = FALSE;
}

CNewDialog::~CNewDialog()
{
	CleanString(m_strMasterPassword);
	CleanString(m_strUserName);
}

void CNewDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNewDialog)
	DDX_Text(pDX, IDC_MASTERPASSWORD, m_strMasterPassword);
	DDX_Text(pDX, IDC_ROUNDS, m_nRounds);
	DDX_Text(pDX, IDC_USERNAME, m_strUserName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNewDialog, CDialog)
	//{{AFX_MSG_MAP(CNewDialog)
	ON_CBN_SELCHANGE(IDC_KEYSIZE, OnSelchangeKeysize)
	ON_BN_CLICKED(IDC_MASTERPASSWORD_TITLE, OnMasterpasswordTitle)
	ON_COMMAND(ID_PASSWORDCONTEXT_GENERATEPASSWORD, OnPasswordcontextGeneratepassword)
	ON_COMMAND(ID_PASSWORDCONTEXT_HIDEPASSWORD, OnPasswordcontextHidepassword)
	ON_COMMAND(ID_PASSWORDCONTEXT_SHOWPASSWORD, OnPasswordcontextShowpassword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewDialog message handlers

void CNewDialog::OnOK() 
{
	UpdateData();

	if(m_strUserName.IsEmpty()) 
	{
		MessageBoxEx(*this, IDS_ENTERUSERNAME, IDS_TITLE);
		return;
	} // if

	if(m_strMasterPassword.IsEmpty()) 
	{
		MessageBoxEx(*this, IDS_ENTERMASTERPASSWORD, IDS_TITLE);
		return;
	} // if

	if((0 > m_nRounds) || (255 < m_nRounds))
	{
		MessageBoxEx(*this, IDS_ENTERROUNDS, IDS_TITLE);
		return;
	} // if

	CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_KEYSIZE);
	
	if(CB_ERR != pCombo->GetCurSel()) 
	{
		m_nKeySize = LOWORD(pCombo->GetItemData(pCombo->GetCurSel()));
	} // if
	else
	{
		MessageBoxEx(*this, IDS_SELECTKEYSIZE, IDS_TITLE);
		return;
	} // else

	CDialog::OnOK();
}

BOOL CNewDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_KEYSIZE);

	for(int n = 0; n < sizeof(g_aKeySizes) / sizeof(g_aKeySizes[0]); ++n)
	{
		pCombo->InsertString(n, g_aKeySizes[n].szItem);
		pCombo->SetItemData(n, g_aKeySizes[n].lInfo);
	} // for

	m_ctrlPassword.SubclassDlgItem(IDC_MASTERPASSWORD_TITLE, this);
	SwitchPassword();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CNewDialog::OnSelchangeKeysize() 
{
	CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_KEYSIZE);
	
	if(CB_ERR != pCombo->GetCurSel()) 
	{
		UpdateData(TRUE);
		m_nRounds = HIWORD(pCombo->GetItemData(pCombo->GetCurSel()));
		UpdateData(FALSE);
	} // if
}

void CNewDialog::OnMasterpasswordTitle() 
{
	CRect rc;
	GetDlgItem(IDC_MASTERPASSWORD_TITLE)->GetWindowRect(rc);
	//GetDlgItem(IDC_MASTERPASSWORD_TITLE)->ClientToScreen(rc);

	CMenu mCtx;
	mCtx.LoadMenu(IDR_PASSWORD_CONTEXT);

	CMenu *pmCtx = mCtx.GetSubMenu(0);

	pmCtx->EnableMenuItem(ID_PASSWORDCONTEXT_HIDEPASSWORD, MF_BYCOMMAND | 
		(m_bPassword ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
	pmCtx->EnableMenuItem(ID_PASSWORDCONTEXT_SHOWPASSWORD, MF_BYCOMMAND | 
		(!m_bPassword ? MF_ENABLED : MF_DISABLED | MF_GRAYED));

	pmCtx->TrackPopupMenu(TPM_LEFTALIGN, rc.left, rc.bottom, this);
}

void CNewDialog::OnPasswordcontextGeneratepassword() 
{
	CPasswordGeneratorDialog dlgPassword;

	//
	// Settings
	//
	dlgPassword.m_bDecimalDigits = m_bDecimalDigits;
	dlgPassword.m_bLowercase = m_bLowercase;
	dlgPassword.m_bPunctuation = m_bPunctuation;
	dlgPassword.m_bSpecial = m_bSpecial;
	dlgPassword.m_bUppercase = m_bUppercase;
	dlgPassword.m_nPasswordLength = m_nPasswordLength;

	if(IDOK == dlgPassword.DoModal()) 
	{
		UpdateData();
		m_strMasterPassword = dlgPassword.m_strPassword;
		UpdateData(FALSE);
		CleanString(dlgPassword.m_strPassword);

		//
		// Settings back
		//
		m_bDecimalDigits = dlgPassword.m_bDecimalDigits;
		m_bLowercase = dlgPassword.m_bLowercase;
		m_bPunctuation = dlgPassword.m_bPunctuation;
		m_bSpecial = dlgPassword.m_bSpecial;
		m_bUppercase = dlgPassword.m_bUppercase;
		m_nPasswordLength = dlgPassword.m_nPasswordLength;
	} // if
}

void CNewDialog::OnPasswordcontextHidepassword() 
{
	m_bPassword = !m_bPassword;
	SwitchPassword();
}

void CNewDialog::OnPasswordcontextShowpassword() 
{
	m_bPassword = !m_bPassword;
	SwitchPassword();
}

void CNewDialog::SwitchPassword()
{
	if(!m_bPassword)
	{
		((CEdit *)GetDlgItem(IDC_MASTERPASSWORD))->SetPasswordChar(_T('*'));
		GetDlgItem(IDC_MASTERPASSWORD)->Invalidate();
	} // if
	else
	{
		((CEdit *)GetDlgItem(IDC_MASTERPASSWORD))->SetPasswordChar(0);
		GetDlgItem(IDC_MASTERPASSWORD)->Invalidate();
	} // if
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
I'll think about it later on...

Comments and Discussions