-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesizedetect.cpp
372 lines (303 loc) · 10.1 KB
/
filesizedetect.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
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <vector>
#include <algorithm>
#include <numeric>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <wx/treectrl.h>
#include <wx/event.h>
#include "treelistctrl.h"
#include "resource.h"
#include "FileSizeDetecter.h"
class FileSizeDetectApp: public wxApp
{
public:
virtual bool OnInit();
};
class FileSizeDetectFrame: public wxFrame
{
public:
FileSizeDetectFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnSelectDir(wxCommandEvent &);
void OnStart(wxCommandEvent &);
void OnStop(wxCommandEvent &);
private:
void FileDeteterErrorHandler(int err_code, const std::wstring &file_path);
void FileDeteter_FileEndHandler(const bfs::path &path, uintmax_t size);
void FileDeteter_DirBegHandler(const bfs::path &path);
void FileDeteter_DirEndHandler(const bfs::path &path);
private:
void OnThreadFileEnd(wxThreadEvent &e);
void OnThreadDirBeg(wxThreadEvent &e);
void OnThreadDirEnd(wxThreadEvent &e);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
private:
wxTextCtrl *m_txtCtrlPath;
wxcode::wxTreeListCtrl *m_treeListCtrlInfo;
wxButton *m_start_btn;
bfs::path m_root_path;
file_size_detecter m_detecter;
boost::shared_ptr<boost::thread> m_detect_thread;
std::vector<wxTreeItemId> m_dir_items;
};
wxBEGIN_EVENT_TABLE(FileSizeDetectFrame, wxFrame)
EVT_THREAD(ID_THREAD_FILE_END, FileSizeDetectFrame::OnThreadFileEnd)
EVT_THREAD(ID_THREAD_DIR_BEG, FileSizeDetectFrame::OnThreadDirBeg)
EVT_THREAD(ID_THREAD_DIR_END, FileSizeDetectFrame::OnThreadDirEnd)
EVT_BUTTON(ID_BTN_SELECT, FileSizeDetectFrame::OnSelectDir)
EVT_BUTTON(ID_BTN_START, FileSizeDetectFrame::OnStart)
EVT_BUTTON(ID_BTN_STOP, FileSizeDetectFrame::OnStop)
EVT_MENU(wxID_EXIT, FileSizeDetectFrame::OnExit)
EVT_MENU(wxID_ABOUT, FileSizeDetectFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(FileSizeDetectApp);
bool FileSizeDetectApp::OnInit()
{
FileSizeDetectFrame *frame = new FileSizeDetectFrame( "FileSizeDetecter", wxPoint(50, 50), wxSize(450, 340) );
frame->Show( true );
return true;
}
/////////////////// Frame
FileSizeDetectFrame::FileSizeDetectFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( "Welcome to FileSizeDetecter" );
wxPanel *panel = new wxPanel(this, wxID_ANY);
wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(topSizer);
// 选择目录
wxBoxSizer *selectFoldSizer = new wxBoxSizer(wxHORIZONTAL);
topSizer->Add(selectFoldSizer, wxSizerFlags(0).Expand());
// static
selectFoldSizer->Add(new wxStaticText(panel, wxID_ANY, wxT("选择文件或目录:")));
// text
m_txtCtrlPath = new wxTextCtrl(panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(300, -1));
selectFoldSizer->Add(m_txtCtrlPath, wxSizerFlags(1).Align(wxALIGN_LEFT));
// button
selectFoldSizer->Add(new wxButton(panel, ID_BTN_SELECT, wxString("...")), wxSizerFlags(0).Align(wxALIGN_RIGHT));
// 树形控件
m_treeListCtrlInfo = new wxcode::wxTreeListCtrl(panel, wxID_ANY, wxDefaultPosition, wxSize(-1, 200), wxTR_DEFAULT_STYLE);
topSizer->Add(m_treeListCtrlInfo, wxSizerFlags(1).Expand());
m_treeListCtrlInfo->AddColumn(wxT("文件名"), 300);
m_treeListCtrlInfo->AddColumn(wxT("文件大小"));
m_treeListCtrlInfo->SetColumnEditable(0, true);
m_treeListCtrlInfo->SetColumnEditable(1, true);
// 开始暂停按钮
wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
topSizer->Add(buttonSizer, wxSizerFlags(0).Expand());
m_start_btn = new wxButton(panel, ID_BTN_START, wxT("start"));
buttonSizer->Add(m_start_btn, wxSizerFlags(1).Center());
buttonSizer->Add(new wxButton(panel, ID_BTN_STOP, wxT("stop")), wxSizerFlags(1).Center());
//topSizer->Fit(this);
topSizer->SetSizeHints(this);
this->Center();
}
void FileSizeDetectFrame::FileDeteterErrorHandler(int err_code, const std::wstring &file_path)
{
switch (err_code)
{
case file_size_detecter::err_file_not_found:
break;
case file_size_detecter::err_file_illegal:
break;
case file_size_detecter::err_file_size:
break;
default:
break;
}
}
void FileSizeDetectFrame::OnExit(wxCommandEvent& event)
{
Close( true );
}
void FileSizeDetectFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox( "检测占用空间最大的目录和文件\nAuthor: 张善禄",
"About FileSizeDetecter", wxOK | wxICON_INFORMATION );
}
void FileSizeDetectFrame::FileDeteter_FileEndHandler( const bfs::path &path, uintmax_t size )
{
wxThreadEvent *e = new wxThreadEvent;
e->SetId(ID_THREAD_FILE_END);
e->SetString(path.wstring().c_str());
e->SetPayload(size);
QueueEvent(e);
}
void FileSizeDetectFrame::FileDeteter_DirBegHandler( const bfs::path &path )
{
wxThreadEvent *e = new wxThreadEvent;
e->SetId(ID_THREAD_DIR_BEG);
e->SetString(path.wstring().c_str());
QueueEvent(e);
}
void FileSizeDetectFrame::FileDeteter_DirEndHandler( const bfs::path &path )
{
wxThreadEvent *e = new wxThreadEvent;
e->SetId(ID_THREAD_DIR_END);
e->SetString(path.wstring().c_str());
QueueEvent(e);
}
void FileSizeDetectFrame::OnThreadFileEnd( wxThreadEvent &e )
{
wxTreeItemId item;
if (m_dir_items.empty())
{
item = m_treeListCtrlInfo->AddRoot(e.GetString());
uintmax_t size = e.GetPayload<uintmax_t>();
m_treeListCtrlInfo->SetItemText(item, 1, boost::lexical_cast<std::wstring>(size));
m_detect_thread->join();
m_start_btn->Enable(true);
}
else
{
item = m_treeListCtrlInfo->AppendItem(m_dir_items.back(), e.GetString());
uintmax_t size = e.GetPayload<uintmax_t>();
m_treeListCtrlInfo->SetItemText(item, 1, boost::lexical_cast<std::wstring>(size));
}
}
void FileSizeDetectFrame::OnThreadDirBeg( wxThreadEvent &e )
{
wxTreeItemId item;
if (m_dir_items.empty())
{
item = m_treeListCtrlInfo->AddRoot(e.GetString());
}
else
{
item = m_treeListCtrlInfo->AppendItem(m_dir_items.back(), e.GetString());
}
m_dir_items.push_back(item);
}
boost::wformat KB_format(L"%.3fKB");
boost::wformat MB_format(L"%.3fMB");
boost::wformat GB_format(L"%.3fGB");
std::wstring get_readable_str(uintmax_t size)
{
if (size > 1024 * 1024 * 1024)
{
GB_format.clear_binds();
GB_format % (size * 1.0 / 1024 / 1024 / 1024);
return GB_format.str();
}
else if (size > 1024 * 1024)
{
MB_format.clear_binds();
MB_format % (size * 1.0 / 1024 / 1024);
return MB_format.str();
}
else if (size > 1024)
{
KB_format.clear_binds();
KB_format % (size * 1.0 / 1024);
return KB_format.str();
}
return boost::lexical_cast<std::wstring>(size) + L" B";
}
void FileSizeDetectFrame::OnThreadDirEnd( wxThreadEvent &e )
{
typedef std::pair<wxTreeItemId, uintmax_t> item_data_t;
std::vector<item_data_t> sizeVec;
wxTreeItemId cur_item = m_dir_items.back();
// 找出最小 和 最大的文件
wxTreeItemIdValue cookie;
for (wxTreeItemId item = m_treeListCtrlInfo->GetFirstChild(cur_item, cookie);
item.IsOk();
item = m_treeListCtrlInfo->GetNextSibling(item))
{
sizeVec.emplace_back(item, boost::lexical_cast<uintmax_t>(m_treeListCtrlInfo->GetItemText(item, 1).c_str() ) );
}
if (!sizeVec.empty())
{
// 排序
std::sort(sizeVec.begin(), sizeVec.end(),
[](const item_data_t &l, const item_data_t &r){ return l.second > r.second; }
);
// set
m_treeListCtrlInfo->SetItemBackgroundColour(sizeVec.front().first, wxColour(wxT("#a0a0f0")));
if (sizeVec.size() > 1)
m_treeListCtrlInfo->SetItemBackgroundColour(sizeVec[1].first, wxColour(wxT("#c4c4f0")));
if (sizeVec.size() > 2)
m_treeListCtrlInfo->SetItemBackgroundColour(sizeVec[2].first, wxColour(wxT("#e8e8ff")));
for (size_t index = 0; index < sizeVec.size() && index < 5; ++index)
{
m_treeListCtrlInfo->SetItemText(sizeVec[index].first, 1, get_readable_str(sizeVec[index].second) );
}
for (size_t index = 5; index < sizeVec.size(); ++index)
{
m_treeListCtrlInfo->Delete(sizeVec[index].first);
}
}
// 计算出此目录的总大小
uintmax_t dir_size = std::accumulate<std::vector<item_data_t>::iterator, uintmax_t>(sizeVec.begin(), sizeVec.end(), 0,
[](uintmax_t l, const item_data_t &r){ return l + r.second; }
);
m_treeListCtrlInfo->SetItemText(cur_item, 1, boost::lexical_cast<std::wstring>(dir_size).c_str());
// 将此目录清除
m_dir_items.pop_back();
if (m_dir_items.empty())
{
m_treeListCtrlInfo->SetItemText(cur_item, 1, get_readable_str(dir_size));
m_detect_thread->join();
m_start_btn->Enable(true);
}
}
void FileSizeDetectFrame::OnSelectDir(wxCommandEvent &)
{
wxDirDialog dlg(this, wxT("选择想要检测的目录") );
if (dlg.ShowModal() == wxID_OK)
{
m_txtCtrlPath->SetValue(dlg.GetPath() );
}
}
void FileSizeDetectFrame::OnStart(wxCommandEvent &)
{
m_root_path = m_txtCtrlPath->GetValue().Trim(true).Trim(false).c_str();
if (m_root_path.empty())
{
wxMessageBox(wxT("路径不能为空或不能为相对路径"));
return;
}
else if ( m_root_path.is_relative())
{
wxMessageBox(wxT("路径不能为相对路径"));
return;
}
else if (!bfs::exists(m_root_path))
{
wxMessageBox(wxT("路径不存在"));
return;
}
m_treeListCtrlInfo->DeleteRoot();
m_root_path.make_preferred();
m_detecter.set_path(m_root_path);
m_detecter.register_file_end_handler( boost::bind(&FileSizeDetectFrame::FileDeteter_FileEndHandler, this, _1, _2) );
m_detecter.register_dir_beg_handler( boost::bind(&FileSizeDetectFrame::FileDeteter_DirBegHandler, this, _1) );
m_detecter.register_dir_end_handler( boost::bind(&FileSizeDetectFrame::FileDeteter_DirEndHandler, this, _1) );
m_detecter.register_err_handler( boost::bind(&FileSizeDetectFrame::FileDeteterErrorHandler, this, _1, _2) );
m_detect_thread.reset(new boost::thread(&file_size_detecter::start, &m_detecter) );
m_start_btn->Enable(false);
}
void FileSizeDetectFrame::OnStop(wxCommandEvent &)
{
}