-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainFrm.h
581 lines (488 loc) · 16.3 KB
/
MainFrm.h
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////
#pragma once
#include "atlwfile.h"
#include <Shlobj.h>
#include "IniFile.h"
inline int WINAPI CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
PFILETIME ft1 = PFILETIME(lParam1);
PFILETIME ft2 = PFILETIME(lParam2);
return CompareFileTime(ft1, ft2);
}
inline bool EnsureDirExists(LPCTSTR filePath)
{
TCHAR drive[_MAX_DRIVE]=_T("\0");
TCHAR dir[_MAX_DIR]=_T("\0");
TCHAR fname[_MAX_FNAME]=_T("\0");
TCHAR ext[_MAX_EXT]=_T("\0");
if(_tsplitpath_s<_MAX_DRIVE, _MAX_DIR, _MAX_FNAME, _MAX_EXT>(filePath, drive, dir, fname, ext) != 0)
return false;
CString sDir(drive);
sDir += dir;
int retval = SHCreateDirectoryEx(NULL, sDir, NULL);
return (retval == ERROR_SUCCESS || retval == ERROR_FILE_EXISTS || retval == ERROR_ALREADY_EXISTS);
}
class CMainFrame :
public CFrameWindowImpl<CMainFrame>,
public CUpdateUI<CMainFrame>,
public CMessageFilter, public CIdleHandler
{
public:
DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)
CQRestoreView m_view;
CCommandBarCtrl m_CmdBar;
virtual BOOL PreTranslateMessage(MSG* pMsg)
{
if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
return TRUE;
return m_view.PreTranslateMessage(pMsg);
}
virtual BOOL OnIdle()
{
static bool first;
if(!first)
{
AddFilesFromDir(GetQuarantineDir());
first = true;
}
UIUpdateToolBar();
return FALSE;
}
BEGIN_UPDATE_UI_MAP(CMainFrame)
UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
END_UPDATE_UI_MAP()
BEGIN_MSG_MAP(CMainFrame)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_SIZE, OnSize)
COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
COMMAND_ID_HANDLER(ID_FILE_OPEN, OnFileOpen)
COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR, OnViewToolBar)
COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR, OnViewStatusBar)
COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
COMMAND_ID_HANDLER(ID_RESTORE_ALL, OnRestoreAll)
COMMAND_ID_HANDLER(ID_RESTORE, OnRestore)
COMMAND_ID_HANDLER(ID_SELECTALL, OnSelectAll)
COMMAND_ID_HANDLER(ID_RECOVERYSCRIPT, OnRecoveryScript)
CHAIN_MSG_MAP(CUpdateUI<CMainFrame>)
CHAIN_MSG_MAP(CFrameWindowImpl<CMainFrame>)
END_MSG_MAP()
// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
void CreateProgressCtrlInStatusBar ( LPCTSTR msg, CProgressBarCtrl& wndProgressBar, int maxRange )
{
CStatusBarCtrl wndStatusBar = m_hWndStatusBar;
CString sMessage = msg;
WTL::CRect rcBar;
CClientDC dc ( wndStatusBar );
CFontHandle hFont = wndStatusBar.GetFont();
CFontHandle hOldFont = dc.SelectFont ( hFont );
WTL::CSize sizeText;
wndStatusBar.GetRect ( 0, &rcBar );
dc.GetTextExtent ( sMessage, sMessage.GetLength(), &sizeText );
dc.SelectFont ( hOldFont );
rcBar.left += sizeText.cx + 10;
wndStatusBar.SetText ( 0, sMessage );
wndStatusBar.RedrawWindow();
wndProgressBar.Create ( wndStatusBar, rcBar, NULL, WS_CHILD|WS_VISIBLE );
wndProgressBar.SetRange ( 0, maxRange);
wndProgressBar.SetPos(0);
wndProgressBar.SetStep(1);
}
CString GetRestoreLogFilename()
{
TCHAR szTempDir[MAX_PATH], szTemp[MAX_PATH];
// Open a temporary file
if(!GetTempPath(MAX_PATH, szTempDir))
return _T("");
if(!GetTempFileName(szTempDir, _T("CLAMW"), 0, szTemp))
return _T("");
DeleteFile(szTemp);
_tcscat_s(szTemp, MAX_PATH, _T(".txt"));
return szTemp;
}
CString GeRecoveryScriptFilename()
{
TCHAR szTempDir[MAX_PATH], szTemp[MAX_PATH];
// Open a temporary file
if(!GetTempPath(MAX_PATH, szTempDir))
return _T("");
if(!GetTempFileName(szTempDir, _T("QRECOVERY"), 0, szTemp))
return _T("");
DeleteFile(szTemp);
_tcscat_s(szTemp, MAX_PATH, _T(".bat"));
return szTemp;
}
bool ShellOpen(LPCTSTR dstName, bool edit = false)
{
SHELLEXECUTEINFO info;
info.cbSize = sizeof(info);
info.fMask = SEE_MASK_FLAG_DDEWAIT;
info.hwnd = NULL;
info.lpVerb = _T("Edit");
info.lpFile = dstName;
info.lpParameters = NULL;
info.lpDirectory = NULL;
info.nShow = SW_SHOWNORMAL;
info.hProcess = 0;
bool success = !!::ShellExecuteEx(&info);
UINT_PTR result = (UINT_PTR) info.hInstApp;
if(!success || result <= 32)
return false;
return true;
}
CString ErrorString(DWORD err)
{
CString Error;
LPTSTR s;
if(::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
0,
(LPTSTR)&s,
0,
NULL) == 0)
{ /* failed */
// Unknown error code %08x (%d)
CString fmt = _T("Unknown Error");
CString t;
t.Format(fmt, err, LOWORD(err));
Error = t;
} /* failed */
else
{ /* success */
LPTSTR p = _tcschr(s, _T('\r'));
if(p != NULL)
{ /* lose CRLF */
*p = _T('\0');
} /* lose CRLF */
Error = s;
::LocalFree(s);
} /* success */
return Error;
} // ErrorString
LRESULT OnRestore(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CString logname = GetRestoreLogFilename();
CFile log;
CString logEntry = _T("Starting File Recovery\r\n");
log.Create(logname, FILE_ALL_ACCESS);
if(log.IsOpen())
{
#ifdef _UNICODE
unsigned short header = 0xFEFF;
log.Write(PVOID(&header), 2);
#endif
log.Write(logEntry.GetBuffer(logEntry.GetLength()), logEntry.GetLength()*sizeof(TCHAR));
}
int count = 0;
for(int iItem = m_view.GetNextItem(-1, LVNI_SELECTED); iItem != -1; iItem = m_view.GetNextItem(iItem, LVNI_SELECTED))
count++;
CProgressBarCtrl wndProgress;
CreateProgressCtrlInStatusBar (_T("Restoring files..."), wndProgress, count);
m_view.SetRedraw (FALSE);
CWaitCursor wait;
for(int iItem = m_view.GetNextItem(-1, LVNI_SELECTED); iItem != -1; iItem = m_view.GetNextItem(iItem, LVNI_SELECTED))
{
CString src, dest;
m_view.GetItemText(iItem, 0, dest);
m_view.GetItemText(iItem, 1, src);
EnsureDirExists(dest);
CString logEntry;
if(MoveFileEx(src, dest, MOVEFILE_COPY_ALLOWED))
{
DeleteFile(src + _T(".txt"));
logEntry = dest + _T(": Successfully recovered ") + CString("\r\n");
PFILETIME ft = (PFILETIME)m_view.GetItemData(iItem);
if(ft)
delete ft;
m_view.DeleteItem(iItem);
iItem -= 1;
}
else
logEntry = dest + CString(" Error: ") + ErrorString(::GetLastError()) + CString("\r\n");
if(log.IsOpen())
log.Write(logEntry.GetBuffer(logEntry.GetLength()), logEntry.GetLength() * sizeof(TCHAR));
wndProgress.StepIt();
}
m_view.SetRedraw ( TRUE );
m_view.Invalidate();
wndProgress.DestroyWindow();
CStatusBarCtrl(m_hWndStatusBar).SetText(0, _T(""));
log.Close();
ShellOpen(logname);
return 0;
}
LRESULT OnRestoreAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_view.SetItemState(-1, LVIS_SELECTED, LVIS_SELECTED);
BOOL b;
OnRestore(0, 0, 0, b);
return 0;
}
LRESULT OnSelectAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_view.SetItemState(-1, LVIS_SELECTED, LVIS_SELECTED);
return 0;
}
LRESULT OnRecoveryScript(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
USES_CONVERSION;
CString logname = GeRecoveryScriptFilename();
CFile log;
CString logEntry = _T("REM QRecover Quarantine File Recovery script\r\n");
log.Create(logname, FILE_ALL_ACCESS);
int count = 0;
for(int iItem = m_view.GetNextItem(-1, LVNI_ALL); iItem != -1; iItem = m_view.GetNextItem(iItem, LVNI_BELOW))
count++;
CProgressBarCtrl wndProgress;
CreateProgressCtrlInStatusBar (_T("Writing Recovery Script..."), wndProgress, count);
m_view.SetRedraw (FALSE);
CWaitCursor wait;
for(int iItem = m_view.GetNextItem(-1, LVNI_ALL); iItem != -1; iItem = m_view.GetNextItem(iItem, LVNI_BELOW))
{
CString src, dest;
m_view.GetItemText(iItem, 0, dest);
m_view.GetItemText(iItem, 1, src);
CString logEntry;
logEntry = _T("copy /Y \"") + src + _T("\" \"") + dest + CString("\"\r\n");
if(log.IsOpen())
log.Write(logEntry.GetBuffer(logEntry.GetLength()), logEntry.GetLength());
wndProgress.StepIt();
}
m_view.SetRedraw ( TRUE );
m_view.Invalidate();
wndProgress.DestroyWindow();
CStatusBarCtrl(m_hWndStatusBar).SetText(0, _T(""));
log.Close();
ShellOpen(logname);
return 0;
}
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// create command bar window
HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
// attach menu
m_CmdBar.AttachMenu(GetMenu());
// load command bar images
m_CmdBar.LoadImages(IDR_MAINFRAME);
// remove old menu
SetMenu(NULL);
HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
AddSimpleReBarBand(hWndCmdBar);
AddSimpleReBarBand(hWndToolBar, NULL, TRUE);
CreateSimpleStatusBar();
m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | LVS_REPORT | LVS_SHOWSELALWAYS, WS_EX_CLIENTEDGE);
UIAddToolBar(hWndToolBar);
UISetCheck(ID_VIEW_TOOLBAR, 1);
UISetCheck(ID_VIEW_STATUS_BAR, 1);
// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
return 0;
}
LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
// unregister message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->RemoveMessageFilter(this);
pLoop->RemoveIdleHandler(this);
bHandled = FALSE;
return 1;
}
LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
PostMessage(WM_CLOSE);
return 0;
}
LRESULT OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CFolderDialog fldDlg(NULL, _T("Select a quarantine directory"),
BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE);
if (IDOK == fldDlg.DoModal())
{
m_view.SetRedraw ( FALSE );
for(int iItem = m_view.GetNextItem(0, LVNI_ALL); iItem != -1; iItem = m_view.GetNextItem(iItem, LVNI_ALL))
{
PFILETIME ft = (PFILETIME)m_view.GetItemData(iItem);
if(ft)
delete ft;
}
m_view.DeleteAllItems();
AddFilesFromDir(CString(fldDlg.m_szFolderPath));
m_view.SetRedraw ( TRUE );
m_view.Invalidate();
}
return 0;
}
bool ParseLog(LPCTSTR filename)
{
const TCHAR pattern[]=_T("^{.*}: moved to '{.*}'$");
CAtlRegExp<> regex;
REParseError status = regex.Parse(pattern, FALSE);
CAtlREMatchContext<> mc;
if (REPARSE_ERROR_OK != status)
{
AtlMessageBox(m_hWnd, _T("Internal error: Invalid Regular Expression"), _T("QRecover"), MB_ICONSTOP|MB_OK);
return false;
}
CTextFileRead log(filename);
if(!log.IsOpen())
{
AtlMessageBox(m_hWnd, _T("Cannot open the log file"), _T("QRecover"), MB_ICONSTOP|MB_OK);
return false;
}
m_view.DeleteAllItems();
m_view.Invalidate();
m_view.SetRedraw ( FALSE );
DWORD size = GetFileSize(log.m_hFile, NULL);
CProgressBarCtrl wndProgress;
CreateProgressCtrlInStatusBar (_T("Processing Log file..."), wndProgress, size/100);
CWaitCursor wait;
tstring line;
int i = 0;
try{
while(log.ReadLine(line))
{
if (!regex.Match(line.c_str(), &mc))
continue;
for (UINT nGroupIndex = 0; nGroupIndex < mc.m_uNumGroups; ++nGroupIndex)
{
const CAtlREMatchContext<>::RECHAR* szStart = 0;
const CAtlREMatchContext<>::RECHAR* szEnd = 0;
mc.GetMatch(nGroupIndex, &szStart, &szEnd);
int nLength = (int) (szEnd - szStart);
CString text(szStart, nLength);
m_view.AddItem(i, nGroupIndex, text);
if(nGroupIndex == mc.m_uNumGroups-1)
i++;
}
DWORD pos = SetFilePointer(log.m_hFile, 0, NULL, FILE_CURRENT);
wndProgress.SetPos(pos/100);
}
}catch(CTextFileException)
{
AtlMessageBox(m_hWnd, _T("Error occured while reading the log file."), _T("QRecover"), MB_ICONSTOP|MB_OK);
return false;
}
m_view.SetRedraw ( TRUE );
m_view.Invalidate();
wndProgress.DestroyWindow();
CStatusBarCtrl(m_hWndStatusBar).SetText(0, _T(""));
return true;
}
LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
static BOOL bVisible = TRUE; // initially visible
bVisible = !bVisible;
CReBarCtrl rebar = m_hWndToolBar;
int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1); // toolbar is 2nd added band
rebar.ShowBand(nBandIndex, bVisible);
UISetCheck(ID_VIEW_TOOLBAR, bVisible);
UpdateLayout();
return 0;
}
LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
UpdateLayout();
return 0;
}
LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}
LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
{
if(m_view.IsWindow())
{
WTL::CRect rcClient(0,0,LOWORD(lParam), HIWORD(lParam));
m_view.SetWindowPos(NULL, rcClient, SWP_NOACTIVATE | SWP_NOZORDER );
m_view.ResizeColumns();
}
bHandled = FALSE;
return 0;
}
CString GetQuarantineDir()
{
CString sPath, sConf;
if(SHGetSpecialFolderPath(0, sConf.GetBuffer(MAX_PATH), CSIDL_APPDATA, FALSE))
{
sConf.ReleaseBuffer();
if(sConf.Right(1) != _T("\\"))
sConf += _T("\\");
sConf += _T(".clamwin\\clamwin.conf");
sPath = CIniFile::GetValue(_T("quarantinedir"), _T("ClamAV"), tstring(sConf)).c_str();
if(sPath[0] == _T(' '))
sPath = sPath.Right(sPath.GetLength() - 1);
}
if(sPath.IsEmpty())
MessageBox(_T("Could not find Quarantine directiory.\n Please use \"Open Quarantine Diirectory\" in the File menu and specify it manually"), _T("QRecover"), MB_ICONWARNING|MB_OK);
return sPath;
}
bool AddFilesFromDir(CString& dir)
{
CString pattern(dir);
if(pattern.Right(1) != _T("\\"))
pattern += _T("\\");
pattern += _T("*.txt");
int i = 0;
CFindFile f;
if(f.FindFile(LPCTSTR(pattern)))
{
do
{
if(f.IsDots() || f.IsDirectory())
continue;
CFile file;
if(file.Open(f.GetFilePath()))
{
char* data = new char[(size_t)file.GetSize()];
if(file.Read(data, (DWORD) file.GetSize()))
{
CString text(data, (int) file.GetSize());
int pos = text.Find(_T('\t'));
if(pos != -1)
{
CString src = text.Left(pos);
if(src.Left(4) == _T("\\\\?\\"))
src = src.Right(src.GetLength() - 4);
CString dst = text.Right(text.GetLength() - pos - 1);
if(dst.Left(4) == _T("\\\\?\\"))
dst = dst.Right(dst.GetLength() - 4);
m_view.AddItem(i, 0, src);
m_view.AddItem(i, 1, dst);
FILETIME *ft = new FILETIME;
f.GetLastWriteTime(ft);
m_view.SetItemData(i,(DWORD_PTR)ft);
i++;
}
}
delete[] data;
}
}while(f.FindNextFile());
m_view.SortItems(CompareFunc, 0);
}
if(!i)
{
CString s;
s.Format(_T("Directory %s does not contain any quarantine information\n To open another directory use \"Open Quarantine Diirectory\" in the File menu"), (LPCTSTR)dir);
MessageBox(s, _T("QRecover"), MB_ICONWARNING|MB_OK);
}
return i > 0;
}
};