summaryrefslogtreecommitdiffstats
path: root/kjsembed/jsbuiltin_imp.cpp
blob: b87da4132bac5fe1b241b68f9c28fffade1cf066 (plain)
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
/*
 *  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.
 */

// #define HAVE_KSPY 1
#include <stdio.h>

#include <tqobject.h>
#include <tqfile.h>
#include <textstream.h>

#ifndef QT_ONLY

#include "global.h"
#include <klocale.h>
#include <kinputdialog.h>
#include <kmessagebox.h>
#include <kstddirs.h>
#include "ksimpleprocess.h"

#else

#include <tqinputdialog.h>
#include <tqmessagebox.h>

#endif // QT_ONLY

#include <kjs/object.h>

#ifdef HAVE_KSPY
#include <kspy.h>
#endif

#include "global.h"
#include "jsfactory.h"
#include "jsobjectproxy.h"
#include "kjsembedpart.h"

#include "builtins/saxhandler.h"

#include "jsbuiltin_imp.h"

//
// KJSEmbedPart Bindings
//
namespace KJSEmbed {
namespace Bindings {

JSBuiltInImp::JSBuiltInImp( JSBuiltIn *builtin, int mid, const TQString &p )
    : JSProxyImp(builtin->part()->globalExec()), part(builtin->part()), id(mid), param(p)
{
    setName( KJS::Identifier( KJS::UString(param) ) );
}

JSBuiltInImp::~JSBuiltInImp() {}

KJS::Value JSBuiltInImp::call( KJS::ExecState *exec, KJS::Object &self, const KJS::List &args )
{
    TQString arg0 = (args.size() > 0) ? args[0].toString(exec).qstring() : TQString::null;
    TQString arg1 = (args.size() > 1) ? args[1].toString(exec).qstring() : TQString::null;

    if ( id == MethodLoadScript ) {

	if ( args.size() == 1 ) {
	    bool ok = part->runFile( arg0 );
	    return KJS::Boolean( ok );
	}
	else if ( args.size() == 2 ) {
	    bool ok = part->runFile( arg0, args[1] );
	    return KJS::Boolean( ok );
	}
	else {
	    TQString msg = i18n( "Method requires 1 or 2 arguments, received %1." ).arg( args.size() );
      return throwError(exec, msg);
	}
    }
    else if ( id == MethodPrint ) {

	(*conout()) << arg0;

	return KJS::Boolean( true );
    }
    else if ( id == MethodPrintLn ) {

	(*conout()) << arg0 << endl;

	return KJS::Boolean( true );
    }
    else if ( id == MethodWarn ) {

	(*conerr()) << arg0 << endl;

	return KJS::Boolean( true );
    }
    else if ( id == MethodReadLine ) {

	TQString line = conin()->readLine();
	if ( !line.isNull() )
	    return KJS::String( line );

    }
    else if ( id == MethodOpenFile ) {
// #warning "This is leaked!!!!!"
// 	TQFile *f=new TQFile( arg0 );
// 	int mode = 0;
// 	if ( arg1 == TQString("ro") )
// 	    mode = IO_ReadOnly;
// 	else if ( arg1 == TQString("wo") )
// 	    mode = IO_WriteOnly;

// 	if ( !f->open( mode ) )
// 	    return KJS::Boolean(false);

// 	Bindings::TextStream *ts = new Bindings::TextStream( part, "file", new TQTextStream(f) );

// 	return KJS::Object( ts );
	return KJS::Null();
    }
    else if ( id == MethodReadFile ) {

	TQFile f( arg0 );
	if ( !f.open( IO_ReadOnly ) ) {
	    TQString msg = i18n( "Could not open file '%1'." ).arg(arg0);
      return throwError(exec, msg);
	}

	TQString s = f.readAll();
	return KJS::String( s );
    }
    else if ( id == MethodWriteFile ) {
	TQFile f( arg0 );
	if ( !f.open( IO_WriteOnly ) )
	    return KJS::Boolean( false );

	TQTextStream ts( &f );
	ts << arg1;

	return KJS::Boolean( true );
    }
    else if ( id == MethodDumpObject ) {
	if ( args.size() == 1 ) {
	    KJS::Object ob = args[0].toObject(exec);
	    return KJS::String( dumpObject( exec, ob ) );
	}
	return KJS::String( dumpObject( exec, self ) );
    }
    else if ( id == MethodRunKSpy ) {
#ifdef HAVE_KSPY
	KSpy::invoke();
#endif
	return KJS::Undefined();
    }
    else if ( id == MethodExit ) {
	int status = args.size() ? args[0].toInteger( exec ) : 0;
	::exit( status );
    }
    else if ( id == MethodSaxLoadFile ) {
	if ( args.size() != 2 ) {
	    TQString msg = i18n( "Method requires 2 arguments, received %1." ).arg( args.size() );
      return throwError(exec, msg);
	}

	BuiltIns::SaxHandler sax( exec );
	sax.setHandler( args[0].toObject(exec) );

	TQFile f( arg1 );
	TQXmlInputSource src( &f );
	TQXmlSimpleReader reader;
	reader.setContentHandler( &sax );

	bool ok = reader.parse( src );
	if ( !ok ) {
      return throwError(exec, sax.errorString().utf8());
	}

	return KJS::Boolean( true );
    }
    else if ( id == MethodDumpCompletion ) {
	KJS::Object obj = obj.isValid() ? args[0].toObject(exec) : KJS::Object();
	if ( !obj.isValid() ) {
	    TQString msg = i18n( "Method requires an object." );
      return throwError(exec, msg,KJS::TypeError);
	}

	TQVariant v = dumpCompletion( exec, obj );
	return convertToValue( exec, v ); // FIXME: This will crash if the type is wrong
    }
    else if ( id == MethodAlert ) {

#ifndef QT_ONLY
    	KMessageBox::information( 0L, arg0 );
#else // QT_ONLY
    	TQMessageBox::information( 0L, i18n("Information"), arg0 );
#endif // QT_ONLY

	return KJS::Undefined();
    }
    else if ( id == MethodConfirm ) {

	int answer;
#ifndef QT_ONLY
    	answer = KMessageBox::questionYesNo( 0L, arg0 );
#else // QT_ONLY
    	answer = TQMessageBox::question(0L, i18n("Question"), arg0 );
#endif // QT_ONLY

	return KJS::Number( answer );
    }
    else if ( id == MethodPrompt ) {

	TQString text;
#ifndef QT_ONLY
	text = KInputDialog::getText( arg0, arg0, arg1 );
#else // QT_ONLY
	text = TQInputDialog::getText( arg0, arg0, TQLineEdit::Normal, arg1 );
#endif // QT_ONLY

	return KJS::String( text );
    }
    else if ( id == MethodI18n ) {
    	TQString retString(i18n(arg0.latin1()));
	return KJS::String( retString );
    }
    else if ( id == MethodImport ) {

#ifndef QT_ONLY
	//  Scan $TDEDIRS/share/apps/$APPNAME/
	//  Scan $TDEDIRS/share/apps/kjsembed/
	KGlobal::dirs()->addResourceType("kjsembed", KStandardDirs::kde_default("data") +"/kjsembed/");
	TQString fname = KGlobal::dirs ()->findResource ("kjsembed", TQString(arg0));

	kdDebug(80001) << "Loading: " << fname  << endl;

	//TQString code = part->loadFile(fname);

	return KJS::Boolean( part->runFile(fname, part->globalObject()));
#else // QT_ONLY
	return KJS::Boolean( false );
#endif // QT_ONLY

    }
    else if ( id == MethodShell ) {

#ifndef QT_ONLY
	// return default data
	kdDebug( 80001 ) << "Run: " << arg0 << endl;
	return KJS::String(KSimpleProcess::exec( arg0 ));
#else // QT_ONLY
	return KJS::String();
#endif // QT_ONLY

    }
    else {
	kdWarning() << "JSBuiltInImp has no method " << id << endl;
    }

    TQString msg = i18n( "JSBuiltInImp has no method with id '%1'." ).arg( id );
    return throwError(exec, msg,KJS::ReferenceError);
}

} // namespace KJSEmbed::Bindings
} // namespace KJSEmbed

// Local Variables:
// c-basic-offset: 4
// End: