khtml Library API Documentation

kjavaappletcontext.cpp

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2000 Richard Moore <rich@kde.org>
00004  *               2000 Wynn Wilkes <wynnw@caldera.com>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #include "kjavaappletcontext.h"
00023 #include "kjavaappletserver.h"
00024 #include "kjavaapplet.h"
00025 #include <klocale.h>
00026 #include <kmessagebox.h>
00027 #include <kdebug.h>
00028 #include <qmap.h>
00029 #include <qguardedptr.h>
00030 #include <qstringlist.h>
00031 #include <qregexp.h>
00032 
00033 // This file was using 6002, but kdebug.areas didn't know about that number
00034 #define DEBUGAREA 6100
00035 
00036 typedef QMap< int, QGuardedPtr<KJavaApplet> > AppletMap;
00037 
00038 // For future expansion
00039 class KJavaAppletContextPrivate
00040 {
00041 friend class KJavaAppletContext;
00042 private:
00043     AppletMap applets;
00044 };
00045 
00046 //  Static Factory Functions
00047 int KJavaAppletContext::contextCount = 0;
00048 
00049 /*  Class Implementation
00050  */
00051 KJavaAppletContext::KJavaAppletContext()
00052     : QObject()
00053 {
00054     d = new KJavaAppletContextPrivate;
00055     server = KJavaAppletServer::allocateJavaServer();
00056 
00057     id = contextCount;
00058     server->createContext( id, this );
00059 
00060     contextCount++;
00061 }
00062 
00063 KJavaAppletContext::~KJavaAppletContext()
00064 {
00065     server->destroyContext( id );
00066     KJavaAppletServer::freeJavaServer();
00067     delete d;
00068 }
00069 
00070 int KJavaAppletContext::contextId()
00071 {
00072     return id;
00073 }
00074 
00075 void KJavaAppletContext::setContextId( int _id )
00076 {
00077     id = _id;
00078 }
00079 
00080 void KJavaAppletContext::registerApplet( KJavaApplet* applet )
00081 {
00082     static int appletId = 0;
00083 
00084     applet->setAppletId( ++appletId );
00085     d->applets.insert( appletId, applet );
00086 }
00087 
00088 bool KJavaAppletContext::create( KJavaApplet* applet )
00089 {
00090     return server->createApplet( id, applet->appletId(),
00091                                 applet->appletName(),
00092                                 applet->appletClass(),
00093                                 applet->baseURL(),
00094                                 applet->user(),
00095                                 applet->password(),
00096                                 applet->authName(),
00097                                 applet->codeBase(),
00098                                 applet->archives(),
00099                                 applet->size(),
00100                                 applet->getParams(),
00101                                 applet->getWindowName() );
00102 
00103 
00104 }
00105 
00106 void KJavaAppletContext::destroy( KJavaApplet* applet )
00107 {
00108     int appletId = applet->appletId();
00109     d->applets.remove( appletId );
00110 
00111     server->destroyApplet( id, appletId );
00112 }
00113 
00114 void KJavaAppletContext::init( KJavaApplet* applet )
00115 {
00116     server->initApplet( id, applet->appletId() );
00117 }
00118 
00119 void KJavaAppletContext::start( KJavaApplet* applet )
00120 {
00121     server->startApplet( id, applet->appletId() );
00122 }
00123 
00124 void KJavaAppletContext::stop( KJavaApplet* applet )
00125 {
00126     server->stopApplet( id, applet->appletId() );
00127 }
00128 
00129 void KJavaAppletContext::processCmd( QString cmd, QStringList args )
00130 {
00131     received( cmd, args );
00132 }
00133 
00134 void KJavaAppletContext::received( const QString& cmd, const QStringList& arg )
00135 {
00136     kdDebug(6100) << "KJavaAppletContext::received, cmd = >>" << cmd << "<<" << endl;
00137     kdDebug(6100) << "arg count = " << arg.count() << endl;
00138 
00139     if ( cmd == QString::fromLatin1("showstatus")
00140          && arg.count() > 0 )
00141     {
00142         QString tmp = arg[0];
00143         tmp.replace(QRegExp("[\n\r]"), "");
00144         kdDebug(6100) << "status message = " << tmp << endl;
00145         emit showStatus( tmp );
00146     }
00147     else if ( cmd == QString::fromLatin1( "showurlinframe" )
00148               && arg.count() > 1 )
00149     {
00150         kdDebug(6100) << "url = " << arg[0] << ", frame = " << arg[1] << endl;
00151         emit showDocument( arg[0], arg[1] );
00152     }
00153     else if ( cmd == QString::fromLatin1( "showdocument" )
00154               && arg.count() > 0 )
00155     {
00156         kdDebug(6100) << "url = " << arg[0] << endl;
00157         emit showDocument( arg[0], "_top" );
00158     }
00159     else if ( cmd == QString::fromLatin1( "resizeapplet" )
00160               && arg.count() > 0 )
00161     {
00162         //arg[1] should be appletID
00163         //arg[2] should be new width
00164         //arg[3] should be new height
00165         bool ok;
00166         int appletID = arg[0].toInt( &ok );
00167         int width = arg[1].toInt( &ok );
00168         int height = arg[2].toInt( &ok );
00169 
00170         if( !ok )
00171         {
00172             kdError(DEBUGAREA) << "could not parse out parameters for resize" << endl;
00173         }
00174         else
00175         {
00176             KJavaApplet* tmp = d->applets[appletID];
00177             tmp->resizeAppletWidget( width, height );
00178         }
00179     }
00180     else if (cmd.startsWith(QString::fromLatin1("audioclip_"))) {
00181         kdDebug(DEBUGAREA) << "process Audio command (not yet implemented): " << cmd  << " " << arg[0] << endl;
00182     }
00183     else if ( cmd == QString::fromLatin1( "JS_Event" )
00184               && arg.count() > 2 )
00185     {
00186         bool ok;
00187         int appletID = arg[0].toInt(&ok);
00188         unsigned long objid = arg[1].toInt(&ok);
00189         if (ok)
00190         {
00191             KJavaApplet * applet = d->applets[appletID];
00192             if (applet)
00193             {
00194                 KParts::LiveConnectExtension::ArgList arglist;
00195                 for (unsigned i = 3; i < arg.count(); i += 2)
00196                     // take a deep breath here
00197                     arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type) arg[i].toInt(), arg[i+1]));
00198 
00199                 emit static_cast<KJavaLiveConnect*>(applet->getLiveConnectExtension())->sendEvent(objid, arg[2], arglist);
00200             }
00201             else
00202                 kdError(DEBUGAREA) << "could find applet for JS event" << endl;
00203         }
00204         else
00205             kdError(DEBUGAREA) << "could not parse applet ID for JS event " << arg[0] << " " << arg[1] << endl;
00206     }
00207     else if ( cmd == QString::fromLatin1( "AppletStateNotification" ) )
00208     {
00209         bool ok;
00210         int appletID = arg[0].toInt(&ok);
00211         if (ok)
00212         {
00213             KJavaApplet * applet = d->applets[appletID];
00214             if ( applet )
00215             {
00216                 int newState   = arg[1].toInt(&ok);
00217                 if (ok)
00218                 {
00219                     applet->stateChange(newState);
00220                     if (newState == KJavaApplet::INITIALIZED) {
00221                         kdDebug(DEBUGAREA) << "emit appletLoaded" << endl;
00222                         emit appletLoaded();
00223                     }
00224                 } else
00225                     kdError(DEBUGAREA) << "AppletStateNotification: status is not numerical" << endl;
00226             } else
00227                 kdWarning(DEBUGAREA) << "AppletStateNotification:  No such Applet with ID=" << arg[0] << endl;
00228         } else
00229             kdError(DEBUGAREA) << "AppletStateNotification: Applet ID is not numerical" << endl;
00230     }
00231     else if ( cmd == QString::fromLatin1( "AppletFailed" ) ) {
00232         bool ok;
00233         int appletID = arg[0].toInt(&ok);
00234         if (ok)
00235         {
00236             KJavaApplet * applet = d->applets[appletID];
00237             /*
00238             QString errorDetail(arg[1]);
00239             errorDetail.replace(QRegExp(":\\s*"), ":\n");
00240             KMessageBox::detailedError(0L, i18n("Java error while loading applet."), errorDetail);
00241             */
00242             applet->setFailed();
00243             emit appletLoaded();
00244         }
00245     }
00246 }
00247 
00248 bool KJavaAppletContext::appletsLoaded() const {
00249     AppletMap::const_iterator it = d->applets.begin();
00250     for (; it != d->applets.end(); it++) {
00251         if (!(*it).isNull()) {
00252             if (!(*it)->isAlive() && !(*it)->failed()) {
00253                 return false;
00254             }
00255         }
00256     }
00257     return true;
00258 }
00259 
00260 bool KJavaAppletContext::getMember(KJavaApplet * applet, const unsigned long objid, const QString & name, int & type, unsigned long & rid, QString & value) {
00261     return server->getMember(id, applet->appletId(), objid, name, type, rid, value);
00262 }
00263 
00264 bool KJavaAppletContext::putMember(KJavaApplet * applet, const unsigned long objid, const QString & name, const QString & value) {
00265     return server->putMember(id, applet->appletId(), objid, name, value);
00266 }
00267 
00268 bool KJavaAppletContext::callMember(KJavaApplet * applet, const unsigned long objid, const QString & name, const QStringList & args, int & type, unsigned long & retobjid, QString & value) {
00269     return server->callMember(id, applet->appletId(), objid, name, args, type, retobjid, value);
00270 }
00271 
00272 void KJavaAppletContext::derefObject(KJavaApplet * applet, const unsigned long jid) {
00273     server->derefObject(id, applet->appletId(), jid);
00274 }
00275 
00276 #include <kjavaappletcontext.moc>
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.3.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Tue May 18 05:39:41 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001