KHTML
kjavaappletviewer.cpp
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kjavaappletviewer.h" 00022 00023 #include "kjavaappletwidget.h" 00024 #include "kjavaappletserver.h" 00025 00026 #include <stdio.h> 00027 00028 #ifdef KDE_USE_FINAL 00029 #undef Always 00030 #endif 00031 #include <QtCore/QDir> 00032 #include <QtCore/QPair> 00033 #include <QtCore/QTimer> 00034 #include <QtCore/QPointer> 00035 #include <QtGui/QLabel> 00036 #include <QtGui/QTableWidget> 00037 #include <QtDBus/QtDBus> 00038 00039 #include <kapplication.h> 00040 #include <kauthorized.h> 00041 #include <kaboutdata.h> 00042 #include <klocale.h> 00043 #include <kstatusbar.h> 00044 #include <kiconloader.h> 00045 #include <kdebug.h> 00046 #include <kconfig.h> 00047 #include <kio/authinfo.h> 00048 00049 00050 K_EXPORT_PLUGIN( KJavaAppletViewerFactory ) 00051 00052 KComponentData *KJavaAppletViewerFactory::s_componentData = 0; 00053 KIconLoader *KJavaAppletViewerFactory::s_iconLoader = 0; 00054 00055 KJavaAppletViewerFactory::KJavaAppletViewerFactory () { 00056 s_componentData = new KComponentData("kjava"); 00057 s_iconLoader = new KIconLoader(s_componentData->componentName(), s_componentData->dirs()); 00058 } 00059 00060 KJavaAppletViewerFactory::~KJavaAppletViewerFactory () { 00061 delete s_iconLoader; 00062 delete s_componentData; 00063 } 00064 00065 QObject *KJavaAppletViewerFactory::create(const char *, QWidget *wparent, QObject *parent, 00066 const QVariantList & args, const QString &) { 00067 QStringList argsStrings; 00068 for (int i = 0; i < args.size(); ++i) 00069 argsStrings.append(args[i].toString()); 00070 return new KJavaAppletViewer (wparent, parent, argsStrings); 00071 } 00072 00073 //----------------------------------------------------------------------------- 00074 00075 class KJavaServerMaintainer; 00076 00077 class KJavaServerMaintainer { 00078 public: 00079 KJavaServerMaintainer () { } 00080 ~KJavaServerMaintainer (); 00081 00082 KJavaAppletContext * getContext (QObject*, const QString &); 00083 void releaseContext (QObject*, const QString &); 00084 void setServer (KJavaAppletServer * s); 00085 QPointer <KJavaAppletServer> server; 00086 private: 00087 typedef QMap <QPair <QObject*, QString>, QPair <KJavaAppletContext*, int> > 00088 ContextMap; 00089 ContextMap m_contextmap; 00090 }; 00091 K_GLOBAL_STATIC(KJavaServerMaintainer, serverMaintainer) 00092 00093 KJavaServerMaintainer::~KJavaServerMaintainer () { 00094 delete server; 00095 } 00096 00097 KJavaAppletContext * KJavaServerMaintainer::getContext (QObject * w, const QString & doc) { 00098 QPair<QObject*,QString> key = qMakePair (w, doc); 00099 ContextMap::iterator it = m_contextmap.find (key); 00100 if (it != m_contextmap.end ()) { 00101 ++((*it).second); 00102 return (*it).first; 00103 } 00104 KJavaAppletContext* const context = new KJavaAppletContext (); 00105 m_contextmap.insert (key, qMakePair(context, 1)); 00106 return context; 00107 } 00108 00109 void KJavaServerMaintainer::releaseContext (QObject * w, const QString & doc) { 00110 ContextMap::iterator it = m_contextmap.find (qMakePair (w, doc)); 00111 if (it != m_contextmap.end () && --(*it).second <= 0) { 00112 kDebug(6100) << "KJavaServerMaintainer::releaseContext"; 00113 (*it).first->deleteLater (); 00114 m_contextmap.erase (it); 00115 } 00116 } 00117 00118 inline void KJavaServerMaintainer::setServer (KJavaAppletServer * s) { 00119 if (!server) 00120 server = s; 00121 } 00122 00123 //----------------------------------------------------------------------------- 00124 00125 AppletParameterDialog::AppletParameterDialog (KJavaAppletWidget * parent) 00126 : KDialog(parent), m_appletWidget (parent) 00127 { 00128 setObjectName( "paramdialog" ); 00129 setCaption( i18n ("Applet Parameters") ); 00130 setButtons( KDialog::Close ); 00131 setDefaultButton( KDialog::Close ); 00132 setModal( true ); 00133 00134 KJavaApplet* const applet = parent->applet (); 00135 table = new QTableWidget (30, 2, this); 00136 table->setMinimumSize (QSize (600, 400)); 00137 table->setColumnWidth (0, 200); 00138 table->setColumnWidth (1, 340); 00139 QTableWidgetItem* const header1 = new QTableWidgetItem(i18n ("Parameter")); 00140 QTableWidgetItem* const header2 = new QTableWidgetItem(i18n ("Value")); 00141 table->setHorizontalHeaderItem(1, header1); 00142 table->setHorizontalHeaderItem(2, header2); 00143 QTableWidgetItem * tit = new QTableWidgetItem(i18n("Class")); 00144 tit->setFlags( tit->flags()^Qt::ItemIsEditable ); 00145 table->setItem (0, 0, tit); 00146 tit = new QTableWidgetItem(applet->appletClass()); 00147 tit->setFlags( tit->flags()|Qt::ItemIsEditable ); 00148 table->setItem (0, 1, tit); 00149 tit = new QTableWidgetItem (i18n ("Base URL")); 00150 tit->setFlags( tit->flags()^Qt::ItemIsEditable ); 00151 table->setItem (1, 0, tit); 00152 tit = new QTableWidgetItem(applet->baseURL()); 00153 tit->setFlags( tit->flags()|Qt::ItemIsEditable ); 00154 table->setItem (1, 1, tit); 00155 tit = new QTableWidgetItem(i18n ("Archives")); 00156 tit->setFlags( tit->flags()^Qt::ItemIsEditable ); 00157 table->setItem (2, 0, tit); 00158 tit = new QTableWidgetItem(applet->archives()); 00159 tit->setFlags( tit->flags()|Qt::ItemIsEditable ); 00160 table->setItem (2, 1, tit); 00161 QMap<QString,QString>::const_iterator it = applet->getParams().constBegin(); 00162 const QMap<QString,QString>::const_iterator itEnd = applet->getParams().constEnd(); 00163 for (int count = 2; it != itEnd; ++it) { 00164 tit = new QTableWidgetItem (it.key ()); 00165 tit->setFlags( tit->flags()|Qt::ItemIsEditable ); 00166 table->setItem (++count, 0, tit); 00167 tit = new QTableWidgetItem(it.value()); 00168 tit->setFlags( tit->flags()|Qt::ItemIsEditable ); 00169 table->setItem (count, 1, tit); 00170 } 00171 setMainWidget (table); 00172 connect(this,SIGNAL(closeClicked()),this,SLOT(slotClose())); 00173 } 00174 00175 void AppletParameterDialog::slotClose () { 00176 table->setRangeSelected(QTableWidgetSelectionRange(0, 0, 0, 0), true); 00177 KJavaApplet* const applet = m_appletWidget->applet (); 00178 applet->setAppletClass (table->item (0, 1)->text ()); 00179 applet->setBaseURL (table->item (1, 1)->text ()); 00180 applet->setArchives (table->item (2, 1)->text ()); 00181 const int lim = table->rowCount(); 00182 for (int i = 3; i < lim; ++i) { 00183 if (table->item (i, 0) && table->item (i, 1) && !table->item (i, 0)->text ().isEmpty ()) 00184 applet->setParameter (table->item (i, 0)->text (), 00185 table->item (i, 1)->text ()); 00186 } 00187 hide (); 00188 } 00189 //----------------------------------------------------------------------------- 00190 00191 class CoverWidget : public QWidget { 00192 KJavaAppletWidget * m_appletwidget; 00193 public: 00194 CoverWidget (QWidget *); 00195 ~CoverWidget () {} 00196 KJavaAppletWidget * appletWidget () const; 00197 protected: 00198 void resizeEvent (QResizeEvent * e); 00199 }; 00200 00201 inline CoverWidget::CoverWidget (QWidget * parent) 00202 : QWidget (parent ) 00203 { 00204 setObjectName( "KJavaAppletViewer Widget"); 00205 m_appletwidget = new KJavaAppletWidget (this); 00206 setFocusProxy (m_appletwidget); 00207 } 00208 00209 inline KJavaAppletWidget * CoverWidget::appletWidget () const { 00210 return m_appletwidget; 00211 } 00212 00213 void CoverWidget::resizeEvent (QResizeEvent * e) { 00214 m_appletwidget->resize (e->size().width(), e->size().height()); 00215 } 00216 00217 //----------------------------------------------------------------------------- 00218 00219 class StatusBarIcon : public QLabel { 00220 public: 00221 StatusBarIcon (QWidget * parent) : QLabel (parent) { 00222 setPixmap ( KJavaAppletViewerFactory::iconLoader()->loadIcon("java", KIconLoader::Small) ); 00223 } 00224 protected: 00225 void mousePressEvent (QMouseEvent *) { 00226 serverMaintainer->server->showConsole (); 00227 } 00228 }; 00229 00230 //----------------------------------------------------------------------------- 00231 00232 KJavaAppletViewer::KJavaAppletViewer (QWidget * wparent, 00233 QObject * parent, const QStringList & args) 00234 : KParts::ReadOnlyPart (parent), 00235 m_browserextension (new KJavaAppletViewerBrowserExtension (this)), 00236 m_liveconnect (new KJavaAppletViewerLiveConnectExtension (this)), 00237 m_statusbar (new KParts::StatusBarExtension (this)), 00238 m_statusbar_icon (0L), 00239 m_closed (true) 00240 { 00241 m_view = new CoverWidget (wparent); 00242 QString classname, classid, codebase, khtml_codebase, src_param; 00243 QString appletname; 00244 int width = -1; 00245 int height = -1; 00246 KJavaApplet* const applet = m_view->appletWidget()->applet (); 00247 QStringList::const_iterator it = args.begin(); 00248 const QStringList::const_iterator itEnd = args.end(); 00249 for ( ; it != itEnd; ++it) { 00250 const int equalPos = (*it).indexOf("="); 00251 if (equalPos > 0) { 00252 const QString name = (*it).left (equalPos).toUpper (); 00253 QString value = (*it).right ((*it).length () - equalPos - 1); 00254 if (value.at(0)=='\"') 00255 value = value.right (value.length () - 1); 00256 if (value.at (value.length () - 1) == '\"') 00257 value.truncate (value.length () - 1); 00258 kDebug(6100) << "name=" << name << " value=" << value; 00259 if (!name.isEmpty()) { 00260 const QString name_lower = name.toLower (); 00261 if (name == "__KHTML__PLUGINBASEURL" || 00262 name == "BASEURL") { 00263 baseurl = KUrl (KUrl (value), QString (".")).url (); 00264 } else if (name == "__KHTML__CODEBASE") 00265 khtml_codebase = value; 00266 else if (name_lower == QLatin1String("codebase") || 00267 name_lower == QLatin1String("java_codebase")) { 00268 if (!value.isEmpty ()) 00269 codebase = value; 00270 } else if (name == "__KHTML__CLASSID") 00271 //else if (name.toLower()==QLatin1String("classid")) 00272 classid = value; 00273 else if (name_lower == QLatin1String("code") || 00274 name_lower == QLatin1String("java_code")) 00275 classname = value; 00276 else if (name_lower == QLatin1String("src")) 00277 src_param = value; 00278 else if (name_lower == QLatin1String("archive") || 00279 name_lower == QLatin1String("java_archive") || 00280 name_lower.startsWith(QLatin1String("cache_archive"))) 00281 applet->setArchives (value); 00282 else if (name_lower == QLatin1String("name")) 00283 appletname = value; 00284 else if (name_lower == QLatin1String("width")) 00285 width = value.toInt(); 00286 else if (name_lower == QLatin1String("height")) 00287 height = value.toInt(); 00288 if (!name.startsWith(QLatin1String("__KHTML__"))) { 00289 applet->setParameter (name, value); 00290 } 00291 } 00292 } 00293 } 00294 if (!classid.isEmpty ()) { 00295 applet->setParameter ("CLSID", classid); 00296 kDebug(6100) << "classid=" << classid << classid.startsWith("clsid:"); 00297 if (classid.startsWith(QLatin1String("clsid:"))) 00298 // codeBase contains the URL to the plugin page 00299 khtml_codebase = baseurl; 00300 else if (classname.isEmpty () && classid.startsWith(QLatin1String("java:"))) 00301 classname = classid.mid(5); 00302 } 00303 if (classname.isEmpty ()) 00304 classname = src_param; 00305 else if (!src_param.isEmpty ()) 00306 applet->setParameter (QString ("SRC"), src_param); 00307 if (codebase.isEmpty ()) 00308 codebase = khtml_codebase; 00309 if (baseurl.isEmpty ()) { 00310 // not embedded in khtml 00311 QString pwd = QDir().absolutePath (); 00312 if (!pwd.endsWith ( QString(QDir::separator ()))) 00313 pwd += QDir::separator (); 00314 baseurl = KUrl (KUrl (pwd), codebase).url (); 00315 } 00316 if (width > 0 && height > 0) { 00317 m_view->resize (width, height); 00318 applet->setSize( QSize( width, height ) ); 00319 } 00320 if (appletname.isEmpty()) 00321 appletname = classname; 00322 applet->setAppletName (appletname); 00323 applet->setBaseURL (baseurl); 00324 // check codebase first 00325 const KUrl kbaseURL( baseurl ); 00326 const KUrl newURL(kbaseURL, codebase); 00327 if (KAuthorized::authorizeUrlAction("redirect", KUrl(baseurl), newURL)) 00328 applet->setCodeBase (newURL.url()); 00329 applet->setAppletClass (classname); 00330 KJavaAppletContext* const cxt = serverMaintainer->getContext (parent, baseurl); 00331 applet->setAppletContext (cxt); 00332 00333 KJavaAppletServer* const server = cxt->getServer (); 00334 00335 serverMaintainer->setServer (server); 00336 00337 if (!server->usingKIO ()) { 00338 /* if this page needs authentication */ 00339 KIO::AuthInfo info; 00340 info.url = baseurl; 00341 info.verifyPath = true; 00342 QByteArray params; 00343 { QDataStream stream(¶ms, QIODevice::WriteOnly); stream << info; } 00344 00345 // make the call 00346 QDBusInterface kpasswdserver("org.kde.kded", "/modules/kpasswdserver", "org.kde.KPasswdServer"); 00347 QDBusReply<QByteArray> reply = kpasswdserver.call ("checkAuthInfo", params, qlonglong(m_view->topLevelWidget()->winId()), qlonglong(kapp->userTimestamp())); 00348 00349 if (!reply.isValid()) { 00350 kWarning() << "checkAuthInfo DBUS call failed: " << reply.error().message(); 00351 } else { 00352 KIO::AuthInfo authResult; 00353 QDataStream stream2(reply.value()); 00354 stream2 >> authResult; 00355 applet->setUser (authResult.username); 00356 applet->setPassword (authResult.password); 00357 applet->setAuthName (authResult.realmValue); 00358 } 00359 } 00360 00361 /* install event filter for close events */ 00362 if (wparent) 00363 wparent->topLevelWidget ()->installEventFilter (this); 00364 00365 setComponentData (KJavaAppletViewerFactory::componentData()); 00366 KParts::Part::setWidget (m_view); 00367 00368 connect (applet->getContext(), SIGNAL(appletLoaded()), this, SLOT(appletLoaded())); 00369 connect (applet->getContext(), SIGNAL(showDocument(const QString&, const QString&)), m_browserextension, SLOT(showDocument(const QString&, const QString&))); 00370 connect (applet->getContext(), SIGNAL(showStatus(const QString &)), this, SLOT(infoMessage(const QString &))); 00371 connect (applet, SIGNAL(jsEvent (const QStringList &)), m_liveconnect, SLOT(jsEvent (const QStringList &))); 00372 } 00373 00374 CoverWidget * KJavaAppletViewer::view () const 00375 { 00376 return m_view; 00377 } 00378 00379 00380 bool KJavaAppletViewer::eventFilter (QObject *o, QEvent *e) { 00381 if (m_liveconnect->jsSessions () > 0) { 00382 switch (e->type()) { 00383 case QEvent::Destroy: 00384 case QEvent::Close: 00385 case QEvent::Quit: 00386 return true; 00387 default: 00388 break; 00389 } 00390 } 00391 return KParts::ReadOnlyPart::eventFilter(o,e); 00392 } 00393 00394 KJavaAppletViewer::~KJavaAppletViewer () { 00395 m_view = 0L; 00396 serverMaintainer->releaseContext (parent(), baseurl); 00397 if (m_statusbar_icon) { 00398 m_statusbar->removeStatusBarItem (m_statusbar_icon); 00399 delete m_statusbar_icon; 00400 } 00401 } 00402 00403 bool KJavaAppletViewer::openUrl (const KUrl & url) { 00404 if (!m_view) return false; 00405 m_closed = false; 00406 KJavaAppletWidget* const w = m_view->appletWidget (); 00407 KJavaApplet* const applet = w->applet (); 00408 if (applet->isCreated ()) 00409 applet->stop (); 00410 if (applet->appletClass ().isEmpty ()) { 00411 // preview without setting a class? 00412 if (applet->baseURL ().isEmpty ()) { 00413 applet->setAppletClass (url.fileName ()); 00414 applet->setBaseURL (url.upUrl ().url ()); 00415 } else 00416 applet->setAppletClass (url.url ()); 00417 AppletParameterDialog (w).exec (); 00418 applet->setSize (w->sizeHint()); 00419 } 00420 if (!m_statusbar_icon) { 00421 KStatusBar *sb = m_statusbar->statusBar(); 00422 if (sb) { 00423 m_statusbar_icon = new StatusBarIcon (sb); 00424 m_statusbar->addStatusBarItem (m_statusbar_icon, 0, false); 00425 } 00426 } 00427 // delay showApplet if size is unknown and m_view not shown 00428 if (applet->size().width() > 0 || m_view->isVisible()) 00429 w->showApplet (); 00430 else 00431 QTimer::singleShot (10, this, SLOT (delayedCreateTimeOut ())); 00432 if (!applet->failed ()) 00433 emit started (0L); 00434 return url.isValid (); 00435 } 00436 00437 bool KJavaAppletViewer::closeUrl () { 00438 kDebug(6100) << "closeUrl"; 00439 m_closed = true; 00440 KJavaApplet* const applet = m_view->appletWidget ()->applet (); 00441 if (applet->isCreated ()) 00442 applet->stop (); 00443 applet->getContext()->getServer()->endWaitForReturnData(); 00444 return true; 00445 } 00446 00447 bool KJavaAppletViewer::appletAlive () const { 00448 return !m_closed && m_view && 00449 m_view->appletWidget ()->applet () && 00450 m_view->appletWidget ()->applet ()->isAlive (); 00451 } 00452 00453 bool KJavaAppletViewer::openFile () { 00454 return false; 00455 } 00456 00457 void KJavaAppletViewer::delayedCreateTimeOut () { 00458 KJavaAppletWidget* const w = m_view->appletWidget (); 00459 if (!w->applet ()->isCreated () && !m_closed) 00460 w->showApplet (); 00461 } 00462 00463 void KJavaAppletViewer::appletLoaded () { 00464 if (!m_view) return; 00465 KJavaApplet* const applet = m_view->appletWidget ()->applet (); 00466 if (applet->isAlive() || applet->failed()) 00467 emit completed(); 00468 } 00469 00470 void KJavaAppletViewer::infoMessage (const QString & msg) { 00471 m_browserextension->infoMessage(msg); 00472 } 00473 00474 KAboutData* KJavaAppletViewer::createAboutData () { 00475 return new KAboutData("KJavaAppletViewer", 0, ki18n("KDE Java Applet Plugin"), "1.0"); 00476 } 00477 00478 //--------------------------------------------------------------------- 00479 00480 KJavaAppletViewerBrowserExtension::KJavaAppletViewerBrowserExtension (KJavaAppletViewer * parent) 00481 : KParts::BrowserExtension (parent ) 00482 { 00483 setObjectName( "KJavaAppletViewer Browser Extension" ); 00484 } 00485 00486 void KJavaAppletViewerBrowserExtension::urlChanged (const QString & url) { 00487 emit setLocationBarUrl (url); 00488 } 00489 00490 void KJavaAppletViewerBrowserExtension::setLoadingProgress (int percentage) { 00491 emit loadingProgress (percentage); 00492 } 00493 00494 void KJavaAppletViewerBrowserExtension::setBrowserArguments (const KParts::BrowserArguments & /*args*/) { 00495 } 00496 00497 void KJavaAppletViewerBrowserExtension::saveState (QDataStream & stream) { 00498 KJavaApplet* const applet = static_cast<KJavaAppletViewer*>(parent())->view()->appletWidget ()->applet (); 00499 stream << applet->appletClass(); 00500 stream << applet->baseURL(); 00501 stream << applet->archives(); 00502 stream << applet->getParams().size (); 00503 QMap<QString,QString>::const_iterator it = applet->getParams().constBegin(); 00504 const QMap<QString,QString>::const_iterator itEnd = applet->getParams().constEnd(); 00505 for ( ; it != itEnd; ++it) { 00506 stream << it.key (); 00507 stream << it.value (); 00508 } 00509 } 00510 00511 void KJavaAppletViewerBrowserExtension::restoreState (QDataStream & stream) { 00512 KJavaAppletWidget* const w = static_cast<KJavaAppletViewer*>(parent())->view()->appletWidget(); 00513 KJavaApplet* const applet = w->applet (); 00514 QString key, val; 00515 int paramcount; 00516 stream >> val; 00517 applet->setAppletClass (val); 00518 stream >> val; 00519 applet->setBaseURL (val); 00520 stream >> val; 00521 applet->setArchives (val); 00522 stream >> paramcount; 00523 for (int i = 0; i < paramcount; ++i) { 00524 stream >> key; 00525 stream >> val; 00526 applet->setParameter (key, val); 00527 kDebug(6100) << "restoreState key:" << key << " val:" << val; 00528 } 00529 applet->setSize (w->sizeHint ()); 00530 if (w->isVisible()) 00531 w->showApplet (); 00532 } 00533 00534 void KJavaAppletViewerBrowserExtension::showDocument (const QString & doc, 00535 const QString & frame) { 00536 const KUrl url (doc); 00537 KParts::BrowserArguments browserArgs; 00538 browserArgs.frameName = frame; 00539 emit openUrlRequest(url, KParts::OpenUrlArguments(), browserArgs); 00540 } 00541 00542 //----------------------------------------------------------------------------- 00543 00544 KJavaAppletViewerLiveConnectExtension::KJavaAppletViewerLiveConnectExtension(KJavaAppletViewer * parent) 00545 : KParts::LiveConnectExtension (parent ), m_viewer (parent) 00546 { 00547 setObjectName( "KJavaAppletViewer LiveConnect Extension" ); 00548 } 00549 00550 bool KJavaAppletViewerLiveConnectExtension::get ( 00551 const unsigned long objid, const QString & name, 00552 KParts::LiveConnectExtension::Type & type, 00553 unsigned long & rid, QString & value) 00554 { 00555 if (!m_viewer->appletAlive ()) 00556 return false; 00557 QStringList args, ret_args; 00558 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet (); 00559 args.append (QString::number (applet->appletId ())); 00560 args.append (QString::number ((int) objid)); 00561 args.append (name); 00562 m_jssessions++; 00563 const bool ret = applet->getContext()->getMember (args, ret_args); 00564 m_jssessions--; 00565 if (!ret || ret_args.count() != 3) return false; 00566 bool ok; 00567 int itype = ret_args[0].toInt (&ok); 00568 if (!ok || itype < 0) return false; 00569 type = (KParts::LiveConnectExtension::Type) itype; 00570 rid = ret_args[1].toInt (&ok); 00571 if (!ok) return false; 00572 value = ret_args[2]; 00573 return true; 00574 } 00575 00576 bool KJavaAppletViewerLiveConnectExtension::put(const unsigned long objid, const QString & name, const QString & value) 00577 { 00578 if (!m_viewer->appletAlive ()) 00579 return false; 00580 QStringList args; 00581 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet (); 00582 args.append (QString::number (applet->appletId ())); 00583 args.append (QString::number ((int) objid)); 00584 args.append (name); 00585 args.append (value); 00586 ++m_jssessions; 00587 const bool ret = applet->getContext()->putMember (args); 00588 --m_jssessions; 00589 return ret; 00590 } 00591 00592 bool KJavaAppletViewerLiveConnectExtension::call( const unsigned long objid, const QString & func, const QStringList & fargs, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value ) 00593 { 00594 if (!m_viewer->appletAlive ()) 00595 return false; 00596 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet (); 00597 QStringList args, ret_args; 00598 args.append (QString::number (applet->appletId ())); 00599 args.append (QString::number ((int) objid)); 00600 args.append (func); 00601 { 00602 QStringList::const_iterator it = fargs.begin(); 00603 const QStringList::const_iterator itEnd = fargs.end(); 00604 for ( ; it != itEnd; ++it) 00605 args.append(*it); 00606 } 00607 00608 ++m_jssessions; 00609 const bool ret = applet->getContext()->callMember (args, ret_args); 00610 --m_jssessions; 00611 if (!ret || ret_args.count () != 3) return false; 00612 bool ok; 00613 const int itype = ret_args[0].toInt (&ok); 00614 if (!ok || itype < 0) return false; 00615 type = (KParts::LiveConnectExtension::Type) itype; 00616 retobjid = ret_args[1].toInt (&ok); 00617 if (!ok) return false; 00618 value = ret_args[2]; 00619 return true; 00620 } 00621 00622 void KJavaAppletViewerLiveConnectExtension::unregister(const unsigned long objid) 00623 { 00624 if (!m_viewer->view () || !m_viewer->view ()) 00625 return; 00626 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet (); 00627 if (!applet || objid == 0) { 00628 // typically a gc after a function call on the applet, 00629 // no need to send to the jvm 00630 return; 00631 } 00632 QStringList args; 00633 args.append (QString::number (applet->appletId ())); 00634 args.append (QString::number ((int) objid)); 00635 applet->getContext()->derefObject (args); 00636 } 00637 00638 void KJavaAppletViewerLiveConnectExtension::jsEvent (const QStringList & args) { 00639 if (args.count () < 2 || !m_viewer->appletAlive ()) 00640 return; 00641 bool ok; 00642 QStringList::ConstIterator it = args.begin(); 00643 const QStringList::ConstIterator itEnd = args.end(); 00644 const unsigned long objid = (*it).toInt(&ok); 00645 ++it; 00646 const QString event = (*it); 00647 ++it; 00648 KParts::LiveConnectExtension::ArgList arglist; 00649 00650 for (; it != itEnd; ++it) { 00651 // take a deep breath here 00652 const QStringList::ConstIterator prev = it++; 00653 arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type) (*prev).toInt(), (*it))); 00654 } 00655 emit partEvent (objid, event, arglist); 00656 } 00657 00658 int KJavaAppletViewerLiveConnectExtension::m_jssessions = 0; 00659 00660 //----------------------------------------------------------------------------- 00661 00662 #include "kjavaappletviewer.moc"
KDE 4.6 API Reference