khtml Library API Documentation

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;
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 = value.left(value.length()-1);
00181             kdDebug(6100) << "name=" << name << " value=" << value << endl;
00182             if (!name.isEmpty()) {
00183                 if (name == "__KHTML__PLUGINBASEURL")
00184                     baseurl = value;
00185                 else if (name == "__KHTML__CODEBASE" ||
00186                          name.lower()==QString::fromLatin1("codebase") ||
00187                          name.lower()==QString::fromLatin1("java_codebase")) {
00188                     if (!value.isEmpty ())
00189                         codebase = value;
00190                 } else if (name == "__KHTML__CLASSID")
00191                 //else if (name.lower()==QString::fromLatin1("classid"))
00192                     classid = value;
00193                 else if (name.lower()==QString::fromLatin1("code") ||
00194                          name.lower()==QString::fromLatin1("java_code") ||
00195                          name.lower()==QString::fromLatin1("src"))
00196                     classname = value;
00197                 else if (name.lower()==QString::fromLatin1("archive") ||
00198                          name.lower()==QString::fromLatin1("java_archieve"))
00199                     applet->setArchives (value);
00200                 else if (name.lower()==QString::fromLatin1("name"))
00201                     applet->setAppletName (value);
00202                 else if (name.lower()==QString::fromLatin1("width"))
00203                     width = value.toInt();
00204                 else if (name.lower()==QString::fromLatin1("height"))
00205                     height = value.toInt();
00206                 else {
00207                     applet->setParameter (name, value);
00208                 }
00209             }
00210         }
00211     }
00212     if (!classid.isEmpty ()) {
00213         applet->setParameter ("CLSID", classid);
00214         kdDebug(6100) << "classid=" << classid << classid.startsWith("clsid:")<< endl;
00215         if (classid.startsWith ("clsid:"))
00216             // codeBase contains the URL to the plugin page
00217             codebase = baseurl;
00218         else if (classname.isEmpty () && classid.startsWith ("java:"))
00219             classname = classid.mid(5);
00220     }
00221 
00222     if (width > 0 && height > 0)
00223         applet->setSize (QSize(width, height));
00224     applet->setBaseURL (baseurl);
00225     applet->setCodeBase (codebase);
00226     applet->setAppletClass (classname);
00227     applet->setAppletContext (serverMaintainer->getContext (parent, baseurl));
00228 
00229     setInstance (KJavaAppletViewerFactory::instance ());
00230     KParts::Part::setWidget (m_view);
00231     insertChild (applet->getLiveConnectExtension ()); // hack
00232 
00233     connect (applet->getContext(), SIGNAL(appletLoaded()), this, SLOT(appletLoaded()));
00234     connect (applet->getContext(), SIGNAL(showDocument(const QString&, const QString&)), m_browserextension, SLOT(showDocument(const QString&, const QString&)));
00235     connect (applet->getContext(), SIGNAL(showStatus(const QString &)), this, SLOT(infoMessage(const QString &)));
00236 }
00237 
00238 KJavaAppletViewer::~KJavaAppletViewer () {
00239     delete m_view;
00240     serverMaintainer->releaseContext (parent(), baseurl);
00241     delete m_browserextension;
00242 }
00243 
00244 bool KJavaAppletViewer::openURL (const KURL & url) {
00245     if (!m_view) return false;
00246     KJavaApplet * applet = m_view->applet ();
00247     if (applet->isCreated ())
00248         applet->stop ();
00249     if (applet->appletClass ().isEmpty ()) {
00250         // preview without setting a class?
00251         if (applet->baseURL ().isEmpty ()) {
00252             applet->setAppletClass (url.fileName ());
00253             applet->setBaseURL (url.upURL ().url ());
00254         } else
00255             applet->setAppletClass (url.url ());
00256         AppletParameterDialog (m_view).exec ();
00257         applet->setSize (m_view->sizeHint());
00258     }
00259     // delay showApplet if size is unknown and m_view not shown
00260     if (applet->size().width() > 0 || m_view->isVisible())
00261         m_view->showApplet ();
00262     emit started (0L);
00263     return url.isValid ();
00264 }
00265 
00266 bool KJavaAppletViewer::openFile () {
00267     return false;
00268 }
00269 
00270 void KJavaAppletViewer::appletLoaded () {
00271     KJavaApplet * applet = m_view->applet ();
00272     if (applet->isAlive() || applet->failed())
00273         emit completed();
00274 }
00275 
00276 void KJavaAppletViewer::infoMessage (const QString & msg) {
00277     m_browserextension->infoMessage(msg);
00278 }
00279 
00280 KAboutData* KJavaAppletViewer::createAboutData () {
00281     //return new KAboutData("KJavaAppletViewer", I18N_NOOP("KDE Java Applet Plugin"), "1.0");
00282     return new KAboutData("KJavaAppletViewer", "KDE Java Applet Plugin", "1.0");
00283 }
00284 
00285 //---------------------------------------------------------------------
00286 
00287 KJavaAppletViewerBrowserExtension::KJavaAppletViewerBrowserExtension (KJavaAppletViewer * parent)
00288   : KParts::BrowserExtension (parent, "KJavaAppletViewer Browser Extension") {
00289 }
00290 
00291 void KJavaAppletViewerBrowserExtension::urlChanged (const QString & url) {
00292     emit setLocationBarURL (url);
00293 }
00294 
00295 void KJavaAppletViewerBrowserExtension::setLoadingProgress (int percentage) {
00296     emit loadingProgress (percentage);
00297 }
00298 
00299 void KJavaAppletViewerBrowserExtension::setURLArgs (const KParts::URLArgs & /*args*/) {
00300 }
00301 
00302 void KJavaAppletViewerBrowserExtension::saveState (QDataStream & stream) {
00303     KJavaApplet * applet = static_cast<KJavaAppletViewer*>(parent())->view()->applet ();
00304     stream << applet->appletClass();
00305     stream << applet->baseURL();
00306     stream << applet->archives();
00307     stream << applet->getParams().size ();
00308     QMap<QString,QString>::iterator it = applet->getParams().begin ();
00309     for ( ; it != applet->getParams().end (); ++it) {
00310         stream << it.key ();
00311         stream << it.data ();
00312     }
00313 }
00314 
00315 void KJavaAppletViewerBrowserExtension::restoreState (QDataStream & stream) {
00316     KJavaAppletViewer * viewer = static_cast<KJavaAppletViewer*>(parent());
00317     KJavaApplet * applet = viewer->view()->applet ();
00318     QString key, val;
00319     int paramcount;
00320     stream >> val;
00321     applet->setAppletClass (val);
00322     stream >> val;
00323     applet->setBaseURL (val);
00324     stream >> val;
00325     applet->setArchives (val);
00326     stream >> paramcount;
00327     for (int i = 0; i < paramcount; ++i) {
00328         stream >> key;
00329         stream >> val;
00330         applet->setParameter (key, val);
00331         kdDebug(6100) << "restoreState key:" << key << " val:" << val << endl;
00332     }
00333     applet->setSize (viewer->view ()->sizeHint ());
00334     if (viewer->view ()->isVisible())
00335         viewer->view ()->showApplet ();
00336 }
00337 
00338 void KJavaAppletViewerBrowserExtension::showDocument (const QString & doc,
00339                                                       const QString & frame) {
00340     KURL url (doc);
00341     KParts::URLArgs args;
00342     args.frameName = frame;
00343     emit openURLRequest (url, args);
00344 }
00345         
00346 //-----------------------------------------------------------------------------
00347 // TODO move this to kjavaappletwidget
00348 
00349 KJavaAppletViewerWidget::KJavaAppletViewerWidget(QWidget* parent, const char* name)
00350   : KJavaAppletWidget(parent, name) {}
00351 
00352 void KJavaAppletViewerWidget::showEvent (QShowEvent * e) {
00353     KJavaAppletWidget::showEvent(e);
00354     if (!applet()->isCreated() && !applet()->appletClass().isEmpty()) {
00355         // delayed showApplet
00356         if (applet()->size().width() <= 0)
00357             applet()->setSize (sizeHint());
00358         showApplet();
00359     }
00360 }
00361 
00362 #include "kjavaappletviewer.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.3.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Tue Oct 5 08:49:05 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001