don't discard iframe children.
[kdelibs.git] / khtml / java / kjavaappletcontext.cpp
blob98989f33d241d0e0087dde941ef9d0c061214b4c
1 /* This file is part of the KDE project
3 * Copyright (C) 2000 Richard Moore <rich@kde.org>
4 * 2000 Wynn Wilkes <wynnw@caldera.com>
6 * This library 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.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "kjavaappletcontext.h"
23 #include "kjavaappletserver.h"
24 #include "kjavaprocess.h"
25 #include "kjavaapplet.h"
26 #include <klocale.h>
27 #include <kmessagebox.h>
28 #include <kdebug.h>
29 #include <QtCore/QMap>
30 #include <QtCore/QPointer>
31 #include <QtCore/QStringList>
32 #include <QtCore/QRegExp>
34 // This file was using 6002, but kdebug.areas didn't know about that number
35 #define DEBUGAREA 6100
37 typedef QMap< int, QPointer<KJavaApplet> > AppletMap;
39 // For future expansion
40 class KJavaAppletContextPrivate
42 friend class KJavaAppletContext;
43 private:
44 AppletMap applets;
47 // Static Factory Functions
48 int KJavaAppletContext::contextCount = 0;
50 /* Class Implementation
52 KJavaAppletContext::KJavaAppletContext()
53 : QObject(),
54 d(new KJavaAppletContextPrivate)
56 server = KJavaAppletServer::allocateJavaServer();
57 connect(server->javaProcess(), SIGNAL(exited(int)), this, SLOT(javaProcessExited(int)));
59 id = contextCount;
60 server->createContext( id, this );
62 ++contextCount;
65 KJavaAppletContext::~KJavaAppletContext()
67 server->destroyContext( id );
68 KJavaAppletServer::freeJavaServer();
69 delete d;
72 int KJavaAppletContext::contextId()
74 return id;
77 void KJavaAppletContext::setContextId( int _id )
79 id = _id;
82 void KJavaAppletContext::registerApplet( KJavaApplet* applet )
84 static int appletId = 0;
86 applet->setAppletId( ++appletId );
87 d->applets.insert( appletId, applet );
90 bool KJavaAppletContext::create( KJavaApplet* applet )
92 return server->createApplet( id, applet->appletId(),
93 applet->appletName(),
94 applet->appletClass(),
95 applet->baseURL(),
96 applet->user(),
97 applet->password(),
98 applet->authName(),
99 applet->codeBase(),
100 applet->archives(),
101 applet->size(),
102 applet->getParams(),
103 applet->getWindowName() );
108 void KJavaAppletContext::destroy( KJavaApplet* applet )
110 const int appletId = applet->appletId();
111 d->applets.remove( appletId );
113 server->destroyApplet( id, appletId );
116 void KJavaAppletContext::init( KJavaApplet* applet )
118 server->initApplet( id, applet->appletId() );
121 void KJavaAppletContext::start( KJavaApplet* applet )
123 server->startApplet( id, applet->appletId() );
126 void KJavaAppletContext::stop( KJavaApplet* applet )
128 server->stopApplet( id, applet->appletId() );
131 void KJavaAppletContext::processCmd( QString cmd, QStringList args )
133 received( cmd, args );
136 void KJavaAppletContext::received( const QString& cmd, const QStringList& arg )
138 kDebug(6100) << "KJavaAppletContext::received, cmd = >>" << cmd << "<<";
139 kDebug(6100) << "arg count = " << arg.count();
141 if ( cmd == QLatin1String("showstatus")
142 && !arg.empty() )
144 QString tmp = arg.first();
145 tmp.remove(QRegExp("[\n\r]"));
146 kDebug(6100) << "status message = " << tmp;
147 emit showStatus( tmp );
149 else if ( cmd == QLatin1String( "showurlinframe" )
150 && arg.count() > 1 )
152 kDebug(6100) << "url = " << arg[0] << ", frame = " << arg[1];
153 emit showDocument( arg[0], arg[1] );
155 else if ( cmd == QLatin1String( "showdocument" )
156 && !arg.empty() )
158 kDebug(6100) << "url = " << arg.first();
159 emit showDocument( arg.first(), "_top" );
161 else if ( cmd == QLatin1String( "resizeapplet" )
162 && arg.count() > 2 )
164 //arg[1] should be appletID
165 //arg[2] should be new width
166 //arg[3] should be new height
167 bool ok;
168 const int appletID = arg[0].toInt( &ok );
169 const int width = arg[1].toInt( &ok );
170 const int height = arg[2].toInt( &ok );
172 if( !ok )
174 kError(DEBUGAREA) << "could not parse out parameters for resize" << endl;
176 else
178 KJavaApplet* const tmp = d->applets[appletID];
179 if (tmp)
180 tmp->resizeAppletWidget( width, height );
183 else if (cmd.startsWith(QLatin1String("audioclip_"))) {
184 kDebug(DEBUGAREA) << "process Audio command (not yet implemented): " << cmd << " " << arg[0];
186 else if ( cmd == QLatin1String( "JS_Event" )
187 && arg.count() > 2 )
189 bool ok;
190 const int appletID = arg.first().toInt(&ok);
191 KJavaApplet * applet;
192 if (ok && (applet = d->applets[appletID]))
194 QStringList js_args(arg);
195 js_args.pop_front();
196 applet->jsData(js_args);
198 else
199 kError(DEBUGAREA) << "parse JS event " << arg[0] << " " << arg[1] << endl;
201 else if ( cmd == QLatin1String( "AppletStateNotification" ) )
203 bool ok;
204 const int appletID = arg.first().toInt(&ok);
205 if (ok)
207 KJavaApplet* const applet = d->applets[appletID];
208 if ( applet )
210 const int newState = arg[1].toInt(&ok);
211 if (ok)
213 applet->stateChange(newState);
214 if (newState == KJavaApplet::INITIALIZED) {
215 kDebug(DEBUGAREA) << "emit appletLoaded";
216 emit appletLoaded();
218 } else
219 kError(DEBUGAREA) << "AppletStateNotification: status is not numerical" << endl;
220 } else
221 kWarning(DEBUGAREA) << "AppletStateNotification: No such Applet with ID=" << arg[0];
222 } else
223 kError(DEBUGAREA) << "AppletStateNotification: Applet ID is not numerical" << endl;
225 else if ( cmd == QLatin1String( "AppletFailed" ) ) {
226 bool ok;
227 const int appletID = arg.first().toInt(&ok);
228 if (ok)
230 KJavaApplet* const applet = d->applets[appletID];
232 QString errorDetail(arg[1]);
233 errorDetail.replace(QRegExp(":\\s*"), ":\n");
234 KMessageBox::detailedError(0L, i18n("Java error while loading applet."), errorDetail);
236 if (applet)
237 applet->setFailed();
238 emit appletLoaded();
243 void KJavaAppletContext::javaProcessExited(int) {
244 AppletMap::iterator it = d->applets.begin();
245 const AppletMap::iterator itEnd = d->applets.end();
246 for (; it != itEnd; ++it)
247 if (!(*it).isNull() && (*it)->isCreated() && !(*it)->failed()) {
248 (*it)->setFailed();
249 if ((*it)->state() < KJavaApplet::INITIALIZED)
250 emit appletLoaded();
254 bool KJavaAppletContext::getMember(QStringList & args, QStringList & ret_args) {
255 args.push_front( QString::number(id) );
256 return server->getMember( args, ret_args );
259 bool KJavaAppletContext::putMember( QStringList & args ) {
260 args.push_front( QString::number(id) );
261 return server->putMember( args );
264 bool KJavaAppletContext::callMember(QStringList & args, QStringList &ret_args) {
265 args.push_front( QString::number(id) );
266 return server->callMember( args, ret_args );
269 void KJavaAppletContext::derefObject( QStringList & args ) {
270 args.push_front( QString::number(id) );
271 server->derefObject( args );
274 #include <kjavaappletcontext.moc>