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