File: codedesigner.cpp

package info (click to toggle)
codelite 14.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (649 lines) | stat: -rw-r--r-- 22,510 bytes parent folder | download | duplicates (2)
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include "codedesigner.h"
#include "codedesignerdlg.h"
#include "project.h"
#include "confcodedesigner.h"
#include "procutils.h"
#include "workspace.h"
#include "processreaderthread.h"
#include "asyncprocess.h"
#include "globals.h"
#include "virtualdirectoryselector.h"
#include <wx/menu.h>
#include <wx/xrc/xmlres.h>
#include <wx/mimetype.h>
#include <wx/file.h>
#include <wx/listimpl.cpp>
#include <wx/tokenzr.h>

#include <vector>
#include <algorithm>

#include "icons/ClassDiag.xpm"
#include "icons/StateChartDiag.xpm"
#include "icons/SimpleStateChartDiag.xpm"

#define TOPIC wxT("CODEDESIGNER SESSION")

wxEventType wxEVT_IPC_POKE = wxNewEventType();

WX_DEFINE_LIST(ConnectionList);

static CodeDesigner* thePlugin = NULL;

// Define the plugin entry point
extern "C" EXPORT IPlugin* CreatePlugin(IManager* manager)
{
    if(thePlugin == 0) {
        thePlugin = new CodeDesigner(manager);
    }
    return thePlugin;
}

extern "C" EXPORT PluginInfo GetPluginInfo()
{
    PluginInfo info;
    info.SetAuthor(wxT("Michal Bližňák"));
    info.SetName(wxT("CodeDesigner"));
    info.SetDescription(_("CodeDesigner RAD integration with CodeLite"));
    info.SetVersion(wxT("v1.0"));
    return info;
}

extern "C" EXPORT int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }

CodeDesigner::CodeDesigner(IManager* manager)
    : IPlugin(manager)
    , m_addFileMenu(true)
{
    m_longName = _("CodeDesigner RAD integration with CodeLite");
    m_shortName = wxT("CodeDesigner");

    m_Server = NULL;

    m_mgr->GetTheApp()->Connect(XRCID("cd_open"), wxEVT_COMMAND_MENU_SELECTED,
                                wxCommandEventHandler(CodeDesigner::OnOpenWithCD), NULL, this);
    m_mgr->GetTheApp()->Connect(XRCID("cd_open"), wxEVT_UPDATE_UI,
                                wxUpdateUIEventHandler(CodeDesigner::OnUpdateOpenWithCD), NULL, this);
    m_mgr->GetTheApp()->Connect(XRCID("cd_new_simplestatechart"), wxEVT_COMMAND_MENU_SELECTED,
                                wxCommandEventHandler(CodeDesigner::OnNewSCH), NULL, this);
    m_mgr->GetTheApp()->Connect(XRCID("cd_new_hierarchicalstatechart"), wxEVT_COMMAND_MENU_SELECTED,
                                wxCommandEventHandler(CodeDesigner::OnNewHSCH), NULL, this);
    m_mgr->GetTheApp()->Connect(XRCID("cd_new_classdiagram"), wxEVT_COMMAND_MENU_SELECTED,
                                wxCommandEventHandler(CodeDesigner::OnNewCD), NULL, this);

    m_mgr->GetTheApp()->Connect(wxEVT_TREE_ITEM_FILE_ACTIVATED, wxCommandEventHandler(CodeDesigner::OnOpenFile), NULL,
                                this);

    m_mgr->GetTheApp()->Connect(wxID_ANY, wxEVT_PROC_TERMINATED, wxCommandEventHandler(CodeDesigner::OnCDTerminated),
                                NULL, this);

    Connect(wxID_ANY, wxEVT_IPC_POKE, wxCommandEventHandler(CodeDesigner::OnPokeData));

    // start IPC server
    StartServer();
}

CodeDesigner::~CodeDesigner() { delete m_Server; }

