KHtml

kjavaappletviewer.cpp
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2003 Koos Vriezen <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kjavaappletviewer.h"
22 
23 #include "kjavaappletserver.h"
24 
25 #include <stdio.h>
26 
27 #include <QDialogButtonBox>
28 #include <QDir>
29 #include <QPair>
30 #include <QTimer>
31 #include <QLabel>
32 #include <QStatusBar>
33 #include <QTableWidget>
34 #include <QVBoxLayout>
35 #include <QDBusInterface>
36 #include <QDBusReply>
37 #include <QDBusConnection>
38 #include <QKeyEvent>
39 
40 #include <kurlauthorized.h>
41 #include <kaboutdata.h>
42 #include <klocalizedstring.h>
43 #include <kiconloader.h>
44 #include "kjavaappletviewer_debug.h"
45 #include <kconfig.h>
46 #include <kio/authinfo.h>
47 #include <kio/global.h>
48 #include <kusertimestamp.h>
49 
50 KAboutData *KJavaAppletViewerFactory::s_aboutData = nullptr;
51 KIconLoader *KJavaAppletViewerFactory::s_iconLoader = nullptr;
52 
53 KJavaAppletViewerFactory::KJavaAppletViewerFactory()
54 {
55  s_aboutData = new KAboutData("KJavaAppletViewer", i18n("KDE Java Applet Plugin"), "1.0");
56  s_iconLoader = new KIconLoader("kjava");
57 }
58 
59 KJavaAppletViewerFactory::~KJavaAppletViewerFactory()
60 {
61  delete s_iconLoader;
62  delete s_aboutData;
63 }
64 
65 QObject *KJavaAppletViewerFactory::create(const char *, QWidget *wparent, QObject *parent,
66  const QVariantList &args, const QString &)
67 {
68  QStringList argsStrings;
69  for (int i = 0; i < args.size(); ++i) {
70  argsStrings.append(args[i].toString());
71  }
72  return new KJavaAppletViewer(wparent, parent, argsStrings);
73 }
74 
75 //-----------------------------------------------------------------------------
76 
77 class KJavaServerMaintainer;
78 
79 class KJavaServerMaintainer
80 {
81 public:
82  KJavaServerMaintainer() { }
83  ~KJavaServerMaintainer();
84 
85  KJavaAppletContext *getContext(QObject *, const QString &);
86  void releaseContext(QObject *, const QString &);
87  void setServer(KJavaAppletServer *s);
89 private:
91  ContextMap;
92  ContextMap m_contextmap;
93 };
94 Q_GLOBAL_STATIC(KJavaServerMaintainer, serverMaintainer)
95 
96 KJavaServerMaintainer::~KJavaServerMaintainer()
97 {
98  delete server;
99 }
100 
101 KJavaAppletContext *KJavaServerMaintainer::getContext(QObject *w, const QString &doc)
102 {
103  QPair<QObject *, QString> key = qMakePair(w, doc);
104  ContextMap::iterator it = m_contextmap.find(key);
105  if (it != m_contextmap.end()) {
106  ++((*it).second);
107  return (*it).first;
108  }
109  KJavaAppletContext *const context = new KJavaAppletContext();
110  m_contextmap.insert(key, qMakePair(context, 1));
111  return context;
112 }
113 
114 void KJavaServerMaintainer::releaseContext(QObject *w, const QString &doc)
115 {
116  ContextMap::iterator it = m_contextmap.find(qMakePair(w, doc));
117  if (it != m_contextmap.end() && --(*it).second <= 0) {
118  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaServerMaintainer::releaseContext";
119  (*it).first->deleteLater();
120  m_contextmap.erase(it);
121  }
122 }
123 
124 inline void KJavaServerMaintainer::setServer(KJavaAppletServer *s)
125 {
126  if (!server) {
127  server = s;
128  }
129 }
130 
131 //-----------------------------------------------------------------------------
132 
133 AppletParameterDialog::AppletParameterDialog(KJavaAppletWidget *parent)
134  : QDialog(parent), m_appletWidget(parent)
135 {
136  setObjectName("paramdialog");
137  setWindowTitle(i18n("Applet Parameters"));
138  setModal(true);
139 
140  KJavaApplet *const applet = parent->applet();
141  table = new QTableWidget(30, 2, this);
142  table->setMinimumSize(QSize(600, 400));
143  table->setColumnWidth(0, 200);
144  table->setColumnWidth(1, 340);
145  QTableWidgetItem *const header1 = new QTableWidgetItem(i18n("Parameter"));
146  QTableWidgetItem *const header2 = new QTableWidgetItem(i18n("Value"));
147  table->setHorizontalHeaderItem(1, header1);
148  table->setHorizontalHeaderItem(2, header2);
149  QTableWidgetItem *tit = new QTableWidgetItem(i18n("Class"));
150  tit->setFlags(tit->flags()^Qt::ItemIsEditable);
151  table->setItem(0, 0, tit);
152  tit = new QTableWidgetItem(applet->appletClass());
153  tit->setFlags(tit->flags() | Qt::ItemIsEditable);
154  table->setItem(0, 1, tit);
155  tit = new QTableWidgetItem(i18n("Base URL"));
156  tit->setFlags(tit->flags()^Qt::ItemIsEditable);
157  table->setItem(1, 0, tit);
158  tit = new QTableWidgetItem(applet->baseURL());
159  tit->setFlags(tit->flags() | Qt::ItemIsEditable);
160  table->setItem(1, 1, tit);
161  tit = new QTableWidgetItem(i18n("Archives"));
162  tit->setFlags(tit->flags()^Qt::ItemIsEditable);
163  table->setItem(2, 0, tit);
164  tit = new QTableWidgetItem(applet->archives());
165  tit->setFlags(tit->flags() | Qt::ItemIsEditable);
166  table->setItem(2, 1, tit);
167  QMap<QString, QString>::const_iterator it = applet->getParams().constBegin();
168  const QMap<QString, QString>::const_iterator itEnd = applet->getParams().constEnd();
169  for (int count = 2; it != itEnd; ++it) {
170  tit = new QTableWidgetItem(it.key());
171  tit->setFlags(tit->flags() | Qt::ItemIsEditable);
172  table->setItem(++count, 0, tit);
173  tit = new QTableWidgetItem(it.value());
174  tit->setFlags(tit->flags() | Qt::ItemIsEditable);
175  table->setItem(count, 1, tit);
176  }
177 
178  QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
180  connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotClose()));
181  connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
182  connect(buttonBox, SIGNAL(rejected()), this, SLOT(slotClose()));
183  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
184 
186  layout->addWidget(table);
187  layout->addWidget(buttonBox);
188  setLayout(layout);
189 }
190 
191 void AppletParameterDialog::slotClose()
192 {
193  table->setRangeSelected(QTableWidgetSelectionRange(0, 0, 0, 0), true);
194  KJavaApplet *const applet = m_appletWidget->applet();
195  applet->setAppletClass(table->item(0, 1)->text());
196  applet->setBaseURL(table->item(1, 1)->text());
197  applet->setArchives(table->item(2, 1)->text());
198  const int lim = table->rowCount();
199  for (int i = 3; i < lim; ++i) {
200  if (table->item(i, 0) && table->item(i, 1) && !table->item(i, 0)->text().isEmpty())
201  applet->setParameter(table->item(i, 0)->text(),
202  table->item(i, 1)->text());
203  }
204  hide();
205 }
206 //-----------------------------------------------------------------------------
207 
208 class CoverWidget : public QWidget
209 {
210  KJavaAppletWidget *m_appletwidget;
211 public:
212  CoverWidget(QWidget *);
213  ~CoverWidget() {}
214  KJavaAppletWidget *appletWidget() const;
215 protected:
216  void resizeEvent(QResizeEvent *e) override;
217 };
218 
219 inline CoverWidget::CoverWidget(QWidget *parent)
220  : QWidget(parent)
221 {
222  setObjectName("KJavaAppletViewer Widget");
223  m_appletwidget = new KJavaAppletWidget(this);
224  setFocusProxy(m_appletwidget);
225 }
226 
227 inline KJavaAppletWidget *CoverWidget::appletWidget() const
228 {
229  return m_appletwidget;
230 }
231 
232 void CoverWidget::resizeEvent(QResizeEvent *e)
233 {
234  m_appletwidget->resize(e->size().width(), e->size().height());
235 }
236 
237 //-----------------------------------------------------------------------------
238 
239 class StatusBarIcon : public QLabel
240 {
241 public:
242  StatusBarIcon(QWidget *parent) : QLabel(parent)
243  {
244  setPixmap(KJavaAppletViewerFactory::iconLoader()->loadIcon("java", KIconLoader::Small));
245  }
246 protected:
247  void mousePressEvent(QMouseEvent *) override
248  {
249  serverMaintainer()->server->showConsole();
250  }
251 };
252 
253 //-----------------------------------------------------------------------------
254 
255 KJavaAppletViewer::KJavaAppletViewer(QWidget *wparent,
256  QObject *parent, const QStringList &args)
257  : KParts::ReadOnlyPart(parent),
258  m_browserextension(new KJavaAppletViewerBrowserExtension(this)),
259  m_liveconnect(new KJavaAppletViewerLiveConnectExtension(this)),
260  m_statusbar(new KParts::StatusBarExtension(this)),
261  m_statusbar_icon(nullptr),
262  m_closed(true)
263 {
264  m_view = new CoverWidget(wparent);
265  QString classname, classid, codebase, khtml_codebase, src_param;
266  QString appletname;
267  int width = -1;
268  int height = -1;
269  KJavaApplet *const applet = m_view->appletWidget()->applet();
271  const QStringList::const_iterator itEnd = args.end();
272  for (; it != itEnd; ++it) {
273  const int equalPos = (*it).indexOf("=");
274  if (equalPos > 0) {
275  const QString name = (*it).left(equalPos).toUpper();
276  QString value = (*it).right((*it).length() - equalPos - 1);
277  if (value.at(0) == '\"') {
278  value = value.right(value.length() - 1);
279  }
280  if (value.at(value.length() - 1) == '\"') {
281  value.truncate(value.length() - 1);
282  }
283  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "name=" << name << " value=" << value;
284  if (!name.isEmpty()) {
285  const QString name_lower = name.toLower();
286  if (name == "__KHTML__PLUGINBASEURL" ||
287  name == "BASEURL") {
288  baseurl = QUrl(QUrl(value).resolved(QUrl("."))).toString();
289  } else if (name == "__KHTML__CODEBASE") {
290  khtml_codebase = value;
291  } else if (name_lower == QLatin1String("codebase") ||
292  name_lower == QLatin1String("java_codebase")) {
293  if (!value.isEmpty()) {
294  codebase = value;
295  }
296  } else if (name == "__KHTML__CLASSID")
297  //else if (name.toLower()==QLatin1String("classid"))
298  {
299  classid = value;
300  } else if (name_lower == QLatin1String("code") ||
301  name_lower == QLatin1String("java_code")) {
302  classname = value;
303  } else if (name_lower == QLatin1String("src")) {
304  src_param = value;
305  } else if (name_lower == QLatin1String("archive") ||
306  name_lower == QLatin1String("java_archive") ||
307  name_lower.startsWith(QLatin1String("cache_archive"))) {
308  applet->setArchives(value);
309  } else if (name_lower == QLatin1String("name")) {
310  appletname = value;
311  } else if (name_lower == QLatin1String("width")) {
312  width = value.toInt();
313  } else if (name_lower == QLatin1String("height")) {
314  height = value.toInt();
315  }
316  if (!name.startsWith(QLatin1String("__KHTML__"))) {
317  applet->setParameter(name, value);
318  }
319  }
320  }
321  }
322  if (!classid.isEmpty()) {
323  applet->setParameter("CLSID", classid);
324  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "classid=" << classid << classid.startsWith("clsid:");
325  if (classid.startsWith(QLatin1String("clsid:")))
326  // codeBase contains the URL to the plugin page
327  {
328  khtml_codebase = baseurl;
329  } else if (classname.isEmpty() && classid.startsWith(QLatin1String("java:"))) {
330  classname = classid.mid(5);
331  }
332  }
333  if (classname.isEmpty()) {
334  classname = src_param;
335  } else if (!src_param.isEmpty()) {
336  applet->setParameter(QString("SRC"), src_param);
337  }
338  if (codebase.isEmpty()) {
339  codebase = khtml_codebase;
340  }
341  if (baseurl.isEmpty()) {
342  // not embedded in khtml
343  QString pwd = QDir().absolutePath();
344  if (!pwd.endsWith(QString(QDir::separator()))) {
345  pwd += QDir::separator();
346  }
347  baseurl = QUrl(QUrl(pwd).resolved(QUrl(codebase))).toString();
348  }
349  if (width > 0 && height > 0) {
350  m_view->resize(width, height);
351  applet->setSize(QSize(width, height));
352  }
353  if (appletname.isEmpty()) {
354  appletname = classname;
355  }
356  applet->setAppletName(appletname);
357  applet->setBaseURL(baseurl);
358  // check codebase first
359  const QUrl kbaseURL(baseurl);
360  const QUrl newURL(kbaseURL.resolved(QUrl(codebase)));
361  if (KUrlAuthorized::authorizeUrlAction("redirect", QUrl(baseurl), newURL)) {
362  applet->setCodeBase(newURL.toString());
363  }
364  applet->setAppletClass(classname);
365  KJavaAppletContext *const cxt = serverMaintainer()->getContext(parent, baseurl);
366  applet->setAppletContext(cxt);
367 
368  KJavaAppletServer *const server = cxt->getServer();
369 
370  serverMaintainer()->setServer(server);
371 
372  if (!server->usingKIO()) {
373  /* if this page needs authentication */
374  KIO::AuthInfo info;
375  info.url = QUrl(baseurl);
376  info.verifyPath = true;
377  QByteArray params;
378  {
379  QDataStream stream(&params, QIODevice::WriteOnly);
380  stream << info;
381  }
382 
383  // make the call
384  QDBusInterface kpasswdserver("org.kde.kpasswdserver", "/modules/kpasswdserver", "org.kde.KPasswdServer");
385  QDBusReply<QByteArray> reply = kpasswdserver.call("checkAuthInfo", params, qlonglong(m_view->topLevelWidget()->winId()), qlonglong(KUserTimestamp::userTimestamp()));
386 
387  if (!reply.isValid()) {
388  qCWarning(KJAVAAPPLETVIEWER_LOG) << "checkAuthInfo DBUS call failed: " << reply.error().message();
389  } else {
390  KIO::AuthInfo authResult;
391  QDataStream stream2(reply.value());
392  stream2 >> authResult;
393  applet->setUser(authResult.username);
394  applet->setPassword(authResult.password);
395  applet->setAuthName(authResult.realmValue);
396  }
397  }
398 
399  /* install event filter for close events */
400  if (wparent) {
401  wparent->topLevelWidget()->installEventFilter(this);
402  }
403 
404  setComponentData(KJavaAppletViewerFactory::componentData());
405  KParts::Part::setWidget(m_view);
406 
407  connect(applet->getContext(), SIGNAL(appletLoaded()), this, SLOT(appletLoaded()));
408  connect(applet->getContext(), SIGNAL(showDocument(QString,QString)), m_browserextension, SLOT(showDocument(QString,QString)));
409  connect(applet->getContext(), SIGNAL(showStatus(QString)), this, SLOT(infoMessage(QString)));
410  connect(applet, SIGNAL(jsEvent(QStringList)), m_liveconnect, SLOT(jsEvent(QStringList)));
411 }
412 
413 CoverWidget *KJavaAppletViewer::view() const
414 {
415  return m_view;
416 }
417 
418 bool KJavaAppletViewer::eventFilter(QObject *o, QEvent *e)
419 {
420  if (m_liveconnect->jsSessions() > 0) {
421  switch (e->type()) {
422  case QEvent::Destroy:
423  case QEvent::Close:
424  case QEvent::Quit:
425  return true;
426  default:
427  break;
428  }
429  }
431 }
432 
433 KJavaAppletViewer::~KJavaAppletViewer()
434 {
435  m_view = nullptr;
436  serverMaintainer()->releaseContext(parent(), baseurl);
437  if (m_statusbar_icon) {
438  m_statusbar->removeStatusBarItem(m_statusbar_icon);
439  delete m_statusbar_icon;
440  }
441 }
442 
443 bool KJavaAppletViewer::openUrl(const QUrl &url)
444 {
445  if (!m_view) {
446  return false;
447  }
448  m_closed = false;
449  KJavaAppletWidget *const w = m_view->appletWidget();
450  KJavaApplet *const applet = w->applet();
451  if (applet->isCreated()) {
452  applet->stop();
453  }
454  if (applet->appletClass().isEmpty()) {
455  // preview without setting a class?
456  if (applet->baseURL().isEmpty()) {
457  QUrl urlInfo(url);
458  applet->setAppletClass(urlInfo.fileName());
459  applet->setBaseURL(KIO::upUrl(urlInfo).toString());
460  } else {
461  applet->setAppletClass(url.toString());
462  }
463  AppletParameterDialog(w).exec();
464  applet->setSize(w->sizeHint());
465  }
466  if (!m_statusbar_icon) {
467  QStatusBar *sb = m_statusbar->statusBar();
468  if (sb) {
469  m_statusbar_icon = new StatusBarIcon(sb);
470  m_statusbar->addStatusBarItem(m_statusbar_icon, 0, false);
471  }
472  }
473  // delay showApplet if size is unknown and m_view not shown
474  if (applet->size().width() > 0 || m_view->isVisible()) {
475  w->showApplet();
476  } else {
477  QTimer::singleShot(10, this, SLOT(delayedCreateTimeOut()));
478  }
479  if (!applet->failed()) {
480  emit started(nullptr);
481  }
482  return url.isValid();
483 }
484 
485 bool KJavaAppletViewer::closeUrl()
486 {
487  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "closeUrl";
488  m_closed = true;
489  KJavaApplet *const applet = m_view->appletWidget()->applet();
490  if (applet->isCreated()) {
491  applet->stop();
492  }
493  applet->getContext()->getServer()->endWaitForReturnData();
494  return true;
495 }
496 
497 bool KJavaAppletViewer::appletAlive() const
498 {
499  return !m_closed && m_view &&
500  m_view->appletWidget()->applet() &&
501  m_view->appletWidget()->applet()->isAlive();
502 }
503 
504 bool KJavaAppletViewer::openFile()
505 {
506  return false;
507 }
508 
509 void KJavaAppletViewer::delayedCreateTimeOut()
510 {
511  KJavaAppletWidget *const w = m_view->appletWidget();
512  if (!w->applet()->isCreated() && !m_closed) {
513  w->showApplet();
514  }
515 }
516 
517 void KJavaAppletViewer::appletLoaded()
518 {
519  if (!m_view) {
520  return;
521  }
522  KJavaApplet *const applet = m_view->appletWidget()->applet();
523  if (applet->isAlive() || applet->failed()) {
524  emit completed();
525  }
526 }
527 
528 void KJavaAppletViewer::infoMessage(const QString &msg)
529 {
530  m_browserextension->infoMessage(msg);
531 }
532 
533 //---------------------------------------------------------------------
534 
535 KJavaAppletViewerBrowserExtension::KJavaAppletViewerBrowserExtension(KJavaAppletViewer *parent)
536  : KParts::BrowserExtension(parent)
537 {
538  setObjectName("KJavaAppletViewer Browser Extension");
539 }
540 
541 void KJavaAppletViewerBrowserExtension::urlChanged(const QString &url)
542 {
543  emit setLocationBarUrl(url);
544 }
545 
546 void KJavaAppletViewerBrowserExtension::setLoadingProgress(int percentage)
547 {
548  emit loadingProgress(percentage);
549 }
550 
551 void KJavaAppletViewerBrowserExtension::setBrowserArguments(const KParts::BrowserArguments & /*args*/)
552 {
553 }
554 
555 void KJavaAppletViewerBrowserExtension::saveState(QDataStream &stream)
556 {
557  KJavaApplet *const applet = static_cast<KJavaAppletViewer *>(parent())->view()->appletWidget()->applet();
558  stream << applet->appletClass();
559  stream << applet->baseURL();
560  stream << applet->archives();
561  stream << applet->getParams().size();
562  QMap<QString, QString>::const_iterator it = applet->getParams().constBegin();
563  const QMap<QString, QString>::const_iterator itEnd = applet->getParams().constEnd();
564  for (; it != itEnd; ++it) {
565  stream << it.key();
566  stream << it.value();
567  }
568 }
569 
570 void KJavaAppletViewerBrowserExtension::restoreState(QDataStream &stream)
571 {
572  KJavaAppletWidget *const w = static_cast<KJavaAppletViewer *>(parent())->view()->appletWidget();
573  KJavaApplet *const applet = w->applet();
574  QString key, val;
575  int paramcount;
576  stream >> val;
577  applet->setAppletClass(val);
578  stream >> val;
579  applet->setBaseURL(val);
580  stream >> val;
581  applet->setArchives(val);
582  stream >> paramcount;
583  for (int i = 0; i < paramcount; ++i) {
584  stream >> key;
585  stream >> val;
586  applet->setParameter(key, val);
587  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "restoreState key:" << key << " val:" << val;
588  }
589  applet->setSize(w->sizeHint());
590  if (w->isVisible()) {
591  w->showApplet();
592  }
593 }
594 
595 void KJavaAppletViewerBrowserExtension::showDocument(const QString &doc,
596  const QString &frame)
597 {
598  const QUrl url(doc);
599  KParts::BrowserArguments browserArgs;
600  browserArgs.frameName = frame;
601  emit openUrlRequest(url, KParts::OpenUrlArguments(), browserArgs);
602 }
603 
604 //-----------------------------------------------------------------------------
605 
606 KJavaAppletViewerLiveConnectExtension::KJavaAppletViewerLiveConnectExtension(KJavaAppletViewer *parent)
607  : KParts::LiveConnectExtension(parent), m_viewer(parent)
608 {
609  setObjectName("KJavaAppletViewer LiveConnect Extension");
610 }
611 
613  const unsigned long objid, const QString &name,
614  KParts::LiveConnectExtension::Type &type,
615  unsigned long &rid, QString &value)
616 {
617  if (!m_viewer->appletAlive()) {
618  return false;
619  }
620  QStringList args, ret_args;
621  KJavaApplet *const applet = m_viewer->view()->appletWidget()->applet();
622  args.append(QString::number(applet->appletId()));
623  args.append(QString::number((int) objid));
624  args.append(name);
625  m_jssessions++;
626  const bool ret = applet->getContext()->getMember(args, ret_args);
627  m_jssessions--;
628  if (!ret || ret_args.count() != 3) {
629  return false;
630  }
631  bool ok;
632  int itype = ret_args[0].toInt(&ok);
633  if (!ok || itype < 0) {
634  return false;
635  }
636  type = (KParts::LiveConnectExtension::Type) itype;
637  rid = ret_args[1].toInt(&ok);
638  if (!ok) {
639  return false;
640  }
641  value = ret_args[2];
642  return true;
643 }
644 
645 bool KJavaAppletViewerLiveConnectExtension::put(const unsigned long objid, const QString &name, const QString &value)
646 {
647  if (!m_viewer->appletAlive()) {
648  return false;
649  }
650  QStringList args;
651  KJavaApplet *const applet = m_viewer->view()->appletWidget()->applet();
652  args.append(QString::number(applet->appletId()));
653  args.append(QString::number((int) objid));
654  args.append(name);
655  args.append(value);
656  ++m_jssessions;
657  const bool ret = applet->getContext()->putMember(args);
658  --m_jssessions;
659  return ret;
660 }
661 
662 bool KJavaAppletViewerLiveConnectExtension::call(const unsigned long objid, const QString &func, const QStringList &fargs, KParts::LiveConnectExtension::Type &type, unsigned long &retobjid, QString &value)
663 {
664  if (!m_viewer->appletAlive()) {
665  return false;
666  }
667  KJavaApplet *const applet = m_viewer->view()->appletWidget()->applet();
668  QStringList args, ret_args;
669  args.append(QString::number(applet->appletId()));
670  args.append(QString::number((int) objid));
671  args.append(func);
672  {
673  QStringList::const_iterator it = fargs.begin();
674  const QStringList::const_iterator itEnd = fargs.end();
675  for (; it != itEnd; ++it) {
676  args.append(*it);
677  }
678  }
679 
680  ++m_jssessions;
681  const bool ret = applet->getContext()->callMember(args, ret_args);
682  --m_jssessions;
683  if (!ret || ret_args.count() != 3) {
684  return false;
685  }
686  bool ok;
687  const int itype = ret_args[0].toInt(&ok);
688  if (!ok || itype < 0) {
689  return false;
690  }
691  type = (KParts::LiveConnectExtension::Type) itype;
692  retobjid = ret_args[1].toInt(&ok);
693  if (!ok) {
694  return false;
695  }
696  value = ret_args[2];
697  return true;
698 }
699 
700 void KJavaAppletViewerLiveConnectExtension::unregister(const unsigned long objid)
701 {
702  if (!m_viewer->view() || !m_viewer->view()) {
703  return;
704  }
705  KJavaApplet *const applet = m_viewer->view()->appletWidget()->applet();
706  if (!applet || objid == 0) {
707  // typically a gc after a function call on the applet,
708  // no need to send to the jvm
709  return;
710  }
711  QStringList args;
712  args.append(QString::number(applet->appletId()));
713  args.append(QString::number((int) objid));
714  applet->getContext()->derefObject(args);
715 }
716 
717 void KJavaAppletViewerLiveConnectExtension::jsEvent(const QStringList &args)
718 {
719  if (args.count() < 2 || !m_viewer->appletAlive()) {
720  return;
721  }
722  bool ok;
723  QStringList::ConstIterator it = args.begin();
724  const QStringList::ConstIterator itEnd = args.end();
725  const unsigned long objid = (*it).toInt(&ok);
726  ++it;
727  const QString event = (*it);
728  ++it;
729  KParts::LiveConnectExtension::ArgList arglist;
730 
731  for (; it != itEnd; ++it) {
732  // take a deep breath here
733  const QStringList::ConstIterator prev = it++;
734  arglist.push_back(KParts::LiveConnectExtension::ArgList::value_type((KParts::LiveConnectExtension::Type)(*prev).toInt(), (*it)));
735  }
736  emit partEvent(objid, event, arglist);
737 }
738 
739 int KJavaAppletViewerLiveConnectExtension::m_jssessions = 0;
740 
741 //-----------------------------------------------------------------------------
742 
QEvent::Type type() const const
QString toUpper() const const
void truncate(int position)
int width() const const
QString name(const QVariant &location)
T * create(QObject *parent=nullptr, const QVariantList &args=QVariantList())
QMap::const_iterator constBegin() const const
QString message() const const
virtual bool call(const unsigned long objid, const QString &func, const QStringList &args, Type &type, unsigned long &retobjid, QString &value)
bool isValid() const const
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
QString toString(QUrl::FormattingOptions options) const const
QChar separator()
int width() const const
void layout()
ensure the display is up to date
Definition: khtmlview.cpp:981
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString number(int n, int base)
void resize(int size)
int count(const T &value) const const
void append(const T &value)
void setLayout(QLayout *layout)
void installEventFilter(QObject *filterObj)
QWidget * topLevelWidget() const const
QDBusReply::Type value() const const
int toInt(bool *ok, int base) const const
void setObjectName(const QString &name)
void setFocusProxy(QWidget *w)
bool isEmpty() const const
QMap::const_iterator constEnd() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
virtual bool eventFilter(QObject *watched, QEvent *event)
void hide()
QString right(int n) const const
QList::iterator end()
QString toLower() const const
const QSize & size() const const
virtual void setWidget(QWidget *widget)
const QDBusError & error()
const Key key(const T &value, const Key &defaultKey) const const
QString i18n(const char *text, const TYPE &arg...)
void setFlags(Qt::ItemFlags flags)
bool isValid() const const
QString mid(int position, int n) const const
char * toString(const T &value)
void setStandardButtons(QDialogButtonBox::StandardButtons buttons)
QString absolutePath() const const
const QChar at(int position) const const
void setWindowTitle(const QString &)
typedef ConstIterator
int height() const const
int length() const const
QString left(int n) const const
Qt::ItemFlags flags() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QList::iterator begin()
int height() const const
bool authorizeUrlAction(const QString &action, const QUrl &baseUrl, const QUrl &destUrl)
const T value(const Key &key, const T &defaultValue) const const
virtual bool get(const unsigned long objid, const QString &field, Type &type, unsigned long &retobjid, QString &value)
ItemIsEditable
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.