File: FileExplorerTab.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (523 lines) | stat: -rw-r--r-- 17,010 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// 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"
#include "cl_command_event.h"
#include "event_notifier.h"

FileExplorerTab::FileExplorerTab(wxWindow* parent)
    : FileExplorerBase(parent)
{
    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()
{
    Disconnect(XRCID("open_file"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnOpenFile),
            NULL,
            this);
    Disconnect(XRCID("open_file_in_text_editor"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnOpenFileInTextEditor),
            NULL,
            this);
    Disconnect(XRCID("refresh_node"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnRefreshNode),
            NULL,
            this);
    Disconnect(XRCID("delete_node"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnDeleteNode),
            NULL,
            this);
    Disconnect(XRCID("search_node"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnSearchNode),
            NULL,
            this);
    Disconnect(XRCID("tags_add_global_include"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnTagNode),
            NULL,
            this);
    Disconnect(XRCID("tags_add_global_exclude"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnTagNode),
            NULL,
            this);
    Disconnect(XRCID("tags_add_workspace_include"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnTagNode),
            NULL,
            this);
    Disconnect(XRCID("tags_add_workspace_exclude"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnTagNode),
            NULL,
            this);
    Disconnect(XRCID("open_shell"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnOpenShell),
            NULL,
            this);
    Disconnect(XRCID("fe_open_file_explorer"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnOpenExplorer),
            NULL,
            this);
    Disconnect(GetId(), wxEVT_COMMAND_TREE_KEY_DOWN, wxTreeEventHandler(FileExplorerTab::OnKeyDown));

    Disconnect(XRCID("open_with_default_application"),
            wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(FileExplorerTab::OnOpenWidthDefaultApp),
            NULL,
            this);
}

void FileExplorerTab::OnContextMenu(wxTreeEvent& event)
{
    wxTreeItemId item = event.GetItem();
    wxMenu* menu = GetBaseContextMenu();
    if(item.IsOk() && menu) {
        wxArrayString folders, files;
        GetSelections(folders, files);
        if(!files.IsEmpty() && folders.IsEmpty()) {
            // Let the plugins alter it
            clContextMenuEvent fileMenuEvent(wxEVT_CONTEXT_MENU_FILE);
            fileMenuEvent.SetMenu(menu);
            fileMenuEvent.SetStrings(files);
            EventNotifier::Get()->ProcessEvent(fileMenuEvent);
        } else if(folders.GetCount() == 1) {
            clContextMenuEvent folderMenuEvent(wxEVT_CONTEXT_MENU_FOLDER);
            folderMenuEvent.SetMenu(menu);
            folderMenuEvent.SetPath(folders.Item(0));
            EventNotifier::Get()->ProcessEvent(folderMenuEvent);
        }

        PluginManager::Get()->HookPopupMenu(menu, MenuTypeFileExplorer);
        PopupMenu(menu);
    }
    wxDELETE(menu);
}

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
        clCommandEvent event(wxEVT_TREE_ITEM_FILE_ACTIVATED);
        event.SetFileName(fp);
        if(EventNotifier::Get()->ProcessEvent(event)) 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;
            clRemoveFile(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)
{
    wxArrayString paths;
    GetSelectedDirectories(paths);
    if(paths.IsEmpty()) return;
    PluginManager::Get()->OpenFindInFileForPaths(paths);
}

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(false);

        // 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;
}

void FileExplorerTab::GetSelectedDirectories(wxArrayString& paths)
{
    wxArrayString files;
    GetSelections(paths, files);
}

wxMenu* FileExplorerTab::GetBaseContextMenu()
{
    wxMenu* menu = wxXmlResource::Get()->LoadMenu(wxT("file_explorer_menu"));
    wxMenuItem* searchNodeItem = menu->FindItem(XRCID("search_node"));
    if(searchNodeItem) {
        searchNodeItem->SetBitmap(wxXmlResource::Get()->LoadBitmap("m_bmpFindInFiles"));
    }
    return menu;
}

void FileExplorerTab::GetSelections(wxArrayString& folders, wxArrayString& files)
{
    wxArrayString selections;
    m_genericDirCtrl->GetPaths(selections);

    for(size_t i = 0; i < selections.GetCount(); ++i) {
        if(wxFileName::DirExists(selections.Item(i))) {
            folders.Add(selections.Item(i));
        } else if(wxFileName::FileExists(selections.Item(i))) {
            files.Add(selections.Item(i));
        }
    }
}