clToolBar* CodeDesigner::CreateToolBar(wxWindow* parent)
{
    // Create the toolbar to be used by the plugin
    clToolBar* tb(NULL);

    /*
        // You can use the below code a snippet:
        // First, check that CodeLite allows plugin to register plugins
        if (m_mgr->AllowToolbar()) {
            // Support both toolbars icon size
            int size = m_mgr->GetToolbarIconSize();

            // Allocate new toolbar, which will be freed later by CodeLite
            tb = new clToolBar(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, clTB_DEFAULT_STYLE);

            // Set the toolbar size
            tb->SetToolBitmapSize(wxSize(size, size));

            // Add tools to the plugins toolbar. You must provide 2 sets of icons: 24x24 and 16x16
            if (size == 24) {
                tb->AddTool(XRCID("new_plugin"), wxT("New CodeLite Plugin Project"),
       wxXmlResource::Get()->LoadBitmap(wxT("plugin24")), wxT("New Plugin Wizard..."));
                tb->AddTool(XRCID("new_class"), wxT("Create New Class"),
       wxXmlResource::Get()->LoadBitmap(wxT("class24")), wxT("New Class..."));
                tb->AddTool(XRCID("new_wx_project"), wxT("New wxWidget Project"),
       wxXmlResource::Get()->LoadBitmap(wxT("new_wx_project24")), wxT("New wxWidget Project"));
            } else {
                tb->AddTool(XRCID("new_plugin"), wxT("New CodeLite Plugin Project"),
       wxXmlResource::Get()->LoadBitmap(wxT("plugin16")), wxT("New Plugin Wizard..."));
                tb->AddTool(XRCID("new_class"), wxT("Create New Class"),
       wxXmlResource::Get()->LoadBitmap(wxT("class16")), wxT("New Class..."));
                tb->AddTool(XRCID("new_wx_project"), wxT("New wxWidget Project"),
       wxXmlResource::Get()->LoadBitmap(wxT("new_wx_project16")), wxT("New wxWidget Project"));
            }
            // And finally, we must call 'Realize()'
            tb->Realize();
        }

        // return the toolbar, it can be NULL if CodeLite does not allow plugins to register toolbars
        // or in case the plugin simply does not require toolbar
    */
    return tb;
}

void CodeDesigner::CreatePluginMenu(wxMenu* pluginsMenu)
{
    // You can use the below code a snippet:
    wxMenu* menu = new wxMenu();
    wxMenuItem* item(NULL);

    item = new wxMenuItem(menu, XRCID("cd_settings"), _("Settings..."), wxEmptyString, wxITEM_NORMAL);
    menu->Append(item);

    pluginsMenu->Append(wxID_ANY, _("CodeDesigner"), menu);

    m_mgr->GetTheApp()->Connect(XRCID("cd_settings"), wxEVT_COMMAND_MENU_SELECTED,
                                wxCommandEventHandler(CodeDesigner::OnSettings), NULL, this);
}

void CodeDesigner::HookPopupMenu(wxMenu* menu, MenuType type)
{
    if(type == MenuTypeEditor) {
        // TODO::Append items for the editor context menu
    } else if(type == MenuTypeFileExplorer) {
        // TODO::Append items for the file explorer context menu
    } else if(type == MenuTypeFileView_Workspace) {
        // TODO::Append items for the file view / workspace context menu
    } else if(type == MenuTypeFileView_Project) {
        // TODO::Append items for the file view/Project context menu
    } else if(type == MenuTypeFileView_Folder) {
        // TODO::Append items for the file view/Virtual folder context menu
        if(!menu->FindItem(XRCID("cd_popup"))) {
            m_separatorItem = menu->AppendSeparator();
            menu->Append(XRCID("cd_popup"), wxT("CodeDesigner"), CreatePopupMenu());
        }
    } else if(type == MenuTypeFileView_File) {
        // TODO::Append items for the file view/file context menu
        if(m_addFileMenu) {
            wxMenuItem* item =
                new wxMenuItem(menu, XRCID("cd_open"), _("Open with CodeDesigner..."), wxEmptyString, wxITEM_NORMAL);

            menu->PrependSeparator();
            menu->Prepend(item);

            m_addFileMenu = false;
        }
    }
}

