KHtml

kjavadownloader.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 "kjavadownloader.h"
23 #include "kjavaappletserver.h"
24 
25 #include <kio/job.h>
26 #include <kio/jobclasses.h>
27 #include "kjavaappletviewer_debug.h"
28 #include <QFile>
29 #include <QUrl>
30 
31 static const int DATA = 0;
32 static const int FINISHED = 1;
33 static const int ERRORCODE = 2;
34 static const int HEADERS = 3;
35 static const int REDIRECT = 4;
36 static const int MIMETYPE = 5;
37 static const int CONNECTED = 6;
38 static const int REQUESTDATA = 7;
39 
40 static const int KJAS_STOP = 0;
41 static const int KJAS_HOLD = 1;
42 static const int KJAS_RESUME = 2;
43 
44 KJavaKIOJob::~KJavaKIOJob() {}
45 
46 void KJavaKIOJob::data(const QByteArray &)
47 {
48  qCCritical(KJAVAAPPLETVIEWER_LOG) << "Job id mixup";
49 }
50 
51 //-----------------------------------------------------------------------------
52 
53 class KJavaDownloaderPrivate
54 {
55  friend class KJavaDownloader;
56 public:
57  KJavaDownloaderPrivate() : responseCode(0), isfirstdata(true) {}
58  ~KJavaDownloaderPrivate()
59  {
60  delete url;
61  if (job) {
62  job->kill(); // KIO::Job::kill deletes itself
63  }
64  }
65 private:
66  int loaderID;
67  QUrl *url;
68  QByteArray file;
69  KIO::TransferJob *job;
70  int responseCode;
71  bool isfirstdata;
72 };
73 
74 KJavaDownloader::KJavaDownloader(int ID, const QString &url)
75  : d(new KJavaDownloaderPrivate)
76 {
77  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaDownloader(" << ID << ") = " << url;
78 
79  d->loaderID = ID;
80  d->url = new QUrl(url);
81 
82  d->job = KIO::get(*d->url, KIO::NoReload, KIO::HideProgressInfo);
83  d->job->addMetaData("PropagateHttpHeader", "true");
84  connect(d->job, SIGNAL(data(KIO::Job*,QByteArray)),
85  this, SLOT(slotData(KIO::Job*,QByteArray)));
86  connect(d->job, SIGNAL(connected(KIO::Job*)),
87  this, SLOT(slotConnected(KIO::Job*)));
88  connect(d->job, SIGNAL(mimetype(KIO::Job*,QString)),
89  this, SLOT(slotMimetype(KIO::Job*,QString)));
90  connect(d->job, SIGNAL(result(KJob*)),
91  this, SLOT(slotResult(KJob*)));
92 }
93 
94 KJavaDownloader::~KJavaDownloader()
95 {
96  delete d;
97 }
98 
99 void KJavaDownloader::slotData(KIO::Job *, const QByteArray &qb)
100 {
101  //qCDebug(KJAVAAPPLETVIEWER_LOG) << "slotData(" << d->loaderID << ")";
102 
103  KJavaAppletServer *server = KJavaAppletServer::allocateJavaServer();
104  if (d->isfirstdata) {
105  QString headers = d->job->queryMetaData("HTTP-Headers");
106  if (!headers.isEmpty()) {
107  d->file.resize(headers.length());
108  memcpy(d->file.data(), headers.toLatin1().constData(), headers.length());
109  server->sendURLData(d->loaderID, HEADERS, d->file);
110  d->file.resize(0);
111  }
112  d->isfirstdata = false;
113  }
114  if (qb.size()) {
115  server->sendURLData(d->loaderID, DATA, qb);
116  }
117  KJavaAppletServer::freeJavaServer();
118 }
119 
120 void KJavaDownloader::slotConnected(KIO::Job *)
121 {
122  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slave connected";
123  d->responseCode = d->job->error();
124 }
125 
126 void KJavaDownloader::slotMimetype(KIO::Job *, const QString &type)
127 {
128  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slave mimetype " << type;
129 }
130 
131 void KJavaDownloader::slotResult(KJob *)
132 {
133  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slotResult(" << d->loaderID << ")";
134 
135  KJavaAppletServer *server = KJavaAppletServer::allocateJavaServer();
136  if (d->job->error()) {
137  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slave had an error = " << d->job->errorString();
138  int code = d->job->error();
139  if (!code) {
140  code = 404;
141  }
142  QString codestr = QString::number(code);
143  d->file.resize(codestr.length());
144  memcpy(d->file.data(), codestr.toLatin1().constData(), codestr.length());
145  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slave had an error = " << code;
146 
147  server->sendURLData(d->loaderID, ERRORCODE, d->file);
148  d->file.resize(0);
149  } else {
150  server->sendURLData(d->loaderID, FINISHED, d->file);
151  }
152  d->job = nullptr; // signal KIO::Job::result deletes itself
153  server->removeDataJob(d->loaderID); // will delete this
154  KJavaAppletServer::freeJavaServer();
155 }
156 
157 void KJavaDownloader::jobCommand(int cmd)
158 {
159  if (!d->job) {
160  return;
161  }
162  switch (cmd) {
163  case KJAS_STOP: {
164  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "jobCommand(" << d->loaderID << ") stop";
165  d->job->kill();
166  d->job = nullptr; // KIO::Job::kill deletes itself
167  KJavaAppletServer *server = KJavaAppletServer::allocateJavaServer();
168  server->removeDataJob(d->loaderID); // will delete this
169  KJavaAppletServer::freeJavaServer();
170  break;
171  }
172  case KJAS_HOLD:
173  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "jobCommand(" << d->loaderID << ") hold";
174  d->job->suspend();
175  break;
176  case KJAS_RESUME:
177  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "jobCommand(" << d->loaderID << ") resume";
178  d->job->resume();
179  break;
180  }
181 }
182 
183 //-----------------------------------------------------------------------------
184 
185 class KJavaUploaderPrivate
186 {
187 public:
188  KJavaUploaderPrivate() {}
189  ~KJavaUploaderPrivate()
190  {
191  delete url;
192  if (job) {
193  job->kill(); // KIO::Job::kill deletes itself
194  }
195  }
196  int loaderID;
197  QUrl *url;
198  QByteArray file;
199  KIO::TransferJob *job;
200  bool finished;
201 };
202 
203 KJavaUploader::KJavaUploader(int ID, const QString &url)
204  : d(new KJavaUploaderPrivate)
205 {
206  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaUploader(" << ID << ") = " << url;
207 
208  d->loaderID = ID;
209  d->url = new QUrl(url);
210  d->job = nullptr;
211  d->finished = false;
212 }
213 
214 void KJavaUploader::start()
215 {
216  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaUploader::start(" << d->loaderID << ")";
217  KJavaAppletServer *server = KJavaAppletServer::allocateJavaServer();
218  // create a suspended job
219  d->job = KIO::put(*d->url, -1, KIO::HideProgressInfo);
220  d->job->suspend();
221  connect(d->job, SIGNAL(dataReq(KIO::Job*,QByteArray&)),
222  this, SLOT(slotDataRequest(KIO::Job*,QByteArray&)));
223  connect(d->job, SIGNAL(result(KJob*)),
224  this, SLOT(slotResult(KJob*)));
225  server->sendURLData(d->loaderID, CONNECTED, d->file);
226  KJavaAppletServer::freeJavaServer();
227 }
228 
229 KJavaUploader::~KJavaUploader()
230 {
231  delete d;
232 }
233 
234 void KJavaUploader::slotDataRequest(KIO::Job *, QByteArray &qb)
235 {
236  // send our data and suspend
237  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slotDataRequest(" << d->loaderID << ") finished:" << d->finished;
238  qb.resize(d->file.size());
239  KJavaAppletServer *server = KJavaAppletServer::allocateJavaServer();
240  if (d->file.size() == 0) {
241  d->job = nullptr; // eof, job deletes itself
242  server->removeDataJob(d->loaderID); // will delete this
243  } else {
244  memcpy(qb.data(), d->file.data(), d->file.size());
245  d->file.resize(0);
246  if (!d->finished) {
247  server->sendURLData(d->loaderID, REQUESTDATA, d->file);
248  d->job->suspend();
249  }
250  }
251  KJavaAppletServer::freeJavaServer();
252 }
253 
254 void KJavaUploader::data(const QByteArray &qb)
255 {
256  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "KJavaUploader::data(" << d->loaderID << ")";
257  d->file.resize(qb.size());
258  memcpy(d->file.data(), qb.data(), qb.size());
259  d->job->resume();
260 }
261 
262 void KJavaUploader::slotResult(KJob *)
263 {
264  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slotResult(" << d->loaderID << ") job:" << d->job;
265 
266  if (!d->job) {
267  return;
268  }
269  KJavaAppletServer *server = KJavaAppletServer::allocateJavaServer();
270  if (d->job->error()) {
271  int code = d->job->error();
272  QString codestr = QString::number(code);
273  d->file.resize(codestr.length());
274  memcpy(d->file.data(), codestr.toLatin1().constData(), codestr.length());
275  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "slave had an error " << code << ": " << d->job->errorString();
276 
277  server->sendURLData(d->loaderID, ERRORCODE, d->file);
278  d->file.resize(0);
279  } else { // shouldn't come here
280  qCCritical(KJAVAAPPLETVIEWER_LOG) << "slotResult(" << d->loaderID << ") job:" << d->job;
281  }
282  d->job = nullptr; // signal KIO::Job::result deletes itself
283  server->removeDataJob(d->loaderID); // will delete this
284  KJavaAppletServer::freeJavaServer();
285 }
286 
287 void KJavaUploader::jobCommand(int cmd)
288 {
289  if (!d->job) {
290  return;
291  }
292  switch (cmd) {
293  case KJAS_STOP: {
294  // qCDebug(KJAVAAPPLETVIEWER_LOG) << "jobCommand(" << d->loaderID << ") stop";
295  d->finished = true;
296  if (d->job->isSuspended()) {
297  d->job->resume();
298  }
299  break;
300  }
301  }
302 }
303 
void resize(int size)
QString number(int n, int base)
bool isEmpty() const const
const char * constData() const const
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT TransferJob * put(const QUrl &url, int permissions, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
QByteArray toLatin1() const const
int length() const const
char * data()
int size() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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.