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 typedef QMap< int, QGuardedPtr<KJavaApplet> > AppletMap;
00034 
00035 // For future expansion
00036 class KJavaAppletContextPrivate
00037 {
00038 friend class KJavaAppletContext;
00039 private:
00040     AppletMap applets;
00041 };
00042 
00043 //  Static Factory Functions
00044 int KJavaAppletContext::contextCount = 0;
00045 
00046 /*  Class Implementation
00047  */
00048 KJavaAppletContext::KJavaAppletContext()
00049     : QObject()
00050 {
00051     d = new KJavaAppletContextPrivate;
00052     server = KJavaAppletServer::allocateJavaServer();
00053 
00054     id = contextCount;
00055     server->createContext( id, this );
00056 
00057     contextCount++;
00058 }
00059 
00060 KJavaAppletContext::~KJavaAppletContext()
00061 {
00062     server->destroyContext( id );
00063     KJavaAppletServer::freeJavaServer();
00064     delete d;
00065 }
00066 
00067 int KJavaAppletContext::contextId()
00068 {
00069     return id;
00070 }
00071 
00072 void KJavaAppletContext::setContextId( int _id )
00073 {
00074     id = _id;
00075 }
00076 
00077 void KJavaAppletContext::registerApplet( KJavaApplet* applet )
00078 {
00079     static int appletId = 0;
00080 
00081     applet->setAppletId( ++appletId );
00082     d->applets.insert( appletId, applet );
00083 }
00084 
00085 void KJavaAppletContext::create( KJavaApplet* applet )
00086 {
00087     server->createApplet( id, applet->appletId(),
00088                           applet->appletName(),
00089                           applet->appletClass(),
00090                           applet->baseURL(),
00091                           applet->codeBase(),
00092                           applet->archives(),
00093                           applet->size(),
00094                           applet->getParams(),
00095                           applet->getWindowName() );
00096 
00097 }
00098 
00099 void KJavaAppletContext::destroy( KJavaApplet* applet )
00100 {
00101     int appletId = applet->appletId();
00102     d->applets.remove( appletId );
00103 
00104     server->destroyApplet( id, appletId );
00105 }
00106 
00107 void KJavaAppletContext::init( KJavaApplet* applet )
00108 {
00109     server->initApplet( id, applet->appletId() );
00110 }
00111 
00112 void KJavaAppletContext::start( KJavaApplet* applet )
00113 {
00114     server->startApplet( id, applet->appletId() );
00115 }
00116 
00117 void KJavaAppletContext::stop( KJavaApplet* applet )
00118 {
00119     server->stopApplet( id, applet->appletId() );
00120 }
00121 
00122 void KJavaAppletContext::processCmd( QString cmd, QStringList args )
00123 {
00124     received( cmd, args );
00125 }
00126 
00127 void KJavaAppletContext::received( const QString& cmd, const QStringList& arg )
00128 {
00129     kdDebug(6100) << "KJavaAppletContext::received, cmd = >>" << cmd << "<<" << endl;
00130     kdDebug(6100) << "arg count = " << arg.count() << endl;
00131 
00132     if ( cmd == QString::fromLatin1("showstatus")
00133          && arg.count() > 0 )
00134     {
00135         QString tmp = arg[0];
00136         tmp.replace(QRegExp("[\n\r]"), "");
00137         kdDebug(6100) << "status message = " << tmp << endl;
00138         emit showStatus( tmp );
00139     }
00140     else if ( cmd == QString::fromLatin1( "showurlinframe" )
00141               && arg.count() > 1 )
00142     {
00143         kdDebug(6100) << "url = " << arg[0] << ", frame = " << arg[1] << endl;
00144         emit showDocument( arg[0], arg[1] );
00145     }
00146     else if ( cmd == QString::fromLatin1( "showdocument" )
00147               && arg.count() > 0 )
00148     {
00149         kdDebug(6100) << "url = " << arg[0] << endl;
00150         emit showDocument( arg[0], "_top" );
00151     }
00152     else if ( cmd == QString::fromLatin1( "resizeapplet" )
00153               && arg.count() > 0 )
00154     {
00155         //arg[1] should be appletID
00156         //arg[2] should be new width
00157         //arg[3] should be new height
00158         bool ok;
00159         int appletID = arg[0].toInt( &ok );
00160         int width = arg[1].toInt( &ok );
00161         int height = arg[2].toInt( &ok );
00162 
00163         if( !ok )
00164         {
00165             kdError(6002) << "could not parse out parameters for resize" << endl;
00166         }
00167         else
00168         {
00169             KJavaApplet* tmp = d->applets[appletID];
00170             tmp->resizeAppletWidget( width, height );
00171         }
00172     }
00173     else if (cmd.startsWith(QString::fromLatin1("audioclip_"))) {
00174         kdDebug(6002) << "process Audio command (not yet implemented): " << cmd  << " " << arg[0] << endl;
00175     }
00176     else if ( cmd == QString::fromLatin1( "JS_Event" )
00177               && arg.count() > 2 )
00178     {
00179         bool ok;
00180         int appletID = arg[0].toInt(&ok);
00181         unsigned long objid = arg[1].toInt(&ok);
00182         if (ok)
00183         {
00184             KJavaApplet * applet = d->applets[appletID];
00185             if (applet)
00186             {
00187                 KParts::LiveConnectExtension::ArgList arglist;
00188                 for (unsigned i = 3; i < arg.count(); i += 2)
00189                     // take a deep breath here
00190                     arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type) arg[i].toInt(), arg[i+1]));
00191 
00192                 emit static_cast<KJavaLiveConnect*>(applet->getLiveConnectExtension())->sendEvent(objid, arg[2], arglist);
00193             }
00194             else
00195                 kdError(6002) << "could find applet for JS event" << endl;
00196         }
00197         else
00198             kdError(6002) << "could not parse applet ID for JS event " << arg[0] << " " << arg[1] << endl;
00199     }
00200     else if ( cmd == QString::fromLatin1( "AppletStateNotification" ) )
00201     {
00202         bool ok;
00203         int appletID = arg[0].toInt(&ok);
00204         if (ok)
00205         {
00206             KJavaApplet * applet = d->applets[appletID];
00207             if ( applet )
00208             {
00209                 int newState   = arg[1].toInt(&ok);
00210                 if (ok)
00211                 {
00212                     applet->stateChange(newState);
00213                     if (newState == KJavaApplet::INITIALIZED) {
00214                         kdDebug(6002) << "emit appletLoaded" << endl;
00215                         emit appletLoaded();
00216                     }
00217                 } else
00218                     kdError(6002) << "AppletStateNotification: status is not numerical" << endl;
00219             } else
00220                 kdWarning(6002) << "AppletStateNotification:  No such Applet with ID=" << arg[0] << endl;
00221         } else
00222             kdError(6002) << "AppletStateNotification: Applet ID is not numerical" << endl;
00223     }
00224     else if ( cmd == QString::fromLatin1( "AppletFailed" ) ) {
00225         bool ok;
00226         int appletID = arg[0].toInt(&ok);
00227         if (ok)
00228         {
00229             KJavaApplet * applet = d->applets[appletID];
00230             /*
00231             QString errorDetail(arg[1]);
00232             errorDetail.replace(QRegExp(":\\s*"), ":\n");
00233             KMessageBox::detailedError(0L, i18n("Java error while loading applet."), errorDetail);
00234             */
00235             applet->setFailed();
00236             emit appletLoaded();
00237         }
00238     }
00239 }
00240 
00241 bool KJavaAppletContext::appletsLoaded() const {
00242     AppletMap::const_iterator it = d->applets.begin();
00243     for (; it != d->applets.end(); it++) {
00244         if (!(*it).isNull()) {
00245             if (!(*it)->isAlive() && !(*it)->failed()) {
00246                 return false;
00247             }
00248         }
00249     }
00250     return true;
00251 }
00252 
00253 bool KJavaAppletContext::getMember(KJavaApplet * applet, const unsigned long objid, const QString & name, int & type, unsigned long & rid, QString & value) {
00254     return server->getMember(id, applet->appletId(), objid, name, type, rid, value);
00255 }
00256 
00257 bool KJavaAppletContext::putMember(KJavaApplet * applet, const unsigned long objid, const QString & name, const QString & value) {
00258     return server->putMember(id, applet->appletId(), objid, name, value);
00259 }
00260 
00261 bool KJavaAppletContext::callMember(KJavaApplet * applet, const unsigned long objid, const QString & name, const QStringList & args, int & type, unsigned long & retobjid, QString & value) {
00262     return server->callMember(id, applet->appletId(), objid, name, args, type, retobjid, value);
00263 }
00264 
00265 void KJavaAppletContext::derefObject(KJavaApplet * applet, const unsigned long jid) {
00266     server->derefObject(id, applet->appletId(), jid);
00267 }
00268 
00269 #include <kjavaappletcontext.moc>
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:22:40 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001