[040794]: PWHistDlg.cpp Maximize Restore History

Download this file

PWHistDlg.cpp    268 lines (226 with data), 7.6 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
// PWHistDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PasswordSafe.h"
#include "ThisMfcApp.h"
#include "resource.h"
#include "PWHistDlg.h"
#include "corelib/ItemData.h"
#include ".\pwhistdlg.h"
// CPWHistDlg dialog
IMPLEMENT_DYNAMIC(CPWHistDlg, CDialog)
CPWHistDlg::CPWHistDlg(CWnd* pParent, bool IsReadOnly,
CMyString &HistStr, PWHistList &PWHistList,
int NumPWHistory, int &MaxPWHistory,
BOOL &SavePWHistory)
: CDialog(CPWHistDlg::IDD, pParent),
m_IsReadOnly(IsReadOnly),
m_HistStr(HistStr), m_PWHistList(PWHistList),
m_NumPWHistory(NumPWHistory), m_MaxPWHistory(MaxPWHistory),
m_SavePWHistory(SavePWHistory),
m_ClearPWHistory(false),
m_iSortedColumn(-1), m_bSortAscending(TRUE)
{
m_oldMaxPWHistory = m_MaxPWHistory;
}
CPWHistDlg::~CPWHistDlg()
{
}
void CPWHistDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PWHISTORY_LIST, m_PWHistListCtrl);
DDX_Check(pDX, IDC_SAVE_PWHIST, m_SavePWHistory);
DDX_Text(pDX, IDC_MAXPWHISTORY, m_MaxPWHistory);
DDV_MinMaxInt(pDX, m_MaxPWHistory, 1, 25);
}
BEGIN_MESSAGE_MAP(CPWHistDlg, CDialog)
ON_BN_CLICKED(IDC_CLEAR_PWHIST, OnBnClickedClearPWHist)
ON_BN_CLICKED(IDC_SAVE_PWHIST, OnCheckedSavePasswordHistory)
ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
ON_NOTIFY(NM_CLICK, IDC_PWHISTORY_LIST, OnHistListClick)
ON_BN_CLICKED(IDC_PWH_COPY_ALL, OnBnClickedPwhCopyAll)
END_MESSAGE_MAP()
BOOL CPWHistDlg::OnInitDialog()
{
CDialog::OnInitDialog();
GetDlgItem(IDC_MAXPWHISTORY)->EnableWindow(m_SavePWHistory ? TRUE : FALSE);
BOOL bpwh_count = (m_PWHistList.GetCount() == 0) ? FALSE : TRUE;
GetDlgItem(IDC_CLEAR_PWHIST)->EnableWindow(bpwh_count);
GetDlgItem(IDC_PWHISTORY_LIST)->EnableWindow(bpwh_count);
if (m_IsReadOnly) {
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_SAVE_PWHIST)->EnableWindow(FALSE);
GetDlgItem(IDC_CLEAR_PWHIST)->EnableWindow(FALSE); // overrides count
}
m_PWHistListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);
m_PWHistListCtrl.InsertColumn(0, _T("Set Date/Time"));
m_PWHistListCtrl.InsertColumn(1, _T("Password"));
int nPos = 0;
POSITION itempos;
POSITION listpos = m_PWHistList.GetHeadPosition();
while (listpos != NULL) {
itempos = listpos;
const PWHistEntry pwhentry = m_PWHistList.GetAt(listpos);
nPos = m_PWHistListCtrl.InsertItem(nPos, pwhentry.changedate);
m_PWHistListCtrl.SetItemText(nPos, 1, pwhentry.password);
m_PWHistListCtrl.SetItemData(nPos, (DWORD)itempos);
m_PWHistList.GetNext(listpos);
}
m_PWHistListCtrl.SetRedraw(FALSE);
for (int i = 0; i < 2; i++) {
m_PWHistListCtrl.SetColumnWidth(i, LVSCW_AUTOSIZE);
int nColumnWidth = m_PWHistListCtrl.GetColumnWidth(i);
m_PWHistListCtrl.SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
int nHeaderWidth = m_PWHistListCtrl.GetColumnWidth(i);
m_PWHistListCtrl.SetColumnWidth(i, max(nColumnWidth, nHeaderWidth));
}
m_PWHistListCtrl.SetRedraw(TRUE);
char buffer[10];
#if _MSC_VER >= 1400
sprintf_s(buffer, 10, "%d", m_NumPWHistory);
#else
sprintf(buffer, "%d", m_NumPWHistory);
#endif
CSpinButtonCtrl* pspin = (CSpinButtonCtrl *)GetDlgItem(IDC_PWHSPIN);
if (m_MaxPWHistory == 0)
m_MaxPWHistory = 1;
pspin->SetBuddy(GetDlgItem(IDC_MAXPWHISTORY));
pspin->SetRange(1, 25);
pspin->SetBase(10);
pspin->SetPos(m_MaxPWHistory);
return TRUE;
}
// CPWHistDlg message handlers
void CPWHistDlg::OnBnClickedClearPWHist()
{
m_ClearPWHistory = true;
m_PWHistListCtrl.DeleteAllItems();
}
void
CPWHistDlg::OnOK()
{
UpdateData(TRUE);
/* Handle history header.
* Header is in the form fmmnn, where:
* f = {0,1} if password history is on/off
* mm = 2 digits max size of history list
* nn = 2 digits current size of history list
*
* Special case: history empty and password history off - do nothing
*/
const CMyString oldHistStr = m_HistStr;
if (m_ClearPWHistory == TRUE) {
m_PWHistList.RemoveAll();
m_HistStr = m_HistStr.Left(5);
}
if (!(m_HistStr.IsEmpty() && m_SavePWHistory == FALSE)) {
TCHAR buffer[6];
#if _MSC_VER >= 1400
sprintf_s
#else
sprintf
#endif
(buffer,
#if _MSC_VER >= 1400
6,
#endif
"%1x%02x%02x",
(m_SavePWHistory == FALSE) ? 0 : 1,
m_MaxPWHistory,
m_PWHistList.GetCount()
);
if (m_HistStr.GetLength() >= 5) {
for (int i = 0; i < 5; i++) m_HistStr.SetAt(i, buffer[i]);
} else {
m_HistStr = buffer;
}
}
CDialog::OnOK();
}
void
CPWHistDlg::OnCheckedSavePasswordHistory()
{
m_SavePWHistory = ((CButton*)GetDlgItem(IDC_SAVE_PWHIST))->GetCheck();
GetDlgItem(IDC_MAXPWHISTORY)->EnableWindow(m_SavePWHistory ? TRUE : FALSE);
}
void
CPWHistDlg::OnHistListClick(NMHDR* pNMHDR, LRESULT*)
{
LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE) pNMHDR;
ASSERT(lpnmitem != NULL);
int item = lpnmitem->iItem;
POSITION itempos = POSITION(m_PWHistListCtrl.GetItemData(item));
const PWHistEntry pwhentry = m_PWHistList.GetAt(itempos);
app.SetClipboardData(pwhentry.password);
}
void
CPWHistDlg::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
if(phdn->iButton == 0) {
// User clicked on header using left mouse button
if(phdn->iItem == m_iSortedColumn)
m_bSortAscending = !m_bSortAscending;
else
m_bSortAscending = TRUE;
m_iSortedColumn = phdn->iItem;
m_PWHistListCtrl.SortItems(CompareFunc, (LPARAM)this);
// Note: WINVER defines the minimum system level for which this is program compiled and
// NOT the level of system it is running on!
// In this case, these values are defined in Windows XP and later and supported
// by V6 of comctl32.dll (supplied with Windows XP) and later.
// They should be ignored by earlier levels of this dll or .....
// we can check the dll version (code available on request)!
#if (WINVER < 0x0501) // These are already defined for WinXP and later
#define HDF_SORTUP 0x0400
#define HDF_SORTDOWN 0x0200
#endif
HDITEM HeaderItem;
HeaderItem.mask = HDI_FORMAT;
m_PWHistListCtrl.GetHeaderCtrl()->GetItem(m_iSortedColumn, &HeaderItem);
// Turn off all arrows
HeaderItem.fmt &= ~(HDF_SORTUP | HDF_SORTDOWN);
// Turn on the correct arrow
HeaderItem.fmt |= ((m_bSortAscending == TRUE) ? HDF_SORTUP : HDF_SORTDOWN);
m_PWHistListCtrl.GetHeaderCtrl()->SetItem(m_iSortedColumn, &HeaderItem);
}
*pResult = 0;
}
int CALLBACK CPWHistDlg::CompareFunc(LPARAM lParam1, LPARAM lParam2,
LPARAM closure)
{
CPWHistDlg *self = (CPWHistDlg*)closure;
int nSortColumn = self->m_iSortedColumn;
POSITION Lpos = (POSITION)lParam1;
POSITION Rpos = (POSITION)lParam2;
const PWHistEntry pLHS = self->m_PWHistList.GetAt(Lpos);
const PWHistEntry pRHS = self->m_PWHistList.GetAt(Rpos);
CMyString password1, changedate1;
CMyString password2, changedate2;
time_t t1, t2;
int iResult;
switch(nSortColumn) {
case 0:
t1 = pLHS.changetttdate;
t2 = pRHS.changetttdate;
iResult = ((long) t1 < (long) t2) ? -1 : 1;
break;
case 1:
password1 = pLHS.password;
password2 = pRHS.password;
iResult = ((CString)password1).Compare(password2);
break;
default:
iResult = 0; // should never happen - just keep compiler happy
ASSERT(FALSE);
}
if (!self->m_bSortAscending)
iResult *= -1;
return iResult;
}
void CPWHistDlg::OnBnClickedPwhCopyAll()
{
// TODO: Add your control notification handler code here
}