KHtml

kjavaapplet.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 "kjavaappletwidget.h"
23 #include "kjavaappletcontext.h"
24 
25 #include <klocalizedstring.h>
26 #include "kjavaappletviewer_debug.h"
27 #include <kparts/browserextension.h>
28 #include <kparts/liveconnectextension.h>
29 
30 class KJavaAppletPrivate
31 {
32 public:
33  bool reallyExists;
34  bool failed;
35  QString className;
37  QString baseURL;
38  QString codeBase;
39  QString archives;
40  QSize size;
41  QString windowName;
42  KJavaApplet::AppletState state;
43 
44  KJavaAppletWidget *UIwidget;
45 };
46 
47 KJavaApplet::KJavaApplet(KJavaAppletWidget *_parent,
48  KJavaAppletContext *_context)
49  : d(new KJavaAppletPrivate), params()
50 {
51 
52  d->UIwidget = _parent;
53  d->state = UNKNOWN;
54  d->failed = false;
55 
56  if (_context) {
57  setAppletContext(_context);
58  }
59 
60  d->reallyExists = false;
61 }
62 
63 KJavaApplet::~KJavaApplet()
64 {
65  if (d->reallyExists) {
66  context->destroy(this);
67  }
68 
69  delete d;
70 }
71 
72 bool KJavaApplet::isCreated()
73 {
74  return d->reallyExists;
75 }
76 
77 void KJavaApplet::setAppletContext(KJavaAppletContext *_context)
78 {
79  context = _context;
80  context->registerApplet(this);
81 }
82 
83 void KJavaApplet::setAppletClass(const QString &_className)
84 {
85  d->className = _className;
86 }
87 
88 QString &KJavaApplet::appletClass()
89 {
90  return d->className;
91 }
92 
93 QString &KJavaApplet::parameter(const QString &name)
94 {
95  return params[ name ];
96 }
97 
98 void KJavaApplet::setParameter(const QString &name, const QString &value)
99 {
100  params.insert(name, value);
101 }
102 
103 QMap<QString, QString> &KJavaApplet::getParams()
104 {
105  return params;
106 }
107 
108 void KJavaApplet::setBaseURL(const QString &baseURL)
109 {
110  d->baseURL = baseURL;
111 }
112 
113 QString &KJavaApplet::baseURL()
114 {
115  return d->baseURL;
116 }
117 
118 void KJavaApplet::setCodeBase(const QString &codeBase)
119 {
120  d->codeBase = codeBase;
121 }
122 
123 QString &KJavaApplet::codeBase()
124 {
125  return d->codeBase;
126 }
127 
128 void KJavaApplet::setSize(QSize size)
129 {
130  d->size = size;
131 }
132 
133 QSize KJavaApplet::size()
134 {
135  return d->size;
136 }
137 
138 void KJavaApplet::setArchives(const QString &_archives)
139 {
140  d->archives = _archives;
141 }
142 
143 QString &KJavaApplet::archives()
144 {
145  return d->archives;
146 }
147 
148 void KJavaApplet::resizeAppletWidget(int width, int height)
149 {
150  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaApplet, id = " << id << ", ::resizeAppletWidget to " << width << ", " << height;
151 
152  QStringList sl;
153  sl.push_back(QString::number(0)); // applet itself has id 0
154  sl.push_back(QString("eval")); // evaluate next script
155  sl.push_back(QString::number(KParts::LiveConnectExtension::TypeString));
156  sl.push_back(QString("this.setAttribute('WIDTH',%1);this.setAttribute('HEIGHT',%2)").arg(width).arg(height));
157  jsData(sl);
158 }
159 
160 void KJavaApplet::setAppletName(const QString &name)
161 {
162  d->appName = name;
163 }
164 
165 void KJavaApplet::setWindowName(const QString &title)
166 {
167  d->windowName = title;
168 }
169 
170 QString &KJavaApplet::getWindowName()
171 {
172  return d->windowName;
173 }
174 
175 QString &KJavaApplet::appletName()
176 {
177  return d->appName;
178 }
179 
180 void KJavaApplet::create()
181 {
182  if (!context->create(this)) {
183  setFailed();
184  }
185  d->reallyExists = true;
186 }
187 
188 void KJavaApplet::init()
189 {
190  context->init(this);
191 }
192 
193 void KJavaApplet::start()
194 {
195  context->start(this);
196 }
197 
198 void KJavaApplet::stop()
199 {
200  context->stop(this);
201 }
202 
203 int KJavaApplet::appletId()
204 {
205  return id;
206 }
207 
208 void KJavaApplet::setAppletId(int _id)
209 {
210  id = _id;
211 }
212 
213 void KJavaApplet::stateChange(const int newStateInt)
214 {
215  AppletState newState = (AppletState)newStateInt;
216  bool ok = false;
217  if (d->failed) {
218  return;
219  }
220  switch (newState) {
221  case CLASS_LOADED:
222  ok = (d->state == UNKNOWN);
223  break;
224  case INSTANCIATED:
225  ok = (d->state == CLASS_LOADED);
226  if (ok) {
227  showStatus(i18n("Initializing Applet \"%1\"...", appletName()));
228  }
229  break;
230  case INITIALIZED:
231  ok = (d->state == INSTANCIATED);
232  if (ok) {
233  showStatus(i18n("Starting Applet \"%1\"...", appletName()));
234  start();
235  }
236  break;
237  case STARTED:
238  ok = (d->state == INITIALIZED || d->state == STOPPED);
239  if (ok) {
240  showStatus(i18n("Applet \"%1\" started", appletName()));
241  }
242  break;
243  case STOPPED:
244  ok = (d->state == INITIALIZED || d->state == STARTED);
245  if (ok) {
246  showStatus(i18n("Applet \"%1\" stopped", appletName()));
247  }
248  break;
249  case DESTROYED:
250  ok = true;
251  break;
252  default:
253  break;
254  }
255  if (ok) {
256  d->state = newState;
257  } else {
258  qCCritical(KJAVAAPPLETVIEWER_LOG) << "KJavaApplet::stateChange : don't want to switch from state "
259  << d->state << " to " << newState;
260  }
261 }
262 
263 void KJavaApplet::showStatus(const QString &msg)
264 {
265  QStringList args;
266  args << msg;
267  context->processCmd("showstatus", args);
268 }
269 
270 void KJavaApplet::setFailed()
271 {
272  d->failed = true;
273 }
274 
275 bool KJavaApplet::isAlive() const
276 {
277  return (
278  !d->failed
279  && d->state >= INSTANCIATED
280  && d->state < STOPPED
281  );
282 }
283 
284 KJavaApplet::AppletState KJavaApplet::state() const
285 {
286  return d->state;
287 }
288 
289 bool KJavaApplet::failed() const
290 {
291  return d->failed;
292 }
293 
void push_back(const T &value)
int width() const const
QSize size() const const
QString number(int n, int base)
QString & insert(int position, QChar ch)
QString i18n(const char *text, const TYPE &arg...)
int height() const const
QCA_EXPORT QString appName()
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.