00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kjavadownloader.h"
00023 #include "kjavaappletserver.h"
00024
00025 #include <kurl.h>
00026 #include <kio/job.h>
00027 #include <kio/jobclasses.h>
00028 #include <kdebug.h>
00029 #include <qfile.h>
00030
00031 static const int DATA = 0;
00032 static const int FINISHED = 1;
00033 static const int ERRORCODE = 2;
00034 static const int HEADERS = 3;
00035 static const int REDIRECT = 4;
00036 static const int MIMETYPE = 5;
00037 static const int CONNECTED = 6;
00038 static const int REQUESTDATA = 7;
00039
00040 static const int KJAS_STOP = 0;
00041 static const int KJAS_HOLD = 1;
00042 static const int KJAS_RESUME = 2;
00043
00044 KJavaKIOJob::~KJavaKIOJob() {}
00045
00046 void KJavaKIOJob::data( const QByteArray& )
00047 {
00048 kdError(6100) << "Job id mixup" << endl;
00049 }
00050
00051
00052
00053 class KJavaDownloaderPrivate
00054 {
00055 friend class KJavaDownloader;
00056 public:
00057 KJavaDownloaderPrivate() : responseCode(0), isfirstdata(true) {}
00058 ~KJavaDownloaderPrivate()
00059 {
00060 delete url;
00061 if (job) job->kill();
00062 }
00063 private:
00064 int loaderID;
00065 KURL* url;
00066 QByteArray file;
00067 KIO::TransferJob* job;
00068 int responseCode;
00069 bool isfirstdata;
00070 };
00071
00072
00073 KJavaDownloader::KJavaDownloader( int ID, const QString& url )
00074 {
00075 kdDebug(6100) << "KJavaDownloader(" << ID << ") = " << url << endl;
00076
00077 d = new KJavaDownloaderPrivate;
00078
00079 d->loaderID = ID;
00080 d->url = new KURL( url );
00081
00082 d->job = KIO::get( *d->url, false, false );
00083 d->job->addMetaData("PropagateHttpHeader", "true");
00084 connect( d->job, SIGNAL(data( KIO::Job*, const QByteArray& )),
00085 this, SLOT(slotData( KIO::Job*, const QByteArray& )) );
00086 connect( d->job, SIGNAL(connected(KIO::Job*)),
00087 this, SLOT(slotConnected(KIO::Job*)));
00088 connect( d->job, SIGNAL(mimetype(KIO::Job*, const QString&)),
00089 this, SLOT(slotMimetype(KIO::Job*, const QString&)));
00090 connect( d->job, SIGNAL(result(KIO::Job*)),
00091 this, SLOT(slotResult(KIO::Job*)) );
00092 }
00093
00094 KJavaDownloader::~KJavaDownloader()
00095 {
00096 delete d;
00097 }
00098
00099 void KJavaDownloader::slotData( KIO::Job*, const QByteArray& qb )
00100 {
00101
00102
00103 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
00104 if (d->isfirstdata) {
00105 QString headers = d->job->queryMetaData("HTTP-Headers");
00106 if (!headers.isEmpty()) {
00107 d->file.resize( headers.length() );
00108 memcpy( d->file.data(), headers.ascii(), headers.length() );
00109 server->sendURLData( d->loaderID, HEADERS, d->file );
00110 d->file.resize( 0 );
00111 }
00112 d->isfirstdata = false;
00113 }
00114 if ( qb.size() )
00115 server->sendURLData( d->loaderID, DATA, qb );
00116 KJavaAppletServer::freeJavaServer();
00117 }
00118
00119 void KJavaDownloader::slotConnected(KIO::Job*)
00120 {
00121 kdDebug(6100) << "slave connected" << endl;
00122 d->responseCode = d->job->error();
00123 }
00124
00125 void KJavaDownloader::slotMimetype(KIO::Job*, const QString & type) {
00126 kdDebug(6100) << "slave mimetype " << type << endl;
00127 }
00128
00129 void KJavaDownloader::slotResult( KIO::Job* )
00130 {
00131 kdDebug(6100) << "slotResult(" << d->loaderID << ")" << endl;
00132
00133 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
00134 if( d->job->error())
00135 {
00136 kdDebug(6100) << "slave had an error = " << d->job->errorString() << endl;
00137 int code = d->job->error();
00138 if (!code)
00139 code = 404;
00140 QString codestr = QString::number(code);
00141 d->file.resize(codestr.length());
00142 memcpy( d->file.data(), codestr.ascii(), codestr.length() );
00143 kdDebug(6100) << "slave had an error = " << code << endl;
00144
00145 server->sendURLData( d->loaderID, ERRORCODE, d->file );
00146 d->file.resize( 0 );
00147 }
00148 else
00149 {
00150 server->sendURLData( d->loaderID, FINISHED, d->file );
00151 }
00152 d->job = 0L;
00153 server->removeDataJob( d->loaderID );
00154 KJavaAppletServer::freeJavaServer();
00155 }
00156
00157 void KJavaDownloader::jobCommand( int cmd )
00158 {
00159 if (!d->job) return;
00160 switch (cmd) {
00161 case KJAS_STOP: {
00162 kdDebug(6100) << "jobCommand(" << d->loaderID << ") stop" << endl;
00163 d->job->kill();
00164 d->job = 0L;
00165 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
00166 server->removeDataJob( d->loaderID );
00167 KJavaAppletServer::freeJavaServer();
00168 break;
00169 }
00170 case KJAS_HOLD:
00171 kdDebug(6100) << "jobCommand(" << d->loaderID << ") hold" << endl;
00172 d->job->suspend();
00173 break;
00174 case KJAS_RESUME:
00175 kdDebug(6100) << "jobCommand(" << d->loaderID << ") resume" << endl;
00176 d->job->resume();
00177 break;
00178 }
00179 }
00180
00181
00182
00183 class KJavaUploaderPrivate
00184 {
00185 public:
00186 KJavaUploaderPrivate() {}
00187 ~KJavaUploaderPrivate()
00188 {
00189 delete url;
00190 if (job) job->kill();
00191 }
00192 int loaderID;
00193 KURL* url;
00194 QByteArray file;
00195 KIO::TransferJob* job;
00196 bool finished;
00197 };
00198
00199 KJavaUploader::KJavaUploader( int ID, const QString& url )
00200 {
00201 kdDebug(6100) << "KJavaUploader(" << ID << ") = " << url << endl;
00202
00203 d = new KJavaUploaderPrivate;
00204
00205 d->loaderID = ID;
00206 d->url = new KURL( url );
00207 d->job = 0L;
00208 d->finished = false;
00209 }
00210
00211 void KJavaUploader::start()
00212 {
00213 kdDebug(6100) << "KJavaUploader::start(" << d->loaderID << ")" << endl;
00214 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
00215
00216 d->job = KIO::put( *d->url, -1, false, false, false );
00217 d->job->suspend();
00218 connect( d->job, SIGNAL(dataReq( KIO::Job*, QByteArray& )),
00219 this, SLOT(slotDataRequest( KIO::Job*, QByteArray& )) );
00220 connect( d->job, SIGNAL(result(KIO::Job*)),
00221 this, SLOT(slotResult(KIO::Job*)) );
00222 server->sendURLData( d->loaderID, CONNECTED, d->file );
00223 KJavaAppletServer::freeJavaServer();
00224 }
00225
00226 KJavaUploader::~KJavaUploader()
00227 {
00228 delete d;
00229 }
00230
00231 void KJavaUploader::slotDataRequest( KIO::Job*, QByteArray& qb )
00232 {
00233
00234 kdDebug(6100) << "slotDataRequest(" << d->loaderID << ") finished:" << d->finished << endl;
00235 qb.resize( d->file.size() );
00236 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
00237 if (d->file.size() == 0) {
00238 d->job = 0L;
00239 server->removeDataJob( d->loaderID );
00240 } else {
00241 memcpy( qb.data(), d->file.data(), d->file.size() );
00242 d->file.resize( 0 );
00243 if (!d->finished) {
00244 server->sendURLData( d->loaderID, REQUESTDATA, d->file );
00245 d->job->suspend();
00246 }
00247 }
00248 KJavaAppletServer::freeJavaServer();
00249 }
00250
00251 void KJavaUploader::data( const QByteArray& qb )
00252 {
00253 kdDebug(6100) << "KJavaUploader::data(" << d->loaderID << ")" << endl;
00254 d->file.resize( qb.size() );
00255 memcpy( d->file.data(), qb.data(), qb.size() );
00256 d->job->resume();
00257 }
00258
00259 void KJavaUploader::slotResult( KIO::Job* )
00260 {
00261 kdDebug(6100) << "slotResult(" << d->loaderID << ") job:" << d->job << endl;
00262
00263 if (!d->job)
00264 return;
00265 KJavaAppletServer* server = KJavaAppletServer::allocateJavaServer();
00266 if (d->job->error())
00267 {
00268 int code = d->job->error();
00269 QString codestr = QString::number(code);
00270 d->file.resize(codestr.length());
00271 memcpy( d->file.data(), codestr.ascii(), codestr.length() );
00272 kdDebug(6100) << "slave had an error " << code << ": " << d->job->errorString() << endl;
00273
00274 server->sendURLData( d->loaderID, ERRORCODE, d->file );
00275 d->file.resize( 0 );
00276 }
00277 else
00278 kdError(6100) << "slotResult(" << d->loaderID << ") job:" << d->job << endl;
00279 d->job = 0L;
00280 server->removeDataJob( d->loaderID );
00281 KJavaAppletServer::freeJavaServer();
00282 }
00283
00284 void KJavaUploader::jobCommand( int cmd )
00285 {
00286 if (!d->job) return;
00287 switch (cmd) {
00288 case KJAS_STOP: {
00289 kdDebug(6100) << "jobCommand(" << d->loaderID << ") stop" << endl;
00290 d->finished = true;
00291 if (d->job->isSuspended())
00292 d->job->resume();
00293 break;
00294 }
00295 }
00296 }
00297
00298 #include "kjavadownloader.moc"