[44646a]: src / ui / Windows / PasswordSubsetDlg.cpp Maximize Restore History

Download this file

PasswordSubsetDlg.cpp    286 lines (233 with data), 8.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (c) 2003-2014 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/// \file PasswordSubsetDlg.cpp
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "PasswordSubsetDlg.h"
#include "DboxMain.h"
#include "Fonts.h"
#include "core/StringX.h"
#include "core/PWSprefs.h"
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern HRGN GetWorkAreaRegion();
//-----------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
//{{AFX_MSG_MAP(CNumEdit)
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
const bool bSeparator = nChar == L' ' || nChar == L';' || nChar == L',';
if (LineLength(LineIndex(0)) == 0) {
// Initialise variables
m_bLastMinus = m_bLastSeparator = false;
// Ignore leading zeroes, backspaces, return or separator if edit field is empty
if (bSeparator || nChar == L'0' || nChar == VK_BACK || nChar == VK_RETURN)
return;
}
// Ensure character is a digit or a valid delimiter
// Minus signs are allowed but must be followed by a digit
// Otherwise just ignore it!
// Ignore pressed character if last character was a minus sign and now another minus sign
// or a zero or a separator is pressed
if (m_bLastMinus && (nChar == L'-' || nChar == L'0' || bSeparator))
return;
// Ignore pressed character if last character was a separator and now another separator
// or a zero is pressed
if (m_bLastSeparator && (nChar == L'0' || bSeparator))
return;
// Do not pass on any character that is not a digit, minus sign or separator
if (isdigit(nChar) || nChar == L'-' || bSeparator || nChar == VK_BACK) {
// Send on to CEdit control
CEdit::OnChar(nChar, nRepCnt, nFlags);
// Set minus sign state if pressed
if (nChar == L'-')
m_bLastMinus = true;
// Reset minus sign state after a digit or last character was a minus sign and
// a backspace has been pressed
if (isdigit(nChar) || (m_bLastMinus && nChar == VK_BACK))
m_bLastMinus = false;
// Set separator state if pressed
if (bSeparator)
m_bLastSeparator = true;
// Reset separator state after a digit or a minus sign or last character was a separator and
// a backspace has been pressed
if (isdigit(nChar) || nChar == L'-' || (m_bLastSeparator && nChar == VK_BACK))
m_bLastSeparator = false;
}
// If a separator is pressed, update displayed password subset
if (bSeparator || nChar == VK_RETURN || LineLength(LineIndex(0)) == 0)
GetParent()->SendMessage(WM_DISPLAYPASSWORDSUBSET);
}
//-----------------------------------------------------------------------------
CPasswordSubsetDlg::CPasswordSubsetDlg(CWnd* pParent, const StringX &passwd)
: CPWDialog(CPasswordSubsetDlg::IDD, pParent),
m_passwd(passwd), m_bshown(false), m_warningmsg(L"")
{
}
CPasswordSubsetDlg::~CPasswordSubsetDlg()
{
m_CopyPswdBitmap.Detach();
}
void CPasswordSubsetDlg::DoDataExchange(CDataExchange* pDX)
{
CPWDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPasswordSubsetDlg)
DDX_Text(pDX, IDC_SUBSET, m_subset);
DDX_Text(pDX, IDC_STATICSUBSETWARNING, m_warningmsg);
DDX_Control(pDX, IDC_SUBSETRESULTS, m_results);
DDX_Control(pDX, IDC_SUBSET, m_ne_subset);
DDX_Control(pDX, IDC_STATICSUBSETWARNING, m_stcwarningmsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPasswordSubsetDlg, CPWDialog)
//{{AFX_MSG_MAP(CPasswordSubsetDlg)
ON_WM_CTLCOLOR()
ON_MESSAGE(WM_DISPLAYPASSWORDSUBSET, OnDisplayStatus)
ON_BN_CLICKED(IDC_COPYPASSWORD, OnCopy)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CPasswordSubsetDlg::OnInitDialog()
{
CPWDialog::OnInitDialog();
Fonts::GetInstance()->ApplyPasswordFont(GetDlgItem(IDC_SUBSETRESULTS));
// Place dialog where user had it last
CRect rect, dlgRect;
GetWindowRect(&dlgRect);
PWSprefs::GetInstance()->GetPrefPSSRect(rect.top, rect.bottom,
rect.left, rect.right);
HRGN hrgnWork = GetWorkAreaRegion();
// also check that window will be visible
if ((rect.top == -1 && rect.bottom == -1 && rect.left == -1 && rect.right == -1) || !RectInRegion(hrgnWork, rect)){
rect = dlgRect;
}
::DeleteObject(hrgnWork);
// Ignore size just set position
SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
m_pToolTipCtrl = new CToolTipCtrl;
if (!m_pToolTipCtrl->Create(this, TTS_BALLOON | TTS_NOPREFIX)) {
pws_os::Trace(L"Unable To create CManagePSWDPols Dialog ToolTip\n");
delete m_pToolTipCtrl;
m_pToolTipCtrl = NULL;
} else {
EnableToolTips(TRUE);
// Delay initial show & reshow
int iTime = m_pToolTipCtrl->GetDelayTime(TTDT_AUTOPOP);
m_pToolTipCtrl->SetDelayTime(TTDT_AUTOPOP, iTime * 4);
m_pToolTipCtrl->Activate(TRUE);
m_pToolTipCtrl->SetMaxTipWidth(500);
const CString cs_ToolTip(MAKEINTRESOURCE(IDS_CLICKTOCOPYGENPSWD));
m_pToolTipCtrl->AddTool(GetDlgItem(IDC_COPYPASSWORD), cs_ToolTip);
}
// Load bitmap
UINT nImageID = PWSprefs::GetInstance()->GetPref(PWSprefs::UseNewToolbar) ?
IDB_COPYPASSWORD_NEW : IDB_COPYPASSWORD_CLASSIC;
m_CopyPswdBitmap.Attach(::LoadImage(
::AfxFindResourceHandle(MAKEINTRESOURCE(nImageID), RT_BITMAP),
MAKEINTRESOURCE(nImageID), IMAGE_BITMAP, 0, 0,
(LR_DEFAULTSIZE | LR_CREATEDIBSECTION | LR_SHARED)));
FixBitmapBackground(m_CopyPswdBitmap);
CButton *pBtn = (CButton *)GetDlgItem(IDC_COPYPASSWORD);
pBtn->SetBitmap(m_CopyPswdBitmap);
ShowWindow(SW_SHOW);
return TRUE;
}
BOOL CPasswordSubsetDlg::PreTranslateMessage(MSG* pMsg)
{
RelayToolTipEvent(pMsg);
return CPWDialog::PreTranslateMessage(pMsg);
}
HBRUSH CPasswordSubsetDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
HBRUSH hbr = CPWDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// Only deal with Static controls and then
// Only with our special one - change colour of warning message
if (nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_STATICSUBSETWARNING) {
if (((CStaticExtn *)pWnd)->GetColourState()) {
COLORREF cfUser = ((CStaticExtn *)pWnd)->GetUserColour();
pDC->SetTextColor(cfUser);
}
}
// Let's get out of here
return hbr;
}
void CPasswordSubsetDlg::OnCancel()
{
CRect rect;
GetWindowRect(&rect);
PWSprefs::GetInstance()->SetPrefPSSRect(rect.top, rect.bottom,
rect.left, rect.right);
if (m_bshown)
CPWDialog::EndDialog(4);
else
CPWDialog::OnCancel();
}
LRESULT CPasswordSubsetDlg::OnDisplayStatus(WPARAM /* wParam */, LPARAM /* lParam */)
{
UpdateData(TRUE);
m_stcwarningmsg.SetWindowText(L"");
m_stcwarningmsg.ResetColour();
m_subset.Trim();
int icurpos(0);
std::vector<int> vpos;
CString resToken(m_subset);
const size_t ipwlengh = m_passwd.length();
while (resToken != L"" && icurpos != -1) {
int lastpos = icurpos;
resToken = m_subset.Tokenize(L";, ", icurpos);
if (resToken == L"")
continue;
int ipos = _wtoi(resToken);
// ipos can't be zero but is valid if:
// a. Positive: 1 <= ipos <= password_length
// b. Negative: -password_lengh < ipos <= -1
if (abs(ipos) > (int)ipwlengh) {
m_warningmsg.Format(ipos > 0 ? IDS_SUBSETINDEXTOOBIG : IDS_SUBSETINDEXTOOSMALL, ipwlengh);
m_stcwarningmsg.SetWindowText(m_warningmsg);
m_stcwarningmsg.SetColour(RGB(255, 0, 0));
m_stcwarningmsg.Invalidate();
vpos.clear();
m_ne_subset.SetSel(lastpos, icurpos);
m_ne_subset.SetFocus();
// Disable Copy to Clipboard
GetDlgItem(IDC_COPYPASSWORD)->EnableWindow(FALSE);
return 0L;
}
if (ipos < 0)
ipos = (int)ipwlengh + ipos + 1;
vpos.push_back(ipos - 1);
};
std::vector<int>::const_iterator citer;
StringX sSubset;
for (citer = vpos.begin(); citer != vpos.end(); citer++) {
sSubset += m_passwd[*citer];
sSubset += L" ";
}
m_results.SetWindowText(sSubset.c_str());
m_bshown = true;
// Enable Copy to Clipboard
GetDlgItem(IDC_COPYPASSWORD)->EnableWindow(TRUE);
return 1L;
}
void CPasswordSubsetDlg::OnCopy()
{
CSecString cs_data;
int len = m_results.LineLength(m_results.LineIndex(0));
m_results.GetLine(0, cs_data.GetBuffer(len), len);
cs_data.ReleaseBuffer(len);
// Remove blanks from between the characters
cs_data.Remove(_T(' '));
GetMainDlg()->SetClipboardData(cs_data);
GetMainDlg()->UpdateLastClipboardAction(CItemData::PASSWORD);
}