forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileExplorerTab.cpp
415 lines (364 loc) · 12.9 KB
/
FileExplorerTab.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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : FileExplorerTab.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// 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.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "FileExplorerTab.h"
#include "pluginmanager.h"
#include "plugin.h"
#include "localworkspace.h"
#include "tags_options_data.h"
#include "frame.h"
#include "manager.h"
#include <wx/wupdlock.h>
#include "globals.h"
#include "dirsaver.h"
#include "environmentconfig.h"
#include "procutils.h"
#include <wx/mimetype.h>
#include "editor_config.h"
#include "fileutils.h"
FileExplorerTab::FileExplorerTab(wxWindow* parent)
: FileExplorerBase(parent)
{
m_rclickMenu = wxXmlResource::Get()->LoadMenu(wxT("file_explorer_menu"));
Connect(XRCID("open_file"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnOpenFile),
NULL,
this);
Connect(XRCID("open_file_in_text_editor"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnOpenFileInTextEditor),
NULL,
this);
Connect(XRCID("refresh_node"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnRefreshNode),
NULL,
this);
Connect(XRCID("delete_node"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnDeleteNode),
NULL,
this);
Connect(XRCID("search_node"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnSearchNode),
NULL,
this);
Connect(XRCID("tags_add_global_include"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnTagNode),
NULL,
this);
Connect(XRCID("tags_add_global_exclude"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnTagNode),
NULL,
this);
Connect(XRCID("tags_add_workspace_include"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnTagNode),
NULL,
this);
Connect(XRCID("tags_add_workspace_exclude"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnTagNode),
NULL,
this);
Connect(XRCID("open_shell"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnOpenShell),
NULL,
this);
Connect(XRCID("fe_open_file_explorer"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnOpenExplorer),
NULL,
this);
Connect(GetId(), wxEVT_COMMAND_TREE_KEY_DOWN, wxTreeEventHandler(FileExplorerTab::OnKeyDown));
Connect(XRCID("open_with_default_application"),
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(FileExplorerTab::OnOpenWidthDefaultApp),
NULL,
this);
}
FileExplorerTab::~FileExplorerTab() { wxDELETE(m_rclickMenu); }
void FileExplorerTab::OnContextMenu(wxTreeEvent& event)
{
wxTreeItemId item = event.GetItem();
if(item.IsOk() && m_rclickMenu) {
// let the plugins hook their content. HookPopupMenu can work only once
// further calls are harmless
PluginManager::Get()->HookPopupMenu(m_rclickMenu, MenuTypeFileExplorer);
PopupMenu(m_rclickMenu);
}
}
void FileExplorerTab::OnItemActivated(wxTreeEvent& event)
{
event.Skip();
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
for(size_t i = 0; i < paths.GetCount(); ++i) {
if(!wxDir::Exists(paths.Item(i))) {
DoOpenItem(paths.Item(i));
}
}
}
void FileExplorerTab::DoOpenItem(const wxString& path)
{
wxFileName fn(path);
wxString fp = fn.GetFullPath();
if(fn.GetExt() == wxT("workspace")) {
// open workspace
ManagerST::Get()->OpenWorkspace(fp);
} else {
// Send event to the plugins to see if any plugin want to handle this file differently
if(SendCmdEvent(wxEVT_TREE_ITEM_FILE_ACTIVATED, &fp)) {
return;
}
clMainFrame::Get()->GetMainBook()->OpenFile(fp, wxEmptyString);
}
}
void FileExplorerTab::OnDeleteNode(wxCommandEvent& event)
{
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
wxFileName gobackPath;
for(size_t i = 0; i < paths.GetCount(); i++) {
wxString path = paths.Item(i);
if(!gobackPath.IsOk()) {
gobackPath = wxFileName(path);
}
if(wxDir::Exists(path)) {
//////////////////////////////////////////////////
// Remove a folder
//////////////////////////////////////////////////
wxString msg;
msg << _("'") << path << _("' is a directory. Are you sure you want to remove it and its content?");
if(wxMessageBox(msg, _("Remove Directory"), wxICON_WARNING | wxYES_NO | wxCANCEL) == wxYES) {
if(!::RemoveDirectory(path)) {
wxMessageBox(_("Failed to remove directory"), _("Remove Directory"), wxICON_ERROR | wxOK);
}
}
} else {
//////////////////////////////////////////////////
// Remove a file
//////////////////////////////////////////////////
wxLogNull noLog;
::wxRemoveFile(path);
}
}
wxWindowUpdateLocker locker(m_genericDirCtrl);
m_genericDirCtrl->ReCreateTree();
m_genericDirCtrl->ExpandPath(gobackPath.GetPath());
}
void FileExplorerTab::OnKeyDown(wxTreeEvent& event)
{
if(event.GetKeyCode() == WXK_DELETE || event.GetKeyCode() == WXK_NUMPAD_DELETE) {
wxCommandEvent dummy;
OnDeleteNode(dummy);
} else {
event.Skip();
}
}
void FileExplorerTab::OnOpenFile(wxCommandEvent& event)
{
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
for(size_t i = 0; i < paths.GetCount(); ++i) {
if(!wxDir::Exists(paths.Item(i))) {
DoOpenItem(paths.Item(i));
}
}
}
void FileExplorerTab::OnOpenFileInTextEditor(wxCommandEvent& event)
{
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
for(size_t i = 0; i < paths.GetCount(); ++i) {
if(!wxDir::Exists(paths.Item(i))) {
clMainFrame::Get()->GetMainBook()->OpenFile(paths.Item(i), wxEmptyString);
}
}
}
void FileExplorerTab::OnOpenShell(wxCommandEvent& event)
{
wxFileName path;
if(!GetSelection(path)) return;
DirSaver ds;
wxSetWorkingDirectory(path.GetPath());
// Apply the environment before launching the console
EnvSetter env;
FileUtils::OpenTerminal(path.GetPath());
}
void FileExplorerTab::OnOpenWidthDefaultApp(wxCommandEvent& e)
{
wxUnusedVar(e);
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
// Apply our environment before executing the file
EnvSetter es;
for(size_t i = 0; i < paths.GetCount(); i++) {
bool bOpenOK = false;
wxFileName fullpath(paths.Item(i));
// switch directory to the current files' path
DirSaver ds;
::wxSetWorkingDirectory(fullpath.GetPath());
wxMimeTypesManager* mgr = wxTheMimeTypesManager;
wxFileType* type = mgr->GetFileTypeFromExtension(fullpath.GetExt());
if(type) {
wxString cmd = type->GetOpenCommand(fullpath.GetFullPath());
delete type;
if(cmd.IsEmpty() == false) {
wxExecute(cmd);
bOpenOK = true;
}
}
#ifdef __WXGTK__
if(!bOpenOK) {
// All hell break loose, try xdg-open
wxString cmd;
wxString escapedString(fullpath.GetFullPath());
escapedString.Replace(" ", "\\ ");
cmd << "xdg-open " << escapedString;
::wxExecute(cmd);
bOpenOK = true;
}
#endif
// fallback code: suggest to the user to open the file with CL
if(!bOpenOK &&
wxMessageBox(wxString::Format(
_("Could not find default application for file '%s'\nWould you like CodeLite to open it?"),
fullpath.GetFullName().c_str()),
_("CodeLite"),
wxICON_QUESTION | wxYES_NO) == wxYES) {
DoOpenItem(fullpath.GetFullPath());
}
}
}
void FileExplorerTab::OnRefreshNode(wxCommandEvent& event)
{
wxWindowUpdateLocker locker(m_genericDirCtrl);
m_genericDirCtrl->ReCreateTree();
}
void FileExplorerTab::OnSearchNode(wxCommandEvent& event)
{
wxFileName fn;
if(!GetSelection(fn)) return;
wxCommandEvent ff(wxEVT_COMMAND_MENU_SELECTED, XRCID("find_in_files"));
ff.SetString(fn.GetPath());
clMainFrame::Get()->GetEventHandler()->AddPendingEvent(ff);
}
void FileExplorerTab::OnTagNode(wxCommandEvent& event)
{
bool retagRequires(false);
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
TagsOptionsData tod = clMainFrame::Get()->GetTagsOptions();
wxArrayString includePaths, excludePaths;
LocalWorkspaceST::Get()->GetParserPaths(includePaths, excludePaths);
for(size_t i = 0; i < paths.GetCount(); i++) {
wxString path = paths.Item(i);
if(path.EndsWith(wxT("\\")) || path.EndsWith(wxT("/"))) {
path.RemoveLast();
}
if(event.GetId() == XRCID("tags_add_global_include")) {
// add this directory as include path
wxArrayString arr = tod.GetParserSearchPaths();
if(arr.Index(path) == wxNOT_FOUND) {
arr.Add(path);
tod.SetParserSearchPaths(arr);
retagRequires = true;
}
} else if(event.GetId() == XRCID("tags_add_global_exclude")) {
wxArrayString arr = tod.GetParserExcludePaths();
if(arr.Index(path) == wxNOT_FOUND) {
arr.Add(path);
tod.SetParserExcludePaths(arr);
retagRequires = true;
}
} else if(event.GetId() == XRCID("tags_add_workspace_include")) {
if(includePaths.Index(path) == wxNOT_FOUND) {
includePaths.Add(path);
retagRequires = true;
}
} else if(event.GetId() == XRCID("tags_add_workspace_exclude")) {
if(excludePaths.Index(path) == wxNOT_FOUND) {
excludePaths.Add(path);
retagRequires = true;
}
}
clMainFrame::Get()->UpdateTagsOptions(tod);
LocalWorkspaceST::Get()->SetParserPaths(includePaths, excludePaths);
// Update the parser
ManagerST::Get()->UpdateParserPaths();
// send notification to the main frame to perform retag
if(retagRequires) {
wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, XRCID("retag_workspace"));
clMainFrame::Get()->GetEventHandler()->AddPendingEvent(event);
}
}
}
TreeItemInfo FileExplorerTab::GetSelectedItemInfo()
{
TreeItemInfo info;
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
if(paths.IsEmpty()) return info;
info.m_paths = paths;
info.m_item = wxTreeItemId();
return info;
}
size_t FileExplorerTab::GetSelectionCount() const
{
wxArrayTreeItemIds items;
return Tree()->GetTreeCtrl()->GetSelections(items);
}
void FileExplorerTab::ClearSelections()
{
if(GetSelectionCount()) {
// multiple selections
Tree()->GetTreeCtrl()->UnselectAll();
}
}
void FileExplorerTab::OnOpenExplorer(wxCommandEvent& event)
{
wxFileName path;
if(!GetSelection(path)) return;
FileUtils::OpenFileExplorer(path.GetPath());
}
bool FileExplorerTab::GetSelection(wxFileName& path)
{
wxArrayString paths;
m_genericDirCtrl->GetPaths(paths);
if(paths.IsEmpty()) return false;
if(wxFileName::DirExists(paths.Item(0))) {
// is a directory
path = wxFileName(paths.Item(0), "");
} else {
// a file
path = wxFileName(paths.Item(0));
}
return true;
}