~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/src/scriptingsettingsdlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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$
 
6
 * $Id$
 
7
 * $HeadURL$
 
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, 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)
 
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
 
 
46
ScriptingSettingsDlg::ScriptingSettingsDlg(wxWindow* parent)
 
47
    : m_IgnoreTextEvents(false)
 
48
{
 
49
    //ctor
 
50
    wxXmlResource::Get()->LoadDialog(this, parent, _T("dlgScriptingSettings"));
 
51
 
 
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);
 
56
 
 
57
    FillScripts();
 
58
 
 
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?"));
 
63
 
 
64
    FillTrusts();
 
65
}
 
66
 
 
67
ScriptingSettingsDlg::~ScriptingSettingsDlg()
 
68
{
 
69
    //dtor
 
70
}
 
71
 
 
72
void ScriptingSettingsDlg::FillScripts()
 
73
{
 
74
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
75
    list->DeleteAllItems();
 
76
 
 
77
    ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("scripting"));
 
78
    wxArrayString keys = mgr->EnumerateKeys(_T("/startup_scripts"));
 
79
 
 
80
    for (size_t i = 0; i < keys.GetCount(); ++i)
 
81
    {
 
82
        ScriptEntry se;
 
83
        wxString ser;
 
84
        if (mgr->Read(_T("/startup_scripts/") + keys[i], &ser))
 
85
        {
 
86
            se.SerializeIn(ser);
 
87
            m_ScriptsVector.push_back(se);
 
88
 
 
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));
 
92
        }
 
93
    }
 
94
 
 
95
    UpdateState();
 
96
}
 
97
 
 
98
void ScriptingSettingsDlg::UpdateState()
 
99
{
 
100
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
101
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
102
 
 
103
    bool en = sel != -1;
 
104
 
 
105
    const ScriptEntry& se = m_ScriptsVector[sel];
 
106
 
 
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);
 
113
}
 
114
 
 
115
void ScriptingSettingsDlg::FillTrusts()
 
116
{
 
117
    wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
 
118
    list->DeleteAllItems();
 
119
 
 
120
    const ScriptingManager::TrustedScripts& trusts = Manager::Get()->GetScriptingManager()->GetTrustedScripts();
 
121
    ScriptingManager::TrustedScripts::const_iterator it;
 
122
    for (it = trusts.begin(); it != trusts.end(); ++it)
 
123
    {
 
124
        const ScriptingManager::TrustedScriptProps& props = it->second;
 
125
 
 
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") : _(""));
 
129
    }
 
130
 
 
131
    UpdateTrustsState();
 
132
 
 
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));
 
142
}
 
143
 
 
144
void ScriptingSettingsDlg::UpdateTrustsState()
 
145
{
 
146
    wxListCtrl* list = XRCCTRL(*this, "lstTrustedScripts", wxListCtrl);
 
147
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
148
 
 
149
    bool en = sel != -1;
 
150
 
 
151
    XRCCTRL(*this, "btnDeleteTrust", wxButton)->Enable(en);
 
152
    XRCCTRL(*this, "btnValidateTrusts", wxButton)->Enable(en);
 
153
}
 
154
 
 
155
void ScriptingSettingsDlg::EndModal(int retCode)
 
156
{
 
157
    if (retCode == wxID_OK)
 
158
    {
 
159
        ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("scripting"));
 
160
        mgr->DeleteSubPath(_T("/startup_scripts"));
 
161
 
 
162
        ScriptsVector::iterator it;
 
163
        int i = 0;
 
164
        for (it = m_ScriptsVector.begin(); it != m_ScriptsVector.end(); ++it, ++i)
 
165
        {
 
166
            ScriptEntry& se = *it;
 
167
            wxString key = wxString::Format(_T("/startup_scripts/script%d"), i);
 
168
            mgr->Write(key, se.SerializeOut());
 
169
        }
 
170
 
 
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());
 
179
    }
 
180
 
 
181
    wxDialog::EndModal(retCode);
 
182
}
 
183
 
 
184
void ScriptingSettingsDlg::LoadItem(long item)
 
185
{
 
186
    m_IgnoreTextEvents = true;
 
187
 
 
188
    // load
 
189
    ScriptEntry& se = m_ScriptsVector[item];
 
190
 
 
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);
 
195
 
 
196
    m_IgnoreTextEvents = false;
 
197
}
 
198
 
 
199
void ScriptingSettingsDlg::SaveItem(long item)
 
200
{
 
201
    m_IgnoreTextEvents = true;
 
202
 
 
203
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
204
    ScriptEntry& se = m_ScriptsVector[item];
 
205
 
 
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();
 
210
 
 
211
    // update view
 
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));
 
215
 
 
216
    m_IgnoreTextEvents = false;
 
217
}
 
218
 
 
219
void ScriptingSettingsDlg::OnListSelection(wxListEvent& event)
 
