~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/Cccc/Cccc.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************
 
2
 * Name:      Cccc.cpp
 
3
 * Purpose:   Code::Blocks Cccc plugin: main functions
 
4
 * Author:    Lieven de Cock (aka killerbot)
 
5
 * Created:   12/11/2009
 
6
 * Copyright: (c) Lieven de Cock (aka killerbot)
 
7
 * License:   GPL
 
8
  **************************************************************/
 
9
#include "sdk.h"
 
10
#ifndef CB_PRECOMP
 
11
#include <wx/arrstr.h>
 
12
#include <wx/dir.h>
 
13
#include <wx/fs_zip.h>
 
14
#include <wx/intl.h>
 
15
#include <wx/menu.h>
 
16
#include <wx/string.h>
 
17
#include <wx/xrc/xmlres.h>
 
18
#include "cbproject.h"
 
19
#include "manager.h"
 
20
#include "logmanager.h"
 
21
#include "projectmanager.h"
 
22
#endif
 
23
#include <wx/busyinfo.h>
 
24
#include <wx/filedlg.h>
 
25
#include <wx/filefn.h>
 
26
#include <wx/utils.h>
 
27
#include "loggers.h"
 
28
#include "Cccc.h"
 
29
 
 
30
// Register the plugin
 
31
namespace
 
32
{
 
33
    PluginRegistrant<Cccc> reg(_T("Cccc"));
 
34
};
 
35
 
 
36
Cccc::Cccc()
 
37
{
 
38
    if(!Manager::LoadResource(_T("Cccc.zip")))
 
39
    {
 
40
        NotifyMissingFile(_T("Cccc.zip"));
 
41
    }
 
42
    m_CcccLog = 0;
 
43
    m_LogPageIndex = 0; // good init value ???
 
44
    m_CcccApp = _T("cccc");
 
45
} // end of constructor
 
46
 
 
47
Cccc::~Cccc()
 
48
{
 
49
} // end of destruccor
 
50
 
 
51
void Cccc::OnAttach()
 
52
{
 
53
    // do whatever initialization you need for your plugin
 
54
    // NOTE: after this function, the inherited member variable
 
55
    // IsAttached() will be TRUE...
 
56
    // You should check for it in other functions, because if it
 
57
    // is FALSE, it means that the application did *not* "load"
 
58
    // (see: does not need) this plugin...
 
59
    if(LogManager* LogMan = Manager::Get()->GetLogManager())
 
60
    {
 
61
        m_CcccLog = new TextCtrlLogger();
 
62
        m_LogPageIndex = LogMan->SetLog(m_CcccLog);
 
63
        LogMan->Slot(m_LogPageIndex).title = _("Cccc");
 
64
        CodeBlocksLogEvent evtAdd1(cbEVT_ADD_LOG_WINDOW, m_CcccLog, LogMan->Slot(m_LogPageIndex).title);
 
65
        Manager::Get()->ProcessEvent(evtAdd1);
 
66
    }
 
67
} // end of OnAttach
 
68
 
 
69
void Cccc::OnRelease(bool /*appShutDown*/)
 
70
{
 
71
    // do de-initialization for your plugin
 
72
    // if appShutDown is false, the plugin is unloaded because Code::Blocks is being shut down,
 
73
    // which means you must not use any of the SDK Managers
 
74
    // NOTE: after this function, the inherited member variable
 
75
    // IsAttached() will be FALSE...
 
76
    if(Manager::Get()->GetLogManager())
 
77
    {
 
78
        if(m_CcccLog)
 
79
        {
 
80
            CodeBlocksLogEvent evt(cbEVT_REMOVE_LOG_WINDOW, m_CcccLog);
 
81
            Manager::Get()->ProcessEvent(evt);
 
82
        }
 
83
    }
 
84
    m_CcccLog = 0;
 
85
} // end of OnRelease
 
86
 
 
87
void Cccc::AppendToLog(const wxString& Text)
 
88
{
 
89
    if(LogManager* LogMan = Manager::Get()->GetLogManager())
 
90
    {
 
91
        CodeBlocksLogEvent evtSwitch(cbEVT_SWITCH_TO_LOG_WINDOW, m_CcccLog);
 
92
        Manager::Get()->ProcessEvent(evtSwitch);
 
93
        LogMan->Log(Text, m_LogPageIndex);
 
94
    }
 
95
} // end of AppendToLog
 
96
 
 
97
namespace
 
