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

# kjavaappletviewer.cpp

    00001 /* This file is part of the KDE project
    00002  *
    00003  * Copyright (C) 2003 Koos Vriezen <koos.vriezen@xs4all.nl>
    00004  *
    00005  * This library is free software; you can redistribute it and/or
    00006  * modify it under the terms of the GNU Library General Public
    00007  * License as published by the Free Software Foundation; either
    00008  * version 2 of the License, or (at your option) any later version.
    00009  *
    00010  * This library is distributed in the hope that it will be useful,
    00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013  * Library General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Library General Public License
    00016  * along with this library; see the file COPYING.LIB.  If not, write to
    00017  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018  * Boston, MA 02111-1307, USA.
    00019  */
    00020 #include <stdio.h>
    00021
    00022 #ifdef KDE_USE_FINAL
    00023 #undef Always
    00024 #include <qdir.h>
    00025 #endif
    00026 #include <qtable.h>
    00027 #include <qpair.h>
    00028
    00029 #include <klibloader.h>
    00030 #include <kaboutdata.h>
    00031 #include <kstaticdeleter.h>
    00032 #include <klocale.h>
    00033 #include <kdebug.h>
    00034 #include <kconfig.h>
    00035
    00036 #include "kjavaappletwidget.h"
    00037 #include "kjavaappletviewer.h"
    00038 #include "kjavaappletserver.h"
    00039
    00040
    00041 K_EXPORT_COMPONENT_FACTORY (kjavaappletviewer, KJavaAppletViewerFactory)
    00042
    00043 KInstance *KJavaAppletViewerFactory::s_instance = 0;
    00044
    00045 KJavaAppletViewerFactory::KJavaAppletViewerFactory () {
    00046     s_instance = new KInstance ("KJavaAppletViewer");
    00047 }
    00048
    00049 KJavaAppletViewerFactory::~KJavaAppletViewerFactory () {
    00050     delete s_instance;
    00051 }
    00052
    00053 KParts::Part *KJavaAppletViewerFactory::createPartObject
    00054   (QWidget *wparent, const char *wname,
    00055    QObject *parent, const char * name, const char *, const QStringList & args) {
    00056     return new KJavaAppletViewer (wparent, wname, parent, name, args);
    00057 }
    00058
    00059 //-----------------------------------------------------------------------------
    00060
    00061 class KJavaServerMaintainer;
    00062 static KJavaServerMaintainer * serverMaintainer = 0;
    00063
    00064 class KJavaServerMaintainer {
    00065 public:
    00066     KJavaServerMaintainer () { }
    00067
    00068     KJavaAppletContext * getContext (QObject*, const QString &);
    00069     void releaseContext (QObject*, const QString &);
    00070 public:
    00071     typedef QMap <QPair <QObject*, QString>, QPair <KJavaAppletContext*, int> >
    00072             ContextMap;
    00073     ContextMap m_contextmap;
    00074 };
    00075
    00076 KJavaAppletContext * KJavaServerMaintainer::getContext (QObject * w, const QString & doc) {
    00077     ContextMap::key_type key = qMakePair (w, doc);
    00078     ContextMap::iterator it = m_contextmap.find (key);
    00079     if (it != m_contextmap.end ()) {
    00080         (*it).second++;
    00081         return (*it).first;
    00082     }
    00083     KJavaAppletContext * context = new KJavaAppletContext ();
    00084     m_contextmap.insert (key, qMakePair(context, 1));
    00085     return context;
    00086 }
    00087
    00088 void KJavaServerMaintainer::releaseContext (QObject * w, const QString & doc) {
    00089     ContextMap::iterator it = m_contextmap.find (qMakePair (w, doc));
    00090     if (it != m_contextmap.end () && --(*it).second <= 0) {
    00091         kdDebug(6100) << "KJavaServerMaintainer::releaseContext" << endl;
    00092         (*it).first->deleteLater ();
    00093         m_contextmap.remove (it);
    00094     }
    00095 }
    00096
    00097 static KStaticDeleter <KJavaServerMaintainer> serverMaintainerDeleter;
    00098
    00099 //-----------------------------------------------------------------------------
    00100
    00101 AppletParameterDialog::AppletParameterDialog (KJavaAppletWidget * parent)
    00102     //: KDialogBase (parent, "paramdialog", true, i18n ("Applet Parameters"),
    00103     : KDialogBase (parent, "paramdialog", true, "Applet Parameters",
    00104                    KDialogBase::Close, KDialogBase::Close, true),
    00105       m_appletWidget (parent) {
    00106     KJavaApplet * applet = parent->applet ();
    00107     table = new QTable (30, 2, this);
    00108     table->setMinimumSize (QSize (600, 400));
    00109     table->setColumnWidth (0, 200);
    00110     table->setColumnWidth (1, 340);
    00111     QHeader *header = table->horizontalHeader();
    00112     //header->setLabel (0, i18n ("Parameter"));
    00113     header->setLabel (0, "Parameter");
    00114     //header->setLabel (1, i18n ("Value"));
    00115     header->setLabel (1, "Value");
    00116     //QTableItem * tit = new QTableItem (table, QTableItem::Never, i18n("Class"));
    00117     QTableItem * tit = new QTableItem (table, QTableItem::Never, "Class");
    00118     table->setItem (0, 0, tit);
    00119     tit = new QTableItem(table, QTableItem::Always, applet->appletClass());
    00120     table->setItem (0, 1, tit);
    00121     //tit = new QTableItem (table, QTableItem::Never, i18n ("Base URL"));
    00122     tit = new QTableItem (table, QTableItem::Never, "Base URL");
    00123     table->setItem (1, 0, tit);
    00124     tit = new QTableItem(table, QTableItem::Always, applet->baseURL());
    00125     table->setItem (1, 1, tit);
    00126     //tit = new QTableItem (table, QTableItem::Never, i18n ("Archives"));
    00127     tit = new QTableItem (table, QTableItem::Never, "Archives");
    00128     table->setItem (2, 0, tit);
    00129     tit = new QTableItem(table, QTableItem::Always, applet->archives());
    00130     table->setItem (2, 1, tit);
    00131     QMap<QString,QString>::iterator it = applet->getParams().begin ();
    00132     for (int count = 2; it != applet->getParams().end (); ++it) {
    00133         tit = new QTableItem (table, QTableItem::Always, it.key ());
    00134         table->setItem (++count, 0, tit);
    00135         tit = new QTableItem(table, QTableItem::Always, it.data ());
    00136         table->setItem (count, 1, tit);
    00137     }
    00138     setMainWidget (table);
    00139 }
    00140
    00141 void AppletParameterDialog::slotClose () {
    00142     table->selectCells (0, 0, 0, 0);
    00143     KJavaApplet * applet = m_appletWidget->applet ();
    00144     applet->setAppletClass (table->item (0, 1)->text ());
    00145     applet->setBaseURL (table->item (1, 1)->text ());
    00146     applet->setArchives (table->item (2, 1)->text ());
    00147     for (int i = 3; i < table->numRows (); ++i) {
    00148         if (table->item (i, 0) && table->item (i, 1) && !table->item (i, 0)->text ().isEmpty ())
    00149             applet->setParameter (table->item (i, 0)->text (),
    00150                                   table->item (i, 1)->text ());
    00151     }
    00152     hide ();
    00153 }
    00154 //-----------------------------------------------------------------------------
    00155
    00156 KJavaAppletViewer::KJavaAppletViewer (QWidget * wparent, const char *,
    00157                  QObject * parent, const char * name, const QStringList & args)
    00158  : KParts::ReadOnlyPart (parent, name),
    00159    m_browserextension (new KJavaAppletViewerBrowserExtension (this))
    00160 {
    00161     if (!serverMaintainer) {
    00162         serverMaintainerDeleter.setObject (serverMaintainer,
    00163                                            new KJavaServerMaintainer);
    00164     }
    00165     m_view = new KJavaAppletViewerWidget (wparent);
    00166
    00167     QString classname, classid, codebase, khtml_codebase;
    00168     int width = -1;
    00169     int height = -1;
    00170     KJavaApplet * applet = m_view->applet ();
    00171     QStringList::const_iterator it = args.begin ();
    00172     for ( ; it != args.end (); ++it) {
    00173         int equalPos = (*it).find("=");
    00174         if (equalPos > 0) {
    00175             QString name = (*it).left (equalPos).upper ();
    00176             QString value = (*it).right ((*it).length () - equalPos - 1);
    00177             if (value.at(0)=='\"')
    00178                 value = value.right (value.length () - 1);
    00179             if (value.at (value.length () - 1) == '\"')
    00180                 value.truncate (value.length () - 1);
    00181             kdDebug(6100) << "name=" << name << " value=" << value << endl;
    00182             if (!name.isEmpty()) {
    00183                 QString name_lower = name.lower ();
    00184                 if (name == "__KHTML__PLUGINBASEURL") {
    00185                     KURL url (value);
    00186                     QString fn = url.fileName (false);
    00187                     baseurl = fn.isEmpty () ?
    00188                         value : value.left (value.length ()-fn.length ());
    00189                 } else if (name == "__KHTML__CODEBASE") {
    00190                     khtml_codebase = value;
    00191                 } else if (name_lower == QString::fromLatin1("codebase") ||
    00192                         name_lower == QString::fromLatin1("java_codebase")) {
    00193                     if (!value.isEmpty ())
    00194                         codebase = value;
    00195                 } else if (name == "__KHTML__CLASSID")
    00196                 //else if (name.lower()==QString::fromLatin1("classid"))
    00197                     classid = value;
    00198                 else if (name_lower == QString::fromLatin1("code") ||
    00199                          name_lower == QString::fromLatin1("java_code") ||
    00200                          name_lower == QString::fromLatin1("src"))
    00201                     classname = value;
    00202                 else if (name_lower == QString::fromLatin1("archive") ||
    00203                          name_lower == QString::fromLatin1("java_archieve"))
    00204                     applet->setArchives (value);
    00205                 else if (name_lower == QString::fromLatin1("name"))
    00206                     applet->setAppletName (value);
    00207                 else if (name_lower == QString::fromLatin1("width"))
    00208                     width = value.toInt();
    00209                 else if (name_lower == QString::fromLatin1("height"))
    00210                     height = value.toInt();
    00211                 else {
    00212                     applet->setParameter (name, value);
    00213                 }
    00214             }
    00215         }
    00216     }
    00217     if (!classid.isEmpty ()) {
    00218         applet->setParameter ("CLSID", classid);
    00219         kdDebug(6100) << "classid=" << classid << classid.startsWith("clsid:")<< endl;
    00220         if (classid.startsWith ("clsid:"))
    00221             // codeBase contains the URL to the plugin page
    00222             khtml_codebase = baseurl;
    00223         else if (classname.isEmpty () && classid.startsWith ("java:"))
    00224             classname = classid.mid(5);
    00225     }
    00226     if (codebase.isEmpty ())
    00227         codebase = khtml_codebase;
    00228
    00229     if (width > 0 && height > 0)
    00230         applet->setSize (QSize(width, height));
    00231     applet->setBaseURL (baseurl);
    00232     applet->setCodeBase (codebase);
    00233     applet->setAppletClass (classname);
    00234     applet->setAppletContext (serverMaintainer->getContext (parent, baseurl));
    00235
    00236     setInstance (KJavaAppletViewerFactory::instance ());
    00237     KParts::Part::setWidget (m_view);
    00238     insertChild (applet->getLiveConnectExtension ()); // hack
    00239
    00240     connect (applet->getContext(), SIGNAL(appletLoaded()), this, SLOT(appletLoaded()));
    00241     connect (applet->getContext(), SIGNAL(showDocument(const QString&, const QString&)), m_browserextension, SLOT(showDocument(const QString&, const QString&)));
    00242     connect (applet->getContext(), SIGNAL(showStatus(const QString &)), this, SLOT(infoMessage(const QString &)));
    00243 }
    00244
    00245 KJavaAppletViewer::~KJavaAppletViewer () {
    00246     delete m_view;
    00247     serverMaintainer->releaseContext (parent(), baseurl);
    00248     delete m_browserextension;
    00249 }
    00250
    00251 bool KJavaAppletViewer::openURL (const KURL & url) {
    00252     if (!m_view) return false;
    00253     KJavaApplet * applet = m_view->applet ();
    00254     if (applet->isCreated ())
    00255         applet->stop ();
    00256     if (applet->appletClass ().isEmpty ()) {
    00257         // preview without setting a class?
    00258         if (applet->baseURL ().isEmpty ()) {
    00259             applet->setAppletClass (url.fileName ());
    00260             applet->setBaseURL (url.upURL ().url ());
    00261         } else
    00262             applet->setAppletClass (url.url ());
    00263         AppletParameterDialog (m_view).exec ();
    00264         applet->setSize (m_view->sizeHint());
    00265     }
    00266     // delay showApplet if size is unknown and m_view not shown
    00267     if (applet->size().width() > 0 || m_view->isVisible())
    00268         m_view->showApplet ();
    00269     emit started (0L);
    00270     return url.isValid ();
    00271 }
    00272
    00273 bool KJavaAppletViewer::openFile () {
    00274     return false;
    00275 }
    00276
    00277 void KJavaAppletViewer::appletLoaded () {
    00278     KJavaApplet * applet = m_view->applet ();
    00279     if (applet->isAlive() || applet->failed())
    00280         emit completed();
    00281 }
    00282
    00283 void KJavaAppletViewer::infoMessage (const QString & msg) {
    00284     m_browserextension->infoMessage(msg);
    00285 }
    00286
    00287 KAboutData* KJavaAppletViewer::createAboutData () {
    00288     //return new KAboutData("KJavaAppletViewer", I18N_NOOP("KDE Java Applet Plugin"), "1.0");
    00289     return new KAboutData("KJavaAppletViewer", "KDE Java Applet Plugin", "1.0");
    00290 }
    00291
    00292 //---------------------------------------------------------------------
    00293
    00294 KJavaAppletViewerBrowserExtension::KJavaAppletViewerBrowserExtension (KJavaAppletViewer * parent)
    00295   : KParts::BrowserExtension (parent, "KJavaAppletViewer Browser Extension") {
    00296 }
    00297
    00298 void KJavaAppletViewerBrowserExtension::urlChanged (const QString & url) {
    00299     emit setLocationBarURL (url);
    00300 }
    00301
    00302 void KJavaAppletViewerBrowserExtension::setLoadingProgress (int percentage) {
    00303     emit loadingProgress (percentage);
    00304 }
    00305
    00306 void KJavaAppletViewerBrowserExtension::setURLArgs (const KParts::URLArgs & /*args*/) {
    00307 }
    00308
    00309 void KJavaAppletViewerBrowserExtension::saveState (QDataStream & stream) {
    00310     KJavaApplet * applet = static_cast<KJavaAppletViewer*>(parent())->view()->applet ();
    00311     stream << applet->appletClass();
    00312     stream << applet->baseURL();
    00313     stream << applet->archives();
    00314     stream << applet->getParams().size ();
    00315     QMap<QString,QString>::iterator it = applet->getParams().begin ();
    00316     for ( ; it != applet->getParams().end (); ++it) {
    00317         stream << it.key ();
    00318         stream << it.data ();
    00319     }
    00320 }
    00321
    00322 void KJavaAppletViewerBrowserExtension::restoreState (QDataStream & stream) {
    00323     KJavaAppletViewer * viewer = static_cast<KJavaAppletViewer*>(parent());
    00324     KJavaApplet * applet = viewer->view()->applet ();
    00325     QString key, val;
    00326     int paramcount;
    00327     stream >> val;
    00328     applet->setAppletClass (val);
    00329     stream >> val;
    00330     applet->setBaseURL (val);
    00331     stream >> val;
    00332     applet->setArchives (val);
    00333     stream >> paramcount;
    00334     for (int i = 0; i < paramcount; ++i) {
    00335         stream >> key;
    00336         stream >> val;
    00337         applet->setParameter (key, val);
    00338         kdDebug(6100) << "restoreState key:" << key << " val:" << val << endl;
    00339     }
    00340     applet->setSize (viewer->view ()->sizeHint ());
    00341     if (viewer->view ()->isVisible())
    00342         viewer->view ()->showApplet ();
    00343 }
    00344
    00345 void KJavaAppletViewerBrowserExtension::showDocument (const QString & doc,
    00346                                                       const QString & frame) {
    00347     KURL url (doc);
    00348     KParts::URLArgs args;
    00349     args.frameName = frame;
    00350     emit openURLRequest (url, args);
    00351 }
    00352
    00353 //-----------------------------------------------------------------------------
    00354 // TODO move this to kjavaappletwidget
    00355
    00356 KJavaAppletViewerWidget::KJavaAppletViewerWidget(QWidget* parent, const char* name)
    00357   : KJavaAppletWidget(parent, name) {}
    00358
    00359 void KJavaAppletViewerWidget::showEvent (QShowEvent * e) {
    00360     KJavaAppletWidget::showEvent(e);
    00361     if (!applet()->isCreated() && !applet()->appletClass().isEmpty()) {
    00362         // delayed showApplet
    00363         if (applet()->size().width() <= 0)
    00364             applet()->setSize (sizeHint());
    00365         showApplet();
    00366     }
    00367 }
    00368
    00369 #include "kjavaappletviewer.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:11 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001