~ubuntu-branches/ubuntu/oneiric/codeblocks/oneiric

« back to all changes in this revision

Viewing changes to src/sdk/scripting/bindings/sc_io.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 Lesser General Public License, version 3
 
3
 * http://www.gnu.org/licenses/lgpl-3.0.html
 
4
 *
 
5
 * $Revision$
 
6
 * $Id$
 
7
 * $HeadURL$
 
8
 */
 
9
 
 
10
#include <sdk_precomp.h>
 
11
#include <manager.h>
 
12
#include <macrosmanager.h>
 
13
#include <configmanager.h>
 
14
#include <annoyingdialog.h>
 
15
 
 
16
#ifndef CB_PRECOMP
 
17
    #include <globals.h>
 
18
    #include <wx/string.h>
 
19
    #include <wx/filedlg.h>
 
20
#endif
 
21
 
 
22
#include <wx/filename.h>
 
23
#include <wx/utils.h>
 
24
 
 
25
#include "scriptsecuritywarningdlg.h"
 
26
#include "sc_base_types.h"
 
27
 
 
28
namespace ScriptBindings
 
29
{
 
30
    namespace IOLib
 
31
    {
 
32
        // not exposed
 
33
        bool SecurityAllows(const wxString& operation, const wxString& descr)
 
34
        {
 
35
            if (Manager::Get()->GetScriptingManager()->IsCurrentlyRunningScriptTrusted())
 
36
                return true;
 
37
 
 
38
            if (Manager::Get()->GetConfigManager(_T("security"))->ReadBool(operation, false))
 
39
                return true;
 
40
 
 
41
            ScriptSecurityWarningDlg dlg(Manager::Get()->GetAppWindow(), operation, descr);
 
42
            if (dlg.ShowModal() != wxID_OK)
 
43
                return false;
 
44
 
 
45
            ScriptSecurityResponse response = dlg.GetResponse();
 
46
            switch (response)
 
47
            {
 
48
                case ssrAllow:
 
49
                    return true;
 
50
 
 
51
                case ssrAllowAll:
 
52
                    Manager::Get()->GetConfigManager(_T("security"))->Write(operation, true);
 
53
                    return true;
 
54
 
 
55
                case ssrTrust: // purposely fall through
 
56
                case ssrTrustPermanently:
 
57
                    Manager::Get()->GetScriptingManager()->TrustCurrentlyRunningScript(response == ssrTrustPermanently);
 
58
                    return true;
 
59
 
 
60
                default:
 
61
                    return false;
 
62
            }
 
63
            return false;
 
64
        }
 
65
 
 
66
        wxString GetCwd()
 
67
        {
 
68
            return wxGetCwd();
 
69
        }
 
70
 
 
71
        void SetCwd(const wxString& dir)
 
72
        {
 
73
            wxSetWorkingDirectory(dir);
 
74
        }
 
75
 
 
76
        bool CreateDirRecursively(const wxString& full_path, int perms)
 
77
        {
 
78
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(full_path));
 
79
            NormalizePath(fname, wxEmptyString);
 
80
            if (!SecurityAllows(_T("CreateDir"), fname.GetFullPath()))
 
81
                return false;
 
82
            return ::CreateDirRecursively(fname.GetFullPath(), perms);
 
83
        }
 
84
 
 
85
        wxString ChooseDir(const wxString& message, const wxString& initialPath, bool showCreateDirButton)
 
86
        {
 
87
            return ChooseDirectory(0, message, Manager::Get()->GetMacrosManager()->ReplaceMacros(initialPath), wxEmptyString, false, showCreateDirButton);
 
88
        }
 
89
 
 
90
        bool RemoveDir(const wxString& src)
 
91
        {
 
92
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
 
93
            NormalizePath(fname, wxEmptyString);
 
94
            if (!SecurityAllows(_T("RemoveDir"), fname.GetFullPath()))
 
95
                return false;
 
96
            return wxRmdir(fname.GetFullPath());
 
97
        }
 
98
 
 
99
        bool DirectoryExists(const wxString& dir)
 
100
        {
 
101
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(dir));
 
102
            NormalizePath(fname, wxEmptyString);
 
103
            return wxDirExists(fname.GetFullPath());
 
104
        }
 
105
 
 
106
        bool CopyFile(const wxString& src, const wxString& dst, bool overwrite)
 
107
        {
 
108
            wxFileName fname1(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
 
109
            wxFileName fname2(Manager::Get()->GetMacrosManager()->ReplaceMacros(dst));
 
110
            NormalizePath(fname1, wxEmptyString);
 
111
            NormalizePath(fname2, wxEmptyString);
 
112
            if (!SecurityAllows(_T("CopyFile"), wxString::Format(_T("%s -> %s"), src.c_str(), dst.c_str())))
 
113
                return false;
 
114
            if (!wxFileExists(fname1.GetFullPath())) return false;
 
115
            return wxCopyFile(fname1.GetFullPath(),
 
116
                            fname2.GetFullPath(),
 
117
                            overwrite);
 
118
        }
 
119
 
 
120
        bool RenameFile(const wxString& src, const wxString& dst)
 
121
        {
 
122
            wxFileName fname1(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
 
123
            wxFileName fname2(Manager::Get()->GetMacrosManager()->ReplaceMacros(dst));
 
124
            NormalizePath(fname1, wxEmptyString);
 
125
            NormalizePath(fname2, wxEmptyString);
 
126
            if (!SecurityAllows(_T("RenameFile"), wxString::Format(_T("%s -> %s"),
 
127
                                            fname1.GetFullPath().c_str(), fname2.GetFullPath().c_str())))
 
128
                return false;
 
129
            if (!wxFileExists(fname1.GetFullPath())) return false;
 
130
            return wxRenameFile(fname1.GetFullPath(),
 
131
                                fname2.GetFullPath());
 
132
        }
 
133
 
 
134
        bool RemoveFile(const wxString& src)
 
135
        {
 
136
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(src));
 
137
            NormalizePath(fname, wxEmptyString);
 
138
            if (!SecurityAllows(_T("RemoveFile"), fname.GetFullPath()))
 
139
                return false;
 
140
            if (!wxFileExists(fname.GetFullPath())) return false;
 
141
            return wxRemoveFile(fname.GetFullPath());
 
142
        }
 
143
 
 
144
        bool FileExists(const wxString& file)
 
145
        {
 
146
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(file));
 
147
            NormalizePath(fname, wxEmptyString);
 
148
            return wxFileExists(fname.GetFullPath());
 
149
        }
 
