KHtml

kjavaappletcontext.cpp
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2000 Richard Moore <[email protected]>
4  * 2000 Wynn Wilkes <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "kjavaappletcontext.h"
23 #include "kjavaappletserver.h"
24 #include "kjavaprocess.h"
25 #include "kjavaapplet.h"
26 #include <klocalizedstring.h>
27 #include <kmessagebox.h>
28 #include "kjavaappletviewer_debug.h"
29 #include <QMap>
30 #include <QPointer>
31 #include <QStringList>
32 #include <QRegExp>
33 
34 // This file was using 6002, but kdebug.areas didn't know about that number
35 #define DEBUGAREA 6100
36 
38 
39 // For future expansion
40 class KJavaAppletContextPrivate
41 {
42  friend class KJavaAppletContext;
43 private:
44  AppletMap applets;
45 };
46 
47 // Static Factory Functions
48 int KJavaAppletContext::contextCount = 0;
49 
50 /* Class Implementation
51  */
52 KJavaAppletContext::KJavaAppletContext()
53  : QObject(),
54  d(new KJavaAppletContextPrivate)
55 {
56  server = KJavaAppletServer::allocateJavaServer();
57  connect(server->javaProcess(), SIGNAL(exited(int)), this, SLOT(javaProcessExited(int)));
58 
59  id = contextCount;
60  server->createContext(id, this);
61 
62  ++contextCount;
63 }
64 
65 KJavaAppletContext::~KJavaAppletContext()
66 {
67  server->destroyContext(id);
68  KJavaAppletServer::freeJavaServer();
69  delete d;
70 }
71 
72 int KJavaAppletContext::contextId()
73 {
74  return id;
75 }
76 
77 void KJavaAppletContext::setContextId(int _id)
78 {
79  id = _id;
80 }
81 
82 void KJavaAppletContext::registerApplet(KJavaApplet *applet)
83 {
84  static int appletId = 0;
85 
86  applet->setAppletId(++appletId);
87  d->applets.insert(appletId, applet);
88 }
89 
90 bool KJavaAppletContext::create(KJavaApplet *applet)
91 {
92  return server->createApplet(id, applet->appletId(),
93  applet->appletName(),
94  applet->appletClass(),
95  applet->baseURL(),
96  applet->user(),
97  applet->password(),
98  applet->authName(),
99  applet->codeBase(),
100  applet->archives(),
101  applet->size(),
102  applet->getParams(),
103  applet->getWindowName());
104 
105 }
106 
107 void KJavaAppletContext::destroy(KJavaApplet *applet)
108 {
109  const int appletId = applet->appletId();
110  d->applets.remove(appletId);
111 
112  server->destroyApplet(id, appletId);
113 }
114 
115 void KJavaAppletContext::init(KJavaApplet *applet)
116 {
117  server->initApplet(id, applet->appletId());
118 }
119 
120 void KJavaAppletContext::start(KJavaApplet *applet)
121 {
122  server->startApplet(id, applet->appletId());
123 }
124 
125 void KJavaAppletContext::stop(KJavaApplet *applet)
126 {
127  server->stopApplet(id, applet->appletId());
128 }
129 
130 void KJavaAppletContext::processCmd(QString cmd, QStringList args)
131 {
132  received(cmd, args);
133 }
134 
135 void KJavaAppletContext::received(const QString &cmd, const QStringList &arg)
136 {
137  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaAppletContext::received, cmd = >>" << cmd << "<<";
138  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "arg count = " << arg.count();
139 
140  if (cmd == QLatin1String("showstatus")
141  && !arg.empty()) {
142  QString tmp = arg.first();
143  tmp.remove(QRegExp("[\n\r]"));
144  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "status message = " << tmp;
145  emit showStatus(tmp);
146  } else if (cmd == QLatin1String("showurlinframe")
147  && arg.count() > 1) {
148  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "url = " << arg[0] << ", frame = " << arg[1];
149  emit showDocument(arg[0], arg[1]);
150  } else if (cmd == QLatin1String("showdocument")
151  && !arg.empty()) {
152  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "url = " << arg.first();
153  emit showDocument(arg.first(), "_top");
154  } else if (cmd == QLatin1String("resizeapplet")
155  && arg.count() > 2) {
156  //arg[1] should be appletID
157  //arg[2] should be new width
158  //arg[3] should be new height
159  bool ok;
160  const int appletID = arg[0].toInt(&ok);
161  const int width = arg[1].toInt(&ok);
162  const int height = arg[2].toInt(&ok);
163 
164  if (!ok) {
165  qCCritical(KJAVAAPPLETVIEWER_LOG) << "could not parse out parameters for resize";
166  } else {
167  KJavaApplet *const tmp = d->applets[appletID];
168  if (tmp) {
169  tmp->resizeAppletWidget(width, height);
170  }
171  }
172  } else if (cmd.startsWith(QLatin1String("audioclip_"))) {
173  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "process Audio command (not yet implemented): " << cmd << " " << arg[0];
174  } else if (cmd == QLatin1String("JS_Event")
175  && arg.count() > 2) {
176  bool ok;
177  const int appletID = arg.first().toInt(&ok);
178  KJavaApplet *applet;
179  if (ok && (applet = d->applets[appletID])) {
180  QStringList js_args(arg);
181  js_args.pop_front();
182  applet->jsData(js_args);
183  } else {
184  qCCritical(KJAVAAPPLETVIEWER_LOG) << "parse JS event " << arg[0] << " " << arg[1];
185  }
186  } else if (cmd == QLatin1String("AppletStateNotification")) {
187  bool ok;
188  const int appletID = arg.first().toInt(&ok);
189  if (ok) {
190  KJavaApplet *const applet = d->applets[appletID];
191  if (applet) {
192  const int newState = arg[1].toInt(&ok);
193  if (ok) {
194  applet->stateChange(newState);
195  if (newState == KJavaApplet::INITIALIZED) {
196  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "emit appletLoaded";
197  emit appletLoaded();
198  }
199  } else {
200  qCCritical(KJAVAAPPLETVIEWER_LOG) << "AppletStateNotification: status is not numerical";
201  }
202  } else {
203  qCWarning(KJAVAAPPLETVIEWER_LOG) << "AppletStateNotification: No such Applet with ID=" << arg[0];
204  }
205  } else {
206  qCCritical(KJAVAAPPLETVIEWER_LOG) << "AppletStateNotification: Applet ID is not numerical";
207  }
208  } else if (cmd == QLatin1String("AppletFailed")) {
209  bool ok;
210  const int appletID = arg.first().toInt(&ok);
211  if (ok) {
212  KJavaApplet *const applet = d->applets[appletID];
213  /*
214  QString errorDetail(arg[1]);
215  errorDetail.replace(QRegExp(":\\s*"), ":\n");
216  KMessageBox::detailedError(0L, i18n("Java error while loading applet."), errorDetail);
217  */
218  if (applet) {
219  applet->setFailed();
220  }
221  emit appletLoaded();
222  }
223  }
224 }
225 
226 void KJavaAppletContext::javaProcessExited(int)
227 {
228  AppletMap::iterator it = d->applets.begin();
229  const AppletMap::iterator itEnd = d->applets.end();
230  for (; it != itEnd; ++it)
231  if (!(*it).isNull() && (*it)->isCreated() && !(*it)->failed()) {
232  (*it)->setFailed();
233  if ((*it)->state() < KJavaApplet::INITIALIZED) {
234  emit appletLoaded();
235  }
236  }
237 }
238 
239 bool KJavaAppletContext::getMember(QStringList &args, QStringList &ret_args)
240 {
241  args.push_front(QString::number(id));
242  return server->getMember(args, ret_args);
243 }
244 
245 bool KJavaAppletContext::putMember(QStringList &args)
246 {
247  args.push_front(QString::number(id));
248  return server->putMember(args);
249 }
250 
251 bool KJavaAppletContext::callMember(QStringList &args, QStringList &ret_args)
252 {
253  args.push_front(QString::number(id));
254  return server->callMember(args, ret_args);
255 }
256 
257 void KJavaAppletContext::derefObject(QStringList &args)
258 {
259  args.push_front(QString::number(id));
260  server->derefObject(args);
261 }
262 
void push_front(const T &value)
QString & remove(int position, int n)
int width() const const
QString number(int n, int base)
int count(const T &value) const const
bool empty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
T & first()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int height() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.