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
|
/***************************************************************************
* scriptcontainer.cpp
* This file is part of the KDE project
* copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
***************************************************************************/
#include "scriptcontainer.h"
#include "../api/object.h"
#include "../api/list.h"
#include "../api/interpreter.h"
#include "../api/script.h"
#include "../main/manager.h"
#include "mainmodule.h"
#include <tqfile.h>
#include <klocale.h>
using namespace Kross::Api;
namespace Kross { namespace Api {
/// @internal
class ScriptContainerPrivate
{
public:
/**
* The \a Script instance the \a ScriptContainer uses
* if initialized. It will be NULL as long as we
* didn't initialized it what will be done on
* demand.
*/
Script* script;
/**
* The unique name the \a ScriptContainer is
* reachable as.
*/
TQString name;
/**
* The scripting code.
*/
TQString code;
/**
* The name of the interpreter. This could be
* something like "python" for the python
* binding.
*/
TQString interpretername;
/**
* The name of the scriptfile that should be
* executed. Those scriptfile will be readed
* and the content will be used to set the
* scripting code and, if not defined, the
* used interpreter.
*/
TQString scriptfile;
/**
* Map of options that overwritte the \a InterpreterInfo::Option::Map
* standard options.
*/
TQMap<TQString, TQVariant> options;
};
}}
ScriptContainer::ScriptContainer(const TQString& name)
: MainModule(name)
, d( new ScriptContainerPrivate() ) // initialize d-pointer class
{
//krossdebug( TQString("ScriptContainer::ScriptContainer() Ctor name='%1'").arg(name) );
d->script = 0;
d->name = name;
}
ScriptContainer::~ScriptContainer()
{
//krossdebug( TQString("ScriptContainer::~ScriptContainer() Dtor name='%1'").arg(d->name) );
finalize();
delete d;
}
const TQString ScriptContainer::getName() const
{
return d->name;
}
void ScriptContainer::setName(const TQString& name)
{
d->name = name;
}
TQString ScriptContainer::getCode() const
{
return d->code;
}
void ScriptContainer::setCode(const TQString& code)
{
finalize();
d->code = code;
}
TQString ScriptContainer::getInterpreterName() const
{
return d->interpretername;
}
void ScriptContainer::setInterpreterName(const TQString& interpretername)
{
finalize();
d->interpretername = interpretername;
}
TQString ScriptContainer::getFile() const
{
return d->scriptfile;
}
void ScriptContainer::setFile(const TQString& scriptfile)
{
finalize();
d->scriptfile = scriptfile;
}
TQMap<TQString, TQVariant>& ScriptContainer::getOptions()
{
return d->options;
}
TQVariant ScriptContainer::getOption(const TQString name, TQVariant defaultvalue, bool /*recursive*/)
{
if(d->options.contains(name))
return d->options[name];
Kross::Api::InterpreterInfo* info = Kross::Api::Manager::scriptManager()->getInterpreterInfo( d->interpretername );
return info ? info->getOptionValue(name, defaultvalue) : defaultvalue;
}
bool ScriptContainer::setOption(const TQString name, const TQVariant& value)
{
Kross::Api::InterpreterInfo* info = Kross::Api::Manager::scriptManager()->getInterpreterInfo( d->interpretername );
if(info) {
if(info->hasOption(name)) {
d->options.replace(name, value);
return true;
} else krosswarning( TQString("Kross::Api::ScriptContainer::setOption(%1, %2): No such option").arg(name).arg(value.toString()) );
} else krosswarning( TQString("Kross::Api::ScriptContainer::setOption(%1, %2): No such interpreterinfo").arg(name).arg(value.toString()) );
return false;
}
Object::Ptr ScriptContainer::execute()
{
if(! d->script)
if(! initialize())
return 0;
if(hadException())
return 0;
Object::Ptr r = d->script->execute();
if(d->script->hadException()) {
setException( d->script->getException() );
finalize();
return 0;
}
return r;
}
const TQStringList ScriptContainer::getFunctionNames()
{
return d->script ? d->script->getFunctionNames() : TQStringList(); //FIXME init before if needed?
}
Object::Ptr ScriptContainer::callFunction(const TQString& functionname, List::Ptr arguments)
{
if(! d->script)
if(! initialize())
return 0;
if(hadException())
return 0;
if(functionname.isEmpty()) {
setException( new Exception(i18n("No functionname defined for ScriptContainer::callFunction().")) );
finalize();
return 0;
}
Object::Ptr r = d->script->callFunction(functionname, arguments);
if(d->script->hadException()) {
setException( d->script->getException() );
finalize();
return 0;
}
return r;
}
TQStringList ScriptContainer::getClassNames()
{
return d->script ? d->script->getClassNames() : TQStringList(); //FIXME init before if needed?
}
Object::Ptr ScriptContainer::classInstance(const TQString& classname)
{
if(! d->script)
if(! initialize())
return 0;
if(hadException())
return 0;
Object::Ptr r = d->script->classInstance(classname);
if(d->script->hadException()) {
setException( d->script->getException() );
finalize();
return 0;
}
return r;
}
bool ScriptContainer::initialize()
{
finalize();
if(! d->scriptfile.isNull()) {
krossdebug( TQString("Kross::Api::ScriptContainer::initialize() file=%1").arg(d->scriptfile) );
if(d->interpretername.isNull()) {
d->interpretername = Manager::scriptManager()->getInterpreternameForFile( d->scriptfile );
if(d->interpretername.isNull()) {
setException( new Exception(i18n("Failed to determinate interpreter for scriptfile '%1'").arg(d->scriptfile)) );
return false;
}
}
TQFile f( d->scriptfile );
if(! f.open(IO_ReadOnly)) {
setException( new Exception(i18n("Failed to open scriptfile '%1'").arg(d->scriptfile)) );
return false;
}
d->code = TQString( f.readAll() );
f.close();
}
Interpreter* interpreter = Manager::scriptManager()->getInterpreter(d->interpretername);
if(! interpreter) {
setException( new Exception(i18n("Unknown interpreter '%1'").arg(d->interpretername)) );
return false;
}
d->script = interpreter->createScript(this);
if(! d->script) {
setException( new Exception(i18n("Failed to create script for interpreter '%1'").arg(d->interpretername)) );
return false;
}
if(d->script->hadException()) {
setException( d->script->getException() );
finalize();
return false;
}
setException( 0 ); // clear old exception
return true;
}
void ScriptContainer::finalize()
{
delete d->script;
d->script = 0;
}
|