source: trunk/Verify/VerifyDlg.cpp @ 7

Revision 7, 10.5 KB checked in by lowjoel, 7 years ago (diff)

Added the source files from Verify (SVN migration commit 5)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1// VerifyDlg.cpp : implementation file
2//
3
4#include "stdafx.h"
5#include "..\EraserDll\EraserDll.h"
6//#include "..\shared\FiledialogEx.h"
7#include "resource.h"
8#include "newDialog.h"
9#include "Verify.h"
10#include "VerifyDlg.h"
11#include "ViewerDlg.h"
12
13#ifdef _DEBUG
14#define new DEBUG_NEW
15#undef THIS_FILE
16static char THIS_FILE[] = __FILE__;
17#endif
18
19#define TIMER_CONTINUE  100
20const DWORD uWait = 2; // seconds
21
22const LPCTSTR szProgress = "Now overwriting pass %u / %u (%u%% completed)";
23
24/////////////////////////////////////////////////////////////////////////////
25// CVerifyDlg dialog
26
27CVerifyDlg::CVerifyDlg(CWnd* pParent /*=NULL*/) :
28CDialog(CVerifyDlg::IDD, pParent),
29m_pfSave(0),
30m_ehContext(ERASER_INVALID_CONTEXT),
31m_bFileSelected(FALSE),
32m_bTerminated(FALSE)
33{
34    //{{AFX_DATA_INIT(CVerifyDlg)
35    m_strFileName = _T("");
36    m_strProgress = _T("");
37    //}}AFX_DATA_INIT
38    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
39    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
40}
41
42void CVerifyDlg::DoDataExchange(CDataExchange* pDX)
43{
44    CDialog::DoDataExchange(pDX);
45    //{{AFX_DATA_MAP(CVerifyDlg)
46    DDX_Text(pDX, IDC_EDIT_FILE, m_strFileName);
47    DDX_Text(pDX, IDC_STATIC_PROGRESS, m_strProgress);
48    //}}AFX_DATA_MAP
49}
50
51BEGIN_MESSAGE_MAP(CVerifyDlg, CDialog)
52    //{{AFX_MSG_MAP(CVerifyDlg)
53    ON_WM_PAINT()
54    ON_WM_QUERYDRAGICON()
55    ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
56    ON_BN_CLICKED(IDC_BUTTON_ERASE, OnButtonErase)
57    ON_BN_CLICKED(IDC_BUTTON_METHOD, OnButtonMethod)
58    ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
59    ON_WM_TIMER()
60    ON_WM_DESTROY()
61    //}}AFX_MSG_MAP
62    ON_MESSAGE(WM_ERASERNOTIFY, OnEraserNotify)
63END_MESSAGE_MAP()
64
65/////////////////////////////////////////////////////////////////////////////
66// CVerifyDlg message handlers
67
68BOOL CVerifyDlg::OnInitDialog()
69{
70    CDialog::OnInitDialog();
71
72    // create and set bold font
73    LOGFONT lgFont;
74    ZeroMemory(&lgFont, sizeof(LOGFONT));
75
76    m_pfSave = GetDlgItem(IDC_STATIC_STEP1)->GetFont();
77    m_pfSave->GetLogFont(&lgFont);
78    lgFont.lfWeight = FW_BOLD;
79
80    m_fBold.CreateFontIndirect(&lgFont);
81    GetDlgItem(IDC_STATIC_STEP1)->SetFont(&m_fBold);
82    GetDlgItem(IDC_STATIC_PROGRESS)->SetFont(&m_fBold);
83
84
85    GetDlgItem(IDC_BUTTON_ERASE)->EnableWindow(FALSE);
86    GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
87
88    return TRUE;  // return TRUE  unless you set the focus to a control
89}
90
91// If you add a minimize button to your dialog, you will need the code below
92//  to draw the icon.  For MFC applications using the document/view model,
93//  this is automatically done for you by the framework.
94
95void CVerifyDlg::OnPaint()
96{
97    if (IsIconic()) {
98        CPaintDC dc(this); // device context for painting
99
100        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
101
102        // Center icon in client rectangle
103        int cxIcon = GetSystemMetrics(SM_CXICON);
104        int cyIcon = GetSystemMetrics(SM_CYICON);
105        CRect rect;
106        GetClientRect(&rect);
107        int x = (rect.Width() - cxIcon + 1) / 2;
108        int y = (rect.Height() - cyIcon + 1) / 2;
109
110        // Draw the icon
111        dc.DrawIcon(x, y, m_hIcon);
112    } else {
113        CDialog::OnPaint();
114    }
115}
116
117// The system calls this to obtain the cursor to display while the user drags
118//  the minimized window.
119HCURSOR CVerifyDlg::OnQueryDragIcon()
120{
121    return (HCURSOR) m_hIcon;
122}
123
124void CVerifyDlg::OnButtonBrowse()
125{
126   /* CFileDialogEx fdlg(TRUE, NULL, NULL,
127                       OFN_PATHMUSTEXIST |OFN_NODEREFERENCELINKS | OFN_FILEMUSTEXIST | OFN_SHOWHELP |OFN_EXPLORER,
128                       "All Files (*.*) | *.*||", this);
129    fdlg.m_ofn.lpstrTitle = "Select File to be Erased";*/
130UpdateData(TRUE);
131    CNewDialog fdlg;
132    if (fdlg.DoModal() == IDOK) {
133        m_strFileName = fdlg.m_sPath;       //fdlg.GetPathName();
134
135        if (!m_bFileSelected) {
136            // move to step 2
137            GetDlgItem(IDC_STATIC_STEP1)->SetFont(m_pfSave);
138            m_pfSave = GetDlgItem(IDC_STATIC_STEP2)->GetFont();
139            GetDlgItem(IDC_STATIC_STEP2)->SetFont(&m_fBold);
140            // enable "Erase" button
141            GetDlgItem(IDC_BUTTON_ERASE)->EnableWindow(TRUE);
142            // clear possible message
143            m_strProgress.Empty();
144        }
145
146        UpdateData(FALSE);
147        m_bFileSelected = TRUE;
148    }
149}
150
151void CVerifyDlg::OnButtonErase()
152{
153    // only one click, thanks
154    GetDlgItem(IDC_BUTTON_ERASE)->EnableWindow(FALSE);
155
156    // no changing preferences at this point
157    GetDlgItem(IDC_BUTTON_METHOD)->EnableWindow(FALSE);
158
159    BOOL bSuccess = FALSE;
160
161    // start erasing
162    if (eraserError(eraserIsValidContext(m_ehContext)) && !m_strFileName.IsEmpty()) {
163        if (eraserOK(eraserCreateContext(&m_ehContext))) {
164            // set data type
165            if (eraserOK(eraserSetDataType(m_ehContext, ERASER_DATA_FILES))) {
166                // add file to erase
167                if (eraserOK(eraserAddItem(m_ehContext, (LPVOID)(LPCTSTR)m_strFileName,
168                        (E_UINT16)m_strFileName.GetLength()))) {
169
170                    // set notification window & message and enable test API notifications
171                    if (eraserOK(eraserSetWindow(m_ehContext, GetSafeHwnd())) &&
172                        eraserOK(eraserSetWindowMessage(m_ehContext, WM_ERASERNOTIFY)) &&
173                        eraserOK(eraserTestEnable(m_ehContext))) {
174
175                        // before...
176                        CViewerDlg viewer(this);
177                        viewer.m_strFileName = m_strFileName;
178                        viewer.m_strMessage = "Before Erasing";
179                        viewer.DoModal();
180
181                        // start erasing (the library will launch a new thread for this)
182                        bSuccess = eraserOK(eraserStart(m_ehContext));
183                    }
184                }
185            }
186        }
187    }
188
189    if (bSuccess) {
190        // move to step 3
191        GetDlgItem(IDC_STATIC_STEP2)->SetFont(m_pfSave);
192        m_pfSave = GetDlgItem(IDC_STATIC_STEP3)->GetFont();
193        GetDlgItem(IDC_STATIC_STEP3)->SetFont(&m_fBold);
194
195        // enable "Stop" button
196        GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(TRUE);
197
198        // disable "Close" button
199        GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
200    } else {
201        // back to step 1
202        GetDlgItem(IDC_STATIC_STEP2)->SetFont(m_pfSave);
203        m_pfSave = GetDlgItem(IDC_STATIC_STEP1)->GetFont();
204        GetDlgItem(IDC_STATIC_STEP1)->SetFont(&m_fBold);
205
206        // re-enable preferences button
207        GetDlgItem(IDC_BUTTON_METHOD)->EnableWindow(TRUE);
208
209        // clear file name
210        m_strFileName.Empty();
211        m_bFileSelected = FALSE;
212
213        // destroy possibly created context
214        eraserDestroyContext(m_ehContext);
215        m_ehContext = ERASER_INVALID_CONTEXT;
216
217        // notify the user
218        m_strProgress = "Failed to start erasing.";
219        UpdateData(FALSE);
220    }
221}
222
223void CVerifyDlg::OnButtonMethod()
224{
225    eraserShowOptions(GetSafeHwnd(), ERASER_PAGE_FILES);
226}
227
228void CVerifyDlg::OnButtonStop()
229{
230    if (eraserOK(eraserIsValidContext(m_ehContext))) {
231        GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
232        m_bTerminated = TRUE;
233        eraserStop(m_ehContext);
234    }
235}
236
237LRESULT CVerifyDlg::OnEraserNotify(WPARAM wParam, LPARAM)
238{
239    if (eraserError(eraserIsValidContext(m_ehContext))) {
240        return 0;
241    }
242
243    switch (wParam) {
244    case ERASER_WIPE_BEGIN:
245        m_strProgress = "Erasing started...";
246        break;
247    case ERASER_WIPE_UPDATE:
248        {
249            E_UINT16 uCurrent = 0, uPasses = 0;
250            E_UINT8 uPercent = 0;
251
252            // percent
253            if (eraserOK(eraserProgGetPercent(m_ehContext, &uPercent)) &&
254                eraserOK(eraserProgGetCurrentPass(m_ehContext, &uCurrent)) &&
255                eraserOK(eraserProgGetPasses(m_ehContext, &uPasses))) {
256                if (uPasses > 0 && uCurrent > 0) {
257                    m_strProgress.Format(szProgress, uCurrent, uPasses,
258                        (uPercent - ((uCurrent - 1) * 100 / uPasses)) * uPasses );
259                }
260            } else {
261                m_strProgress = "Erasing...";
262            }
263        }
264        break;
265    case ERASER_WIPE_DONE:
266        if (!m_bTerminated) {
267            m_strProgress = "Erasing completed.";
268        } else {
269            m_strProgress = "Erasing terminated.";
270        }
271
272        // and we're back to step 1
273        GetDlgItem(IDC_STATIC_STEP3)->SetFont(m_pfSave);
274        m_pfSave = GetDlgItem(IDC_STATIC_STEP1)->GetFont();
275        GetDlgItem(IDC_STATIC_STEP1)->SetFont(&m_fBold);
276
277        // re-enable preferences button
278        GetDlgItem(IDC_BUTTON_METHOD)->EnableWindow(TRUE);
279
280        // and "Close" button
281        GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
282
283        // no more stopping
284        GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);
285        m_bTerminated = FALSE;
286
287        // clear file name
288        m_strFileName.Empty();
289        m_bFileSelected = FALSE;
290
291        // destroy context
292        eraserDestroyContext(m_ehContext);
293        m_ehContext = ERASER_INVALID_CONTEXT;
294
295        break;
296    case ERASER_TEST_PAUSED:
297        {
298            TCHAR szCurrentData[_MAX_PATH];
299            m_strProgress = "Viewing File Contents...";
300            UpdateData(FALSE);
301
302            E_UINT16 uLength = _MAX_PATH, uCurrent = 0, uPasses = 0;
303
304            eraserProgGetCurrentPass(m_ehContext, &uCurrent);
305            eraserProgGetPasses(m_ehContext, &uPasses);
306
307            // if the file has alternate data streams and user has opted to
308            // erase them, the file name will change at some point
309            eraserProgGetCurrentDataString(m_ehContext, (LPVOID)szCurrentData, &uLength);
310
311            try {
312                CViewerDlg viewer(this);
313                viewer.m_strFileName = szCurrentData;
314
315                if (uCurrent > 0) {
316                    viewer.m_strMessage.Format("After Pass %u", uCurrent);
317                }
318                viewer.DoModal();
319            } catch (...) {
320                ASSERT(0);
321            }
322
323            if (uCurrent < uPasses) {
324                // and continue after uWait seconds
325                if (!SetTimer(TIMER_CONTINUE, uWait * 1000, NULL)) {
326                    eraserTestContinueProcess(m_ehContext);
327                }
328                m_strProgress = "Starting next overwriting pass...";
329            } else {
330                eraserTestContinueProcess(m_ehContext);
331            }
332        }
333        break;
334    default:
335        break;
336    }
337
338    UpdateData(FALSE);
339    return 1;
340}
341
342void CVerifyDlg::OnTimer(UINT_PTR nIDEvent)
343{
344    if (nIDEvent == TIMER_CONTINUE && eraserOK(eraserIsValidContext(m_ehContext))) {
345        KillTimer(TIMER_CONTINUE);
346        eraserTestContinueProcess(m_ehContext);
347    }
348    CDialog::OnTimer(nIDEvent);
349}
350
351void CVerifyDlg::OnDestroy()
352{
353    eraserDestroyContext(m_ehContext);
354    CDialog::OnDestroy();
355}
Note: See TracBrowser for help on using the repository browser.