void CodeDesigner::UnHookPopupMenu(wxMenu* menu, MenuType type)
{
    if(type == MenuTypeEditor) {
        // TODO::Unhook items for the editor context menu
    } else if(type == MenuTypeFileExplorer) {
        // TODO::Unhook  items for the file explorer context menu
    } else if(type == MenuTypeFileView_Workspace) {
        // TODO::Unhook  items for the file view / workspace context menu
    } else if(type == MenuTypeFileView_Project) {
        // TODO::Unhook  items for the file view/Project context menu
    } else if(type == MenuTypeFileView_Folder) {
        // TODO::Unhook  items for the file view/Virtual folder context menu
    } else if(type == MenuTypeFileView_File) {
        // TODO::Unhook  items for the file view/file context menu
    }
}

void CodeDesigner::UnPlug()
{
    // TODO:: perform the unplug action for this plugin
}

void CodeDesigner::OnSettings(wxCommandEvent& e)
{
    CodeDesignerDlg dlg(m_mgr->GetTheApp()->GetTopWindow());

    ConfCodeDesigner data;
    m_mgr->GetConfigTool()->ReadObject(wxT("CodeDesigner"), &data);

    dlg.SetPort(data.GetPort());
    dlg.SetPath(data.GetPath());

    if(dlg.ShowModal() == wxID_OK) {
        bool fRestartServer = false;

        if(data.GetPort() != dlg.GetPort()) fRestartServer = true;

        data.SetPort(dlg.GetPort());
        data.SetPath(dlg.GetPath());

        m_mgr->GetConfigTool()->WriteObject(wxT("CodeDesigner"), &data);

        if(fRestartServer) StartServer();
    }
}

wxMenu* CodeDesigner::CreatePopupMenu()
{
    // Create the popup menu for the file explorer
    // The only menu that we are interseted is the file explorer menu
    wxMenu* menu = new wxMenu();
    wxMenuItem* item(NULL);

    item = new wxMenuItem(menu, XRCID("cd_new_simplestatechart"), _("New simple state chart..."), wxEmptyString,
                          wxITEM_NORMAL);
    item->SetBitmap(wxBitmap(SimpleStateChartDiag_xpm));
    menu->Append(item);

    item = new wxMenuItem(menu, XRCID("cd_new_hierarchicalstatechart"), _("New hierarchical state chart..."),
                          wxEmptyString, wxITEM_NORMAL);
    item->SetBitmap(wxBitmap(StateChartDiag_xpm));
    menu->Append(item);

    item = new wxMenuItem(menu, XRCID("cd_new_classdiagram"), _("New class diagram..."), wxEmptyString, wxITEM_NORMAL);
    item->SetBitmap(wxBitmap(ClassDiag_xpm));
    menu->Append(item);

    return menu;
}

void CodeDesigner::OnOpenFile(wxCommandEvent& e)
{
    wxString* fn = (wxString*)e.GetClientData();
    if(fn) {
        // launch it with the default application
        wxFileName fullpath(*fn);
        if(fullpath.GetExt().MakeLower() != wxT("cdp")) {
            e.Skip();
            return;
        }
#ifdef __WXGTK__
        // Under Linux, use xdg-open
        wxString cmd;
        cmd << wxT("/bin/sh -c 'xdg-open \"") << fullpath.GetFullPath() << wxT("\"' 2> /dev/null");
        wxExecute(cmd);
        return;
#else

        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);
                return;
            }
        }
#endif
    }

    // we failed, call event.Skip()
    e.Skip();
}

void CodeDesigner::OnOpenWithCD(wxCommandEvent& e)
{
    // get the file name
    TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
    if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeFile) {
        if(item.m_fileName.GetExt() == wxT("cdp")) {
            DoLaunchCD(item.m_fileName.GetFullPath());
        } else {
            wxMessageBox(_("Please select a 'cdp' (CodeDesigner Project) file only"), _("CodeLite"),
                         wxOK | wxCENTER | wxICON_INFORMATION);
            return;
        }
    }
}

void CodeDesigner::DoLaunchCD(const wxString& file)
{
    wxString cdpath = GetCDPath();

    if(cdpath.IsEmpty()) {
        wxMessageBox(_("Failed to launch CodeDesigner, no path specified\nPlease set CodeDesigner path from Plugins -> "
                       "CodeDesigner -> Settings..."),
                     _("CodeLite"), wxOK | wxCENTER | wxICON_WARNING);
        return;
    }

    wxString cmd = wxT("$(cd) $(cd_project)");
    cmd.Replace(wxT("$(cd)"), cdpath);
    cmd.Replace(wxT("$(cd_project)"), wxString::Format(wxT("\"%s\""), file.c_str()));

#ifdef __WXMSW__
    wxExecute(cmd, wxEXEC_ASYNC);
#else
    WrapInShell(cmd);
    CreateAsyncProcess(this, cmd, IProcessCreateWithHiddenConsole);
#endif
}

