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
11
#include "scriptingsettingsdlg.h"
14
#include "configmanager.h"
15
#include "filefilters.h"
18
#include "scriptingmanager.h"
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>
29
BEGIN_EVENT_TABLE(ScriptingSettingsDlg, wxDialog)
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)
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)
46
ScriptingSettingsDlg::ScriptingSettingsDlg(wxWindow* parent)
47
: m_IgnoreTextEvents(false)
50
wxXmlResource::Get()->LoadDialog(this, parent, _T("dlgScriptingSettings"));
52
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
53
list->InsertColumn(0, _("Script"), wxLIST_FORMAT_LEFT, 160);
54
list->InsertColumn(1, _("Enabled"), wxLIST_FORMAT_LEFT, 64);
55
list->InsertColumn(2, _("Menu"), wxLIST_FORMAT_LEFT, 160);
59
list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
60
list->InsertColumn(0, _("Script"), wxLIST_FORMAT_LEFT, 160);
61
list->InsertColumn(1, _("Signature"));
62
list->InsertColumn(2, _("Temp?"));
67
ScriptingSettingsDlg::~ScriptingSettingsDlg()
72
void ScriptingSettingsDlg::FillScripts()
74
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
75
list->DeleteAllItems();
77
ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("scripting"));
78
wxArrayString keys = mgr->EnumerateKeys(_T("/startup_scripts"));
80
for (size_t i = 0; i < keys.GetCount(); ++i)
84
if (mgr->Read(_T("/startup_scripts/") + keys[i], &ser))
87
m_ScriptsVector.push_back(se);
89
long item = list->InsertItem(list->GetItemCount(), se.script);
90
list->SetItem(item, 1, se.enabled ? _("Yes") : _("No"));
91
list->SetItem(item, 2, se.registered && !se.menu.IsEmpty() ? se.menu : wxString(wxEmptyString));
98
void ScriptingSettingsDlg::UpdateState()
100
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
101
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
105
const ScriptEntry& se = m_ScriptsVector[sel];
107
XRCCTRL(*this, "btnDelete", wxButton)->Enable(en);
108
XRCCTRL(*this, "chkEnableScript", wxCheckBox)->Enable(en);
109
XRCCTRL(*this, "txtScript", wxTextCtrl)->Enable(en && se.enabled);
110
XRCCTRL(*this, "btnBrowseScript", wxButton)->Enable(en && se.enabled);
111
XRCCTRL(*this, "chkRegisterScript", wxCheckBox)->Enable(en && se.enabled);
112
XRCCTRL(*this, "txtScriptMenu", wxTextCtrl)->Enable(en && se.enabled && se.registered);
115
void ScriptingSettingsDlg::FillTrusts()
117
wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
118
list->DeleteAllItems();
120
const ScriptingManager::TrustedScripts& trusts = Manager::Get()->GetScriptingManager()->GetTrustedScripts();
121
ScriptingManager::TrustedScripts::const_iterator it;
122
for (it = trusts.begin(); it != trusts.end(); ++it)
124
const ScriptingManager::TrustedScriptProps& props = it->second;
126
long item = list->InsertItem(list->GetItemCount(), it->first);
127
list->SetItem(item, 1, wxString::Format(_T("%x"), props.crc));
128
list->SetItem(item, 2, !props.permanent ? _("Yes") : _(""));
133
// fill main switches
134
ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("security"));
135
XRCCTRL(*this, "chkMkDir", wxCheckBox)->SetValue(mgr->ReadBool(_T("CreateDir"), false));
136
XRCCTRL(*this, "chkRmDir", wxCheckBox)->SetValue(mgr->ReadBool(_T("RemoveDir"), false));
137
XRCCTRL(*this, "chkCp", wxCheckBox)->SetValue(mgr->ReadBool(_T("CopyFile"), false));
138
XRCCTRL(*this, "chkMv", wxCheckBox)->SetValue(mgr->ReadBool(_T("RenameFile"), false));
139
XRCCTRL(*this, "chkRm", wxCheckBox)->SetValue(mgr->ReadBool(_T("RemoveFile"), false));
140
XRCCTRL(*this, "chkTouch", wxCheckBox)->SetValue(mgr->ReadBool(_T("CreateFile"), false));
141
XRCCTRL(*this, "chkExec", wxCheckBox)->SetValue(mgr->ReadBool(_T("Execute"), false));
144
void ScriptingSettingsDlg::UpdateTrustsState()
146
wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
147
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
151
XRCCTRL(*this, "btnDeleteTrust", wxButton)->Enable(en);
152
XRCCTRL(*this, "btnValidateTrusts", wxButton)->Enable(en);
155
void ScriptingSettingsDlg::EndModal(int retCode)
157
if (retCode == wxID_OK)
159
ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("scripting"));
160
mgr->DeleteSubPath(_T("/startup_scripts"));
162
ScriptsVector::iterator it;
164
for (it = m_ScriptsVector.begin(); it != m_ScriptsVector.end(); ++it, ++i)
166
ScriptEntry& se = *it;
167
wxString key = wxString::Format(_T("/startup_scripts/script%d"), i);
168
mgr->Write(key, se.SerializeOut());
171
mgr = Manager::Get()->GetConfigManager(_T("security"));
172
mgr->Write(_T("CreateDir"), XRCCTRL(*this, "chkMkDir", wxCheckBox)->GetValue());
173
mgr->Write(_T("RemoveDir"), XRCCTRL(*this, "chkRmDir", wxCheckBox)->GetValue());
174
mgr->Write(_T("CopyFile"), XRCCTRL(*this, "chkCp", wxCheckBox)->GetValue());
175
mgr->Write(_T("RenameFile"), XRCCTRL(*this, "chkMv", wxCheckBox)->GetValue());
176
mgr->Write(_T("RemoveFile"), XRCCTRL(*this, "chkRm", wxCheckBox)->GetValue());
177
mgr->Write(_T("CreateFile"), XRCCTRL(*this, "chkTouch", wxCheckBox)->GetValue());
178
mgr->Write(_T("Execute"), XRCCTRL(*this, "chkExec", wxCheckBox)->GetValue());
181
wxDialog::EndModal(retCode);
184
void ScriptingSettingsDlg::LoadItem(long item)
186
m_IgnoreTextEvents = true;
189
ScriptEntry& se = m_ScriptsVector[item];
191
XRCCTRL(*this, "chkEnableScript", wxCheckBox)->SetValue(se.enabled);
192
XRCCTRL(*this, "txtScript", wxTextCtrl)->SetValue(se.script);
193
XRCCTRL(*this, "chkRegisterScript", wxCheckBox)->SetValue(se.registered);
194
XRCCTRL(*this, "txtScriptMenu", wxTextCtrl)->SetValue(se.menu);
196
m_IgnoreTextEvents = false;
199
void ScriptingSettingsDlg::SaveItem(long item)
201
m_IgnoreTextEvents = true;
203
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
204
ScriptEntry& se = m_ScriptsVector[item];
206
se.enabled = XRCCTRL(*this, "chkEnableScript", wxCheckBox)->GetValue();
207
se.script = XRCCTRL(*this, "txtScript", wxTextCtrl)->GetValue();
208
se.registered = XRCCTRL(*this, "chkRegisterScript", wxCheckBox)->GetValue();
209
se.menu = XRCCTRL(*this, "txtScriptMenu", wxTextCtrl)->GetValue();
212
list->SetItem(item, 0, se.script);
213
list->SetItem(item, 1, se.enabled ? _("Yes") : _("No"));
214
list->SetItem(item, 2, se.registered && !se.menu.IsEmpty() ? se.menu : wxString(wxEmptyString));
216
m_IgnoreTextEvents = false;
219
void ScriptingSettingsDlg::OnListSelection(wxListEvent& event)
221
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Selected %d"), event.GetIndex()));
224
long sel = event.GetIndex();
230
void ScriptingSettingsDlg::OnListDeselection(wxListEvent& event)
232
// Manager::Get()->GetLogManager()->DebugLog(F(_T("Deselected %d"), event.GetIndex()));
235
long sel = event.GetIndex();
241
void ScriptingSettingsDlg::OnScriptChanged(wxCommandEvent& event)
243
if (m_IgnoreTextEvents)
245
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
246
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
251
void ScriptingSettingsDlg::OnScriptMenuChanged(wxCommandEvent& event)
253
if (m_IgnoreTextEvents)
255
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
256
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
261
void ScriptingSettingsDlg::OnEnable(wxCommandEvent& event)
263
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
264
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
269
void ScriptingSettingsDlg::OnRegister(wxCommandEvent& event)
271
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
272
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
277
void ScriptingSettingsDlg::OnAddScript(wxCommandEvent& event)
280
se.script = _T("new.script");
282
se.registered = false;
283
m_ScriptsVector.push_back(se);
285
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
288
long item = list->InsertItem(list->GetItemCount(), se.script);
289
list->SetItem(item, 1, _("No"));
290
list->SetItem(item, 2, wxString(_("No")));
292
list->SetItemState(item, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
297
void ScriptingSettingsDlg::OnRemoveScript(wxCommandEvent& event)
299
wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
300
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
302
list->DeleteItem(sel);
303
m_ScriptsVector.erase(m_ScriptsVector.begin() + sel);
305
if (sel > list->GetItemCount())
307
list->SetItemState(sel, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
313
void ScriptingSettingsDlg::OnBrowse(wxCommandEvent& event)
315
wxFileDialog* dlg = new wxFileDialog(this,
316
_("Select script file"),
317
XRCCTRL(*this, "txtScript", wxTextCtrl)->GetValue(),
318
XRCCTRL(*this, "txtScript", wxTextCtrl)->GetValue(),
319
FileFilters::GetFilterString(_T(".script")),
320
wxOPEN | compatibility::wxHideReadonly );
322
if (dlg->ShowModal() == wxID_OK)
324
wxString sel = UnixFilename(dlg->GetPath());
325
wxString userdir = UnixFilename(ConfigManager::GetFolder(sdScriptsUser));
326
wxString globaldir = UnixFilename(ConfigManager::GetFolder(sdScriptsGlobal));
328
if (sel.StartsWith(userdir))
330
f.MakeRelativeTo(userdir);
332
else if (sel.StartsWith(globaldir))
334
f.MakeRelativeTo(globaldir);
336
XRCCTRL(*this, "txtScript", wxTextCtrl)->SetValue(f.GetFullPath());
341
void ScriptingSettingsDlg::OnTrustSelection(wxListEvent& event)
346
void ScriptingSettingsDlg::OnDeleteTrust(wxCommandEvent& event)
348
wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
349
long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
351
wxString script = list->GetItemText(sel);
352
Manager::Get()->GetScriptingManager()->RemoveTrust(script);
353
list->DeleteItem(sel);
358
void ScriptingSettingsDlg::OnValidateTrusts(wxCommandEvent& event)
361
wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
362
for (int i = 0; i < list->GetItemCount(); ++i)
364
wxString script = list->GetItemText(i);
365
if (!Manager::Get()->GetScriptingManager()->IsScriptTrusted(script))
370
cbMessageBox(_("All script trusts are valid!"), _("Information"), wxICON_INFORMATION);