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 * 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 * 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 *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>::iterator it = applet->getParams().begin ();
00143 for (int count = 2; it != applet->getParams().end (); ++it) {
00144 tit = new QTableItem (table, QTableItem::Always, it.key ());
00145 table->setItem (++count, 0, tit);
00146 tit = new QTableItem(table, QTableItem::Always, it.data ());
00147 table->setItem (count, 1, tit);
00148 }
00149 setMainWidget (table);
00150 }
00151
00152 void AppletParameterDialog::slotClose () {
00153 table->selectCells (0, 0, 0, 0);
00154 KJavaApplet * applet = m_appletWidget->applet ();
00155 applet->setAppletClass (table->item (0, 1)->text ());
00156 applet->setBaseURL (table->item (1, 1)->text ());
00157 applet->setArchives (table->item (2, 1)->text ());
00158 for (int i = 3; i < table->numRows (); ++i) {
00159 if (table->item (i, 0) && table->item (i, 1) && !table->item (i, 0)->text ().isEmpty ())
00160 applet->setParameter (table->item (i, 0)->text (),
00161 table->item (i, 1)->text ());
00162 }
00163 hide ();
00164 }
00165
00166
00167 class CoverWidget : public QWidget {
00168 KJavaAppletWidget * m_appletwidget;
00169 public:
00170 CoverWidget (QWidget *);
00171 ~CoverWidget () {}
00172 KJavaAppletWidget * appletWidget () const;
00173 protected:
00174 void resizeEvent (QResizeEvent * e);
00175 };
00176
00177 inline CoverWidget::CoverWidget (QWidget * parent)
00178 : QWidget (parent, "KJavaAppletViewer Widget")
00179 {
00180 m_appletwidget = new KJavaAppletWidget (this);
00181 setFocusProxy (m_appletwidget);
00182 }
00183
00184 inline KJavaAppletWidget * CoverWidget::appletWidget () const {
00185 return m_appletwidget;
00186 }
00187
00188 void CoverWidget::resizeEvent (QResizeEvent * e) {
00189 m_appletwidget->resize (e->size().width(), e->size().height());
00190 }
00191
00192
00193
00194 KJavaAppletViewer::KJavaAppletViewer (QWidget * wparent, const char *,
00195 QObject * parent, const char * name, const QStringList & args)
00196 : KParts::ReadOnlyPart (parent, name),
00197 m_browserextension (new KJavaAppletViewerBrowserExtension (this)),
00198 m_liveconnect (new KJavaAppletViewerLiveConnectExtension (this)),
00199 m_closed (true)
00200 {
00201 if (!serverMaintainer) {
00202 serverMaintainerDeleter.setObject (serverMaintainer,
00203 new KJavaServerMaintainer);
00204 }
00205 m_view = new CoverWidget (wparent);
00206 QString classname, classid, codebase, khtml_codebase;
00207 int width = -1;
00208 int height = -1;
00209 KJavaApplet * applet = m_view->appletWidget()->applet ();
00210 QStringList::const_iterator it = args.begin ();
00211 for ( ; it != args.end (); ++it) {
00212 int equalPos = (*it).find("=");
00213 if (equalPos > 0) {
00214 QString name = (*it).left (equalPos).upper ();
00215 QString value = (*it).right ((*it).length () - equalPos - 1);
00216 if (value.at(0)=='\"')
00217 value = value.right (value.length () - 1);
00218 if (value.at (value.length () - 1) == '\"')
00219 value.truncate (value.length () - 1);
00220 kdDebug(6100) << "name=" << name << " value=" << value << endl;
00221 if (!name.isEmpty()) {
00222 QString name_lower = name.lower ();
00223 if (name == "__KHTML__PLUGINBASEURL") {
00224 baseurl = KURL (KURL (value), QString (".")).url ();
00225 } else if (name == "__KHTML__CODEBASE")
00226 khtml_codebase = value;
00227 else if (name_lower == QString::fromLatin1("codebase") ||
00228 name_lower == QString::fromLatin1("java_codebase")) {
00229 if (!value.isEmpty ())
00230 codebase = value;
00231 } else if (name == "__KHTML__CLASSID")
00232
00233 classid = value;
00234 else if (name_lower == QString::fromLatin1("code") ||
00235 name_lower == QString::fromLatin1("java_code") ||
00236 name_lower == QString::fromLatin1("src"))
00237 classname = value;
00238 else if (name_lower == QString::fromLatin1("archive") ||
00239 name_lower == QString::fromLatin1("java_archive") ||
00240 name_lower.startsWith ("cache_archive"))
00241 applet->setArchives (value);
00242 else if (name_lower == QString::fromLatin1("name"))
00243 applet->setAppletName (value);
00244 else if (name_lower == QString::fromLatin1("width"))
00245 width = value.toInt();
00246 else if (name_lower == QString::fromLatin1("height"))
00247 height = value.toInt();
00248 else {
00249 applet->setParameter (name, value);
00250 }
00251 }
00252 }
00253 }
00254 if (!classid.isEmpty ()) {
00255 applet->setParameter ("CLSID", classid);
00256 kdDebug(6100) << "classid=" << classid << classid.startsWith("clsid:")<< endl;
00257 if (classid.startsWith ("clsid:"))
00258
00259 khtml_codebase = baseurl;
00260 else if (classname.isEmpty () && classid.startsWith ("java:"))
00261 classname = classid.mid(5);
00262 }
00263 if (codebase.isEmpty ())
00264 codebase = khtml_codebase;
00265 if (baseurl.isEmpty ()) {
00266
00267 QString pwd = QDir().absPath ();
00268 if (!pwd.endsWith (QChar (QDir::separator ())))
00269 pwd += QDir::separator ();
00270 baseurl = KURL (KURL (pwd), codebase).url ();
00271 }
00272 if (width > 0 && height > 0) {
00273 m_view->resize (width, height);
00274 applet->setSize( QSize( width, height ) );
00275 }
00276 applet->setBaseURL (baseurl);
00277
00278 KURL kbaseURL( baseurl );
00279 KURL newURL(kbaseURL, codebase);
00280 if (kapp->authorizeURLAction("redirect", KURL(baseurl), newURL))
00281 applet->setCodeBase (newURL.url());
00282 applet->setAppletClass (classname);
00283 KJavaAppletContext * cxt = serverMaintainer->getContext (parent, baseurl);
00284 applet->setAppletContext (cxt);
00285
00286 KJavaAppletServer * server = cxt->getServer ();
00287
00288 serverMaintainer->setServer (server);
00289
00290 if (!server->usingKIO ()) {
00291
00292 KIO::AuthInfo info;
00293 QString errorMsg;
00294 QCString replyType;
00295 QByteArray params;
00296 QByteArray reply;
00297 KIO::AuthInfo authResult;
00298
00299
00300 info.url = baseurl;
00301 info.verifyPath = true;
00302
00303 QDataStream stream(params, IO_WriteOnly);
00304 stream << info << m_view->topLevelWidget()->winId();
00305
00306 if (!kapp->dcopClient ()->call( "kded", "kpasswdserver", "checkAuthInfo(KIO::AuthInfo, long int)", params, replyType, reply ) ) {
00307 kdWarning() << "Can't communicate with kded_kpasswdserver!" << endl;
00308 } else if ( replyType == "KIO::AuthInfo" ) {
00309 QDataStream stream2( reply, IO_ReadOnly );
00310 stream2 >> authResult;
00311 applet->setUser (authResult.username);
00312 applet->setPassword (authResult.password);
00313 applet->setAuthName (authResult.realmValue);
00314 }
00315 }
00316
00317
00318 if (wparent)
00319 wparent->topLevelWidget ()->installEventFilter (this);
00320
00321 setInstance (KJavaAppletViewerFactory::instance ());
00322 KParts::Part::setWidget (m_view);
00323
00324 connect (applet->getContext(), SIGNAL(appletLoaded()), this, SLOT(appletLoaded()));
00325 connect (applet->getContext(), SIGNAL(showDocument(const QString&, const QString&)), m_browserextension, SLOT(showDocument(const QString&, const QString&)));
00326 connect (applet->getContext(), SIGNAL(showStatus(const QString &)), this, SLOT(infoMessage(const QString &)));
00327 connect (applet, SIGNAL(jsEvent (const QStringList &)), m_liveconnect, SLOT(jsEvent (const QStringList &)));
00328 }
00329
00330 bool KJavaAppletViewer::eventFilter (QObject *o, QEvent *e) {
00331 if (m_liveconnect->jsSessions () > 0) {
00332 switch (e->type()) {
00333 case QEvent::Destroy:
00334 case QEvent::Close:
00335 case QEvent::Quit:
00336 return true;
00337 default:
00338 break;
00339 }
00340 }
00341 return KParts::ReadOnlyPart::eventFilter(o,e);
00342 }
00343
00344 KJavaAppletViewer::~KJavaAppletViewer () {
00345 m_view = 0L;
00346 serverMaintainer->releaseContext (parent(), baseurl);
00347 }
00348
00349 bool KJavaAppletViewer::openURL (const KURL & url) {
00350 if (!m_view) return false;
00351 m_closed = false;
00352 KJavaAppletWidget * w = m_view->appletWidget ();
00353 KJavaApplet * applet = w->applet ();
00354 if (applet->isCreated ())
00355 applet->stop ();
00356 if (applet->appletClass ().isEmpty ()) {
00357
00358 if (applet->baseURL ().isEmpty ()) {
00359 applet->setAppletClass (url.fileName ());
00360 applet->setBaseURL (url.upURL ().url ());
00361 } else
00362 applet->setAppletClass (url.url ());
00363 AppletParameterDialog (w).exec ();
00364 applet->setSize (w->sizeHint());
00365 }
00366
00367 if (applet->size().width() > 0 || m_view->isVisible())
00368 w->showApplet ();
00369 else
00370 QTimer::singleShot (10, this, SLOT (delayedCreateTimeOut ()));
00371 if (!applet->failed ())
00372 emit started (0L);
00373 return url.isValid ();
00374 }
00375
00376 bool KJavaAppletViewer::closeURL () {
00377 kdDebug(6100) << "closeURL" << endl;
00378 m_closed = true;
00379 KJavaApplet * applet = m_view->appletWidget ()->applet ();
00380 if (applet->isCreated ())
00381 applet->stop ();
00382 applet->getContext()->getServer()->endWaitForReturnData();
00383 return true;
00384 }
00385
00386 bool KJavaAppletViewer::appletAlive () const {
00387 return !m_closed && m_view &&
00388 m_view->appletWidget ()->applet () &&
00389 m_view->appletWidget ()->applet ()->isAlive ();
00390 }
00391
00392 bool KJavaAppletViewer::openFile () {
00393 return false;
00394 }
00395
00396 void KJavaAppletViewer::delayedCreateTimeOut () {
00397 KJavaAppletWidget * w = m_view->appletWidget ();
00398 if (!w->applet ()->isCreated () && !m_closed)
00399 w->showApplet ();
00400 }
00401
00402 void KJavaAppletViewer::appletLoaded () {
00403 if (!m_view) return;
00404 KJavaApplet * applet = m_view->appletWidget ()->applet ();
00405 if (applet->isAlive() || applet->failed())
00406 emit completed();
00407 }
00408
00409 void KJavaAppletViewer::infoMessage (const QString & msg) {
00410 m_browserextension->infoMessage(msg);
00411 }
00412
00413 KAboutData* KJavaAppletViewer::createAboutData () {
00414 return new KAboutData("KJavaAppletViewer", I18N_NOOP("KDE Java Applet Plugin"), "1.0");
00415 }
00416
00417
00418
00419 KJavaAppletViewerBrowserExtension::KJavaAppletViewerBrowserExtension (KJavaAppletViewer * parent)
00420 : KParts::BrowserExtension (parent, "KJavaAppletViewer Browser Extension") {
00421 }
00422
00423 void KJavaAppletViewerBrowserExtension::urlChanged (const QString & url) {
00424 emit setLocationBarURL (url);
00425 }
00426
00427 void KJavaAppletViewerBrowserExtension::setLoadingProgress (int percentage) {
00428 emit loadingProgress (percentage);
00429 }
00430
00431 void KJavaAppletViewerBrowserExtension::setURLArgs (const KParts::URLArgs & ) {
00432 }
00433
00434 void KJavaAppletViewerBrowserExtension::saveState (QDataStream & stream) {
00435 KJavaApplet * applet = static_cast<KJavaAppletViewer*>(parent())->view()->appletWidget ()->applet ();
00436 stream << applet->appletClass();
00437 stream << applet->baseURL();
00438 stream << applet->archives();
00439 stream << applet->getParams().size ();
00440 QMap<QString,QString>::iterator it = applet->getParams().begin ();
00441 for ( ; it != applet->getParams().end (); ++it) {
00442 stream << it.key ();
00443 stream << it.data ();
00444 }
00445 }
00446
00447 void KJavaAppletViewerBrowserExtension::restoreState (QDataStream & stream) {
00448 KJavaAppletWidget * w = static_cast<KJavaAppletViewer*>(parent())->view()->appletWidget();
00449 KJavaApplet * applet = w->applet ();
00450 QString key, val;
00451 int paramcount;
00452 stream >> val;
00453 applet->setAppletClass (val);
00454 stream >> val;
00455 applet->setBaseURL (val);
00456 stream >> val;
00457 applet->setArchives (val);
00458 stream >> paramcount;
00459 for (int i = 0; i < paramcount; ++i) {
00460 stream >> key;
00461 stream >> val;
00462 applet->setParameter (key, val);
00463 kdDebug(6100) << "restoreState key:" << key << " val:" << val << endl;
00464 }
00465 applet->setSize (w->sizeHint ());
00466 if (w->isVisible())
00467 w->showApplet ();
00468 }
00469
00470 void KJavaAppletViewerBrowserExtension::showDocument (const QString & doc,
00471 const QString & frame) {
00472 KURL url (doc);
00473 KParts::URLArgs args;
00474 args.frameName = frame;
00475 emit openURLRequest (url, args);
00476 }
00477
00478
00479
00480 KJavaAppletViewerLiveConnectExtension::KJavaAppletViewerLiveConnectExtension(KJavaAppletViewer * parent)
00481 : KParts::LiveConnectExtension (parent, "KJavaAppletViewer LiveConnect Extension"), m_viewer (parent) {
00482 }
00483
00484 bool KJavaAppletViewerLiveConnectExtension::get (
00485 const unsigned long objid, const QString & name,
00486 KParts::LiveConnectExtension::Type & type,
00487 unsigned long & rid, QString & value)
00488 {
00489 if (!m_viewer->appletAlive ())
00490 return false;
00491 QStringList args, ret_args;
00492 KJavaApplet * applet = m_viewer->view ()->appletWidget ()->applet ();
00493 args.append (QString::number (applet->appletId ()));
00494 args.append (QString::number ((int) objid));
00495 args.append (name);
00496 m_jssessions++;
00497 bool ret = applet->getContext()->getMember (args, ret_args);
00498 m_jssessions--;
00499 if (!ret || ret_args.count() != 3) return false;
00500 bool ok;
00501 int itype = ret_args[0].toInt (&ok);
00502 if (!ok || itype < 0) return false;
00503 type = (KParts::LiveConnectExtension::Type) itype;
00504 rid = ret_args[1].toInt (&ok);
00505 if (!ok) return false;
00506 value = ret_args[2];
00507 return true;
00508 }
00509
00510 bool KJavaAppletViewerLiveConnectExtension::put(const unsigned long objid, const QString & name, const QString & value)
00511 {
00512 if (!m_viewer->appletAlive ())
00513 return false;
00514 QStringList args;
00515 KJavaApplet * applet = m_viewer->view ()->appletWidget ()->applet ();
00516 args.append (QString::number (applet->appletId ()));
00517 args.append (QString::number ((int) objid));
00518 args.append (name);
00519 args.append (value);
00520 m_jssessions++;
00521 bool ret = applet->getContext()->putMember (args);
00522 m_jssessions--;
00523 return ret;
00524 }
00525
00526 bool KJavaAppletViewerLiveConnectExtension::call( const unsigned long objid, const QString & func, const QStringList & fargs, KParts::LiveConnectExtension::Type & type, unsigned long & retobjid, QString & value )
00527 {
00528 if (!m_viewer->appletAlive ())
00529 return false;
00530 KJavaApplet * applet = m_viewer->view ()->appletWidget ()->applet ();
00531 QStringList args, ret_args;
00532 args.append (QString::number (applet->appletId ()));
00533 args.append (QString::number ((int) objid));
00534 args.append (func);
00535 for (QStringList::const_iterator it=fargs.begin(); it != fargs.end(); ++it)
00536 args.append(*it);
00537 m_jssessions++;
00538 bool ret = applet->getContext()->callMember (args, ret_args);
00539 m_jssessions--;
00540 if (!ret || ret_args.count () != 3) return false;
00541 bool ok;
00542 int itype = ret_args[0].toInt (&ok);
00543 if (!ok || itype < 0) return false;
00544 type = (KParts::LiveConnectExtension::Type) itype;
00545 retobjid = ret_args[1].toInt (&ok);
00546 if (!ok) return false;
00547 value = ret_args[2];
00548 return true;
00549 }
00550
00551 void KJavaAppletViewerLiveConnectExtension::unregister(const unsigned long objid)
00552 {
00553 if (!m_viewer->view () || !m_viewer->view ())
00554 return;
00555 KJavaApplet * applet = m_viewer->view ()->appletWidget ()->applet ();
00556 if (!applet || objid == 0) {
00557
00558
00559 return;
00560 }
00561 QStringList args;
00562 args.append (QString::number (applet->appletId ()));
00563 args.append (QString::number ((int) objid));
00564 applet->getContext()->derefObject (args);
00565 }
00566
00567 void KJavaAppletViewerLiveConnectExtension::jsEvent (const QStringList & args) {
00568 if (args.count () < 2 || !m_viewer->appletAlive ())
00569 return;
00570 bool ok;
00571 unsigned long objid = args[0].toInt(&ok);
00572 QString event = args[1];
00573 KParts::LiveConnectExtension::ArgList arglist;
00574 for (unsigned i = 2; i < args.count(); i += 2)
00575
00576 arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type) args[i].toInt(), args[i+1]));
00577 emit partEvent (objid, event, arglist);
00578 }
00579
00580 int KJavaAppletViewerLiveConnectExtension::m_jssessions = 0;
00581
00582
00583
00584 #include "kjavaappletviewer.moc"