don't discard iframe children.
[kdelibs.git] / khtml / java / kjavadownloader.cpp
blobeb5260e8feaf7388dc0f5568d18653a26ec501be
1 /* This file is part of the KDE project
3 * Copyright (C) 2000 Richard Moore <rich@kde.org>
4 * 2000 Wynn Wilkes <wynnw@caldera.com>
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.
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.
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.
22 #include "kjavadownloader.h"
23 #include "kjavaappletserver.h"
25 #include <kurl.h>
26 #include <kio/job.h>
27 #include <kio/jobclasses.h>
28 #include <kdebug.h>
29 #include <QtCore/QFile>
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;
40 static const int KJAS_STOP = 0;
41 static const int KJAS_HOLD = 1;
42 static const int KJAS_RESUME = 2;
44 KJavaKIOJob::~KJavaKIOJob() {}
46 void KJavaKIOJob::data( const QByteArray& )
48 kError(6100) << "Job id mixup" << endl;
51 //-----------------------------------------------------------------------------
53 class KJavaDownloaderPrivate
55 friend class KJavaDownloader;
56 public:
57 KJavaDownloaderPrivate() : responseCode(0), isfirstdata(true) {}
58 ~KJavaDownloaderPrivate()
60 delete url;
61 if (job) job->kill(); // KIO::Job::kill deletes itself
63 private:
64 int loaderID;
65 KUrl* url;
66 QByteArray file;
67 KIO::TransferJob* job;
68 int responseCode;
69 bool isfirstdata;
73 KJavaDownloader::KJavaDownloader( int ID, const QString& url )
74 :d(new KJavaDownloaderPrivate)
76 kDebug(6100) << "KJavaDownloader(" << ID << ") = " << url;
79 d->loaderID = ID;
80 d->url = new KUrl( url );
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*, const QByteArray& )),
85 this, SLOT(slotData( KIO::Job*, const QByteArray& )) );
86 connect( d->job, SIGNAL(connected(KIO::Job*)),
87 this, SLOT(slotConnected(KIO::Job*)));
88 connect( d->job, SIGNAL(mimetype(KIO::Job*, const QString&)),
89 this, SLOT(slotMimetype(KIO::Job*, const QString&)));
90 connect( d->job, SIGNAL(result(KJob*)),
91 this, SLOT(slotResult(KJob*)) );
94 KJavaDownloader::~KJavaDownloader()
96 delete d;
99 void KJavaDownloader::slotData( KIO::Job*, const QByteArray& qb )
101 //kDebug(6100) << "slotData(" << d->loaderID << ")";
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.toAscii().constData(), headers.length() );
109 server->sendURLData( d->loaderID, HEADERS, d->file );
110 d->file.resize( 0 );
112 d->isfirstdata = false;
114 if ( qb.size() )
115 server->sendURLData( d->loaderID, DATA, qb );
116 KJavaAppletServer::freeJavaServer();
119 void KJavaDownloader::slotConnected(KIO::Job*)
121 kDebug(6100) << "slave connected";
122 d->responseCode = d->job->error();
125 void KJavaDownloader::slotMimetype(KIO::Job*, const QString & type) {
126 kDebug(6100) << "slave mimetype " << type;
129 void KJavaDownloader::slotResult( KJob* )
131 kDebug(6100) << "slotResult(" << d->loaderID << ")";
133 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
134 if( d->job->error())
136 kDebug(6100) << "slave had an error = " << d->job->errorString();
137 int code = d->job->error();
138 if (!code)
139 code = 404;
140 QString codestr = QString::number(code);
141 d->file.resize(codestr.length());
142 memcpy( d->file.data(), codestr.toAscii().constData(), codestr.length() );
143 kDebug(6100) << "slave had an error = " << code;
145 server->sendURLData( d->loaderID, ERRORCODE, d->file );
146 d->file.resize( 0 );
148 else
150 server->sendURLData( d->loaderID, FINISHED, d->file );
152 d->job = 0L; // signal KIO::Job::result deletes itself
153 server->removeDataJob( d->loaderID ); // will delete this
154 KJavaAppletServer::freeJavaServer();
157 void KJavaDownloader::jobCommand( int cmd )
159 if (!d->job) return;
160 switch (cmd) {
161 case KJAS_STOP: {
162 kDebug(6100) << "jobCommand(" << d->loaderID << ") stop";
163 d->job->kill();
164 d->job = 0L; // KIO::Job::kill deletes itself
165 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
166 server->removeDataJob( d->loaderID ); // will delete this
167 KJavaAppletServer::freeJavaServer();
168 break;
170 case KJAS_HOLD:
171 kDebug(6100) << "jobCommand(" << d->loaderID << ") hold";
172 d->job->suspend();
173 break;
174 case KJAS_RESUME:
175 kDebug(6100) << "jobCommand(" << d->loaderID << ") resume";
176 d->job->resume();
177 break;
181 //-----------------------------------------------------------------------------
183 class KJavaUploaderPrivate
185 public:
186 KJavaUploaderPrivate() {}
187 ~KJavaUploaderPrivate()
189 delete url;
190 if (job) job->kill(); // KIO::Job::kill deletes itself
192 int loaderID;
193 KUrl* url;
194 QByteArray file;
195 KIO::TransferJob* job;
196 bool finished;
199 KJavaUploader::KJavaUploader( int ID, const QString& url )
200 :d(new KJavaUploaderPrivate)
202 kDebug(6100) << "KJavaUploader(" << ID << ") = " << url;
205 d->loaderID = ID;
206 d->url = new KUrl( url );
207 d->job = 0L;
208 d->finished = false;
211 void KJavaUploader::start()
213 kDebug(6100) << "KJavaUploader::start(" << d->loaderID << ")";
214 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
215 // create a suspended job
216 d->job = KIO::put( *d->url, -1, KIO::HideProgressInfo );
217 d->job->suspend();
218 connect( d->job, SIGNAL(dataReq( KIO::Job*, QByteArray& )),
219 this, SLOT(slotDataRequest( KIO::Job*, QByteArray& )) );
220 connect( d->job, SIGNAL(result(KJob*)),
221 this, SLOT(slotResult(KJob*)) );
222 server->sendURLData( d->loaderID, CONNECTED, d->file );
223 KJavaAppletServer::freeJavaServer();
226 KJavaUploader::~KJavaUploader()
228 delete d;
231 void KJavaUploader::slotDataRequest( KIO::Job*, QByteArray& qb )
233 // send our data and suspend
234 kDebug(6100) << "slotDataRequest(" << d->loaderID << ") finished:" << d->finished;
235 qb.resize( d->file.size() );
236 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
237 if (d->file.size() == 0) {
238 d->job = 0L; // eof, job deletes itself
239 server->removeDataJob( d->loaderID ); // will delete this
240 } else {
241 memcpy( qb.data(), d->file.data(), d->file.size() );
242 d->file.resize( 0 );
243 if (!d->finished) {
244 server->sendURLData( d->loaderID, REQUESTDATA, d->file );
245 d->job->suspend();
248 KJavaAppletServer::freeJavaServer();
251 void KJavaUploader::data( const QByteArray& qb )
253 kDebug(6100) << "KJavaUploader::data(" << d->loaderID << ")";
254 d->file.resize( qb.size() );
255 memcpy( d->file.data(), qb.data(), qb.size() );
256 d->job->resume();
259 void KJavaUploader::slotResult( KJob* )
261 kDebug(6100) << "slotResult(" << d->loaderID << ") job:" << d->job;
263 if (!d->job)
264 return;
265 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
266 if (d->job->error())
268 int code = d->job->error();
269 QString codestr = QString::number(code);
270 d->file.resize(codestr.length());
271 memcpy( d->file.data(), codestr.toAscii().constData(), codestr.length() );
272 kDebug(6100) << "slave had an error " << code << ": " << d->job->errorString();
274 server->sendURLData( d->loaderID, ERRORCODE, d->file );
275 d->file.resize( 0 );
277 else // shouldn't come here
278 kError(6100) << "slotResult(" << d->loaderID << ") job:" << d->job << endl;
279 d->job = 0L; // signal KIO::Job::result deletes itself
280 server->removeDataJob( d->loaderID ); // will delete this
281 KJavaAppletServer::freeJavaServer();
284 void KJavaUploader::jobCommand( int cmd )
286 if (!d->job) return;
287 switch (cmd) {
288 case KJAS_STOP: {
289 kDebug(6100) << "jobCommand(" << d->loaderID << ") stop";
290 d->finished = true;
291 if (d->job->isSuspended())
292 d->job->resume();
293 break;
298 #include "kjavadownloader.moc"