lib

scriptcontainer.cpp
1/***************************************************************************
2 * scriptcontainer.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "scriptcontainer.h"
21#include "../api/object.h"
22#include "../api/list.h"
23#include "../api/interpreter.h"
24#include "../api/script.h"
25#include "../main/manager.h"
26#include "mainmodule.h"
27
28#include <tqfile.h>
29
30#include <tdelocale.h>
31
32using namespace Kross::Api;
33
34namespace Kross { namespace Api {
35
37 class ScriptContainerPrivate
38 {
39 public:
40
47 Script* script;
48
53 TQString name;
54
58 TQString code;
59
65 TQString interpretername;
66
74 TQString scriptfile;
75
80 TQStringVariantMap options;
81
82 };
83
84}}
85
87 : MainModule(name)
88 , d( new ScriptContainerPrivate() ) // initialize d-pointer class
89{
90 //krossdebug( TQString("ScriptContainer::ScriptContainer() Ctor name='%1'").arg(name) );
91
92 d->script = 0;
93 d->name = name;
94}
95
97{
98 //krossdebug( TQString("ScriptContainer::~ScriptContainer() Dtor name='%1'").arg(d->name) );
99
100 finalize();
101 delete d;
102}
103
104const TQString ScriptContainer::getName() const
105{
106 return d->name;
107}
108
109void ScriptContainer::setName(const TQString& name)
110{
111 d->name = name;
112}
113
115{
116 return d->code;
117}
118
119void ScriptContainer::setCode(const TQString& code)
120{
121 finalize();
122 d->code = code;
123}
124
126{
127 return d->interpretername;
128}
129
130void ScriptContainer::setInterpreterName(const TQString& interpretername)
131{
132 finalize();
133 d->interpretername = interpretername;
134}
135
137{
138 return d->scriptfile;
139}
140
141void ScriptContainer::setFile(const TQString& scriptfile)
142{
143 finalize();
144 d->scriptfile = scriptfile;
145}
146
147TQStringVariantMap& ScriptContainer::getOptions()
148{
149 return d->options;
150}
151
152TQVariant ScriptContainer::getOption(const TQString name, TQVariant defaultvalue, bool /*recursive*/)
153{
154 if(d->options.contains(name))
155 return d->options[name];
157 return info ? info->getOptionValue(name, defaultvalue) : defaultvalue;
158}
159
160bool ScriptContainer::setOption(const TQString name, const TQVariant& value)
161{
163 if(info) {
164 if(info->hasOption(name)) {
165 d->options.replace(name, value);
166 return true;
167 } else krosswarning( TQString("Kross::Api::ScriptContainer::setOption(%1, %2): No such option").arg(name).arg(value.toString()) );
168 } else krosswarning( TQString("Kross::Api::ScriptContainer::setOption(%1, %2): No such interpreterinfo").arg(name).arg(value.toString()) );
169 return false;
170}
171
173{
174 if(! d->script)
175 if(! initialize())
176 return 0;
177
178 if(hadException())
179 return 0;
180
181 Object::Ptr r = d->script->execute();
182 if(d->script->hadException()) {
183 setException( d->script->getException() );
184 finalize();
185 return 0;
186 }
187 return r;
188}
189
191{
192 return d->script ? d->script->getFunctionNames() : TQStringList(); //FIXME init before if needed?
193}
194
195Object::Ptr ScriptContainer::callFunction(const TQString& functionname, List::Ptr arguments)
196{
197 if(! d->script)
198 if(! initialize())
199 return 0;
200
201 if(hadException())
202 return 0;
203
204 if(functionname.isEmpty()) {
205 setException( new Exception(i18n("No functionname defined for ScriptContainer::callFunction().")) );
206 finalize();
207 return 0;
208 }
209
210 Object::Ptr r = d->script->callFunction(functionname, arguments);
211 if(d->script->hadException()) {
212 setException( d->script->getException() );
213 finalize();
214 return 0;
215 }
216 return r;
217}
218
220{
221 return d->script ? d->script->getClassNames() : TQStringList(); //FIXME init before if needed?
222}
223
225{
226 if(! d->script)
227 if(! initialize())
228 return 0;
229
230 if(hadException())
231 return 0;
232
233 Object::Ptr r = d->script->classInstance(classname);
234 if(d->script->hadException()) {
235 setException( d->script->getException() );
236 finalize();
237 return 0;
238 }
239 return r;
240}
241
243{
244 finalize();
245
246 if(! d->scriptfile.isNull()) {
247 krossdebug( TQString("Kross::Api::ScriptContainer::initialize() file=%1").arg(d->scriptfile) );
248
249 if(d->interpretername.isNull()) {
250 d->interpretername = Manager::scriptManager()->getInterpreternameForFile( d->scriptfile );
251 if(d->interpretername.isNull()) {
252 setException( new Exception(i18n("Failed to determinate interpreter for scriptfile '%1'").arg(d->scriptfile)) );
253 return false;
254 }
255 }
256
257 TQFile f( d->scriptfile );
258 if(! f.open(IO_ReadOnly)) {
259 setException( new Exception(i18n("Failed to open scriptfile '%1'").arg(d->scriptfile)) );
260 return false;
261 }
262 d->code = TQString( f.readAll() );
263 f.close();
264 }
265
266 Interpreter* interpreter = Manager::scriptManager()->getInterpreter(d->interpretername);
267 if(! interpreter) {
268 setException( new Exception(i18n("Unknown interpreter '%1'").arg(d->interpretername)) );
269 return false;
270 }
271
272 d->script = interpreter->createScript(this);
273 if(! d->script) {
274 setException( new Exception(i18n("Failed to create script for interpreter '%1'").arg(d->interpretername)) );
275 return false;
276 }
277 if(d->script->hadException()) {
278 setException( d->script->getException() );
279 finalize();
280 return false;
281 }
282 setException( 0 ); // clear old exception
283
284 return true;
285}
286
288{
289 delete d->script;
290 d->script = 0;
291}
292
293
Common exception class used for representing exceptions in Kross.
Definition exception.h:39
While the Interpreter is the implemented interpreter this class is used to provide some abstract info...
Definition interpreter.h:42
bool hasOption(const TQString &key)
const TQVariant getOptionValue(const TQString name, TQVariant defaultvalue=TQVariant())
Base class for interpreters.
virtual Script * createScript(ScriptContainer *scriptcontainer)=0
Create and return a new interpreter dependend Script instance.
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:44
MainModule(const TQString &name)
Constructor.
void setException(Exception::Ptr exception)
Set the Exception this module throwed.
const TQString getInterpreternameForFile(const TQString &file)
Definition manager.cpp:142
InterpreterInfo * getInterpreterInfo(const TQString &interpretername)
Definition manager.cpp:137
static Manager * scriptManager()
Return the Manager instance.
Definition manager.cpp:67
Interpreter * getInterpreter(const TQString &interpretername)
Return the Interpreter instance defined by the interpretername.
Definition manager.cpp:167
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:57
void setFile(const TQString &scriptfile)
Set the filename which will be executed on execute.
ScriptContainer(const TQString &name=TQString())
Constructor.
void setName(const TQString &name)
Set the name this ScriptContainer is reachable as.
const TQString getName() const
Object::Ptr execute()
Execute the script container.
TDESharedPtr< Object > callFunction(const TQString &functionname, TDESharedPtr< List > arguments=0)
Call a function in the script container.
TQStringList getClassNames()
Return a list of classes.
TQStringVariantMap & getOptions()
bool initialize()
Initialize the Script instance.
TQVariant getOption(const TQString name, TQVariant defaultvalue=TQVariant(), bool recursive=false)
TQString getCode() const
Return the scriptcode this ScriptContainer holds.
bool setOption(const TQString name, const TQVariant &value)
Set the Interpreter::Option value.
void setInterpreterName(const TQString &interpretername)
Set the name of the interpreter used on execute.
virtual ~ScriptContainer()
Destructor.
TDESharedPtr< Object > classInstance(const TQString &classname)
Create and return a new class instance.
void setCode(const TQString &code)
Set the scriptcode this ScriptContainer holds.
void finalize()
Finalize the Script instance and free's any cached or still running executions.
const TQStringList getFunctionNames()
Return a list of functionnames the with setCode defined scriptcode spends.
The common Kross API used as common codebase.
Definition callable.h:31
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:31
void krossdebug(const TQString &s)
Debugging function.
void krosswarning(const TQString &s)
Warning function.