wxString CodeDesigner::GetCDPath()
{
    // Launch wxFB
    ConfCodeDesigner confData;
    m_mgr->GetConfigTool()->ReadObject(wxT("CodeDesigner"), &confData);
    wxString cdpath = confData.GetPath();

#ifdef __WXGTK__
    if(cdpath.IsEmpty()) {
        // try to locate the file at '/usr/bin' or '/usr/local/bin'
        if(wxFileName::FileExists(wxT("/usr/local/bin/codedesigner"))) {
            cdpath = wxT("/usr/local/bin/codedesigner");
        } else if(wxFileName::FileExists(wxT("/usr/bin/codedesigner"))) {
            cdpath = wxT("/usr/bin/codedesigner");
        }
    }
#endif
    return cdpath;
}

void CodeDesigner::OnUpdateOpenWithCD(wxUpdateUIEvent& e)
{
    TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
    if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeFile) {
        e.Enable(item.m_fileName.GetExt() == wxT("cdp"));
    }
}

void CodeDesigner::OnCDTerminated(wxCommandEvent& e)
{
    ProcessEventData* ped = (ProcessEventData*)e.GetClientData();
    if(ped) {
        if(ped->GetProcess()) delete ped->GetProcess();
        delete ped;
    }
}

void CodeDesigner::OnNewCD(wxCommandEvent& e)
{
    CodeDesignerItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr, false);
    dlg.SetTitle(_("New Class Diagram"));

    CDItemInfo info;
    info.m_PackageName = wxT("Classes");

    dlg.SetCDInfo(info);
    if(dlg.ShowModal() == wxID_OK) {
        info = dlg.GetCDInfo();
        info.m_Type = cdtClassDiagram;

        DoCreateCDProject(info);
    }
}

void CodeDesigner::OnNewHSCH(wxCommandEvent& e)
{
    CodeDesignerItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr, true);
    dlg.SetTitle(_("New Hierarchical State Chart"));

    CDItemInfo info;
    info.m_PackageName = wxT("State charts");

    dlg.SetCDInfo(info);
    if(dlg.ShowModal() == wxID_OK) {
        info = dlg.GetCDInfo();
        info.m_Type = cdtHierarchicalStateChart;

        DoCreateCDProject(info);
    }
}

void CodeDesigner::OnNewSCH(wxCommandEvent& e)
{
    CodeDesignerItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr, true);
    dlg.SetTitle(_("New Simple State Chart"));

    CDItemInfo info;
    info.m_PackageName = wxT("State charts");

    dlg.SetCDInfo(info);
    if(dlg.ShowModal() == wxID_OK) {
        info = dlg.GetCDInfo();
        info.m_Type = cdtStateChart;

        DoCreateCDProject(info);
    }
}

