00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
#include "kjavaappletcontext.h"
00023
#include "kjavaappletserver.h"
00024
#include "kjavaprocess.h"
00025
#include "kjavaapplet.h"
00026
#include <klocale.h>
00027
#include <kmessagebox.h>
00028
#include <kdebug.h>
00029
#include <qmap.h>
00030
#include <qguardedptr.h>
00031
#include <qstringlist.h>
00032
#include <qregexp.h>
00033
00034
00035
#define DEBUGAREA 6100
00036
00037
typedef QMap< int, QGuardedPtr<KJavaApplet> >
AppletMap;
00038
00039
00040
class KJavaAppletContextPrivate
00041 {
00042
friend class KJavaAppletContext;
00043
private:
00044 AppletMap applets;
00045 };
00046
00047
00048
int KJavaAppletContext::contextCount = 0;
00049
00050
00051
00052 KJavaAppletContext::KJavaAppletContext()
00053 :
QObject()
00054 {
00055 d =
new KJavaAppletContextPrivate;
00056 server = KJavaAppletServer::allocateJavaServer();
00057 connect(server->javaProcess(), SIGNAL(exited(
int)),
this, SLOT(javaProcessExited(
int)));
00058
00059
id = contextCount;
00060 server->createContext(
id,
this );
00061
00062 ++contextCount;
00063 }
00064
00065 KJavaAppletContext::~KJavaAppletContext()
00066 {
00067 server->destroyContext(
id );
00068 KJavaAppletServer::freeJavaServer();
00069
delete d;
00070 }
00071
00072
int KJavaAppletContext::contextId()
00073 {
00074
return id;
00075 }
00076
00077
void KJavaAppletContext::setContextId(
int _id )
00078 {
00079
id = _id;
00080 }
00081
00082
void KJavaAppletContext::registerApplet( KJavaApplet* applet )
00083 {
00084
static int appletId = 0;
00085
00086 applet->setAppletId( ++appletId );
00087 d->applets.insert( appletId, applet );
00088 }
00089
00090
bool KJavaAppletContext::create( KJavaApplet* applet )
00091 {
00092
return server->createApplet(
id, applet->appletId(),
00093 applet->appletName(),
00094 applet->appletClass(),
00095 applet->baseURL(),
00096 applet->user(),
00097 applet->password(),
00098 applet->authName(),
00099 applet->codeBase(),
00100 applet->archives(),
00101 applet->size(),
00102 applet->getParams(),
00103 applet->getWindowName() );
00104
00105
00106 }
00107
00108
void KJavaAppletContext::destroy( KJavaApplet* applet )
00109 {
00110
const int appletId = applet->appletId();
00111 d->applets.remove( appletId );
00112
00113 server->destroyApplet(
id, appletId );
00114 }
00115
00116
void KJavaAppletContext::init( KJavaApplet* applet )
00117 {
00118 server->initApplet(
id, applet->appletId() );
00119 }
00120
00121
void KJavaAppletContext::start( KJavaApplet* applet )
00122 {
00123 server->startApplet(
id, applet->appletId() );
00124 }
00125
00126
void KJavaAppletContext::stop( KJavaApplet* applet )
00127 {
00128 server->stopApplet(
id, applet->appletId() );
00129 }
00130
00131
void KJavaAppletContext::processCmd(
QString cmd,
QStringList args )
00132 {
00133 received( cmd, args );
00134 }
00135
00136
void KJavaAppletContext::received(
const QString& cmd,
const QStringList& arg )
00137 {
00138
kdDebug(6100) <<
"KJavaAppletContext::received, cmd = >>" << cmd <<
"<<" <<
endl;
00139
kdDebug(6100) <<
"arg count = " << arg.count() <<
endl;
00140
00141
if ( cmd == QString::fromLatin1(
"showstatus")
00142 && !arg.empty() )
00143 {
00144
QString tmp = arg.first();
00145 tmp.replace(
QRegExp(
"[\n\r]"),
"");
00146
kdDebug(6100) <<
"status message = " << tmp <<
endl;
00147 emit showStatus( tmp );
00148 }
00149
else if ( cmd == QString::fromLatin1(
"showurlinframe" )
00150 && arg.count() > 1 )
00151 {
00152
kdDebug(6100) <<
"url = " << arg[0] <<
", frame = " << arg[1] <<
endl;
00153 emit showDocument( arg[0], arg[1] );
00154 }
00155
else if ( cmd == QString::fromLatin1(
"showdocument" )
00156 && !arg.empty() )
00157 {
00158
kdDebug(6100) <<
"url = " << arg.first() <<
endl;
00159 emit showDocument( arg.first(),
"_top" );
00160 }
00161
else if ( cmd == QString::fromLatin1(
"resizeapplet" )
00162 && arg.count() > 2 )
00163 {
00164
00165
00166
00167
bool ok;
00168
const int appletID = arg[0].toInt( &ok );
00169
const int width = arg[1].toInt( &ok );
00170
const int height = arg[2].toInt( &ok );
00171
00172
if( !ok )
00173 {
00174
kdError(DEBUGAREA) <<
"could not parse out parameters for resize" <<
endl;
00175 }
00176
else
00177 {
00178 KJavaApplet*
const tmp = d->applets[appletID];
00179
if (tmp)
00180 tmp->resizeAppletWidget( width, height );
00181 }
00182 }
00183
else if (cmd.startsWith(QString::fromLatin1(
"audioclip_"))) {
00184
kdDebug(DEBUGAREA) <<
"process Audio command (not yet implemented): " << cmd <<
" " << arg[0] <<
endl;
00185 }
00186
else if ( cmd == QString::fromLatin1(
"JS_Event" )
00187 && arg.count() > 2 )
00188 {
00189
bool ok;
00190
const int appletID = arg.first().toInt(&ok);
00191 KJavaApplet * applet;
00192
if (ok && (applet = d->applets[appletID]))
00193 {
00194
QStringList js_args(arg);
00195 js_args.pop_front();
00196 applet->jsData(js_args);
00197 }
00198
else
00199
kdError(DEBUGAREA) <<
"parse JS event " << arg[0] <<
" " << arg[1] <<
endl;
00200 }
00201
else if ( cmd == QString::fromLatin1(
"AppletStateNotification" ) )
00202 {
00203
bool ok;
00204
const int appletID = arg.first().toInt(&ok);
00205
if (ok)
00206 {
00207 KJavaApplet*
const applet = d->applets[appletID];
00208
if ( applet )
00209 {
00210
const int newState = arg[1].toInt(&ok);
00211
if (ok)
00212 {
00213 applet->stateChange(newState);
00214
if (newState == KJavaApplet::INITIALIZED) {
00215
kdDebug(DEBUGAREA) <<
"emit appletLoaded" <<
endl;
00216 emit appletLoaded();
00217 }
00218 }
else
00219
kdError(DEBUGAREA) <<
"AppletStateNotification: status is not numerical" <<
endl;
00220 }
else
00221
kdWarning(DEBUGAREA) <<
"AppletStateNotification: No such Applet with ID=" << arg[0] <<
endl;
00222 }
else
00223
kdError(DEBUGAREA) <<
"AppletStateNotification: Applet ID is not numerical" <<
endl;
00224 }
00225
else if ( cmd == QString::fromLatin1(
"AppletFailed" ) ) {
00226
bool ok;
00227
const int appletID = arg.first().toInt(&ok);
00228
if (ok)
00229 {
00230 KJavaApplet*
const applet = d->applets[appletID];
00231
00232
00233
00234
00235
00236
if (applet)
00237 applet->setFailed();
00238 emit appletLoaded();
00239 }
00240 }
00241 }
00242
00243
void KJavaAppletContext::javaProcessExited(
int) {
00244 AppletMap::iterator it = d->applets.begin();
00245
const AppletMap::iterator itEnd = d->applets.end();
00246
for (; it != itEnd; ++it)
00247
if (!(*it).isNull() && (*it)->isCreated() && !(*it)->failed()) {
00248 (*it)->setFailed();
00249
if ((*it)->state() < KJavaApplet::INITIALIZED)
00250 emit appletLoaded();
00251 }
00252 }
00253
00254
bool KJavaAppletContext::getMember(
QStringList & args,
QStringList & ret_args) {
00255 args.push_front( QString::number(
id) );
00256
return server->getMember( args, ret_args );
00257 }
00258
00259
bool KJavaAppletContext::putMember(
QStringList & args ) {
00260 args.push_front( QString::number(
id) );
00261
return server->putMember( args );
00262 }
00263
00264
bool KJavaAppletContext::callMember(
QStringList & args,
QStringList &ret_args) {
00265 args.push_front( QString::number(
id) );
00266
return server->callMember( args, ret_args );
00267 }
00268
00269
void KJavaAppletContext::derefObject(
QStringList & args ) {
00270 args.push_front( QString::number(
id) );
00271 server->derefObject( args );
00272 }
00273
00274
#include <kjavaappletcontext.moc>