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"
27 #include <kio/jobclasses.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
;
57 KJavaDownloaderPrivate() : responseCode(0), isfirstdata(true) {}
58 ~KJavaDownloaderPrivate()
61 if (job
) job
->kill(); // KIO::Job::kill deletes itself
67 KIO::TransferJob
* job
;
73 KJavaDownloader::KJavaDownloader( int ID
, const QString
& url
)
74 :d(new KJavaDownloaderPrivate
)
76 kDebug(6100) << "KJavaDownloader(" << ID
<< ") = " << url
;
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()
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
);
112 d
->isfirstdata
= false;
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();
136 kDebug(6100) << "slave had an error = " << d
->job
->errorString();
137 int code
= d
->job
->error();
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
);
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
)
162 kDebug(6100) << "jobCommand(" << d
->loaderID
<< ") stop";
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();
171 kDebug(6100) << "jobCommand(" << d
->loaderID
<< ") hold";
175 kDebug(6100) << "jobCommand(" << d
->loaderID
<< ") resume";
181 //-----------------------------------------------------------------------------
183 class KJavaUploaderPrivate
186 KJavaUploaderPrivate() {}
187 ~KJavaUploaderPrivate()
190 if (job
) job
->kill(); // KIO::Job::kill deletes itself
195 KIO::TransferJob
* job
;
199 KJavaUploader::KJavaUploader( int ID
, const QString
& url
)
200 :d(new KJavaUploaderPrivate
)
202 kDebug(6100) << "KJavaUploader(" << ID
<< ") = " << url
;
206 d
->url
= new KUrl( url
);
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
);
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()
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
241 memcpy( qb
.data(), d
->file
.data(), d
->file
.size() );
244 server
->sendURLData( d
->loaderID
, REQUESTDATA
, d
->file
);
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() );
259 void KJavaUploader::slotResult( KJob
* )
261 kDebug(6100) << "slotResult(" << d
->loaderID
<< ") job:" << d
->job
;
265 KJavaAppletServer
* server
= KJavaAppletServer::allocateJavaServer();
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
);
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
)
289 kDebug(6100) << "jobCommand(" << d
->loaderID
<< ") stop";
291 if (d
->job
->isSuspended())
298 #include "kjavadownloader.moc"