void CodeDesigner::DoCreateCDProject(CDItemInfo& info)
{
    // add new virtual folder to the selected virtual directory
    wxString codedesignerVD;
    codedesignerVD = info.m_TreeFolderName.BeforeFirst(wxT(':'));

    m_mgr->CreateVirtualDirectory(codedesignerVD, wxT("codedesigner"));
    wxString templateFile(m_mgr->GetInstallDirectory() + wxT("/templates/codedesigner/"));
    // wxString
    // templateFile(wxT("/home/michal/Src/Projekty/CodeLite_dev/CLInstall/share/codelite/templates/codedesigner/"));

    switch(info.m_Type) {
    default:
    case cdtClassDiagram:
        templateFile << wxT("CDTemplate.cdp");
        break;
    case cdtStateChart:
        if(info.m_UseMain)
            templateFile << wxT("SCHTemplateWithMain.cdp");
        else
            templateFile << wxT("SCHTemplate.cdp");
        break;
    case cdtHierarchicalStateChart:
        if(info.m_UseMain)
            templateFile << wxT("HSCHTemplateWithMain.cdp");
        else
            templateFile << wxT("HSCHTemplate.cdp");
        break;
    }

    wxFileName tmplFile(templateFile);
    if(!tmplFile.FileExists()) {
        wxMessageBox(wxString::Format(_("Can't find CodeDesigner template file '%s'"), tmplFile.GetFullPath().c_str()),
                     _("CodeLite"), wxOK | wxCENTER | wxICON_WARNING);
        return;
    }

    // place the files under the VD's project owner
    wxString err_msg;
    wxString project = info.m_TreeFolderName.BeforeFirst(wxT(':'));
    ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project, err_msg);
    if(proj) {
        wxString files_path = proj->GetFileName().GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME);
        // copy the file to here
        wxFileName cdpFile(files_path, info.m_DiagramName.Lower() + wxT(".cdp"));
        if(!wxCopyFile(tmplFile.GetFullPath(), cdpFile.GetFullPath())) {
            wxMessageBox(wxString::Format(_("Failed to copy template file to '%s'"), cdpFile.GetFullPath().c_str()),
                         _("CodeLite"), wxOK | wxCENTER | wxICON_WARNING);
            return;
        }
        // open the file, and replace expand its macros
        wxString content;
        if(!ReadFileWithConversion(cdpFile.GetFullPath().c_str(), content)) {
            wxMessageBox(wxString::Format(_("Failed to read file '%s'"), cdpFile.GetFullPath().c_str()), _("CodeLite"),
                         wxOK | wxCENTER | wxICON_WARNING);
            return;
        }

        wxString fileName = info.m_DiagramName.Lower();

        content.Replace(wxT("$(BaseFileName)"), fileName);
        content.Replace(wxT("$(CIFileName)"), fileName + wxT("_ci"));
        content.Replace(wxT("$(PackageName)"), info.m_PackageName);
        content.Replace(wxT("$(DiagramName)"), info.m_DiagramName);
        content.Replace(wxT("$(Language)"), info.m_Language);

        if(!WriteFileWithBackup(cdpFile.GetFullPath().c_str(), content, false)) {
            wxMessageBox(wxString::Format(_("Failed to write file '%s'"), cdpFile.GetFullPath().c_str()), _("CodeLite"),
                         wxOK | wxCENTER | wxICON_WARNING);
            return;
        }

        // add the file to the project
        wxArrayString paths;
        paths.Add(cdpFile.GetFullPath());
        m_mgr->AddFilesToVirtualFolder(project + wxT(":codedesigner"), paths);

        if(info.m_GenerateCode) {
            // first we launch CodeDesigner with the -g flag set
            wxString genFileCmd;
            genFileCmd << GetCDPath() << wxT(" -g ") << cdpFile.GetFullPath();

            wxArrayString dummy;
            ProcUtils::SafeExecuteCommand(genFileCmd, dummy);

            wxFileName cFile(cdpFile.GetPath(), fileName + wxT(".c"));
            wxFileName cppFile(cdpFile.GetPath(), fileName + wxT(".cpp"));
            wxFileName cCiFile(cdpFile.GetPath(), fileName + wxT("_ci.c"));
            wxFileName cppCiFile(cdpFile.GetPath(), fileName + wxT("_ci.cpp"));
            wxFileName headerFile(cdpFile.GetPath(), fileName + wxT(".h"));
            wxFileName headerCiFile(cdpFile.GetPath(), fileName + wxT("_ci.h"));
            wxFileName pyFile(cdpFile.GetPath(), fileName + wxT(".py"));
            wxFileName pyCiFile(cdpFile.GetPath(), fileName + wxT("_ci.py"));

            paths.Clear();

            if(cFile.FileExists()) paths.Add(cFile.GetFullPath());
            if(cppFile.FileExists()) paths.Add(cppFile.GetFullPath());
            if(headerFile.FileExists()) paths.Add(headerFile.GetFullPath());
            if(cCiFile.FileExists()) paths.Add(cCiFile.GetFullPath());
            if(cppCiFile.FileExists()) paths.Add(cppCiFile.GetFullPath());
            if(headerCiFile.FileExists()) paths.Add(headerCiFile.GetFullPath());
            if(pyFile.FileExists()) paths.Add(pyFile.GetFullPath());
            if(pyCiFile.FileExists()) paths.Add(pyCiFile.GetFullPath());

            if(paths.GetCount()) {
                m_mgr->AddFilesToVirtualFolder(info.m_TreeFolderName, paths);
            }

            RetagWorkspace();
        }

        DoLaunchCD(cdpFile.GetFullPath());
    }
}

