kjavaapplet.cpp Source File (khtml Library)

| # khtml Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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     if (d->baseURL.isEmpty())
    00126         return; //FIXME
    00127     KURL newURL(d->baseURL, codeBase);
    00128     if (kapp->authorizeURLAction("redirect", KURL(d->baseURL), newURL))
    00129         d->codeBase = newURL.url();
    00130 }
    00131
    00132 QString& KJavaApplet::codeBase()
    00133 {
    00134     return d->codeBase;
    00135 }
    00136
    00137 void KJavaApplet::setSize( QSize size )
    00138 {
    00139     d->size = size;
    00140 }
    00141
    00142 QSize KJavaApplet::size()
    00143 {
    00144     return d->size;
    00145 }
    00146
    00147 void KJavaApplet::setArchives( const QString& _archives )
    00148 {
    00149     d->archives = _archives;
    00150 }
    00151
    00152 QString& KJavaApplet::archives()
    00153 {
    00154     return d->archives;
    00155 }
    00156
    00157 void KJavaApplet::resizeAppletWidget( int width, int height )
    00158 {
    00159     kdDebug(6100) << "KJavaApplet, id = " << id << ", ::resizeAppletWidget to " << width << ", " << height << endl;
    00160
    00161     if( d->UIwidget )
    00162         d->UIwidget->resize( width, height );
    00163 }
    00164
    00165 void KJavaApplet::setAppletName( const QString& name )
    00166 {
    00167     d->appName = name;
    00168 }
    00169
    00170 void KJavaApplet::setWindowName( const QString& title )
    00171 {
    00172     d->windowName = title;
    00173 }
    00174
    00175 QString& KJavaApplet::getWindowName()
    00176 {
    00177     return d->windowName;
    00178 }
    00179
    00180 QString& KJavaApplet::appletName()
    00181 {
    00182     return d->appName;
    00183 }
    00184
    00185 void KJavaApplet::create( )
    00186 {
    00187     KIO::AuthInfo info;
    00188     QString errorMsg;
    00189     QCString replyType;
    00190     QByteArray params;
    00191     QByteArray reply;
    00192     KIO::AuthInfo authResult;
    00193
    00194     //(void) dcopClient(); // Make sure to have a dcop client.
    00195     info.url = d->baseURL;
    00196     info.verifyPath = true;
    00197
    00198     QDataStream stream(params, IO_WriteOnly);
    00199     stream << info << d->UIwidget->topLevelWidget()->winId();
    00200
    00201     if (!kapp->dcopClient ()->call( "kded", "kpasswdserver", "checkAuthInfo(KIO::AuthInfo, long int)", params, replyType, reply ) ) {
    00202         kdWarning() << "Can't communicate with kded_kpasswdserver!" << endl;
    00203     } else if ( replyType == "KIO::AuthInfo" ) {
    00204         QDataStream stream2( reply, IO_ReadOnly );
    00205         stream2 >> authResult;
    00206         setUser (authResult.username);
    00207         setPassword (authResult.password);
    00208         setAuthName (authResult.realmValue);
    00209     }
    00210
    00211     if (  !context->create( this ) )
    00212         setFailed();
    00213     d->reallyExists = true;
    00214 }
    00215
    00216 void KJavaApplet::init()
    00217 {
    00218     context->init( this );
    00219 }
    00220
    00221 void KJavaApplet::start()
    00222 {
    00223     context->start( this );
    00224 }
    00225
    00226 void KJavaApplet::stop()
    00227 {
    00228     context->stop( this );
    00229 }
    00230
    00231 int KJavaApplet::appletId()
    00232 {
    00233     return id;
    00234 }
    00235
    00236 void KJavaApplet::setAppletId( int _id )
    00237 {
    00238     id = _id;
    00239 }
    00240
    00241 KJavaLiveConnect::KJavaLiveConnect(KJavaAppletContext* c, KJavaApplet* a)
    00242     : KParts::LiveConnectExtension(0), context(c), applet(a) {
    00243 }
    00244
    00245 bool KJavaLiveConnect::get(const unsigned long objid, const QString & field, KParts::LiveConnectExtension::Type & type, unsigned long & rid, QString & value )
    00246 {
    00247     if (!applet->isAlive())
    00248         return false;
    00249     int itype;
    00250     bool ret = context->getMember(applet, objid, field, itype, rid, value);
    00251     type = (KParts::LiveConnectExtension::Type) itype;
    00252     return ret;
    00253 }
    00254
    00255 bool KJavaLiveConnect::put(const unsigned long objid, const QString & name, const QString & value)
    00256 {
    00257     if (!applet->isAlive())
    00258         return false;
    00259     return context->putMember(applet, objid, name, value);
    00260 }
    00261
    00262 bool KJavaLiveConnect::call( const unsigned long objid, const QString & func, const QStringList & args, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value )
    00263 {
    00264     if (!applet->isAlive())
    00265         return false;
    00266     int itype;
    00267     bool ret = context->callMember(applet, objid, func, args, itype, retobjid, value);
    00268     type = (KParts::LiveConnectExtension::Type) itype;
    00269     return ret;
    00270 }
    00271
    00272 void KJavaLiveConnect::unregister(const unsigned long objid)
    00273 {
    00274     if (objid == 0) {
    00275         // typically a gc after a function call on the applet,
    00276         // no need to send to the jvm
    00277         return;
    00278     }
    00279     context->derefObject(applet, objid);
    00280 }
    00281
    00282 void KJavaApplet::stateChange( const int newStateInt ) {
    00283     AppletState newState = (AppletState)newStateInt;
    00284     bool ok = false;
    00285     if (d->failed) {
    00286         return;
    00287     }
    00288     switch ( newState ) {
    00289         case CLASS_LOADED:
    00290             ok = (d->state == UNKNOWN);
    00291             break;
    00292         case INSTANCIATED:
    00293             if (ok) {
    00294                 // FIXME enable after 3.1
    00295                 //showStatus(i18n("Java Applet \"$1\" loaded").arg(appletName()));
    00296             }
    00297             ok = (d->state == CLASS_LOADED);
    00298             break;
    00299         case INITIALIZED:
    00300             ok = (d->state == INSTANCIATED);
    00301             if (ok) {
    00302                 // FIXME enable after 3.1
    00303                 //showStatus(i18n("Java Applet \"%1\" initialized").arg(appletName()));
    00304                 start();
    00305             }
    00306             break;
    00307         case STARTED:
    00308             ok = (d->state == INITIALIZED || d->state == STOPPED);
    00309             if (ok) {
    00310                 // FIXME enable after 3.1
    00311                 //showStatus(i18n("Java Applet \"%1\" started").arg(appletName()));
    00312             }
    00313             break;
    00314         case STOPPED:
    00315             ok = (d->state == INITIALIZED || d->state == STARTED);
    00316             if (ok) {
    00317                 // FIXME enable after 3.1
    00318                 //showStatus(i18n("Java Applet \"%1\" stopped").arg(appletName()));
    00319             }
    00320             break;
    00321         case DESTROYED:
    00322             ok = true;
    00323             break;
    00324         default:
    00325             break;
    00326     }
    00327     if (ok) {
    00328         d->state = newState;
    00329     } else {
    00330         kdError(6100) << "KJavaApplet::stateChange : don't want to switch from state "
    00331             << d->state << " to " << newState << endl;
    00332     }
    00333 }
    00334
    00335 void KJavaApplet::showStatus(const QString &msg) {
    00336     QStringList args;
    00337     args << msg;
    00338     context->processCmd("showstatus", args);
    00339 }
    00340
    00341 void KJavaApplet::setFailed() {
    00342     d->failed = true;
    00343 }
    00344
    00345 bool KJavaApplet::isAlive() const {
    00346    return (
    00347         !d->failed
    00348         && d->state >= INSTANCIATED
    00349         && d->state < DESTROYED
    00350    );
    00351 }
    00352
    00353 KJavaApplet::AppletState KJavaApplet::state() const {
    00354     return d->state;
    00355 }
    00356
    00357 bool KJavaApplet::failed() const {
    00358     return d->failed;
    00359 }
    00360
    00361 #include "kjavaapplet.moc"

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