Code::Blocks  SVN r11506
scriptingsettingsdlg.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 10912 $
6  * $Id: scriptingsettingsdlg.cpp 10912 2016-09-25 16:10:13Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/src/scriptingsettingsdlg.cpp $
8  */
9 
10 #include <sdk.h>
11 #include "scriptingsettingsdlg.h"
12 
13 #include "manager.h"
14 #include "configmanager.h"
15 #include "filefilters.h"
16 
17 #ifndef CB_PRECOMP
18  #include "scriptingmanager.h"
19 #endif
20 
21 #include <wx/textctrl.h>
22 #include <wx/button.h>
23 #include <wx/checkbox.h>
24 #include <wx/xrc/xmlres.h>
25 #include <wx/filedlg.h>
26 #include <wx/filename.h>
27 #include <wx/intl.h>
28 
29 BEGIN_EVENT_TABLE(ScriptingSettingsDlg, wxScrollingDialog)
30  EVT_LIST_ITEM_SELECTED(XRCID("chkStartupScripts"), ScriptingSettingsDlg::OnListSelection)
31  EVT_LIST_ITEM_DESELECTED(XRCID("chkStartupScripts"), ScriptingSettingsDlg::OnListDeselection)
32  EVT_TEXT(XRCID("txtScript"), ScriptingSettingsDlg::OnScriptChanged)
33  EVT_TEXT(XRCID("txtScriptMenu"), ScriptingSettingsDlg::OnScriptMenuChanged)
34  EVT_BUTTON(XRCID("btnAdd"), ScriptingSettingsDlg::OnAddScript)
35  EVT_BUTTON(XRCID("btnDelete"), ScriptingSettingsDlg::OnRemoveScript)
36  EVT_BUTTON(XRCID("btnBrowseScript"), ScriptingSettingsDlg::OnBrowse)
37  EVT_CHECKBOX(XRCID("chkEnableScript"), ScriptingSettingsDlg::OnEnable)
38  EVT_CHECKBOX(XRCID("chkRegisterScript"), ScriptingSettingsDlg::OnRegister)
39 
40  EVT_LIST_ITEM_SELECTED(XRCID("lstTrustedScripts"), ScriptingSettingsDlg::OnTrustSelection)
41  EVT_LIST_ITEM_DESELECTED(XRCID("lstTrustedScripts"), ScriptingSettingsDlg::OnTrustSelection)
42  EVT_BUTTON(XRCID("btnValidateTrusts"), ScriptingSettingsDlg::OnValidateTrusts)
43  EVT_BUTTON(XRCID("btnDeleteTrust"), ScriptingSettingsDlg::OnDeleteTrust)
44 END_EVENT_TABLE()
45 
47  : m_IgnoreTextEvents(false)
48 {
49  //ctor
50  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgScriptingSettings"),_T("wxScrollingDialog"));
51  XRCCTRL(*this, "wxID_OK", wxButton)->SetDefault();
52 
53  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
54  list->InsertColumn(0, _("Script"), wxLIST_FORMAT_LEFT, 160);
55  list->InsertColumn(1, _("Enabled"), wxLIST_FORMAT_LEFT, 64);
56  list->InsertColumn(2, _("Menu"), wxLIST_FORMAT_LEFT, 160);
57 
58  FillScripts();
59 
60  list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
61  list->InsertColumn(0, _("Script"), wxLIST_FORMAT_LEFT, 160);
62  list->InsertColumn(1, _("Signature"));
63  list->InsertColumn(2, _("Temp?"));
64 
65  FillTrusts();
66 }
67 
69 {
70  //dtor
71 }
72 
74 {
75  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
76  list->DeleteAllItems();
77 
78  ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("scripting"));
79  wxArrayString keys = mgr->EnumerateKeys(_T("/startup_scripts"));
80 
81  for (size_t i = 0; i < keys.GetCount(); ++i)
82  {
83  ScriptEntry se;
84  wxString ser;
85  if (mgr->Read(_T("/startup_scripts/") + keys[i], &ser))
86  {
87  se.SerializeIn(ser);
88  m_ScriptsVector.push_back(se);
89 
90  long item = list->InsertItem(list->GetItemCount(), se.script);
91  list->SetItem(item, 1, se.enabled ? _("Yes") : _("No"));
92  list->SetItem(item, 2, se.registered && !se.menu.IsEmpty() ? se.menu : wxString(wxEmptyString));
93  }
94  }
95 
96  UpdateState();
97 }
98 
100 {
101  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
102  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
103 
104  bool en = sel != -1;
105 
106  const ScriptEntry& se = m_ScriptsVector[sel];
107 
108  XRCCTRL(*this, "btnDelete", wxButton)->Enable(en);
109  XRCCTRL(*this, "chkEnableScript", wxCheckBox)->Enable(en);
110  XRCCTRL(*this, "txtScript", wxTextCtrl)->Enable(en && se.enabled);
111  XRCCTRL(*this, "btnBrowseScript", wxButton)->Enable(en && se.enabled);
112  XRCCTRL(*this, "chkRegisterScript", wxCheckBox)->Enable(en && se.enabled);
113  XRCCTRL(*this, "txtScriptMenu", wxTextCtrl)->Enable(en && se.enabled && se.registered);
114 }
115 
117 {
118  wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
119  list->DeleteAllItems();
120 
122  ScriptingManager::TrustedScripts::const_iterator it;
123  for (it = trusts.begin(); it != trusts.end(); ++it)
124  {
125  const ScriptingManager::TrustedScriptProps& props = it->second;
126 
127  long item = list->InsertItem(list->GetItemCount(), it->first);
128  list->SetItem(item, 1, wxString::Format(_T("%x"), props.crc));
129  list->SetItem(item, 2, !props.permanent ? _("Yes") : _(""));
130  }
131 
133 
134  // fill main switches
135  ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("security"));
136  XRCCTRL(*this, "chkMkDir", wxCheckBox)->SetValue(mgr->ReadBool(_T("CreateDir"), false));
137  XRCCTRL(*this, "chkRmDir", wxCheckBox)->SetValue(mgr->ReadBool(_T("RemoveDir"), false));
138  XRCCTRL(*this, "chkCp", wxCheckBox)->SetValue(mgr->ReadBool(_T("CopyFile"), false));
139  XRCCTRL(*this, "chkMv", wxCheckBox)->SetValue(mgr->ReadBool(_T("RenameFile"), false));
140  XRCCTRL(*this, "chkRm", wxCheckBox)->SetValue(mgr->ReadBool(_T("RemoveFile"), false));
141  XRCCTRL(*this, "chkTouch", wxCheckBox)->SetValue(mgr->ReadBool(_T("CreateFile"), false));
142  XRCCTRL(*this, "chkExec", wxCheckBox)->SetValue(mgr->ReadBool(_T("Execute"), false));
143 }
144 
146 {
147  wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
148  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
149 
150  bool en = sel != -1;
151 
152  XRCCTRL(*this, "btnDeleteTrust", wxButton)->Enable(en);
153  XRCCTRL(*this, "btnValidateTrusts", wxButton)->Enable(en);
154 }
155 
157 {
158  if (retCode == wxID_OK)
159  {
160  ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("scripting"));
161  mgr->DeleteSubPath(_T("/startup_scripts"));
162 
163  ScriptsVector::iterator it;
164  int i = 0;
165  for (it = m_ScriptsVector.begin(); it != m_ScriptsVector.end(); ++it, ++i)
166  {
167  ScriptEntry& se = *it;
168  wxString key = wxString::Format(_T("/startup_scripts/script%d"), i);
169  mgr->Write(key, se.SerializeOut());
170  }
171 
172  mgr = Manager::Get()->GetConfigManager(_T("security"));
173  mgr->Write(_T("CreateDir"), XRCCTRL(*this, "chkMkDir", wxCheckBox)->GetValue());
174  mgr->Write(_T("RemoveDir"), XRCCTRL(*this, "chkRmDir", wxCheckBox)->GetValue());
175  mgr->Write(_T("CopyFile"), XRCCTRL(*this, "chkCp", wxCheckBox)->GetValue());
176  mgr->Write(_T("RenameFile"), XRCCTRL(*this, "chkMv", wxCheckBox)->GetValue());
177  mgr->Write(_T("RemoveFile"), XRCCTRL(*this, "chkRm", wxCheckBox)->GetValue());
178  mgr->Write(_T("CreateFile"), XRCCTRL(*this, "chkTouch", wxCheckBox)->GetValue());
179  mgr->Write(_T("Execute"), XRCCTRL(*this, "chkExec", wxCheckBox)->GetValue());
180  }
181 
183 }
184 
186 {
187  m_IgnoreTextEvents = true;
188 
189  // load
190  ScriptEntry& se = m_ScriptsVector[item];
191 
192  XRCCTRL(*this, "chkEnableScript", wxCheckBox)->SetValue(se.enabled);
193  XRCCTRL(*this, "txtScript", wxTextCtrl)->SetValue(se.script);
194  XRCCTRL(*this, "chkRegisterScript", wxCheckBox)->SetValue(se.registered);
195  XRCCTRL(*this, "txtScriptMenu", wxTextCtrl)->SetValue(se.menu);
196 
197  m_IgnoreTextEvents = false;
198 }
199 
201 {
202  m_IgnoreTextEvents = true;
203 
204  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
205  ScriptEntry& se = m_ScriptsVector[item];
206 
207  se.enabled = XRCCTRL(*this, "chkEnableScript", wxCheckBox)->GetValue();
208  se.script = XRCCTRL(*this, "txtScript", wxTextCtrl)->GetValue();
209  se.registered = XRCCTRL(*this, "chkRegisterScript", wxCheckBox)->GetValue();
210  se.menu = XRCCTRL(*this, "txtScriptMenu", wxTextCtrl)->GetValue();
211 
212  // update view
213  list->SetItem(item, 0, se.script);
214  list->SetItem(item, 1, se.enabled ? _("Yes") : _("No"));
215  list->SetItem(item, 2, se.registered && !se.menu.IsEmpty() ? se.menu : wxString(wxEmptyString));
216 
217  m_IgnoreTextEvents = false;
218 }
219 
221 {
222 // Manager::Get()->GetLogManager()->DebugLog(F(_T("Selected %d"), event.GetIndex()));
223 
224  // load
225  long sel = event.GetIndex();
226  LoadItem(sel);
227 
228  UpdateState();
229 }
230 
232 {
233 // Manager::Get()->GetLogManager()->DebugLog(F(_T("Deselected %d"), event.GetIndex()));
234 
235  // save
236  long sel = event.GetIndex();
237  SaveItem(sel);
238 
239  UpdateState();
240 }
241 
243 {
244  if (m_IgnoreTextEvents)
245  return;
246  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
247  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
248  SaveItem(sel);
249  UpdateState();
250 }
251 
253 {
254  if (m_IgnoreTextEvents)
255  return;
256  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
257  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
258  SaveItem(sel);
259  UpdateState();
260 }
261 
263 {
264  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
265  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
266  SaveItem(sel);
267  UpdateState();
268 }
269 
271 {
272  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
273  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
274  SaveItem(sel);
275  UpdateState();
276 }
277 
279 {
280  ScriptEntry se;
281  se.script = _T("new.script");
282  se.enabled = true;
283  se.registered = false;
284  m_ScriptsVector.push_back(se);
285 
286  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
287 
288  // update view
289  long item = list->InsertItem(list->GetItemCount(), se.script);
290  list->SetItem(item, 1, _("No"));
291  list->SetItem(item, 2, wxString(_("No")));
292 
294 
295  OnBrowse(event);
296 }
297 
299 {
300  wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
301  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
302 
303  list->DeleteItem(sel);
304  m_ScriptsVector.erase(m_ScriptsVector.begin() + sel);
305 
306  if (sel > list->GetItemCount())
307  --sel;
309  if (sel >= 0)
310  LoadItem(sel);
311  UpdateState();
312 }
313 
315 {
316  wxFileDialog dlg(this,
317  _("Select script file"),
318  XRCCTRL(*this, "txtScript", wxTextCtrl)->GetValue(),
319  XRCCTRL(*this, "txtScript", wxTextCtrl)->GetValue(),
320  FileFilters::GetFilterString(_T(".script")),
321  wxFD_OPEN | compatibility::wxHideReadonly );
322  PlaceWindow(&dlg);
323  if (dlg.ShowModal() == wxID_OK)
324  {
325  wxString sel = UnixFilename(dlg.GetPath());
328  wxFileName f(sel);
329  if (sel.StartsWith(userdir))
330  {
331  f.MakeRelativeTo(userdir);
332  }
333  else if (sel.StartsWith(globaldir))
334  {
335  f.MakeRelativeTo(globaldir);
336  }
337  XRCCTRL(*this, "txtScript", wxTextCtrl)->SetValue(f.GetFullPath());
338  }
339 }
340 
342 {
344 }
345 
347 {
348  wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
349  long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
350 
351  wxString script = list->GetItemText(sel);
353  list->DeleteItem(sel);
354 
356 }
357 
359 {
360  bool check = true;
361  wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
362  for (int i = 0; i < list->GetItemCount(); ++i)
363  {
364  wxString script = list->GetItemText(i);
365  if (!Manager::Get()->GetScriptingManager()->IsScriptTrusted(script))
366  check = false;
367  }
368 
369  if (check)
370  cbMessageBox(_("All script trusts are valid!"), _("Information"), wxICON_INFORMATION, this);
371 }
void EndModal(int retCode)
Definition: sc_dialog.cpp:112
static wxString GetFolder(SearchDirs dir)
Access one of Code::Blocks&#39; folders.
virtual int ShowModal()
void SerializeIn(const wxString &s)
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
long GetNextItem(long item, int geometry=wxLIST_NEXT_ALL, int state=wxLIST_STATE_DONTCARE) const
void OnScriptChanged(wxCommandEvent &event)
void OnListDeselection(wxListEvent &event)
Scripts folder in base dir.
Definition: configmanager.h:80
void OnAddScript(wxCommandEvent &event)
bool ReadBool(const wxString &name, bool defaultVal=false)
void OnScriptMenuChanged(wxCommandEvent &event)
Scripts folder in user&#39;s dir.
Definition: configmanager.h:74
const TrustedScripts & GetTrustedScripts()
Access the script trusts container (const).
#define wxLIST_STATE_SELECTED
#define _T(string)
void OnValidateTrusts(wxCommandEvent &event)
#define wxICON_INFORMATION
std::map< wxString, TrustedScriptProps > TrustedScripts
Script trusts container struct.
void OnBrowse(wxCommandEvent &event)
void OnTrustSelection(wxListEvent &event)
bool SetItemState(long item, long state, long stateMask)
bool MakeRelativeTo(const wxString &pathBase=wxEmptyString, wxPathFormat format=wxPATH_NATIVE)
long InsertItem(wxListItem &info)
void Write(const wxString &name, const wxString &value, bool ignoreEmpty=false)
DLLIMPORT wxString UnixFilename(const wxString &filename, wxPathFormat format=wxPATH_NATIVE)
Definition: globals.cpp:228
void OnListSelection(wxListEvent &event)
wxString Read(const wxString &key, const wxString &defaultVal=wxEmptyString)
bool DeleteAllItems()
int GetItemCount() const
wxString GetItemText(long item, int col=0) const
wxString wxEmptyString
virtual wxString GetPath() const
void OnRemoveScript(wxCommandEvent &event)
const wxString & _(const wxString &string)
bool DeleteItem(long item)
DLLIMPORT wxString GetFilterString(const wxString &ext=wxEmptyString)
Generates and returns the filter string for use in file dialogs.
Definition: filefilters.cpp:60
void DeleteSubPath(const wxString &strPath)
bool IsEmpty() const
DLLIMPORT void PlaceWindow(wxTopLevelWindow *w, cbPlaceDialogMode mode=pdlBest, bool enforce=false)
Definition: globals.cpp:1177
void OnDeleteTrust(wxCommandEvent &event)
long InsertColumn(long col, const wxListItem &info)
Script trusts container struct.
bool StartsWith(const wxString &prefix, wxString *rest=NULL) const
bool SetItem(wxListItem &info)
bool RemoveTrust(const wxString &script)
Remove a script trust.
wxString SerializeOut() const
size_t GetCount() const
static wxXmlResource * Get()
ScriptingManager * GetScriptingManager() const
Definition: manager.cpp:469
wxString GetFullPath(wxPathFormat format=wxPATH_NATIVE) const
wxArrayString EnumerateKeys(const wxString &path)
void OnEnable(wxCommandEvent &event)
static wxString Format(const wxString &format,...)
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
void OnRegister(wxCommandEvent &event)
DLLIMPORT int cbMessageBox(const wxString &message, const wxString &caption=wxEmptyString, int style=wxOK, wxWindow *parent=NULL, int x=-1, int y=-1)
wxMessageBox wrapper.
Definition: globals.cpp:1395
#define wxLIST_STATE_FOCUSED