Menu

[r6913]: / branches / wxsmith_addons / src / sdk / toolsmanager.cpp  Maximize  Restore  History

Download this file

407 lines (346 with data), 11.7 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
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
/*
* This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
* http://www.gnu.org/licenses/lgpl-3.0.html
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include "sdk_precomp.h"
#ifndef CB_PRECOMP
#include <wx/intl.h>
#include <wx/process.h>
#include <wx/menu.h>
#include <wx/msgdlg.h>
#include "toolsmanager.h"
#include "manager.h"
#include "macrosmanager.h"
#include "configmanager.h"
#include "logmanager.h"
#include "configmanager.h"
#include "pipedprocess.h"
#include "globals.h"
#include "sdk_events.h"
#endif
#include <wx/mdi.h>
#include <wx/listimpl.cpp>
#include "configuretoolsdlg.h"
template<> ToolsManager* Mgr<ToolsManager>::instance = 0;
template<> bool Mgr<ToolsManager>::isShutdown = false;
WX_DEFINE_LIST(ToolsList);
const int idToolsConfigure = wxNewId();
const int idToolProcess = wxNewId();
BEGIN_EVENT_TABLE(ToolsManager, wxEvtHandler)
EVT_MENU(idToolsConfigure, ToolsManager::OnConfigure)
EVT_IDLE(ToolsManager::OnIdle)
EVT_PIPEDPROCESS_STDOUT(idToolProcess, ToolsManager::OnToolStdOutput)
EVT_PIPEDPROCESS_STDERR(idToolProcess, ToolsManager::OnToolErrOutput)
EVT_PIPEDPROCESS_TERMINATED(idToolProcess, ToolsManager::OnToolTerminated)
END_EVENT_TABLE()
ToolsManager::ToolsManager()
: m_Menu(0L),
m_pProcess(0L),
m_Pid(0)
{
LoadTools();
Manager::Get()->GetAppWindow()->PushEventHandler(this);
}
ToolsManager::~ToolsManager()
{
// this is a core manager, so it is removed when the app is shutting down.
// in this case, the app has already un-hooked us, so no need to do it ourselves...
// Manager::Get()->GetAppWindow()->RemoveEventHandler(this);
m_ItemsManager.Clear();
// free-up any memory used for tools
m_Tools.DeleteContents(true);
m_Tools.Clear();
}
void ToolsManager::CreateMenu(wxMenuBar* menuBar)
{
}
void ToolsManager::ReleaseMenu(wxMenuBar* menuBar)
{
}
bool ToolsManager::Execute(const cbTool* tool)
{
if (m_pProcess)
{
cbMessageBox(_("Another tool is currently executing.\n"
"Please allow for it to finish before launching another tool..."),
_("Error"), wxICON_ERROR);
return false;
}
if (!tool)
return false;
wxString cmdline;
wxString cmd = tool->GetCommand();
wxString params = tool->GetParams();
wxString dir = tool->GetWorkingDir();
// hack to force-update macros
Manager::Get()->GetMacrosManager()->RecalcVars(0, 0, 0);
Manager::Get()->GetMacrosManager()->ReplaceMacros(cmd);
Manager::Get()->GetMacrosManager()->ReplaceMacros(params);
Manager::Get()->GetMacrosManager()->ReplaceMacros(dir);
if (tool->GetLaunchOption() == cbTool::LAUNCH_NEW_CONSOLE_WINDOW)
{
#ifndef __WXMSW__
// for non-win platforms, use m_ConsoleTerm to run the console app
wxString term = Manager::Get()->GetConfigManager(_T("app"))->Read(_T("/console_terminal"), DEFAULT_CONSOLE_TERM);
term.Replace(_T("$TITLE"), _T("'") + tool->GetName() + _T("'"));
cmdline << term << _T(" ");
#define CONSOLE_RUNNER "cb_console_runner"
#else
#define CONSOLE_RUNNER "cb_console_runner.exe"
#endif
wxString baseDir = ConfigManager::GetExecutableFolder();
if (wxFileExists(baseDir + wxT("/" CONSOLE_RUNNER)))
cmdline << baseDir << wxT("/" CONSOLE_RUNNER " ");
}
if (!cmdline.Replace(_T("$SCRIPT"), cmd << _T(" ") << params))
// if they didn't specify $SCRIPT, append:
cmdline << cmd;
if(!(Manager::Get()->GetMacrosManager()))
return false; // We cannot afford the Macros Manager to fail here!
// What if it failed already?
wxSetWorkingDirectory(dir);
// log info so user can troubleshoot
dir = wxGetCwd(); // read in the actual working dir
#if wxCHECK_VERSION(2, 9, 0)
Manager::Get()->GetLogManager()->Log(F(_("Launching tool '%s': %s (in %s)"), tool->GetName().wx_str(), cmdline.wx_str(), dir.wx_str()));
#else
Manager::Get()->GetLogManager()->Log(F(_("Launching tool '%s': %s (in %s)"), tool->GetName().c_str(), cmdline.c_str(), dir.c_str()));
#endif
bool pipe = true;
int flags = wxEXEC_ASYNC;
switch (tool->GetLaunchOption())
{
case cbTool::LAUNCH_NEW_CONSOLE_WINDOW:
pipe = false; // no need to pipe output channels...
break;
case cbTool::LAUNCH_HIDDEN:
break; // use the default values of pipe and flags...
case cbTool::LAUNCH_VISIBLE:
case cbTool::LAUNCH_VISIBLE_DETACHED:
flags |= wxEXEC_NOHIDE;
pipe = false;
break;
}
if (tool->GetLaunchOption() == cbTool::LAUNCH_VISIBLE_DETACHED)
{
int pid = wxExecute(cmdline, flags);
if (!pid)
{
cbMessageBox(_("Couldn't execute tool. Check the log for details."), _("Error"), wxICON_ERROR);
return false;
}
else
{
CodeBlocksLogEvent evtSwitch(cbEVT_SWITCH_TO_LOG_WINDOW, LogManager::app_log);
Manager::Get()->ProcessEvent(evtSwitch); // switch to default log
}
}
else
{
m_pProcess = new PipedProcess((void**)&m_pProcess, this, idToolProcess, pipe, dir);
m_Pid = wxExecute(cmdline, flags, m_pProcess);
if (!m_Pid)
{
cbMessageBox(_("Couldn't execute tool. Check the log for details."), _("Error"), wxICON_ERROR);
delete m_pProcess;
m_pProcess = 0;
m_Pid = 0;
return false;
}
else
{
CodeBlocksLogEvent evtSwitch(cbEVT_SWITCH_TO_LOG_WINDOW, LogManager::app_log);
Manager::Get()->ProcessEvent(evtSwitch); // switch to default log
}
}
return true;
} // end of Execute
void ToolsManager::AddTool(const cbTool* tool, bool save)
{
if (tool)
{
InsertTool(m_Tools.GetCount(), tool, save);
}
} // end of AddTool
void ToolsManager::InsertTool(int position, const cbTool* tool, bool save)
{
m_Tools.Insert(position, new cbTool(*tool));
if (save)
{
SaveTools();
}
} // end of InsertTool
void ToolsManager::RemoveToolByIndex(int index)
{
int idx = 0;
for (ToolsList::Node* node = m_Tools.GetFirst(); node; node = node->GetNext())
{
if (idx == index)
{
DoRemoveTool(node);
return;
}
++idx;
}
}
void ToolsManager::DoRemoveTool(ToolsList::Node* node)
{
if (node)
{
m_Tools.DeleteNode(node);
SaveTools();
}
}
cbTool* ToolsManager::GetToolByMenuId(int id)
{
for (ToolsList::Node* node = m_Tools.GetFirst(); node; node = node->GetNext())
{
cbTool* tool = node->GetData();
if (tool->GetMenuId() == id)
return tool;
}
return 0L;
}
cbTool* ToolsManager::GetToolByIndex(int index)
{
int idx = 0;
for (ToolsList::Node* node = m_Tools.GetFirst(); node; node = node->GetNext())
{
cbTool* tool = node->GetData();
if (idx == index)
return tool;
++idx;
}
return 0L;
}
void ToolsManager::LoadTools()
{
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("tools"));
wxArrayString list = cfg->EnumerateSubPaths(_("/"));
for (unsigned int i = 0; i < list.GetCount(); ++i)
{
cbTool tool;
tool.SetName( cfg->Read(_T("/") + list[i] + _T("/name")));
if (tool.GetName().IsEmpty())
continue;
tool.SetCommand(cfg->Read(_T("/") + list[i] + _T("/command")));
if (tool.GetCommand().IsEmpty())
continue;
tool.SetParams(cfg->Read(_T("/") + list[i] + _T("/params")));
tool.SetWorkingDir(cfg->Read(_T("/") + list[i] + _T("/workingDir")));
tool.SetLaunchOption(static_cast<cbTool::eLaunchOption>(cfg->ReadInt(_T("/") + list[i] + _T("/launchOption"))));
AddTool(&tool, false);
}
Manager::Get()->GetLogManager()->Log(F(_("Configured %d tools"), m_Tools.GetCount()));
}
void ToolsManager::SaveTools()
{
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("tools"));
wxArrayString list = cfg->EnumerateSubPaths(_("/"));
for (unsigned int i = 0; i < list.GetCount(); ++i)
{
cfg->DeleteSubPath(list[i]);
}
int count = 0;
for (ToolsList::Node* node = m_Tools.GetFirst(); node; node = node->GetNext())
{
cbTool* tool = node->GetData();
wxString elem;
// prepend a 0-padded 2-digit number to keep ordering
wxString tmp;
tmp.Printf(_T("tool%2.2d"), count++);
elem << _T("/") << tmp << _T("/");
cfg->Write(elem + _T("name"), tool->GetName());
cfg->Write(elem + _T("command"), tool->GetCommand());
cfg->Write(elem + _T("params"), tool->GetParams());
cfg->Write(elem + _T("workingDir"), tool->GetWorkingDir());
cfg->Write(elem + _T("launchOption"), static_cast<int>(tool->GetLaunchOption()));
}
}
void ToolsManager::BuildToolsMenu(wxMenu* menu)
{
// clear previously added menu items
m_ItemsManager.Clear();
// add menu items for tools
m_Menu = menu;
if (m_Menu->GetMenuItemCount() > 0)
{
m_ItemsManager.Add(menu, wxID_SEPARATOR, _T(""), _T(""));
}
for (ToolsList::Node* node = m_Tools.GetFirst(); node; node = node->GetNext())
{
cbTool* tool = node->GetData();
if (tool->GetName() == CB_TOOLS_SEPARATOR)
{
m_ItemsManager.Add(menu, wxID_SEPARATOR, _T(""), _T(""));
continue;
}
if (tool->GetMenuId() == -1)
{
tool->SetMenuId(wxNewId());
}
m_ItemsManager.Add(menu, tool->GetMenuId(), tool->GetName(), tool->GetName());
Connect(tool->GetMenuId(), -1, wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction)
&ToolsManager::OnToolClick);
}
if (m_Tools.GetCount() > 0)
{
m_ItemsManager.Add(menu, wxID_SEPARATOR, _T(""), _T(""));
}
m_ItemsManager.Add(menu, idToolsConfigure, _("&Configure tools..."), _("Add/remove user-defined tools"));
}
int ToolsManager::Configure()
{
CodeBlocksEvent event(cbEVT_MENUBAR_CREATE_BEGIN);
Manager::Get()->ProcessEvent(event);
ConfigureToolsDlg dlg(Manager::Get()->GetAppWindow());
PlaceWindow(&dlg);
dlg.ShowModal();
SaveTools();
BuildToolsMenu(m_Menu);
CodeBlocksEvent event2(cbEVT_MENUBAR_CREATE_END);
Manager::Get()->ProcessEvent(event2);
return 0;
} // end of Configure
// events
void ToolsManager::OnConfigure(wxCommandEvent& event)
{
Configure();
}
void ToolsManager::OnToolClick(wxCommandEvent& event)
{
cbTool* tool = GetToolByMenuId(event.GetId());
if (!Execute(tool))
cbMessageBox(_("Could not execute ") + tool->GetName());
}
void ToolsManager::OnIdle(wxIdleEvent& event)
{
if (m_pProcess)
{
if (m_pProcess->HasInput())
{
event.RequestMore();
}
}
else
event.Skip();
}
void ToolsManager::OnToolStdOutput(CodeBlocksEvent& event)
{
Manager::Get()->GetLogManager()->Log(_T("stdout> ") + event.GetString());
}
void ToolsManager::OnToolErrOutput(CodeBlocksEvent& event)
{
Manager::Get()->GetLogManager()->Log(_T("stderr> ") + event.GetString());
}
void ToolsManager::OnToolTerminated(CodeBlocksEvent& event)
{
m_Pid = 0;
m_pProcess = 0;
Manager::Get()->GetLogManager()->Log(F(_T("Tool execution terminated with status %d"), event.GetInt()));
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.