220
{
 
221
//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Selected %d"), event.GetIndex()));
 
222
 
 
223
    // load
 
224
    long sel = event.GetIndex();
 
225
    LoadItem(sel);
 
226
 
 
227
    UpdateState();
 
228
}
 
229
 
 
230
void ScriptingSettingsDlg::OnListDeselection(wxListEvent& event)
 
231
{
 
232
//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Deselected %d"), event.GetIndex()));
 
233
 
 
234
    // save
 
235
    long sel = event.GetIndex();
 
236
    SaveItem(sel);
 
237
 
 
238
    UpdateState();
 
239
}
 
240
 
 
241
void ScriptingSettingsDlg::OnScriptChanged(wxCommandEvent& event)
 
242
{
 
243
    if (m_IgnoreTextEvents)
 
244
        return;
 
245
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
246
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
247
    SaveItem(sel);
 
248
    UpdateState();
 
249
}
 
250
 
 
251
void ScriptingSettingsDlg::OnScriptMenuChanged(wxCommandEvent& event)
 
252
{
 
253
    if (m_IgnoreTextEvents)
 
254
        return;
 
255
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
256
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
257
    SaveItem(sel);
 
258
    UpdateState();
 
259
}
 
260
 
 
261
void ScriptingSettingsDlg::OnEnable(wxCommandEvent& event)
 
262
{
 
263
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
264
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
265
    SaveItem(sel);
 
266
    UpdateState();
 
267
}
 
268
 
 
269
void ScriptingSettingsDlg::OnRegister(wxCommandEvent& event)
 
270
{
 
271
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
272
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
273
    SaveItem(sel);
 
274
    UpdateState();
 
275
}
 
276
 
 
277
void ScriptingSettingsDlg::OnAddScript(wxCommandEvent& event)
 
278
{
 
279
    ScriptEntry se;
 
280
    se.script = _T("new.script");
 
281
    se.enabled = true;
 
282
    se.registered = false;
 
283
    m_ScriptsVector.push_back(se);
 
284
 
 
285
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
286
 
 
287
    // update view
 
288
    long item = list->InsertItem(list->GetItemCount(), se.script);
 
289
    list->SetItem(item, 1, _("No"));
 
290
    list->SetItem(item, 2, wxString(_("No")));
 
291
 
 
292
    list->SetItemState(item, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
 
293
 
 
294
    OnBrowse(event);
 
295
}
 
296
 
 
297
void ScriptingSettingsDlg::OnRemoveScript(wxCommandEvent& event)
 
298
{
 
299
    wxListCtrl* list = XRCCTRL(*this, "chkStartupScripts", wxListCtrl);
 
300
    long sel = list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
 
301
 
 
302
    list->DeleteItem(sel);
 
303
    m_ScriptsVector.erase(m_ScriptsVector.begin() + sel);
 
304
 
 
305
    if (sel > list->GetItemCount())
 
306
        --sel;
 
307
    list->SetItemState(sel, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
 
308
    if (sel >= 0)
 
309
        LoadItem(sel);
 
310
    UpdateState();
 
311
}
 
312
 
 
313
void ScriptingSettingsDlg::OnBrowse(wxCommandEvent& event)
 
314
{
 
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 );
 
321
    PlaceWindow(dlg);
 
322
    if (dlg->ShowModal() == wxID_OK)
 
323
    {
 
324
        wxString sel = UnixFilename(dlg->GetPath());
 
325
        wxString userdir = UnixFilename(ConfigManager::GetFolder(sdScriptsUser));
 
326
        wxString globaldir = UnixFilename(ConfigManager::GetFolder(sdScriptsGlobal));
 
327
        wxFileName f(sel);
 
328
        if (sel.StartsWith(userdir))
 
329
        {
 
330
            f.MakeRelativeTo(userdir);
 
331
        }
 
332
        else if (sel.StartsWith(globaldir))
 
333
        {
 
334
            f.MakeRelativeTo(globaldir);
 
335
        }
 
336
        XRCCTRL(*this, "txtScript", wxTextCtrl)->SetValue(f.GetFullPath());
 
337
    }
 
338
    dlg->Destroy();
 
339
}
 
340
 
 
341
void ScriptingSettingsDlg::OnTrustSelection(wxListEvent& event)
 
342
{
 
343
    UpdateTrustsState();
 
344
}
 
345
 
 
346
void ScriptingSettingsDlg::OnDeleteTrust(wxCommandEvent& event)
 
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);
 
352
    Manager::Get()->GetScriptingManager()->RemoveTrust(script);
 
353
    list->DeleteItem(sel);
 
354
 
 
355
    UpdateTrustsState();
 
356
}
 
357
 
 
358
void ScriptingSettingsDlg::OnValidateTrusts(wxCommandEvent& event)
 
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);
 
371
}