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             if (tmp)
00178                 tmp->resizeAppletWidget( width, height );
00179         }
00180     }
00181     else if (cmd.startsWith(QString::fromLatin1("audioclip_"))) {
00182         kdDebug(DEBUGAREA) << "process Audio command (not yet implemented): " << cmd  << " " << arg[0] << endl;
00183     }
00184     else if ( cmd == QString::fromLatin1( "JS_Event" )
00185               && arg.count() > 2 )
00186     {
00187         bool ok;
00188         int appletID = arg[0].toInt(&ok);
00189         unsigned long objid = arg[1].toInt(&ok);
00190         if (ok)
00191         {
00192             KJavaApplet * applet = d->applets[appletID];
00193             if (applet)
00194             {
00195                 KParts::LiveConnectExtension::ArgList arglist;
00196                 for (unsigned i = 3; i < arg.count(); i += 2)
00197                     // take a deep breath here
00198                     arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type) arg[i].toInt(), arg[i+1]));
00199 
00200                 emit static_cast<KJavaLiveConnect*>(applet->getLiveConnectExtension())->sendEvent(objid, arg[2], arglist);
00201             }
00202             else
00203                 kdError(DEBUGAREA) << "could find applet for JS event" << endl;
00204         }
00205         else
00206             kdError(DEBUGAREA) << "could not parse applet ID for JS event " << arg[0] << " " << arg[1] << endl;
00207     }
00208     else if ( cmd == QString::fromLatin1( "AppletStateNotification" ) )
00209     {
00210         bool ok;
00211         int appletID = arg[0].toInt(&ok);
00212         if (ok)
00213         {
00214             KJavaApplet * applet = d->applets[appletID];
00215             if ( applet )
00216             {
00217                 int newState   = arg[1].toInt(&ok);
00218                 if (ok)
00219                 {
00220                     applet->stateChange(newState);
00221                     if (newState == KJavaApplet::INITIALIZED) {
00222                         kdDebug(DEBUGAREA) << "emit appletLoaded" << endl;
00223                         emit appletLoaded();
00224                     }
00225                 } else
00226                     kdError(DEBUGAREA) << "AppletStateNotification: status is not numerical" << endl;
00227             } else
00228                 kdWarning(DEBUGAREA) << "AppletStateNotification:  No such Applet with ID=" << arg[0] << endl;
00229         } else
00230             kdError(DEBUGAREA) << "AppletStateNotification: Applet ID is not numerical" << endl;
00231     }
00232     else if ( cmd == QString::fromLatin1( "AppletFailed" ) ) {
00233         bool ok;
00234         int appletID = arg[0].toInt(&ok);
00235         if (ok)
00236         {
00237             KJavaApplet * applet = d->applets[appletID];
00238             /*
00239             QString errorDetail(arg[1]);
00240             errorDetail.replace(QRegExp(":\\s*"), ":\n");
00241             KMessageBox::detailedError(0L, i18n("Java error while loading applet."), errorDetail);
00242             */
00243             if (applet)
00244                 applet->setFailed();
00245             emit appletLoaded();
00246         }
00247     }
00248 }
00249 
00250 bool KJavaAppletContext::appletsLoaded() const {
00251     AppletMap::const_iterator it = d->applets.begin();
00252     for (; it != d->applets.end(); it++) {
00253         if (!(*it).isNull()) {
00254             if (!(*it)->isAlive() && !(*it)->failed()) {
00255                 return false;
00256             }
00257         }
00258     }
00259     return true;
00260 }
00261 
00262 bool KJavaAppletContext::getMember(KJavaApplet * applet, const unsigned long objid, const QString & name, int & type, unsigned long & rid, QString & value) {
00263     return server->getMember(id, applet->appletId(), objid, name, type, rid, value);
00264 }
00265 
00266 bool KJavaAppletContext::putMember(KJavaApplet * applet, const unsigned long objid, const QString & name, const QString & value) {
00267     return server->putMember(id, applet->appletId(), objid, name, value);
00268 }
00269 
00270 bool KJavaAppletContext::callMember(KJavaApplet * applet, const unsigned long objid, const QString & name, const QStringList & args, int & type, unsigned long & retobjid, QString & value) {
00271     return server->callMember(id, applet->appletId(), objid, name, args, type, retobjid, value);
00272 }
00273 
00274 void KJavaAppletContext::derefObject(KJavaApplet * applet, const unsigned long jid) {
00275     server->derefObject(id, applet->appletId(), jid);
00276 }
00277 
00278 #include <kjavaappletcontext.moc>
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:34:10 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001