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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*
* Copyright (C) 2001-2003, Richard J. Moore <rich@kde.org>
*
* This library 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 library 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 library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <qobject.h>
#include <qdialog.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qwidget.h>
#include <qwidgetfactory.h>
#ifndef QT_ONLY
#include <kaction.h>
#endif // QT_ONLY
#include <kjs/interpreter.h>
#include <kjs/object.h>
#include <kjs/ustring.h>
#include <kjs/types.h>
#include <kjs/value.h>
#include "global.h"
#include "jsfactory.h"
#include "jsbinding.h"
#include "jsobjectproxy.h"
#include "jsconsolewidget.h"
#include "jsbuiltin.h"
#ifndef QT_ONLY
#include "bindings/image_imp.h"
#endif // QT_ONLY
#include "kjsembedpart_imp.h"
#include "kjsembedpart.h"
#ifndef QT_ONLY
#include "kjsembedpart.moc"
namespace KJSEmbed {
class KJSEmbedActionRunner : public XMLActionRunner
{
public:
KJSEmbedActionRunner( KJSEmbedPart *jspart ) : XMLActionRunner(), part(jspart) {}
virtual ~KJSEmbedActionRunner() {}
virtual bool run( XMLActionClient *client, const XMLActionClient::XMLActionScript &script ) {
if ( script.type == "js" )
return part->runFile( script.src );
else
return XMLActionRunner::run( client, script );
}
private:
KJSEmbedPart *part;
};
} // namespace KJSEmbed
#else // QT_ONLY
#include "qtstubs.h"
#endif // QT_ONLY
namespace KJSEmbed {
//
// KPart
//
KJSEmbedPart::KJSEmbedPart( QObject *parent, const char *name )
: KParts::ReadOnlyPart( parent, name ? name : "kjsembed_part" ),
jsConsole(0), jsfactory(0),
widgetparent(0), widgetname(name ? name : "kjsembed_part"), deletejs(false)
{
createInterpreter();
createBuiltIn( js->globalExec(), js->globalObject() );
createActionClient();
}
KJSEmbedPart::KJSEmbedPart( QWidget *wparent, const char *wname, QObject *parent, const char *name )
: KParts::ReadOnlyPart( parent, name ? name : (wname?wname:"jsembed_part") ),
jsConsole(0), jsfactory(0),
widgetparent(wparent), widgetname(wname), deletejs(false)
{
createInterpreter();
createBuiltIn( js->globalExec(), js->globalObject() );
createActionClient();
}
KJSEmbedPart::KJSEmbedPart( KJS::Interpreter *jsi, QWidget *wparent, const char *wname,
QObject *parent, const char *name )
: KParts::ReadOnlyPart( parent, name ? name : (wname?wname:"jsembed_part") ),
jsConsole(0), jsfactory(0), builtins(0),
widgetparent(wparent), widgetname(wname), js(jsi), deletejs(false)
{
if ( !js ) {
createInterpreter();
createBuiltIn( js->globalExec(), js->globalObject() );
}
createActionClient();
}
KJSEmbedPart::~KJSEmbedPart()
{
if ( deletejs )
delete js;
delete jsfactory;
delete builtins;
}
void KJSEmbedPart::createInterpreter()
{
deletejs = true;
js = new KJS::Interpreter();
jsfactory = new JSFactory( this );
jsfactory->addType( className() );
jsfactory->addTypes( js->globalExec(), js->globalObject() );
}
void KJSEmbedPart::createBuiltIn( KJS::ExecState *exec, KJS::Object &parent )
{
partobj = addObject( this, parent, "part" );
builtins = new JSBuiltIn( this );
builtins->add( exec, parent );
}
void KJSEmbedPart::createActionClient()
{
#ifndef QT_ONLY
xmlclient = new KJSEmbed::XMLActionClient( this );
xmlclient->setActionCollection( actionCollection() );
xmlclient->setRunner( new KJSEmbedActionRunner(this) );
#endif // QT_ONLY
}
JSConsoleWidget *KJSEmbedPart::view()
{
#ifndef QT_ONLY
if ( !jsConsole ) {
QCString name = widgetname ? widgetname : QCString("jsembed_console");
jsConsole = new JSConsoleWidget( this, widgetparent, name );
setWidget( jsConsole );
jsfactory->addType( jsConsole->className() );
}
return jsConsole;
#endif // QT_ONLY
return 0L;
}
QStringList KJSEmbedPart::constructorNames() const
{
QStringList classes;
KJS::List cons = constructorList();
KJS::ListIterator it = cons.begin();
while ( it != cons.end() ) {
KJS::Value v = *it;
classes += v.toString( js->globalExec() ).qstring();
it++;
}
return classes;
}
KJS::Value KJSEmbedPart::constructors() const
{
KJS::List items = constructorList();
kdDebug(80001) << "KJSEmbedPart::constructors() returning " << items.size() << " items" << endl;
return KJS::Object( js->builtinArray().construct( js->globalExec(), items ) );
}
KJS::List KJSEmbedPart::constructorList() const
{
KJS::List items;
KJS::Object obj = js->globalObject();
KJS::ExecState *exec = js->globalExec();
KJS::ReferenceList l = obj.propList( exec );
KJS::ReferenceListIterator propIt = l.begin();
while ( propIt != l.end() ) {
KJS::Identifier name = propIt->getPropertyName( exec );
if ( obj.hasProperty( exec, name ) ) {
KJS::Value v = obj.get( exec, name );
KJS::Object vobj = v.toObject( exec );
if ( vobj.implementsConstruct() )
items.append( KJS::String( name.ustring() ) );
}
propIt++;
}
return items;
}
//
// Version information
//
QCString KJSEmbedPart::versionString() const
{
return QCString(KJSEMBED_VERSION_STRING);
}
int KJSEmbedPart::versionMajor() const
{
return KJSEMBED_VERSION_MAJOR;
}
int KJSEmbedPart::versionMinor() const
{
return KJSEMBED_VERSION_MINOR;
}
//
// Execute a JS script.
//
KJS::Value KJSEmbedPart::evaluate( const QString &script, const KJS::Value &self )
{
if ( execute( res, script, self ) )
return res.value();
return KJS::Null();
}
bool KJSEmbedPart::execute( const QString &script, const KJS::Value &self )
{
return execute( res, script, self );
}
bool KJSEmbedPart::execute( KJS::Completion &result, const QString &script, const KJS::Value &self )
{
KJS::Value val( self );
result = js->evaluate( script, self.isNull() ? partobj : val );
return (result.complType() == KJS::Normal) || (result.complType() == KJS::ReturnValue);
}
bool KJSEmbedPart::openURL( const KURL &url )
{
if ( url.protocol() == "javascript" ) {
// kdDebug(80001) << "KJSEmbedPart: openURL '" << url.url() << "' is javascript" << endl;
#ifndef QT_ONLY
QString cmd = url.url();
#else
QString cmd = url.toString();
#endif
QString js( "javascript:" );
cmd = cmd.replace( 0, js.length(), QString("") );
// kdDebug(80001) << "KJSEmbedPart: JS command is '" << cmd << "'" << endl;
return execute( cmd );
}
return false;
}
//
// Invoke a script file.
//
bool KJSEmbedPart::runFile( const QString &name, const KJS::Value &self )
{
kdDebug(80001) << "KJSEmbedPart::runFile(): file is '" << name << "'" << endl;
QString script = loadFile( name );
return execute( script, self );
}
bool KJSEmbedPart::loadActionSet( const QString &file )
{
#ifndef QT_ONLY
return xmlclient->load( file );
#else // QT_ONLY
Q_UNUSED( file );
return false;
#endif // QT_ONLY
}
QString KJSEmbedPart::loadFile( const QString &src )
{
QString script;
if ( src == "-" ) {
QTextStream ts( stdin, IO_ReadOnly );
script = ts.read();
}
else {
QFile file( src );
if ( file.open( IO_ReadOnly ) ) {
script = QString( file.readAll() );
}
else {
kdWarning() << "Could not open file '" << src << "', "
<< strerror( errno ) << endl;
return QString::null;
}
}
if ( script.startsWith( "#!" ) ) {
int pos = script.find( "\n" );
if ( pos > 0 )
script = script.mid( pos );
}
return script;
}
//
// Publishing objects.
//
KJS::Object KJSEmbedPart::bind( QObject *obj )
{
KJS::Object jsobj = jsfactory->createProxy( js->globalExec(), obj );
JSProxy::toObjectProxy(jsobj.imp() )->setOwner(JSProxy::Native);
return jsobj;
}
KJS::Object KJSEmbedPart::addObject( QObject *obj, KJS::Object &parent, const char *name )
{
KJS::Object jsobj = bind( obj );
parent.put( js->globalExec(), name ? name : obj->name(), jsobj );
return jsobj;
}
KJS::Object KJSEmbedPart::addObject( QObject *obj, const char *name )
{
return addObject( obj, js->globalObject(), name );
}
void KJSEmbedPart::virtual_hook( int /*id*/, void * /*data*/ )
{
}
static KJS::Object scopeWalker( KJS::ExecState *exec, const KJS::Object &root, const QString &objectString )
{
KJS::Object returnObject = root;
QStringList objects = QStringList::split(".", objectString);
for( uint idx = 0; idx < objects.count(); ++idx)
{
KJS::Identifier id = KJS::Identifier( KJS::UString( objects[idx] ));
KJS::Value newObject = returnObject.get(exec, id );
if( newObject.isValid() )
returnObject = newObject.toObject(exec);
}
return returnObject;
}
KJS::Value KJSEmbedPart::callMethod( const QString & methodName, const KJS::List & args ) const
{
KJS::ExecState *exec = js->globalExec();
KJS::Identifier id = KJS::Identifier( KJS::UString(methodName.latin1() ));
KJS::Object obj = js->globalObject();
KJS::Object fun = obj.get(exec, id ).toObject( exec );
KJS::Value retValue;
if ( !fun.implementsCall() )
{
// We need to create an exception here...
}
else
retValue = fun.call(exec, obj, args);
kdDebug( 80001 ) << "Returned type is: " << retValue.type() << endl;
if( exec->hadException() )
{
kdWarning( 80001 ) << "Got error: " << exec->exception().toString(exec).qstring() << endl;
return exec->exception();
}
else
{
if( retValue.type() == 1 && retValue.type() == 0)
{
kdDebug( 80001 ) << "Got void return type. " << endl;
return KJS::Null();
}
}
return retValue;
}
KJS::Value KJSEmbedPart::getValue( const QString & valueName ) const
{
KJS::ExecState *exec = js->globalExec();
KJS::Identifier id = KJS::Identifier( KJS::UString(valueName.latin1() ));
KJS::Object obj = js->globalObject();
return obj.get(exec, id );
}
void KJSEmbedPart::putValue( const QString & valueName, const KJS::Value & value )
{
KJS::ExecState *exec = js->globalExec();
KJS::Identifier id = KJS::Identifier( KJS::UString(valueName.latin1() ));
KJS::Object obj = js->globalObject();
obj.put(exec, id, value);
}
void KJSEmbedPart::putVariant( const QString & valueName, const QVariant & value )
{
KJS::Value val = convertToValue( js->globalExec(), value);
putValue( valueName, val );
}
QVariant KJSEmbedPart::getVariant( const QString & valueName ) const
{
return convertToVariant( js->globalExec(), getValue( valueName ) );
}
bool KJSEmbedPart::hasMethod( const QString & methodName )
{
KJS::ExecState *exec = js->globalExec();
KJS::Identifier id = KJS::Identifier( KJS::UString(methodName.latin1() ));
KJS::Object obj = js->globalObject();
KJS::Object fun = obj.get(exec, id ).toObject( exec );
return fun.implementsCall();
}
}// namespace KJSEmbed
// Local Variables:
// c-basic-offset: 4
// End:
|