khtml Library API Documentation

kjavaapplet.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 "kjavaappletwidget.h"
00023 #include "kjavaappletcontext.h"
00024 
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <kapplication.h>
00028 #include <kio/authinfo.h>
00029 #include <dcopclient.h>
00030 
00031 
00032 
00033 class KJavaAppletPrivate
00034 {
00035 public:
00036    bool    reallyExists;
00037    QString className;
00038    QString appName;
00039    QString baseURL;
00040    QString codeBase;
00041    QString archives;
00042    QSize   size;
00043    QString windowName;
00044    KJavaApplet::AppletState state;
00045    bool    failed;
00046 
00047    KJavaAppletWidget* UIwidget;
00048 };
00049 
00050 
00051 KJavaApplet::KJavaApplet( KJavaAppletWidget* _parent,
00052                           KJavaAppletContext* _context )
00053     : params()
00054 {
00055     d = new KJavaAppletPrivate;
00056 
00057     d->UIwidget = _parent;
00058     d->state = UNKNOWN;
00059     d->failed = false;
00060 
00061     if( _context )
00062         setAppletContext( _context );
00063 
00064     d->reallyExists = false;
00065 }
00066 
00067 KJavaApplet::~KJavaApplet()
00068 {
00069     if ( d->reallyExists )
00070         context->destroy( this );
00071 
00072     delete d;
00073     delete liveconnect;
00074 }
00075 
00076 bool KJavaApplet::isCreated()
00077 {
00078     return d->reallyExists;
00079 }
00080 
00081 void KJavaApplet::setAppletContext( KJavaAppletContext* _context )
00082 {
00083     context = _context;
00084     context->registerApplet( this );
00085     liveconnect = new KJavaLiveConnect( _context, this );
00086 }
00087 
00088 void KJavaApplet::setAppletClass( const QString& _className )
00089 {
00090     d->className = _className;
00091 }
00092 
00093 QString& KJavaApplet::appletClass()
00094 {
00095     return d->className;
00096 }
00097 
00098 QString& KJavaApplet::parameter( const QString& name )
00099 {
00100     return params[ name ];
00101 }
00102 
00103 void KJavaApplet::setParameter( const QString& name, const QString& value )
00104 {
00105     params.insert( name, value );
00106 }
00107 
00108 QMap<QString,QString>& KJavaApplet::getParams()
00109 {
00110     return params;
00111 }
00112 
00113 void KJavaApplet::setBaseURL( const QString& baseURL )
00114 {
00115     d->baseURL = baseURL;
00116 }
00117 
00118 QString& KJavaApplet::baseURL()
00119 {
00120     return d->baseURL;
00121 }
00122 
00123 void KJavaApplet::setCodeBase( const QString& codeBase )
00124 {
00125     d->codeBase = codeBase;
00126 }
00127 
00128 QString& KJavaApplet::codeBase()
00129 {
00130     return d->codeBase;
00131 }
00132 
00133 void KJavaApplet::setSize( QSize size )
00134 {
00135     d->size = size;
00136 }
00137 
00138 QSize KJavaApplet::size()
00139 {
00140     return d->size;
00141 }
00142 
00143 void KJavaApplet::setArchives( const QString& _archives )
00144 {
00145     d->archives = _archives;
00146 }
00147 
00148 QString& KJavaApplet::archives()
00149 {
00150     return d->archives;
00151 }
00152 
00153 void KJavaApplet::resizeAppletWidget( int width, int height )
00154 {
00155     kdDebug(6100) << "KJavaApplet, id = " << id << ", ::resizeAppletWidget to " << width << ", " << height << endl;
00156 
00157     if( d->UIwidget )
00158         d->UIwidget->resize( width, height );
00159 }
00160 
00161 void KJavaApplet::setAppletName( const QString& name )
00162 {
00163     d->appName = name;
00164 }
00165 
00166 void KJavaApplet::setWindowName( const QString& title )
00167 {
00168     d->windowName = title;
00169 }
00170 
00171 QString& KJavaApplet::getWindowName()
00172 {
00173     return d->windowName;
00174 }
00175 
00176 QString& KJavaApplet::appletName()
00177 {
00178     return d->appName;
00179 }
00180 
00181 void KJavaApplet::create( )
00182 {
00183     KIO::AuthInfo info;
00184     QString errorMsg;
00185     QCString replyType;
00186     QByteArray params;
00187     QByteArray reply;
00188     KIO::AuthInfo authResult;
00189 
00190     //(void) dcopClient(); // Make sure to have a dcop client.
00191     info.url = d->baseURL;
00192     info.verifyPath = true;
00193 
00194     QDataStream stream(params, IO_WriteOnly);
00195     stream << info << d->UIwidget->topLevelWidget()->winId();
00196 
00197     if (!kapp->dcopClient ()->call( "kded", "kpasswdserver", "checkAuthInfo(KIO::AuthInfo, long int)", params, replyType, reply ) ) {
00198         kdWarning() << "Can't communicate with kded_kpasswdserver!" << endl;
00199     } else if ( replyType == "KIO::AuthInfo" ) {
00200         QDataStream stream2( reply, IO_ReadOnly );
00201         stream2 >> authResult;
00202         setUser (authResult.username);
00203         setPassword (authResult.password);
00204         setAuthName (authResult.realmValue);
00205     }
00206 
00207     if (  !context->create( this ) )
00208         setFailed();
00209     d->reallyExists = true;
00210 }
00211 
00212 void KJavaApplet::init()
00213 {
00214     context->init( this );
00215 }
00216 
00217 void KJavaApplet::start()
00218 {
00219     context->start( this );
00220 }
00221 
00222 void KJavaApplet::stop()
00223 {
00224     context->stop( this );
00225 }
00226 
00227 int KJavaApplet::appletId()
00228 {
00229     return id;
00230 }
00231 
00232 void KJavaApplet::setAppletId( int _id )
00233 {
00234     id = _id;
00235 }
00236 
00237 KJavaLiveConnect::KJavaLiveConnect(KJavaAppletContext* c, KJavaApplet* a) 
00238     : KParts::LiveConnectExtension(0), context(c), applet(a) {
00239 }
00240 
00241 bool KJavaLiveConnect::get(const unsigned long objid, const QString & field, KParts::LiveConnectExtension::Type & type, unsigned long & rid, QString & value )
00242 {
00243     if (!applet->isAlive())
00244         return false;
00245     int itype;
00246     bool ret = context->getMember(applet, objid, field, itype, rid, value);
00247     type = (KParts::LiveConnectExtension::Type) itype;
00248     return ret;
00249 }
00250 
00251 bool KJavaLiveConnect::put(const unsigned long objid, const QString & name, const QString & value)
00252 {
00253     if (!applet->isAlive())
00254         return false;
00255     return context->putMember(applet, objid, name, value);
00256 }
00257 
00258 bool KJavaLiveConnect::call( const unsigned long objid, const QString & func, const QStringList & args, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value )
00259 {
00260     if (!applet->isAlive())
00261         return false;
00262     int itype;
00263     bool ret = context->callMember(applet, objid, func, args, itype, retobjid, value);
00264     type = (KParts::LiveConnectExtension::Type) itype;
00265     return ret;
00266 }
00267 
00268 void KJavaLiveConnect::unregister(const unsigned long objid)
00269 {
00270     if (objid == 0) {
00271         // typically a gc after a function call on the applet, 
00272         // no need to send to the jvm
00273         return;
00274     }
00275     context->derefObject(applet, objid);
00276 }
00277 
00278 void KJavaApplet::stateChange( const int newStateInt ) {
00279     AppletState newState = (AppletState)newStateInt;
00280     bool ok = false;
00281     if (d->failed) {
00282         return;
00283     }
00284     switch ( newState ) {
00285         case CLASS_LOADED:
00286             ok = (d->state == UNKNOWN);
00287             break;
00288         case INSTANCIATED:
00289             if (ok) {
00290                 // FIXME enable after 3.1
00291                 //showStatus(i18n("Java Applet \"$1\" loaded").arg(appletName()));
00292             }
00293             ok = (d->state == CLASS_LOADED);
00294             break;
00295         case INITIALIZED:
00296             ok = (d->state == INSTANCIATED);
00297             if (ok) { 
00298                 // FIXME enable after 3.1
00299                 //showStatus(i18n("Java Applet \"%1\" initialized").arg(appletName()));
00300                 start();
00301             }
00302             break;
00303         case STARTED:
00304             ok = (d->state == INITIALIZED || d->state == STOPPED);
00305             if (ok) {    
00306                 // FIXME enable after 3.1
00307                 //showStatus(i18n("Java Applet \"%1\" started").arg(appletName()));
00308             }
00309             break;
00310         case STOPPED:
00311             ok = (d->state == INITIALIZED || d->state == STARTED);
00312             if (ok) {    
00313                 // FIXME enable after 3.1
00314                 //showStatus(i18n("Java Applet \"%1\" stopped").arg(appletName()));
00315             }
00316             break;
00317         case DESTROYED:
00318             ok = true;
00319             break;
00320         default:
00321             break;
00322     }
00323     if (ok) {
00324         d->state = newState;
00325     } else {
00326         kdError(6100) << "KJavaApplet::stateChange : don't want to switch from state "
00327             << d->state << " to " << newState << endl;
00328     } 
00329 }
00330 
00331 void KJavaApplet::showStatus(const QString &msg) {
00332     QStringList args;
00333     args << msg;
00334     context->processCmd("showstatus", args); 
00335 }
00336 
00337 void KJavaApplet::setFailed() {
00338     d->failed = true;
00339 }
00340 
00341 bool KJavaApplet::isAlive() const {
00342    return (
00343         !d->failed 
00344         && d->state >= INSTANCIATED
00345         && d->state < DESTROYED
00346    ); 
00347 }
00348 
00349 KJavaApplet::AppletState KJavaApplet::state() const {
00350     return d->state;
00351 }
00352 
00353 bool KJavaApplet::failed() const {
00354     return d->failed;
00355 }
00356 
00357 #include "kjavaapplet.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:16:37 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001