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"
27 #include <kmessagebox.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
;
47 // Static Factory Functions
48 int KJavaAppletContext::contextCount
= 0;
50 /* Class Implementation
52 KJavaAppletContext::KJavaAppletContext()
54 d(new KJavaAppletContextPrivate
)
56 server
= KJavaAppletServer::allocateJavaServer();
57 connect(server
->javaProcess(), SIGNAL(exited(int)), this, SLOT(javaProcessExited(int)));
60 server
->createContext( id
, this );
65 KJavaAppletContext::~KJavaAppletContext()
67 server
->destroyContext( id
);
68 KJavaAppletServer::freeJavaServer();
72 int KJavaAppletContext::contextId()
77 void KJavaAppletContext::setContextId( int _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(),
94 applet
->appletClass(),
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")
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" )
152 kDebug(6100) << "url = " << arg
[0] << ", frame = " << arg
[1];
153 emit
showDocument( arg
[0], arg
[1] );
155 else if ( cmd
== QLatin1String( "showdocument" )
158 kDebug(6100) << "url = " << arg
.first();
159 emit
showDocument( arg
.first(), "_top" );
161 else if ( cmd
== QLatin1String( "resizeapplet" )
164 //arg[1] should be appletID
165 //arg[2] should be new width
166 //arg[3] should be new height
168 const int appletID
= arg
[0].toInt( &ok
);
169 const int width
= arg
[1].toInt( &ok
);
170 const int height
= arg
[2].toInt( &ok
);
174 kError(DEBUGAREA
) << "could not parse out parameters for resize" << endl
;
178 KJavaApplet
* const tmp
= d
->applets
[appletID
];
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" )
190 const int appletID
= arg
.first().toInt(&ok
);
191 KJavaApplet
* applet
;
192 if (ok
&& (applet
= d
->applets
[appletID
]))
194 QStringList
js_args(arg
);
196 applet
->jsData(js_args
);
199 kError(DEBUGAREA
) << "parse JS event " << arg
[0] << " " << arg
[1] << endl
;
201 else if ( cmd
== QLatin1String( "AppletStateNotification" ) )
204 const int appletID
= arg
.first().toInt(&ok
);
207 KJavaApplet
* const applet
= d
->applets
[appletID
];
210 const int newState
= arg
[1].toInt(&ok
);
213 applet
->stateChange(newState
);
214 if (newState
== KJavaApplet::INITIALIZED
) {
215 kDebug(DEBUGAREA
) << "emit appletLoaded";
219 kError(DEBUGAREA
) << "AppletStateNotification: status is not numerical" << endl
;
221 kWarning(DEBUGAREA
) << "AppletStateNotification: No such Applet with ID=" << arg
[0];
223 kError(DEBUGAREA
) << "AppletStateNotification: Applet ID is not numerical" << endl
;
225 else if ( cmd
== QLatin1String( "AppletFailed" ) ) {
227 const int appletID
= arg
.first().toInt(&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);
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()) {
249 if ((*it
)->state() < KJavaApplet::INITIALIZED
)
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>