void CodeDesigner::StartServer()
{
    ConfCodeDesigner data;
    m_mgr->GetConfigTool()->ReadObject(wxT("CodeDesigner"), &data);

    delete m_Server;

    m_Server = new CDServer();
    if(!m_Server->Create(data.GetPort())) {
        delete m_Server;
        m_Server = NULL;

        // wxMessageBox( _("Unable to create CodeDesigner plugin's IPC server. Please, check communication port value"),
        // _("CodeLite"), wxOK | wxICON_WARNING );
    }
}

void CodeDesigner::RetagWorkspace()
{
    wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("retag_workspace"));
    m_mgr->GetTheApp()->GetTopWindow()->GetEventHandler()->AddPendingEvent(e);
}

bool CodeDesigner::AddFilesToWorkspace(wxArrayString& paths, CDConnection* connection)
{
    // get tree folder if not specified
    if(connection->GetTreeFolder().IsEmpty()) {
        m_mgr->GetTheApp()->GetTopWindow()->Raise();

        VirtualDirectorySelector dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr->GetWorkspace(), wxT(""));
        dlg.SetTitle(_("Insert generated files into..."));

        if(dlg.ShowModal() == wxID_OK) {
            connection->SetTreeFolder(dlg.GetVirtualDirectoryPath());
        } else
            return false;
    }

    // check whether given files are already included in the project
    wxArrayString arrFilesToAdd;

    wxString err_msg;
    wxString project = connection->GetTreeFolder().BeforeFirst(wxT(':'));
    ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project, err_msg);
    if(proj) {
        std::vector<wxFileName> vctPresentFiles;
        proj->GetFiles(vctPresentFiles, true);

        for(size_t i = 0; i < paths.GetCount(); ++i) {
            wxFileName file(paths[i]);
            if(std::find(vctPresentFiles.begin(), vctPresentFiles.end(), file) == vctPresentFiles.end()) {
                arrFilesToAdd.Add(paths[i]);
            }
        }
    }

    // update CodeLite workspace
    if(!arrFilesToAdd.IsEmpty()) m_mgr->AddFilesToVirtualFolder(connection->GetTreeFolder(), arrFilesToAdd);

    RetagWorkspace();

    return true;
}

void CodeDesigner::OnPokeData(wxCommandEvent& e)
{
    wxArrayString arrFiles;
    wxStringTokenizer tokens(e.GetString(), wxT("\n"), wxTOKEN_STRTOK);

    while(tokens.HasMoreTokens()) {
        arrFiles.Add(tokens.GetNextToken());
    }

    AddFilesToWorkspace(arrFiles, (CDConnection*)e.GetClientData());
}

// IPC /////////////////////////////////////////////////////////////////////////

CDConnection::CDConnection(CDServer* server) { m_Server = server; }

CDConnection::~CDConnection() { Disconnect(); }

bool CDConnection::OnDisconnect()
{
    m_Server->Disconnect(this);
    return true;
}

bool CDConnection::OnPoke(const wxString& topic, const wxString& item, wxChar* data, int size, wxIPCFormat format)
{
    if(topic == TOPIC) {
        if(item == wxT("ADD FILES")) {
            wxCommandEvent e(wxEVT_IPC_POKE, wxID_ANY);
            e.SetString(wxString(data));
            e.SetClientData(this);

            wxPostEvent(thePlugin, e);
        }

        return true;
    } else
        return false;
}

CDServer::CDServer() { m_Connections.DeleteContents(true); }

CDServer::~CDServer() { m_Connections.Clear(); }

wxConnectionBase* CDServer::OnAcceptConnection(const wxString& topic)
{
    if(topic == TOPIC) {
        CDConnection* connection = new CDConnection(this);
        m_Connections.Append(connection);

        return connection;
    } else
        return NULL;
}

void CDServer::Disconnect(CDConnection* connection)
{
    if(connection) {
        m_Connections.DeleteObject(connection);
    }
}