summaryrefslogtreecommitdiffstats
path: root/kjsembed/jsfactory_imp.cpp
blob: c1f1f5b708d4cacb55a3328e7ef1a9d064226fdd (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *  Copyright (C) 2001-2004, 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 <qwidget.h>
#include <qwidgetfactory.h>
#include <qobjectlist.h>
#include <qtoolbox.h>
#include <qtabwidget.h>
#include <qwizard.h>

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

#include "jsfactory.h"
#include "jsfactory_imp.h"

namespace KJSEmbed {
namespace Bindings {

JSFactoryImp::JSFactoryImp( KJS::ExecState *exec, JSFactory *jsfact, int mid, const QString &p )
    : JSProxyImp(exec), fact(jsfact), id(mid), param(p), defaultVal()
{
    setName( KJS::Identifier(KJS::UString(param)) );
}

JSFactoryImp::~JSFactoryImp()
{
}

KJS::Object JSFactoryImp::construct( KJS::ExecState *exec, const KJS::List &args )
{
    if ( id != NewInstance ) {
	kdDebug(80001) << "JSFactoryImp has no such constructor, id " << id << endl;

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

    return fact->create( exec, param, args );
}

void JSFactoryImp::setDefaultValue( const KJS::Value &value )
{
    defaultVal = value;
}

KJS::Value JSFactoryImp::defaultValue( KJS::ExecState *exec, KJS::Type hint ) const
{
    if ( defaultVal.isValid() )
	return defaultVal;
    return JSProxyImp::defaultValue( exec, hint );
}

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

    kdDebug( 80001 ) << "JSFactoryImp::call() " << id << endl;
    KJS::Object err;

    switch(id){
    case MethodLoadUI:
    {
	JSObjectProxy *cproxy = JSProxy::toObjectProxy( args[1].imp() );
	QObject *cqo = cproxy ? cproxy->object() : 0;

	JSObjectProxy *proxy = JSProxy::toObjectProxy( args[2].imp() );
	QWidget *qw = proxy ? proxy->widget() : 0;
	QWidget *obj = fact->loadUI( arg0, cqo, qw, arg1.latin1() );
	if ( obj )
	{
		KJS::Object parentObject = fact->createProxy( exec, obj );
	// Add properties for child widgets.
		const QObjectList *lst = obj->children();
		if ( lst )
		{
			QObjectListIt it( *lst );
			QObject *child;
			while ( (child = it.current()) != 0 )
			{
				publishChildren( child, parentObject);
				++it;
			}
		}
		return parentObject;
	}

	QString msg = i18n( "Unable to create ui from file '%1'." ).arg(arg0);
  err = throwError(exec, msg,KJS::ReferenceError);
	break;
    }
    case MethodCreateROPart:
    {

	kdDebug(80001) << "MethodCreateROPart called, args.size is " << args.size() << endl;
	QObject *qo = 0;
	QObject *ropart = 0;

	if ( args.size() == 3 ) {
	    JSObjectProxy *proxy = JSProxy::toObjectProxy( args[1].imp() );
	    qo = proxy ? proxy->object() : 0;
	    ropart = fact->createROPart( arg0, qo, args[2].toString(exec).ascii() );
	}
	else if ( args.size() == 4 ) {
	    JSObjectProxy *proxy = JSProxy::toObjectProxy( args[2].imp() );
	    qo = proxy ? proxy->object() : 0;
	    ropart = fact->createROPart( arg0, arg1, qo, args[3].toString(exec).ascii() );
	}
	else if ( args.size() == 5 ) {
	    JSObjectProxy *proxy = JSProxy::toObjectProxy( args[2].imp() );
	    qo = proxy ? proxy->object() : 0;
	    QStringList slist = extractQStringList( exec, args, 4 );
	    ropart = fact->createROPart( arg0, arg1, qo, args[3].toString(exec).ascii(), slist );
	}

	if ( ropart ) {
	    kdDebug(80001) << "should be returning kpart type " << ropart->className() << endl;
	}
	else {
	    kdDebug(80001) << "could not create the kpart" << endl;
	}
	if ( ropart )
	    return fact->createProxy( exec, ropart );

	QString msg = i18n( "Unable to create read-only part for service '%1'." ).arg(arg0);
  err = throwError(exec, msg,KJS::ReferenceError);
	break;
    }
    case MethodConstructors:
    {
	kdDebug(80001) << "constructors called" << endl;
	return fact->part()->constructors();
	break;
    }
    case MethodWidgets:
    {

	KJS::List l;
	QStringList widgets = QWidgetFactory::widgets();
	for ( QStringList::Iterator it = widgets.begin(); it != widgets.end(); ++it ) {
	    l.append( KJS::String( *it ) );
	}

	return KJS::Object( exec->interpreter()->builtinArray().construct( exec, l ) );
	break;
    }
    case MethodIsSupported:
    {
	return KJS::Boolean( fact->isSupported( arg0 ) );
	break;
    }
    case MethodIsQObject:
    {
	return KJS::Boolean( fact->isQObject( arg0 ) );
	break;
    }
    case MethodIsOpaque:
    {
	return KJS::Boolean( fact->isOpaque( arg0 ) );
	break;
    }
    case MethodIsValue:
    {
	return KJS::Boolean( fact->isValue( arg0 ) );
	break;
    }
    case MethodTypes:
    {
	KJS::List l;
	QStringList types = fact->types();
	for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
	    l.append( KJS::String( *it ) );
	}

	return KJS::Object( exec->interpreter()->builtinArray().construct( exec, l ) );
	break;
    }
    case MethodListPlugins:
    {
	return convertToValue( exec, fact->listBindingPlugins( exec, self)  );
	break;
    }
    case MethodCreateRWPart:
    {

	kdDebug(80001) << "MethodCreateRWPart called, args.size is " << args.size() << endl;
	QObject *qo = 0;
	QObject *rwpart = 0;

	if ( args.size() == 3 ) {
	    JSObjectProxy *proxy = JSProxy::toObjectProxy( args[1].imp() );
	    qo = proxy ? proxy->object() : 0;
	    rwpart = fact->createRWPart( arg0, qo, args[2].toString(exec).ascii() );
	}
	else if ( args.size() == 4 ) {
	    JSObjectProxy *proxy = JSProxy::toObjectProxy( args[2].imp() );
	    qo = proxy ? proxy->object() : 0;
	    rwpart = fact->createRWPart( arg0, arg1, qo, args[3].toString(exec).ascii() );
	}

	if ( rwpart ) {
	    kdDebug(80001) << "should be returning kpart type " << rwpart->className() << endl;
	}
	else {
	    kdDebug(80001) << "could not create the kpart" << endl;
	}
	if ( rwpart )
	    return fact->createProxy( exec, rwpart );

	QString msg = i18n( "Unable to create read-write part for service '%1'." ).arg(arg0);
  err = throwError(exec, msg,KJS::ReferenceError);
	break;
    }
    case MethodCreateObject:
    {
	// Trim first argument from the list and forward the rest on.
	KJS::List objectArgs;
	if( args.size() > 1 )
	{
		for( int idx = 1; idx < args.size(); idx++)
			objectArgs.append(args[idx]);
	}
        return fact->create(exec,arg0, objectArgs);
        break;
    }
    default:
    {
	kdWarning() << "JSBuiltInImp has no method " << id << endl;
	QString msg = i18n( "JSFactoryImp has no method with id '%1'." ).arg( id );
  err = throwError(exec, msg,KJS::ReferenceError);
    }
    }
    return err;
}


void JSFactoryImp::publishChildren(QObject *obj, KJS::Object &parent)
{
	QString name = obj->name();
	name.remove(":");
	name.remove(" ");
	name.remove("<");
	name.remove(")");
	KJS::Object newParent = fact->part()->addObject( obj, parent, name.latin1() );
	JSProxy::toProxy( newParent.imp() ) ->setOwner( JSProxy::JavaScript );

	// Hack to publish QToolBoxes children
	QToolBox *box = dynamic_cast<QToolBox*>(obj);
	QTabWidget *tab = dynamic_cast<QTabWidget*>(obj);
	QWizard *wiz = dynamic_cast<QWizard*>(obj);

	if( box ) {
		int count = box->count();
		for( int idx = 0; idx < count; ++idx)
		publishChildren(box->item(idx),newParent);
	} else if ( tab ) {
		int count = tab->count();
		for( int idx = 0; idx < count; ++idx)
		publishChildren(tab->page(idx),newParent);
	} else if ( wiz ) {
		int count = wiz->pageCount();
		for( int idx = 0; idx < count; ++idx)
		publishChildren(wiz->page(idx),newParent);
	} else {
		const QObjectList *lst = obj->children();
		if ( lst ) {
			QObjectListIt it( *lst );
			QObject *child;
			while ( (child = it.current()) != 0 ) {
				publishChildren( child, newParent);
				++it;
			}
		}
	}
}

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

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