-
Notifications
You must be signed in to change notification settings - Fork 1
/
windirstat.cpp
615 lines (504 loc) · 14.4 KB
/
windirstat.cpp
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// windirstat.cpp - Implementation of CDirstatApp and some globals
//
// WinDirStat - Directory Statistics
// Copyright (C) 2003-2005 Bernhard Seifert
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: bseifert@users.sourceforge.net, bseifert@daccord.net
//
// Last modified: $Date: 2005/04/17 12:27:21 $
#include "stdafx.h"
#include "windirstat.h"
#include "mainframe.h"
#include "selectdrivesdlg.h"
#include "aboutdlg.h"
#include "reportbugdlg.h"
#include "modalsendmail.h"
#include "dirstatdoc.h"
#include "graphview.h"
#include "FileFindWDS.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CMainFrame *GetMainFrame()
{
// Not: return (CMainFrame *)AfxGetMainWnd();
// because CWinApp::m_pMainWnd is set too late.
return CMainFrame::GetTheFrame();
}
CDirstatApp *GetApp()
{
return (CDirstatApp *)AfxGetApp();
}
CString GetAuthorEmail()
{
return _T("bseifert@users.sourceforge.net");
}
CString GetWinDirStatHomepage()
{
return _T("windirstat.sourceforge.net");
}
CString GetFeedbackEmail()
{
return _T("windirstat-feedback@lists.sourceforge.net");
}
CMyImageList *GetMyImageList()
{
return GetApp()->GetMyImageList();
}
// CDirstatApp
BEGIN_MESSAGE_MAP(CDirstatApp, CWinApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_HELP_MANUAL, OnHelpManual)
ON_UPDATE_COMMAND_UI(ID_HELP_REPORTBUG, OnUpdateHelpReportbug)
ON_COMMAND(ID_HELP_REPORTBUG, OnHelpReportbug)
END_MESSAGE_MAP()
CDirstatApp _theApp;
CDirstatApp::CDirstatApp()
{
m_workingSet= 0;
m_pageFaults= 0;
m_lastPeriodicalRamUsageUpdate= GetTickCount();
m_altEncryptionColor= GetAlternativeColor(RGB(0x00, 0x80, 0x00), _T("AltEncryptionColor"));
m_altColor= GetAlternativeColor(RGB(0x00, 0x00, 0xFF), _T("AltColor"));
#ifdef _DEBUG
TestScanResourceDllName();
#endif
}
CMyImageList *CDirstatApp::GetMyImageList()
{
m_myImageList.Initialize();
return &m_myImageList;
}
void CDirstatApp::UpdateRamUsage()
{
CWinThread::OnIdle(0);
}
void CDirstatApp::PeriodicalUpdateRamUsage()
{
if (GetTickCount() - m_lastPeriodicalRamUsageUpdate > 1200)
{
UpdateRamUsage();
m_lastPeriodicalRamUsageUpdate= GetTickCount();
}
}
CString CDirstatApp::FindResourceDllPathByLangid(LANGID& langid)
{
return FindAuxiliaryFileByLangid(_T("wdsr"), _T(".dll"), langid, true);
}
CString CDirstatApp::FindHelpfilePathByLangid(LANGID langid)
{
CString s;
if (langid == GetBuiltInLanguage())
{
// The english help file is named windirstat.chm.
s= GetAppFolder() + _T("\\windirstat.chm");
if (FileExists(s))
return s;
}
// Help files for other languages are named wdshxxxx.chm (xxxx = LANGID).
s= FindAuxiliaryFileByLangid(_T("wdsh"), _T(".chm"), langid, false);
if (!s.IsEmpty())
return s;
// Else, try windirstat.chm again.
s= GetAppFolder() + _T("\\windirstat.chm");
if (FileExists(s))
return s;
// Not found.
return _T("");
}
void CDirstatApp::GetAvailableResourceDllLangids(CArray<LANGID, LANGID>& arr)
{
arr.RemoveAll();
CFileFind finder;
BOOL b= finder.FindFile(GetAppFolder() + _T("\\wdsr*.dll"));
while (b)
{
b= finder.FindNextFile();
if (finder.IsDirectory())
continue;
LANGID langid;
if (ScanResourceDllName(finder.GetFileName(), langid) && IsCorrectResourceDll(finder.GetFilePath()))
arr.Add(langid);
}
}
void CDirstatApp::RestartApplication()
{
// First, try to create the suspended process
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb= sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
BOOL success = CreateProcess(GetAppFileName(), NULL, NULL, NULL, false, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
if (!success)
{
CString s;
s.FormatMessage(IDS_CREATEPROCESSsFAILEDs, GetAppFileName(), MdGetWinerrorText(GetLastError()));
AfxMessageBox(s);
return;
}
// We _send_ the WM_CLOSE here to ensure that all CPersistence-Settings
// like column widths an so on are saved before the new instance is resumed.
// This will post a WM_QUIT message.
GetMainFrame()->SendMessage(WM_CLOSE);
DWORD dw= ::ResumeThread(pi.hThread);
if (dw != 1)
TRACE(_T("ResumeThread() didn't return 1\r\n"));
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
bool CDirstatApp::ScanResourceDllName(LPCTSTR name, LANGID& langid)
{
return ScanAuxiliaryFileName(_T("wdsr"), _T(".dll"), name, langid);
}
bool CDirstatApp::ScanAuxiliaryFileName(LPCTSTR prefix, LPCTSTR suffix, LPCTSTR name, LANGID& langid)
{
ASSERT(lstrlen(prefix) == 4); // "wdsr" or "wdsh"
ASSERT(lstrlen(suffix) == 4); // ".dll" or ".chm"
CString s= name; // "wdsr0a01.dll"
s.MakeLower();
if (s.Left(4) != prefix)
return false;
s= s.Mid(4); // "0a01.dll"
if (s.GetLength() != 8)
return false;
if (s.Mid(4) != suffix)
return false;
s= s.Left(4); // "0a01"
for (int i=0; i < 4; i++)
if (!IsHexDigit(s[i]))
return false;
int id;
VERIFY(1 == _stscanf(s, _T("%04x"), &id));
langid= (LANGID)id;
return true;
}
#ifdef _DEBUG
void CDirstatApp::TestScanResourceDllName()
{
LANGID id;
ASSERT(!ScanResourceDllName(_T(""), id));
ASSERT(!ScanResourceDllName(_T("wdsr.dll"), id));
ASSERT(!ScanResourceDllName(_T("wdsr123.dll"), id));
ASSERT(!ScanResourceDllName(_T("wdsr12345.dll"), id));
ASSERT(!ScanResourceDllName(_T("wdsr1234.exe"), id));
ASSERT(ScanResourceDllName(_T("wdsr0123.dll"), id));
ASSERT(id == 0x0123);
ASSERT(ScanResourceDllName(_T("WDsRa13F.dll"), id));
ASSERT(id == 0xa13f);
}
#endif
CString CDirstatApp::FindAuxiliaryFileByLangid(LPCTSTR prefix, LPCTSTR suffix, LANGID& langid, bool checkResource)
{
CString number;
number.Format(_T("%04x"), langid);
CString exactName;
exactName.Format(_T("%s%s%s"), prefix, number, suffix);
CString exactPath= GetAppFolder() + _T("\\") + exactName;
if (FileExists(exactPath) && (!checkResource || IsCorrectResourceDll(exactPath)))
return exactPath;
CString search;
search.Format(_T("%s*%s"), prefix, suffix);
CFileFind finder;
BOOL b= finder.FindFile(GetAppFolder() + _T("\\") + search);
while (b)
{
b= finder.FindNextFile();
if (finder.IsDirectory())
continue;
LANGID id;
if (!ScanAuxiliaryFileName(prefix, suffix, finder.GetFileName(), id))
continue;
if (PRIMARYLANGID(id) == PRIMARYLANGID(langid) && (!checkResource || IsCorrectResourceDll(finder.GetFilePath())))
{
langid= id;
return finder.GetFilePath();
}
}
return _T("");
}
CString CDirstatApp::ConstructHelpFileName()
{
return FindHelpfilePathByLangid(CLanguageOptions::GetLanguage());
}
bool CDirstatApp::IsCorrectResourceDll(LPCTSTR path)
{
HMODULE module = LoadLibrary(path);
if (module == NULL)
return false;
CString reference = LoadString(IDS_RESOURCEVERSION);
int bufsize = reference.GetLength() * 2;
CString s;
int r = LoadString(module, IDS_RESOURCEVERSION, s.GetBuffer(bufsize), bufsize);
s.ReleaseBuffer();
FreeLibrary(module);
if (r == 0 || s != reference)
return false;
return true;
}
void CDirstatApp::ReReadMountPoints()
{
m_mountPoints.Initialize();
}
bool CDirstatApp::IsMountPoint(CString path)
{
return m_mountPoints.IsMountPoint(path);
}
bool CDirstatApp::IsJunctionPoint(CString path)
{
return m_mountPoints.IsJunctionPoint(path);
}
// Get the alternative colors for compressed and encrypted files/folders.
// This function uses either the value defined in the Explorer configuration
// or the default color values.
COLORREF CDirstatApp::GetAlternativeColor(COLORREF clrDefault, LPCTSTR which)
{
const LPCTSTR explorerKey = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer");
COLORREF x; DWORD cbValue = sizeof(x); CRegKey key;
// Open the explorer key
key.Open(HKEY_CURRENT_USER, explorerKey, KEY_READ);
// Try to read the REG_BINARY value
if (ERROR_SUCCESS == key.QueryBinaryValue(which, &x, &cbValue))
{
// Return the read value upon success
return x;
}
else
{
// Return the default upon failure
return clrDefault;
}
}
COLORREF CDirstatApp::AltColor()
{
// Return property value
return m_altColor;
}
COLORREF CDirstatApp::AltEncryptionColor()
{
// Return property value
return m_altEncryptionColor;
}
CString CDirstatApp::GetCurrentProcessMemoryInfo()
{
UpdateMemoryInfo();
if (m_workingSet == 0)
return _T("");
CString n= PadWidthBlanks(FormatBytes(m_workingSet), 11);
CString s;
s.FormatMessage(IDS_RAMUSAGEs, n);
return s;
}
CGetCompressedFileSizeApi *CDirstatApp::GetComprSizeApi()
{
return &m_comprSize;
}
CSecurityApi *CDirstatApp::GetSecurityApi()
{
return &m_security;
}
bool CDirstatApp::UpdateMemoryInfo()
{
if (!m_psapi.IsSupported())
return false;
PROCESS_MEMORY_COUNTERS pmc;
ZeroMemory(&pmc, sizeof(pmc));
pmc.cb= sizeof(pmc);
if (!m_psapi.GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
return false;
m_workingSet= pmc.WorkingSetSize;
bool ret= false;
if (pmc.PageFaultCount > m_pageFaults + 500)
ret= true;
m_pageFaults= pmc.PageFaultCount;
return ret;
}
LANGID CDirstatApp::GetBuiltInLanguage()
{
return MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
}
BOOL CDirstatApp::InitInstance()
{
CWinApp::InitInstance();
InitCommonControls(); // InitCommonControls() is necessary for Windows XP.
VERIFY(AfxOleInit()); // For SHBrowseForFolder()
AfxEnableControlContainer(); // For our rich edit controls in the about dialog
VERIFY(AfxInitRichEdit()); // Rich edit control in out about box
VERIFY(AfxInitRichEdit2()); // On NT, this helps.
EnableHtmlHelp();
SetRegistryKey(_T("Seifert"));
LoadStdProfileSettings(4);
m_langid= GetBuiltInLanguage();
LANGID langid = CLanguageOptions::GetLanguage();
if (langid != GetBuiltInLanguage())
{
CString resourceDllPath = FindResourceDllPathByLangid(langid);
if (!resourceDllPath.IsEmpty())
{
// Load language resource DLL
HINSTANCE dll = LoadLibrary(resourceDllPath);
if (dll != NULL)
{
// Set default module handle for loading of resources
AfxSetResourceHandle(dll);
m_langid = langid;
}
else
{
TRACE(_T("LoadLibrary(%s) failed: %u\r\n"), resourceDllPath, GetLastError());
}
}
// else: We use our built-in English resources.
CLanguageOptions::SetLanguage(m_langid);
}
GetOptions()->LoadFromRegistry();
free((void*)m_pszHelpFilePath);
m_pszHelpFilePath=_tcsdup(ConstructHelpFileName()); // ~CWinApp() will free this memory.
m_pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDirstatDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CGraphView));
if (!m_pDocTemplate)
return FALSE;
AddDocTemplate(m_pDocTemplate);
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
m_nCmdShow= SW_HIDE;
if (!ProcessShellCommand(cmdInfo))
return FALSE;
GetMainFrame()->InitialShowWindow();
m_pMainWnd->UpdateWindow();
// When called by setup.exe, windirstat remained in the
// background, so we do a
m_pMainWnd->BringWindowToTop();
m_pMainWnd->SetForegroundWindow();
if (cmdInfo.m_nShellCommand != CCommandLineInfo::FileOpen)
{
OnFileOpen();
}
return TRUE;
}
int CDirstatApp::ExitInstance()
{
return CWinApp::ExitInstance();
}
LANGID CDirstatApp::GetLangid()
{
return m_langid;
}
LANGID CDirstatApp::GetEffectiveLangid()
{
if (GetOptions()->IsUseWdsLocale())
return GetLangid();
else
return GetUserDefaultLangID();
}
void CDirstatApp::OnAppAbout()
{
StartAboutDialog();
}
void CDirstatApp::OnFileOpen()
{
CSelectDrivesDlg dlg;
if (IDOK == dlg.DoModal())
{
CFileFindWDS::InitializeFilter();
CString path= CDirstatDoc::EncodeSelection((RADIO)dlg.m_radio, dlg.m_folderName, dlg.m_drives);
m_pDocTemplate->OpenDocumentFile(path, true);
}
}
BOOL CDirstatApp::OnIdle(LONG lCount)
{
bool more= false;
CDirstatDoc *doc= GetDocument();
if (doc != NULL && !doc->Work(600))
more= true;
if (CWinApp::OnIdle(lCount))
more= true;
// The status bar (RAM usage) is updated only when count == 0.
// That's why we call an extra OnIdle(0) here.
if (CWinThread::OnIdle(0))
more= true;
return more;
}
void CDirstatApp::OnHelpManual()
{
DoContextHelp(IDH_StartPage);
}
void CDirstatApp::DoContextHelp(DWORD topic)
{
if (FileExists(m_pszHelpFilePath))
{
// I want a NULL parent window. So I don't use CWinApp::HtmlHelp().
::HtmlHelp(NULL, m_pszHelpFilePath, HH_HELP_CONTEXT, topic);
}
else
{
CString msg;
msg.FormatMessage(IDS_HELPFILEsCOULDNOTBEFOUND, _T("windirstat.chm"));
AfxMessageBox(msg);
}
}
void CDirstatApp::OnUpdateHelpReportbug(CCmdUI *pCmdUI)
{
pCmdUI->Enable(CModalSendMail::IsSendMailAvailable());
}
void CDirstatApp::OnHelpReportbug()
{
CReportBugDlg dlg;
if (IDOK == dlg.DoModal())
{
CModalSendMail msm;
msm.SendMail(dlg.m_recipient, dlg.m_subject, dlg.m_body);
}
}
// $Log: windirstat.cpp,v $
// Revision 1.16 2005/04/17 12:27:21 assarbad
// - For details see changelog of 2005-04-17
//
// Revision 1.15 2005/04/10 16:49:30 assarbad
// - Some smaller fixes including moving the resource string version into the rc2 files
//
// Revision 1.14 2004/12/19 10:52:39 bseifert
// Minor fixes.
//
// Revision 1.13 2004/11/28 14:40:06 assarbad
// - Extended CFileFindWDS to replace a global function
// - Now packing/unpacking the file attributes. This even spares a call to find encrypted/compressed files.
//
// Revision 1.12 2004/11/25 11:58:52 assarbad
// - Minor fixes (odd behavior of coloring in ANSI version, caching of the GetCompressedFileSize API)
// for details see the changelog.txt
//
// Revision 1.11 2004/11/14 08:49:06 bseifert
// Date/Time/Number formatting now uses User-Locale. New option to force old behavior.
//
// Revision 1.10 2004/11/12 22:14:16 bseifert
// Eliminated CLR_NONE. Minor corrections.
//
// Revision 1.9 2004/11/10 01:03:00 assarbad
// - Style cleaning of the alternative coloring code for compressed/encrypted items
//
// Revision 1.8 2004/11/08 00:46:26 assarbad
// - Added feature to distinguish compressed and encrypted files/folders by color as in the Windows 2000/XP explorer.
// Same rules apply. (Green = encrypted / Blue = compressed)
//
// Revision 1.7 2004/11/05 16:53:08 assarbad
// Added Date and History tag where appropriate.
//