98
{
 
99
bool CheckRequirements()
 
100
{
 
101
    cbProject* Project = Manager::Get()->GetProjectManager()->GetActiveProject();
 
102
    // if no project open, exit
 
103
    if (!Project)
 
104
    {
 
105
        wxString msg = _("You need to open a project\nbefore using the plugin!");
 
106
        cbMessageBox(msg, _("Error"), wxICON_ERROR | wxOK, Manager::Get()->GetAppWindow());
 
107
        Manager::Get()->GetLogManager()->DebugLog(msg);
 
108
        return false;
 
109
    }
 
110
    return true;
 
111
}  // end of CheckRequirements
 
112
}
 
113
 
 
114
int Cccc::Execute()
 
115
{
 
116
    if(!CheckRequirements())
 
117
    {
 
118
        return -1;
 
119
    }
 
120
 
 
121
    cbProject* Project = Manager::Get()->GetProjectManager()->GetActiveProject();
 
122
    ::wxSetWorkingDirectory(Project->GetBasePath());
 
123
    wxString ListOfFileNames;
 
124
    for (FilesList::iterator it = Project->GetFilesList().begin(); it != Project->GetFilesList().end(); ++it)
 
125
    {
 
126
        ProjectFile* pf = *it;
 
127
        ListOfFileNames += _T("\"") + pf->relativeFilename + _T("\" ");
 
128
    }
 
129
 
 
130
    wxString CommandLine = m_CcccApp + _T(" ") + ListOfFileNames.Trim();
 
131
    AppendToLog(CommandLine);
 
132
    wxArrayString Output, Errors;
 
133
    long pid = -1;
 
134
    {
 
135
        wxWindowDisabler disableAll;
 
136
        wxBusyInfo running(_("Running cccc... please wait (this may take several minutes)..."),
 
137
                           Manager::Get()->GetAppWindow());
 
138
        pid = wxExecute(CommandLine, Output, Errors);
 
139
    } // end lifetime of wxWindowDisabler, wxBusyInfo
 
140
    if (pid==-1)
 
141
    {
 
142
        bool failed = true;
 
143
        if (cbMessageBox(_("Failed to lauch cccc.\nDo you want to select the cccc executable?"),
 
144
                         _("Question"), wxICON_QUESTION | wxYES_NO, Manager::Get()->GetAppWindow()) == wxID_YES)
 
145
        {
 
146
            wxString filename = wxFileSelector(_("Select the cccc executable"));
 
147
            if (!filename.empty()) // otherwise the user selected cancel
 
148
            {
 
149
                // try again using the user-provided executable
 
150
                CommandLine = filename + _T(" ") + ListOfFileNames.Trim();
 
151
                AppendToLog(CommandLine);
 
152
                {
 
153
                    wxWindowDisabler disableAll;
 
154
                    wxBusyInfo running(_("Running cccc... please wait (this may take several minutes)..."),
 
155
                                       Manager::Get()->GetAppWindow());
 
156
                    pid = wxExecute(CommandLine, Output, Errors);
 
157
                } // end lifetime of wxWindowDisabler, wxBusyInfo
 
158
                if (pid==-1)
 
159
                {
 
160
                    failed = true;
 
161
                }
 
162
                else
 
163
                {
 
164
                    m_CcccApp = filename;
 
165
                    failed = false;
 
166
                }
 
167
            }
 
168
        }
 
169
        if (failed)
 
170
        {
 
171
            AppendToLog(_("Failed to lauch cccc."));
 
172
            cbMessageBox(_("Failed to lauch cccc."), _("Error"), wxICON_ERROR | wxOK, Manager::Get()->GetAppWindow());
 
173
            return -1;
 
174
        }
 
175
    }
 
176
 
 
177
    size_t Count = Output.GetCount();
 
178
    for(size_t idxCount = 0; idxCount < Count; ++idxCount)
 
179
    {
 
180
        AppendToLog(Output[idxCount]);
 
181
    } // end for : idx: idxCount
 
182
    Count = Errors.GetCount();
 
183
    for(size_t idxCount = 0; idxCount < Count; ++idxCount)
 
184
    {
 
185
        AppendToLog(Errors[idxCount]);
 
186
    } // end for : idx: idxCount
 
187
    const wxString FileName = _T("./.cccc/cccc.html");
 
188
    if(wxFile::Exists(FileName))
 
189
    {
 
190
        if (cbMimePlugin* p = Manager::Get()->GetPluginManager()->GetMIMEHandlerForFile(FileName))
 
191
        {
 
192
            p->OpenFile(FileName);
 
193
        }
 
194
    }
 
195
 
 
196
    return 0;
 
197
} // end of Execute