forwardingslavebase.cpp
00001 /* This file is part of the KDE project 00002 Copyright (c) 2004 Kevin Ottens <ervin ipsquad net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <kdebug.h> 00021 #include <kio/job.h> 00022 #include <kmimetype.h> 00023 #include <kprotocolinfo.h> 00024 00025 #include <tqapplication.h> 00026 #include <tqeventloop.h> 00027 00028 #include "forwardingslavebase.h" 00029 00030 namespace KIO 00031 { 00032 00033 class ForwardingSlaveBasePrivate 00034 { 00035 }; 00036 00037 ForwardingSlaveBase::ForwardingSlaveBase(const TQCString &protocol, 00038 const TQCString &poolSocket, 00039 const TQCString &appSocket) 00040 : TQObject(), SlaveBase(protocol, poolSocket, appSocket) 00041 { 00042 } 00043 00044 ForwardingSlaveBase::~ForwardingSlaveBase() 00045 { 00046 } 00047 00048 bool ForwardingSlaveBase::internalRewriteURL(const KURL &url, KURL &newURL) 00049 { 00050 bool result = true; 00051 00052 if ( url.protocol().ascii()==mProtocol ) 00053 { 00054 result = rewriteURL(url, newURL); 00055 } 00056 else 00057 { 00058 newURL = url; 00059 } 00060 00061 m_processedURL = newURL; 00062 m_requestedURL = url; 00063 return result; 00064 } 00065 00066 void ForwardingSlaveBase::prepareUDSEntry(KIO::UDSEntry &entry, 00067 bool listing) const 00068 { 00069 kdDebug() << "ForwardingSlaveBase::prepareUDSEntry: listing==" 00070 << listing << endl; 00071 00072 TQString name; 00073 KURL url; 00074 00075 KIO::UDSEntry::iterator it = entry.begin(); 00076 KIO::UDSEntry::iterator end = entry.end(); 00077 00078 for(; it!=end; ++it) 00079 { 00080 KURL new_url = m_requestedURL; 00081 00082 switch( (*it).m_uds ) 00083 { 00084 case KIO::UDS_NAME: 00085 name = (*it).m_str; 00086 kdDebug() << "Name = " << name << endl; 00087 break; 00088 case KIO::UDS_URL: 00089 url = (*it).m_str; 00090 if (listing) 00091 { 00092 new_url.addPath(url.fileName()); 00093 } 00094 (*it).m_str = new_url.url(); 00095 kdDebug() << "URL = " << url << endl; 00096 kdDebug() << "New URL = " << (*it).m_str << endl; 00097 break; 00098 } 00099 } 00100 00101 if ( m_processedURL.isLocalFile() ) 00102 { 00103 KURL new_url = m_processedURL; 00104 if (listing) 00105 { 00106 new_url.addPath( name ); 00107 } 00108 00109 KIO::UDSAtom atom; 00110 atom.m_uds = KIO::UDS_LOCAL_PATH; 00111 atom.m_long = 0; 00112 atom.m_str = new_url.path(); 00113 entry.append(atom); 00114 } 00115 } 00116 00117 void ForwardingSlaveBase::get(const KURL &url) 00118 { 00119 kdDebug() << "ForwardingSlaveBase::get: " << url << endl; 00120 00121 KURL new_url; 00122 if ( internalRewriteURL(url, new_url) ) 00123 { 00124 KIO::TransferJob *job = KIO::get(new_url, false, false); 00125 connectTransferJob(job); 00126 00127 tqApp->eventLoop()->enterLoop(); 00128 } 00129 } 00130 00131 void ForwardingSlaveBase::put(const KURL &url, int permissions, 00132 bool overwrite, bool resume ) 00133 { 00134 kdDebug() << "ForwardingSlaveBase::put: " << url << endl; 00135 00136 KURL new_url; 00137 if ( internalRewriteURL(url, new_url) ) 00138 { 00139 KIO::TransferJob *job = KIO::put(new_url, permissions, overwrite, 00140 resume, false); 00141 connectTransferJob(job); 00142 00143 tqApp->eventLoop()->enterLoop(); 00144 } 00145 } 00146 00147 void ForwardingSlaveBase::stat(const KURL &url) 00148 { 00149 kdDebug() << "ForwardingSlaveBase::stat: " << url << endl; 00150 00151 KURL new_url; 00152 if ( internalRewriteURL(url, new_url) ) 00153 { 00154 KIO::SimpleJob *job = KIO::stat(new_url, false); 00155 connectSimpleJob(job); 00156 00157 tqApp->eventLoop()->enterLoop(); 00158 } 00159 } 00160 00161 void ForwardingSlaveBase::mimetype(const KURL &url) 00162 { 00163 kdDebug() << "ForwardingSlaveBase::mimetype: " << url << endl; 00164 00165 KURL new_url; 00166 if ( internalRewriteURL(url, new_url) ) 00167 { 00168 KIO::TransferJob *job = KIO::mimetype(new_url, false); 00169 connectTransferJob(job); 00170 00171 tqApp->eventLoop()->enterLoop(); 00172 } 00173 } 00174 00175 void ForwardingSlaveBase::listDir(const KURL &url) 00176 { 00177 kdDebug() << "ForwardingSlaveBase::listDir: " << url << endl; 00178 00179 KURL new_url; 00180 if ( internalRewriteURL(url, new_url) ) 00181 { 00182 KIO::ListJob *job = KIO::listDir(new_url, false); 00183 connectListJob(job); 00184 00185 tqApp->eventLoop()->enterLoop(); 00186 } 00187 } 00188 00189 void ForwardingSlaveBase::mkdir(const KURL &url, int permissions) 00190 { 00191 kdDebug() << "ForwardingSlaveBase::mkdir: " << url << endl; 00192 00193 KURL new_url; 00194 if ( internalRewriteURL(url, new_url) ) 00195 { 00196 KIO::SimpleJob *job = KIO::mkdir(new_url, permissions); 00197 connectSimpleJob(job); 00198 00199 tqApp->eventLoop()->enterLoop(); 00200 } 00201 } 00202 00203 void ForwardingSlaveBase::rename(const KURL &src, const KURL &dest, 00204 bool overwrite) 00205 { 00206 kdDebug() << "ForwardingSlaveBase::rename: " << src << ", " << dest << endl; 00207 00208 KURL new_src, new_dest; 00209 if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) ) 00210 { 00211 KIO::Job *job = KIO::rename(new_src, new_dest, overwrite); 00212 connectJob(job); 00213 00214 tqApp->eventLoop()->enterLoop(); 00215 } 00216 } 00217 00218 void ForwardingSlaveBase::symlink(const TQString &target, const KURL &dest, 00219 bool overwrite) 00220 { 00221 kdDebug() << "ForwardingSlaveBase::symlink: " << target << ", " << dest << endl; 00222 00223 KURL new_dest; 00224 if ( internalRewriteURL(dest, new_dest) ) 00225 { 00226 KIO::SimpleJob *job = KIO::symlink(target, new_dest, overwrite, false); 00227 connectSimpleJob(job); 00228 00229 tqApp->eventLoop()->enterLoop(); 00230 } 00231 } 00232 00233 void ForwardingSlaveBase::chmod(const KURL &url, int permissions) 00234 { 00235 kdDebug() << "ForwardingSlaveBase::chmod: " << url << endl; 00236 00237 KURL new_url; 00238 if ( internalRewriteURL(url, new_url) ) 00239 { 00240 KIO::SimpleJob *job = KIO::chmod(new_url, permissions); 00241 connectSimpleJob(job); 00242 00243 tqApp->eventLoop()->enterLoop(); 00244 } 00245 } 00246 00247 void ForwardingSlaveBase::copy(const KURL &src, const KURL &dest, 00248 int permissions, bool overwrite) 00249 { 00250 kdDebug() << "ForwardingSlaveBase::copy: " << src << ", " << dest << endl; 00251 00252 KURL new_src, new_dest; 00253 if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) ) 00254 { 00255 KIO::Job *job = KIO::file_copy(new_src, new_dest, permissions, 00256 overwrite, false); 00257 connectJob(job); 00258 00259 tqApp->eventLoop()->enterLoop(); 00260 } 00261 } 00262 00263 void ForwardingSlaveBase::del(const KURL &url, bool isfile) 00264 { 00265 kdDebug() << "ForwardingSlaveBase::del: " << url << endl; 00266 00267 KURL new_url; 00268 if ( internalRewriteURL(url, new_url) ) 00269 { 00270 if (isfile) 00271 { 00272 KIO::DeleteJob *job = KIO::del(new_url, false, false); 00273 connectJob(job); 00274 } 00275 else 00276 { 00277 KIO::SimpleJob *job = KIO::rmdir(new_url); 00278 connectSimpleJob(job); 00279 } 00280 00281 tqApp->eventLoop()->enterLoop(); 00282 } 00283 } 00284 00285 00287 00288 void ForwardingSlaveBase::connectJob(KIO::Job *job) 00289 { 00290 // We will forward the warning message, no need to let the job 00291 // display it itself 00292 job->setInteractive(false); 00293 00294 // Forward metadata (e.g. modification time for put()) 00295 job->setMetaData( allMetaData() ); 00296 #if 0 // debug code 00297 kdDebug() << k_funcinfo << "transferring metadata:" << endl; 00298 const MetaData md = allMetaData(); 00299 for ( MetaData::const_iterator it = md.begin(); it != md.end(); ++it ) 00300 kdDebug() << it.key() << " = " << it.data() << endl; 00301 #endif 00302 00303 connect( job, TQT_SIGNAL( result(KIO::Job *) ), 00304 this, TQT_SLOT( slotResult(KIO::Job *) ) ); 00305 connect( job, TQT_SIGNAL( warning(KIO::Job *, const TQString &) ), 00306 this, TQT_SLOT( slotWarning(KIO::Job *, const TQString &) ) ); 00307 connect( job, TQT_SIGNAL( infoMessage(KIO::Job *, const TQString &) ), 00308 this, TQT_SLOT( slotInfoMessage(KIO::Job *, const TQString &) ) ); 00309 connect( job, TQT_SIGNAL( totalSize(KIO::Job *, KIO::filesize_t) ), 00310 this, TQT_SLOT( slotTotalSize(KIO::Job *, KIO::filesize_t) ) ); 00311 connect( job, TQT_SIGNAL( processedSize(KIO::Job *, KIO::filesize_t) ), 00312 this, TQT_SLOT( slotProcessedSize(KIO::Job *, KIO::filesize_t) ) ); 00313 connect( job, TQT_SIGNAL( speed(KIO::Job *, unsigned long) ), 00314 this, TQT_SLOT( slotSpeed(KIO::Job *, unsigned long) ) ); 00315 } 00316 00317 void ForwardingSlaveBase::connectSimpleJob(KIO::SimpleJob *job) 00318 { 00319 connectJob(job); 00320 connect( job, TQT_SIGNAL( redirection(KIO::Job *, const KURL &) ), 00321 this, TQT_SLOT( slotRedirection(KIO::Job *, const KURL &) ) ); 00322 } 00323 00324 void ForwardingSlaveBase::connectListJob(KIO::ListJob *job) 00325 { 00326 connectSimpleJob(job); 00327 connect( job, TQT_SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList &) ), 00328 this, TQT_SLOT( slotEntries(KIO::Job *, const KIO::UDSEntryList &) ) ); 00329 } 00330 00331 void ForwardingSlaveBase::connectTransferJob(KIO::TransferJob *job) 00332 { 00333 connectSimpleJob(job); 00334 connect( job, TQT_SIGNAL( data(KIO::Job *, const TQByteArray &) ), 00335 this, TQT_SLOT( slotData(KIO::Job *, const TQByteArray &) ) ); 00336 connect( job, TQT_SIGNAL( dataReq(KIO::Job *, TQByteArray &) ), 00337 this, TQT_SLOT( slotDataReq(KIO::Job *, TQByteArray &) ) ); 00338 connect( job, TQT_SIGNAL( mimetype(KIO::Job *, const TQString &) ), 00339 this, TQT_SLOT( slotMimetype(KIO::Job *, const TQString &) ) ); 00340 connect( job, TQT_SIGNAL( canResume(KIO::Job *, KIO::filesize_t) ), 00341 this, TQT_SLOT( slotCanResume(KIO::Job *, KIO::filesize_t) ) ); 00342 } 00343 00345 00346 void ForwardingSlaveBase::slotResult(KIO::Job *job) 00347 { 00348 if ( job->error() != 0) 00349 { 00350 error( job->error(), job->errorText() ); 00351 } 00352 else 00353 { 00354 KIO::StatJob *stat_job = dynamic_cast<KIO::StatJob *>(job); 00355 if ( stat_job!=0L ) 00356 { 00357 KIO::UDSEntry entry = stat_job->statResult(); 00358 prepareUDSEntry(entry); 00359 statEntry( entry ); 00360 } 00361 finished(); 00362 } 00363 00364 tqApp->eventLoop()->exitLoop(); 00365 } 00366 00367 void ForwardingSlaveBase::slotWarning(KIO::Job* /*job*/, const TQString &msg) 00368 { 00369 warning(msg); 00370 } 00371 00372 void ForwardingSlaveBase::slotInfoMessage(KIO::Job* /*job*/, const TQString &msg) 00373 { 00374 infoMessage(msg); 00375 } 00376 00377 void ForwardingSlaveBase::slotTotalSize(KIO::Job* /*job*/, KIO::filesize_t size) 00378 { 00379 totalSize(size); 00380 } 00381 00382 void ForwardingSlaveBase::slotProcessedSize(KIO::Job* /*job*/, KIO::filesize_t size) 00383 { 00384 processedSize(size); 00385 } 00386 00387 void ForwardingSlaveBase::slotSpeed(KIO::Job* /*job*/, unsigned long bytesPerSecond) 00388 { 00389 speed(bytesPerSecond); 00390 } 00391 00392 void ForwardingSlaveBase::slotRedirection(KIO::Job *job, const KURL &url) 00393 { 00394 redirection(url); 00395 00396 // We've been redirected stop everything. 00397 job->kill( true ); 00398 finished(); 00399 00400 tqApp->eventLoop()->exitLoop(); 00401 } 00402 00403 void ForwardingSlaveBase::slotEntries(KIO::Job* /*job*/, 00404 const KIO::UDSEntryList &entries) 00405 { 00406 KIO::UDSEntryList final_entries = entries; 00407 00408 KIO::UDSEntryList::iterator it = final_entries.begin(); 00409 KIO::UDSEntryList::iterator end = final_entries.end(); 00410 00411 for(; it!=end; ++it) 00412 { 00413 prepareUDSEntry(*it, true); 00414 } 00415 00416 listEntries( final_entries ); 00417 } 00418 00419 void ForwardingSlaveBase::slotData(KIO::Job* /*job*/, const TQByteArray &d) 00420 { 00421 data(d); 00422 } 00423 00424 void ForwardingSlaveBase::slotDataReq(KIO::Job* /*job*/, TQByteArray &data) 00425 { 00426 dataReq(); 00427 readData(data); 00428 } 00429 00430 void ForwardingSlaveBase::slotMimetype (KIO::Job* /*job*/, const TQString &type) 00431 { 00432 mimeType(type); 00433 } 00434 00435 void ForwardingSlaveBase::slotCanResume (KIO::Job* /*job*/, KIO::filesize_t offset) 00436 { 00437 canResume(offset); 00438 } 00439 00440 } 00441 00442 #include "forwardingslavebase.moc" 00443