00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021
00022 #ifdef KDE_USE_FINAL
00023 #undef Always
00024 #endif
00025 #include <qdir.h>
00026 #include <qtable.h>
00027 #include <qpair.h>
00028 #include <qtimer.h>
00029 #include <qguardedptr.h>
00030
00031 #include <klibloader.h>
00032 #include <kaboutdata.h>
00033 #include <kstaticdeleter.h>
00034 #include <klocale.h>
00035 #include <kapplication.h>
00036 #include <kdebug.h>
00037 #include <kconfig.h>
00038 #include <kio/authinfo.h>
00039 #include <dcopclient.h>
00040
00041 #include "kjavaappletwidget.h"
00042 #include "kjavaappletviewer.h"
00043 #include "kjavaappletserver.h"
00044
00045
00046 K_EXPORT_COMPONENT_FACTORY (kjavaappletviewer, KJavaAppletViewerFactory)
00047
00048 KInstance *KJavaAppletViewerFactory::s_instance = 0;
00049
00050 KJavaAppletViewerFactory::KJavaAppletViewerFactory () {
00051 s_instance = new KInstance ("KJavaAppletViewer");
00052 }
00053
00054 KJavaAppletViewerFactory::~KJavaAppletViewerFactory () {
00055 delete s_instance;
00056 }
00057
00058 KParts::Part *KJavaAppletViewerFactory::createPartObject
00059 (QWidget *wparent, const char *wname,
00060 QObject *parent, const char * name, const char *, const QStringList & args) {
00061 return new KJavaAppletViewer (wparent, wname, parent, name, args);
00062 }
00063
00064
00065
00066 class KJavaServerMaintainer;
00067 static KJavaServerMaintainer * serverMaintainer = 0;
00068
00069 class KJavaServerMaintainer {
00070 public:
00071 KJavaServerMaintainer () { }
00072 ~KJavaServerMaintainer ();
00073
00074 KJavaAppletContext * getContext (QObject*, const QString &);
00075 void releaseContext (QObject*, const QString &);
00076 void setServer (KJavaAppletServer * s);
00077 private:
00078 typedef QMap <QPair <QObject*, QString>, QPair <KJavaAppletContext*, int> >
00079 ContextMap;
00080 ContextMap m_contextmap;
00081 QGuardedPtr <KJavaAppletServer> server;
00082 };
00083
00084 KJavaServerMaintainer::~KJavaServerMaintainer () {
00085 delete server;
00086 }
00087
00088 KJavaAppletContext * KJavaServerMaintainer::getContext (QObject * w, const QString & doc) {
00089 ContextMap::key_type key = qMakePair (w, doc);
00090 ContextMap::iterator it = m_contextmap.find (key);
00091 if (it != m_contextmap.end ()) {
00092 ++((*it).second);
00093 return (*it).first;
00094 }
00095 KJavaAppletContext* const context = new KJavaAppletContext ();
00096 m_contextmap.insert (key, qMakePair(context, 1));
00097 return context;
00098 }
00099
00100 void KJavaServerMaintainer::releaseContext (QObject * w, const QString & doc) {
00101 ContextMap::iterator it = m_contextmap.find (qMakePair (w, doc));
00102 if (it != m_contextmap.end () && --(*it).second <= 0) {
00103 kdDebug(6100) << "KJavaServerMaintainer::releaseContext" << endl;
00104 (*it).first->deleteLater ();
00105 m_contextmap.remove (it);
00106 }
00107 }
00108
00109 inline void KJavaServerMaintainer::setServer (KJavaAppletServer * s) {
00110 if (!server)
00111 server = s;
00112 }
00113
00114 static KStaticDeleter <KJavaServerMaintainer> serverMaintainerDeleter;
00115
00116
00117
00118 AppletParameterDialog::AppletParameterDialog (KJavaAppletWidget * parent)
00119 : KDialogBase (parent, "paramdialog", true, i18n ("Applet Parameters"),
00120 KDialogBase::Close, KDialogBase::Close, true),
00121 m_appletWidget (parent) {
00122 KJavaApplet* const applet = parent->applet ();
00123 table = new QTable (30, 2, this);
00124 table->setMinimumSize (QSize (600, 400));
00125 table->setColumnWidth (0, 200);
00126 table->setColumnWidth (1, 340);
00127 QHeader* const header = table->horizontalHeader();
00128 header->setLabel (0, i18n ("Parameter"));
00129 header->setLabel (1, i18n ("Value"));
00130 QTableItem * tit = new QTableItem (table, QTableItem::Never, i18n("Class"));
00131 table->setItem (0, 0, tit);
00132 tit = new QTableItem(table, QTableItem::Always, applet->appletClass());
00133 table->setItem (0, 1, tit);
00134 tit = new QTableItem (table, QTableItem::Never, i18n ("Base URL"));
00135 table->setItem (1, 0, tit);
00136 tit = new QTableItem(table, QTableItem::Always, applet->baseURL());
00137 table->setItem (1, 1, tit);
00138 tit = new QTableItem (table, QTableItem::Never, i18n ("Archives"));
00139 table->setItem (2, 0, tit);
00140 tit = new QTableItem(table, QTableItem::Always, applet->archives());
00141 table->setItem (2, 1, tit);
00142 QMap<QString,QString>::const_iterator it = applet->getParams().begin();
00143 const QMap<QString,QString>::const_iterator itEnd = applet->getParams().end();
00144 for (int count = 2; it != itEnd; ++it) {
00145 tit = new QTableItem (table, QTableItem::Always, it.key ());
00146 table->setItem (++count, 0, tit);
00147 tit = new QTableItem(table, QTableItem::Always, it.data ());
00148 table->setItem (count, 1, tit);
00149 }
00150 setMainWidget (table);
00151 }
00152
00153 void AppletParameterDialog::slotClose () {
00154 table->selectCells (0, 0, 0, 0);
00155 KJavaApplet* const applet = m_appletWidget->applet ();
00156 applet->setAppletClass (table->item (0, 1)->text ());
00157 applet->setBaseURL (table->item (1, 1)->text ());
00158 applet->setArchives (table->item (2, 1)->text ());
00159 const int lim = table->numRows();
00160 for (int i = 3; i < lim; ++i) {
00161 if (table->item (i, 0) && table->item (i, 1) && !table->item (i, 0)->text ().isEmpty ())
00162 applet->setParameter (table->item (i, 0)->text (),
00163 table->item (i, 1)->text ());
00164 }
00165 hide ();
00166 }
00167
00168
00169 class CoverWidget : public QWidget {
00170 KJavaAppletWidget * m_appletwidget;
00171 public:
00172 CoverWidget (QWidget *);
00173 ~CoverWidget () {}
00174 KJavaAppletWidget * appletWidget () const;
00175 protected:
00176 void resizeEvent (QResizeEvent * e);
00177 };
00178
00179 inline CoverWidget::CoverWidget (QWidget * parent)
00180 : QWidget (parent, "KJavaAppletViewer Widget")
00181 {
00182 m_appletwidget = new KJavaAppletWidget (this);
00183 setFocusProxy (m_appletwidget);
00184 }
00185
00186 inline KJavaAppletWidget * CoverWidget::appletWidget () const {
00187 return m_appletwidget;
00188 }
00189
00190 void CoverWidget::resizeEvent (QResizeEvent * e) {
00191 m_appletwidget->resize (e->size().width(), e->size().height());
00192 }
00193
00194
00195
00196 KJavaAppletViewer::KJavaAppletViewer (QWidget * wparent, const char *,
00197 QObject * parent, const char * name, const QStringList & args)
00198 : KParts::ReadOnlyPart (parent, name),
00199 m_browserextension (new KJavaAppletViewerBrowserExtension (this)),
00200 m_liveconnect (new KJavaAppletViewerLiveConnectExtension (this)),
00201 m_closed (true)
00202 {
00203 if (!serverMaintainer) {
00204 serverMaintainerDeleter.setObject (serverMaintainer,
00205 new KJavaServerMaintainer);
00206 }
00207 m_view = new CoverWidget (wparent);
00208 QString classname, classid, codebase, khtml_codebase, src_param;
00209 int width = -1;
00210 int height = -1;
00211 KJavaApplet* const applet = m_view->appletWidget()->applet ();
00212 QStringList::const_iterator it = args.begin();
00213 const QStringList::const_iterator itEnd = args.end();
00214 for ( ; it != itEnd; ++it) {
00215 const int equalPos = (*it).find("=");
00216 if (equalPos > 0) {
00217 const QString name = (*it).left (equalPos).upper ();
00218 QString value = (*it).right ((*it).length () - equalPos - 1);
00219 if (value.at(0)=='\"')
00220 value = value.right (value.length () - 1);
00221 if (value.at (value.length () - 1) == '\"')
00222 value.truncate (value.length () - 1);
00223 kdDebug(6100) << "name=" << name << " value=" << value << endl;
00224 if (!name.isEmpty()) {
00225 const QString name_lower = name.lower ();
00226 if (name == "__KHTML__PLUGINBASEURL") {
00227 baseurl = KURL (KURL (value), QString (".")).url ();
00228 } else if (name == "__KHTML__CODEBASE")
00229 khtml_codebase = value;
00230 else if (name_lower == QString::fromLatin1("codebase") ||
00231 name_lower == QString::fromLatin1("java_codebase")) {
00232 if (!value.isEmpty ())
00233 codebase = value;
00234 } else if (name == "__KHTML__CLASSID")
00235
00236 classid = value;
00237 else if (name_lower == QString::fromLatin1("code") ||
00238 name_lower == QString::fromLatin1("java_code"))
00239 classname = value;
00240 else if (name_lower == QString::fromLatin1("src"))
00241 src_param = value;
00242 else if (name_lower == QString::fromLatin1("archive") ||
00243 name_lower == QString::fromLatin1("java_archive") ||
00244 name_lower.startsWith ("cache_archive"))
00245 applet->setArchives (value);
00246 else if (name_lower == QString::fromLatin1("name"))
00247 applet->setAppletName (value);
00248 else if (name_lower == QString::fromLatin1("width"))
00249 width = value.toInt();
00250 else if (name_lower == QString::fromLatin1("height"))
00251 height = value.toInt();
00252 if (!name.startsWith ("__KHTML__")) {
00253 applet->setParameter (name, value);
00254 }
00255 }
00256 }
00257 }
00258 if (!classid.isEmpty ()) {
00259 applet->setParameter ("CLSID", classid);
00260 kdDebug(6100) << "classid=" << classid << classid.startsWith("clsid:")<< endl;
00261 if (classid.startsWith ("clsid:"))
00262
00263 khtml_codebase = baseurl;
00264 else if (classname.isEmpty () && classid.startsWith ("java:"))
00265 classname = classid.mid(5);
00266 }
00267 if (classname.isEmpty ())
00268 classname = src_param;
00269 else if (!src_param.isEmpty ())
00270 applet->setParameter (QString ("SRC"), src_param);
00271 if (codebase.isEmpty ())
00272 codebase = khtml_codebase;
00273 if (baseurl.isEmpty ()) {
00274
00275 QString pwd = QDir().absPath ();
00276 if (!pwd.endsWith (QChar (QDir::separator ())))
00277 pwd += QDir::separator ();
00278 baseurl = KURL (KURL (pwd), codebase).url ();
00279 }
00280 if (width > 0 && height > 0) {
00281 m_view->resize (width, height);
00282 applet->setSize( QSize( width, height ) );
00283 }
00284 applet->setBaseURL (baseurl);
00285
00286 const KURL kbaseURL( baseurl );
00287 const KURL newURL(kbaseURL, codebase);
00288 if (kapp->authorizeURLAction("redirect", KURL(baseurl), newURL))
00289 applet->setCodeBase (newURL.url());
00290 applet->setAppletClass (classname);
00291 KJavaAppletContext* const cxt = serverMaintainer->getContext (parent, baseurl);
00292 applet->setAppletContext (cxt);
00293
00294 KJavaAppletServer* const server = cxt->getServer ();
00295
00296 serverMaintainer->setServer (server);
00297
00298 if (!server->usingKIO ()) {
00299
00300 KIO::AuthInfo info;
00301 QString errorMsg;
00302 QCString replyType;
00303 QByteArray params;
00304 QByteArray reply;
00305 KIO::AuthInfo authResult;
00306
00307
00308 info.url = baseurl;
00309 info.verifyPath = true;
00310
00311 QDataStream stream(params, IO_WriteOnly);
00312 stream << info << m_view->topLevelWidget()->winId();
00313
00314 if (!kapp->dcopClient ()->call( "kded", "kpasswdserver", "checkAuthInfo(KIO::AuthInfo, long int)", params, replyType, reply ) ) {
00315 kdWarning() << "Can't communicate with kded_kpasswdserver!" << endl;
00316 } else if ( replyType == "KIO::AuthInfo" ) {
00317 QDataStream stream2( reply, IO_ReadOnly );
00318 stream2 >> authResult;
00319 applet->setUser (authResult.username);
00320 applet->setPassword (authResult.password);
00321 applet->setAuthName (authResult.realmValue);
00322 }
00323 }
00324
00325
00326 if (wparent)
00327 wparent->topLevelWidget ()->installEventFilter (this);
00328
00329 setInstance (KJavaAppletViewerFactory::instance ());
00330 KParts::Part::setWidget (m_view);
00331
00332 connect (applet->getContext(), SIGNAL(appletLoaded()), this, SLOT(appletLoaded()));
00333 connect (applet->getContext(), SIGNAL(showDocument(const QString&, const QString&)), m_browserextension, SLOT(showDocument(const QString&, const QString&)));
00334 connect (applet->getContext(), SIGNAL(showStatus(const QString &)), this, SLOT(infoMessage(const QString &)));
00335 connect (applet, SIGNAL(jsEvent (const QStringList &)), m_liveconnect, SLOT(jsEvent (const QStringList &)));
00336 }
00337
00338 bool KJavaAppletViewer::eventFilter (QObject *o, QEvent *e) {
00339 if (m_liveconnect->jsSessions () > 0) {
00340 switch (e->type()) {
00341 case QEvent::Destroy:
00342 case QEvent::Close:
00343 case QEvent::Quit:
00344 return true;
00345 default:
00346 break;
00347 }
00348 }
00349 return KParts::ReadOnlyPart::eventFilter(o,e);
00350 }
00351
00352 KJavaAppletViewer::~KJavaAppletViewer () {
00353 m_view = 0L;
00354 serverMaintainer->releaseContext (parent(), baseurl);
00355 }
00356
00357 bool KJavaAppletViewer::openURL (const KURL & url) {
00358 if (!m_view) return false;
00359 m_closed = false;
00360 KJavaAppletWidget* const w = m_view->appletWidget ();
00361 KJavaApplet* const applet = w->applet ();
00362 if (applet->isCreated ())
00363 applet->stop ();
00364 if (applet->appletClass ().isEmpty ()) {
00365
00366 if (applet->baseURL ().isEmpty ()) {
00367 applet->setAppletClass (url.fileName ());
00368 applet->setBaseURL (url.upURL ().url ());
00369 } else
00370 applet->setAppletClass (url.url ());
00371 AppletParameterDialog (w).exec ();
00372 applet->setSize (w->sizeHint());
00373 }
00374
00375 if (applet->size().width() > 0 || m_view->isVisible())
00376 w->showApplet ();
00377 else
00378 QTimer::singleShot (10, this, SLOT (delayedCreateTimeOut ()));
00379 if (!applet->failed ())
00380 emit started (0L);
00381 return url.isValid ();
00382 }
00383
00384 bool KJavaAppletViewer::closeURL () {
00385 kdDebug(6100) << "closeURL" << endl;
00386 m_closed = true;
00387 KJavaApplet* const applet = m_view->appletWidget ()->applet ();
00388 if (applet->isCreated ())
00389 applet->stop ();
00390 applet->getContext()->getServer()->endWaitForReturnData();
00391 return true;
00392 }
00393
00394 bool KJavaAppletViewer::appletAlive () const {
00395 return !m_closed && m_view &&
00396 m_view->appletWidget ()->applet () &&
00397 m_view->appletWidget ()->applet ()->isAlive ();
00398 }
00399
00400 bool KJavaAppletViewer::openFile () {
00401 return false;
00402 }
00403
00404 void KJavaAppletViewer::delayedCreateTimeOut () {
00405 KJavaAppletWidget* const w = m_view->appletWidget ();
00406 if (!w->applet ()->isCreated () && !m_closed)
00407 w->showApplet ();
00408 }
00409
00410 void KJavaAppletViewer::appletLoaded () {
00411 if (!m_view) return;
00412 KJavaApplet* const applet = m_view->appletWidget ()->applet ();
00413 if (applet->isAlive() || applet->failed())
00414 emit completed();
00415 }
00416
00417 void KJavaAppletViewer::infoMessage (const QString & msg) {
00418 m_browserextension->infoMessage(msg);
00419 }
00420
00421 KAboutData* KJavaAppletViewer::createAboutData () {
00422 return new KAboutData("KJavaAppletViewer", I18N_NOOP("KDE Java Applet Plugin"), "1.0");
00423 }
00424
00425
00426
00427 KJavaAppletViewerBrowserExtension::KJavaAppletViewerBrowserExtension (KJavaAppletViewer * parent)
00428 : KParts::BrowserExtension (parent, "KJavaAppletViewer Browser Extension") {
00429 }
00430
00431 void KJavaAppletViewerBrowserExtension::urlChanged (const QString & url) {
00432 emit setLocationBarURL (url);
00433 }
00434
00435 void KJavaAppletViewerBrowserExtension::setLoadingProgress (int percentage) {
00436 emit loadingProgress (percentage);
00437 }
00438
00439 void KJavaAppletViewerBrowserExtension::setURLArgs (const KParts::URLArgs & ) {
00440 }
00441
00442 void KJavaAppletViewerBrowserExtension::saveState (QDataStream & stream) {
00443 KJavaApplet* const applet = static_cast<KJavaAppletViewer*>(parent())->view()->appletWidget ()->applet ();
00444 stream << applet->appletClass();
00445 stream << applet->baseURL();
00446 stream << applet->archives();
00447 stream << applet->getParams().size ();
00448 QMap<QString,QString>::const_iterator it = applet->getParams().begin();
00449 const QMap<QString,QString>::const_iterator itEnd = applet->getParams().end();
00450 for ( ; it != itEnd; ++it) {
00451 stream << it.key ();
00452 stream << it.data ();
00453 }
00454 }
00455
00456 void KJavaAppletViewerBrowserExtension::restoreState (QDataStream & stream) {
00457 KJavaAppletWidget* const w = static_cast<KJavaAppletViewer*>(parent())->view()->appletWidget();
00458 KJavaApplet* const applet = w->applet ();
00459 QString key, val;
00460 int paramcount;
00461 stream >> val;
00462 applet->setAppletClass (val);
00463 stream >> val;
00464 applet->setBaseURL (val);
00465 stream >> val;
00466 applet->setArchives (val);
00467 stream >> paramcount;
00468 for (int i = 0; i < paramcount; ++i) {
00469 stream >> key;
00470 stream >> val;
00471 applet->setParameter (key, val);
00472 kdDebug(6100) << "restoreState key:" << key << " val:" << val << endl;
00473 }
00474 applet->setSize (w->sizeHint ());
00475 if (w->isVisible())
00476 w->showApplet ();
00477 }
00478
00479 void KJavaAppletViewerBrowserExtension::showDocument (const QString & doc,
00480 const QString & frame) {
00481 const KURL url (doc);
00482 KParts::URLArgs args;
00483 args.frameName = frame;
00484 emit openURLRequest (url, args);
00485 }
00486
00487
00488
00489 KJavaAppletViewerLiveConnectExtension::KJavaAppletViewerLiveConnectExtension(KJavaAppletViewer * parent)
00490 : KParts::LiveConnectExtension (parent, "KJavaAppletViewer LiveConnect Extension"), m_viewer (parent) {
00491 }
00492
00493 bool KJavaAppletViewerLiveConnectExtension::get (
00494 const unsigned long objid, const QString & name,
00495 KParts::LiveConnectExtension::Type & type,
00496 unsigned long & rid, QString & value)
00497 {
00498 if (!m_viewer->appletAlive ())
00499 return false;
00500 QStringList args, ret_args;
00501 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet ();
00502 args.append (QString::number (applet->appletId ()));
00503 args.append (QString::number ((int) objid));
00504 args.append (name);
00505 m_jssessions++;
00506 const bool ret = applet->getContext()->getMember (args, ret_args);
00507 m_jssessions--;
00508 if (!ret || ret_args.count() != 3) return false;
00509 bool ok;
00510 int itype = ret_args[0].toInt (&ok);
00511 if (!ok || itype < 0) return false;
00512 type = (KParts::LiveConnectExtension::Type) itype;
00513 rid = ret_args[1].toInt (&ok);
00514 if (!ok) return false;
00515 value = ret_args[2];
00516 return true;
00517 }
00518
00519 bool KJavaAppletViewerLiveConnectExtension::put(const unsigned long objid, const QString & name, const QString & value)
00520 {
00521 if (!m_viewer->appletAlive ())
00522 return false;
00523 QStringList args;
00524 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet ();
00525 args.append (QString::number (applet->appletId ()));
00526 args.append (QString::number ((int) objid));
00527 args.append (name);
00528 args.append (value);
00529 ++m_jssessions;
00530 const bool ret = applet->getContext()->putMember (args);
00531 --m_jssessions;
00532 return ret;
00533 }
00534
00535 bool KJavaAppletViewerLiveConnectExtension::call( const unsigned long objid, const QString & func, const QStringList & fargs, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value )
00536 {
00537 if (!m_viewer->appletAlive ())
00538 return false;
00539 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet ();
00540 QStringList args, ret_args;
00541 args.append (QString::number (applet->appletId ()));
00542 args.append (QString::number ((int) objid));
00543 args.append (func);
00544 {
00545 QStringList::const_iterator it = fargs.begin();
00546 const QStringList::const_iterator itEnd = fargs.end();
00547 for ( ; it != itEnd; ++it)
00548 args.append(*it);
00549 }
00550
00551 ++m_jssessions;
00552 const bool ret = applet->getContext()->callMember (args, ret_args);
00553 --m_jssessions;
00554 if (!ret || ret_args.count () != 3) return false;
00555 bool ok;
00556 const int itype = ret_args[0].toInt (&ok);
00557 if (!ok || itype < 0) return false;
00558 type = (KParts::LiveConnectExtension::Type) itype;
00559 retobjid = ret_args[1].toInt (&ok);
00560 if (!ok) return false;
00561 value = ret_args[2];
00562 return true;
00563 }
00564
00565 void KJavaAppletViewerLiveConnectExtension::unregister(const unsigned long objid)
00566 {
00567 if (!m_viewer->view () || !m_viewer->view ())
00568 return;
00569 KJavaApplet* const applet = m_viewer->view ()->appletWidget ()->applet ();
00570 if (!applet || objid == 0) {
00571
00572
00573 return;
00574 }
00575 QStringList args;
00576 args.append (QString::number (applet->appletId ()));
00577 args.append (QString::number ((int) objid));
00578 applet->getContext()->derefObject (args);
00579 }
00580
00581 void KJavaAppletViewerLiveConnectExtension::jsEvent (const QStringList & args) {
00582 if (args.count () < 2 || !m_viewer->appletAlive ())
00583 return;
00584 bool ok;
00585 QStringList::ConstIterator it = args.begin();
00586 const QStringList::ConstIterator itEnd = args.end();
00587 const unsigned long objid = (*it).toInt(&ok);
00588 ++it;
00589 const QString event = (*it);
00590 ++it;
00591 KParts::LiveConnectExtension::ArgList arglist;
00592
00593 for (; it != itEnd; ++it) {
00594
00595 const QStringList::ConstIterator prev = it++;
00596 arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type) (*prev).toInt(), (*it)));
00597 }
00598 emit partEvent (objid, event, arglist);
00599 }
00600
00601 int KJavaAppletViewerLiveConnectExtension::m_jssessions = 0;
00602
00603
00604
00605 #include "kjavaappletviewer.moc"