150
 
 
151
        wxString ChooseFile(const wxString& title, const wxString& defaultFile, const wxString& filter)
 
152
        {
 
153
            wxFileDialog dlg(0,
 
154
                            title,
 
155
                            wxEmptyString,
 
156
                            Manager::Get()->GetMacrosManager()->ReplaceMacros(defaultFile),
 
157
                            filter,
 
158
                            wxOPEN | compatibility::wxHideReadonly);
 
159
            PlaceWindow(&dlg);
 
160
            if (dlg.ShowModal() == wxID_OK)
 
161
                return dlg.GetPath();
 
162
            return wxEmptyString;
 
163
        }
 
164
 
 
165
        wxString ReadFileContents(const wxString& filename)
 
166
        {
 
167
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(filename));
 
168
            NormalizePath(fname, wxEmptyString);
 
169
            wxFile f(fname.GetFullPath());
 
170
            return cbReadFileContents(f);
 
171
        }
 
172
 
 
173
        bool WriteFileContents(const wxString& filename, const wxString& contents)
 
174
        {
 
175
            wxFileName fname(Manager::Get()->GetMacrosManager()->ReplaceMacros(filename));
 
176
            NormalizePath(fname, wxEmptyString);
 
177
            if (!SecurityAllows(_T("CreateFile"), fname.GetFullPath()))
 
178
                return false;
 
179
            wxFile f(fname.GetFullPath(), wxFile::write);
 
180
            return cbWrite(f, contents);
 
181
        }
 
182
 
 
183
        int Execute(const wxString& command)
 
184
        {
 
185
            if (!SecurityAllows(_T("Execute"), command))
 
186
                return -1;
 
187
            wxArrayString output;
 
188
            return wxExecute(command, output, wxEXEC_NODISABLE);
 
189
        }
 
190
 
 
191
        wxString ExecuteAndGetOutput(const wxString& command)
 
192
        {
 
193
            if (!SecurityAllows(_T("Execute"), command))
 
194
                return wxEmptyString;
 
195
            wxArrayString output;
 
196
            wxExecute(command, output, wxEXEC_NODISABLE);
 
197
            return GetStringFromArray(output, _T("\n"));
 
198
        }
 
199
 
 
200
    } // namespace IOLib
 
201
} // namespace ScriptBindings
 
202
 
 
203
namespace ScriptBindings
 
204
{
 
205
    struct IONamespace {};
 
206
 
 
207
    void Register_IO()
 
208
    {
 
209
        SqPlus::SQClassDef<IONamespace>("IO").
 
210
 
 
211
                #ifndef NO_INSECURE_SCRIPTS
 
212
                staticFunc(&IOLib::CreateDirRecursively, "CreateDirectory").
 
213
                staticFunc(&IOLib::RemoveDir, "RemoveDirectory").
 
214
                staticFunc(&IOLib::CopyFile, "CopyFile").
 
215
                staticFunc(&IOLib::RenameFile, "RenameFile").
 
216
                staticFunc(&IOLib::RemoveFile, "RemoveFile").
 
217
                staticFunc(&IOLib::WriteFileContents, "WriteFileContents").
 
218
                staticFunc(&IOLib::Execute, "Execute").
 
219
                staticFunc(&IOLib::ExecuteAndGetOutput, "ExecuteAndGetOutput").
 
220
                #endif // NO_INSECURE_SCRIPTS
 
221
 
 
222
                staticFunc(&IOLib::GetCwd, "GetCwd").
 
223
                staticFunc(&IOLib::SetCwd, "SetCwd").
 
224
 
 
225
                staticFunc(&IOLib::DirectoryExists, "DirectoryExists").
 
226
                staticFunc(&IOLib::ChooseDir, "SelectDirectory").
 
227
                staticFunc(&IOLib::FileExists, "FileExists").
 
228
                staticFunc(&IOLib::ChooseFile, "SelectFile").
 
229
                staticFunc(&IOLib::ReadFileContents, "ReadFileContents");
 
230
 
 
231
        #ifndef NO_INSECURE_SCRIPTS
 
232
        SqPlus::BindConstant(true, "allowInsecureScripts");
 
233
        #else
 
234
        SqPlus::BindConstant(false, "allowInsecureScripts");
 
235
        #endif // NO_INSECURE_SCRIPTS
 
236
    }
 
237
} // namespace ScriptBindings