kio Library API Documentation

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., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include <kdebug.h> 00021 #include <kio/job.h> 00022 #include <kmimetype.h> 00023 #include <kprotocolinfo.h> 00024 00025 #include <qapplication.h> 00026 #include <qeventloop.h> 00027 00028 #include "forwardingslavebase.h" 00029 00030 namespace KIO 00031 { 00032 00033 class ForwardingSlaveBasePrivate 00034 { 00035 }; 00036 00037 ForwardingSlaveBase::ForwardingSlaveBase(const QCString &protocol, 00038 const QCString &poolSocket, 00039 const QCString &appSocket) 00040 : QObject(), 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 bool mimetype_found = false; 00073 bool name_found = false; 00074 bool url_found = false; 00075 QString name, mimetype; 00076 KURL url; 00077 00078 KIO::UDSEntry::iterator it = entry.begin(); 00079 KIO::UDSEntry::iterator end = entry.end(); 00080 00081 for(; it!=end; ++it) 00082 { 00083 KURL new_url = m_processedURL; 00084 00085 switch( (*it).m_uds ) 00086 { 00087 case KIO::UDS_NAME: 00088 name_found = true; 00089 name = (*it).m_str; 00090 kdDebug() << "Name = " << name << endl; 00091 break; 00092 case KIO::UDS_URL: 00093 url_found = true; 00094 url = (*it).m_str; 00095 if (listing) 00096 { 00097 new_url.addPath(url.fileName()); 00098 } 00099 (*it).m_str = new_url.url(); 00100 kdDebug() << "URL = " << url << endl; 00101 kdDebug() << "New URL = " << (*it).m_str << endl; 00102 break; 00103 case KIO::UDS_MIME_TYPE: 00104 mimetype_found = true; 00105 mimetype = (*it).m_str; 00106 kdDebug() << "Mimetype = " << (*it).m_str << endl; 00107 break; 00108 } 00109 } 00110 00111 if (!mimetype_found) 00112 { 00113 KURL new_url = m_processedURL; 00114 if (url_found && listing) 00115 { 00116 new_url.addPath( url.fileName() ); 00117 } 00118 else if (name_found && listing) 00119 { 00120 new_url.addPath( name ); 00121 } 00122 00123 KMimeType::Ptr mime = KMimeType::findByURL(new_url); 00124 mimetype = mime->name(); 00125 00126 KIO::UDSAtom atom; 00127 atom.m_uds = KIO::UDS_MIME_TYPE; 00128 atom.m_long = 0; 00129 atom.m_str = mimetype; 00130 entry.append(atom); 00131 00132 kdDebug() << "New Mimetype = " << mime->name() << endl; 00133 } 00134 00135 if ( mimetype=="application/x-desktop" && name_found 00136 && m_processedURL.isLocalFile() ) 00137 { 00138 QString prot_class = KProtocolInfo::protocolClass( mProtocol ); 00139 00140 if ( prot_class==":local" ) 00141 { 00142 KURL new_url = m_processedURL; 00143 if (listing) 00144 { 00145 new_url.addPath( name ); 00146 } 00147 00148 KIO::UDSAtom atom; 00149 atom.m_uds = KIO::UDS_URL; 00150 atom.m_long = 0; 00151 atom.m_str = new_url.url(); 00152 entry.append(atom); 00153 } 00154 } 00155 00156 if ( m_processedURL.isLocalFile() ) 00157 { 00158 KURL new_url = m_processedURL; 00159 if (listing) 00160 { 00161 new_url.addPath( name ); 00162 } 00163 00164 KIO::UDSAtom atom; 00165 atom.m_uds = KIO::UDS_LOCAL_PATH; 00166 atom.m_long = 0; 00167 atom.m_str = new_url.path(); 00168 entry.append(atom); 00169 } 00170 } 00171 00172 void ForwardingSlaveBase::get(const KURL &url) 00173 { 00174 kdDebug() << "ForwardingSlaveBase::get: " << url << endl; 00175 00176 KURL new_url; 00177 if ( internalRewriteURL(url, new_url) ) 00178 { 00179 KIO::TransferJob *job = KIO::get(new_url, false, false); 00180 connectTransferJob(job); 00181 00182 qApp->eventLoop()->enterLoop(); 00183 } 00184 } 00185 00186 void ForwardingSlaveBase::put(const KURL &url, int permissions, 00187 bool overwrite, bool resume ) 00188 { 00189 kdDebug() << "ForwardingSlaveBase::put: " << url << endl; 00190 00191 KURL new_url; 00192 if ( internalRewriteURL(url, new_url) ) 00193 { 00194 KIO::TransferJob *job = KIO::put(new_url, permissions, overwrite, 00195 resume, false); 00196 connectTransferJob(job); 00197 00198 qApp->eventLoop()->enterLoop(); 00199 } 00200 } 00201 00202 void ForwardingSlaveBase::stat(const KURL &url) 00203 { 00204 kdDebug() << "ForwardingSlaveBase::stat: " << url << endl; 00205 00206 KURL new_url; 00207 if ( internalRewriteURL(url, new_url) ) 00208 { 00209 KIO::SimpleJob *job = KIO::stat(new_url, false); 00210 connectSimpleJob(job); 00211 00212 qApp->eventLoop()->enterLoop(); 00213 } 00214 } 00215 00216 void ForwardingSlaveBase::mimetype(const KURL &url) 00217 { 00218 kdDebug() << "ForwardingSlaveBase::mimetype: " << url << endl; 00219 00220 KURL new_url; 00221 if ( internalRewriteURL(url, new_url) ) 00222 { 00223 KIO::TransferJob *job = KIO::mimetype(new_url, false); 00224 connectTransferJob(job); 00225 00226 qApp->eventLoop()->enterLoop(); 00227 } 00228 } 00229 00230 void ForwardingSlaveBase::listDir(const KURL &url) 00231 { 00232 kdDebug() << "ForwardingSlaveBase::listDir: " << url << endl; 00233 00234 KURL new_url; 00235 if ( internalRewriteURL(url, new_url) ) 00236 { 00237 KIO::ListJob *job = KIO::listDir(new_url, false); 00238 connectListJob(job); 00239 00240 qApp->eventLoop()->enterLoop(); 00241 } 00242 } 00243 00244 void ForwardingSlaveBase::mkdir(const KURL &url, int permissions) 00245 { 00246 kdDebug() << "ForwardingSlaveBase::mkdir: " << url << endl; 00247 00248 KURL new_url; 00249 if ( internalRewriteURL(url, new_url) ) 00250 { 00251 KIO::SimpleJob *job = KIO::mkdir(new_url, permissions); 00252 connectSimpleJob(job); 00253 00254 qApp->eventLoop()->enterLoop(); 00255 } 00256 } 00257 00258 void ForwardingSlaveBase::rename(const KURL &src, const KURL &dest, 00259 bool overwrite) 00260 { 00261 kdDebug() << "ForwardingSlaveBase::rename: " << src << ", " << dest << endl; 00262 00263 KURL new_src, new_dest; 00264 if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) ) 00265 { 00266 KIO::Job *job = KIO::rename(new_src, new_dest, overwrite); 00267 connectJob(job); 00268 00269 qApp->eventLoop()->enterLoop(); 00270 } 00271 } 00272 00273 void ForwardingSlaveBase::symlink(const QString &target, const KURL &dest, 00274 bool overwrite) 00275 { 00276 kdDebug() << "ForwardingSlaveBase::symlink: " << target << ", " << dest << endl; 00277 00278 KURL new_dest; 00279 if ( internalRewriteURL(dest, new_dest) ) 00280 { 00281 KIO::SimpleJob *job = KIO::symlink(target, new_dest, overwrite, false); 00282 connectSimpleJob(job); 00283 00284 qApp->eventLoop()->enterLoop(); 00285 } 00286 } 00287 00288 void ForwardingSlaveBase::chmod(const KURL &url, int permissions) 00289 { 00290 kdDebug() << "ForwardingSlaveBase::chmod: " << url << endl; 00291 00292 KURL new_url; 00293 if ( internalRewriteURL(url, new_url) ) 00294 { 00295 KIO::SimpleJob *job = KIO::chmod(new_url, permissions); 00296 connectSimpleJob(job); 00297 00298 qApp->eventLoop()->enterLoop(); 00299 } 00300 } 00301 00302 void ForwardingSlaveBase::copy(const KURL &src, const KURL &dest, 00303 int permissions, bool overwrite) 00304 { 00305 kdDebug() << "ForwardingSlaveBase::copy: " << src << ", " << dest << endl; 00306 00307 KURL new_src, new_dest; 00308 if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) ) 00309 { 00310 KIO::Job *job = KIO::file_copy(new_src, new_dest, permissions, 00311 overwrite, false); 00312 connectJob(job); 00313 00314 qApp->eventLoop()->enterLoop(); 00315 } 00316 } 00317 00318 void ForwardingSlaveBase::del(const KURL &url, bool isfile) 00319 { 00320 kdDebug() << "ForwardingSlaveBase::del: " << url << endl; 00321 00322 KURL new_url; 00323 if ( internalRewriteURL(url, new_url) ) 00324 { 00325 if (isfile) 00326 { 00327 KIO::DeleteJob *job = KIO::del(new_url, false, false); 00328 connectJob(job); 00329 } 00330 else 00331 { 00332 KIO::SimpleJob *job = KIO::rmdir(new_url); 00333 connectSimpleJob(job); 00334 } 00335 00336 qApp->eventLoop()->enterLoop(); 00337 } 00338 } 00339 00340 00342 00343 void ForwardingSlaveBase::connectJob(KIO::Job *job) 00344 { 00345 connect( job, SIGNAL( result(KIO::Job *) ), 00346 this, SLOT( slotResult(KIO::Job *) ) ); 00347 connect( job, SIGNAL( infoMessage(KIO::Job *, const QString &) ), 00348 this, SLOT( slotInfoMessage(KIO::Job *, const QString &) ) ); 00349 connect( job, SIGNAL( totalSize(KIO::Job *, KIO::filesize_t) ), 00350 this, SLOT( slotTotalSize(KIO::Job *, KIO::filesize_t) ) ); 00351 connect( job, SIGNAL( processedSize(KIO::Job *, KIO::filesize_t) ), 00352 this, SLOT( slotProcessedSize(KIO::Job *, KIO::filesize_t) ) ); 00353 connect( job, SIGNAL( speed(KIO::Job *, unsigned long) ), 00354 this, SLOT( slotSpeed(KIO::Job *, unsigned long) ) ); 00355 } 00356 00357 void ForwardingSlaveBase::connectSimpleJob(KIO::SimpleJob *job) 00358 { 00359 connectJob(job); 00360 connect( job, SIGNAL( redirection(KIO::Job *, const KURL &) ), 00361 this, SLOT( slotRedirection(KIO::Job *, const KURL &) ) ); 00362 } 00363 00364 void ForwardingSlaveBase::connectListJob(KIO::ListJob *job) 00365 { 00366 connectSimpleJob(job); 00367 connect( job, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList &) ), 00368 this, SLOT( slotEntries(KIO::Job *, const KIO::UDSEntryList &) ) ); 00369 } 00370 00371 void ForwardingSlaveBase::connectTransferJob(KIO::TransferJob *job) 00372 { 00373 connectSimpleJob(job); 00374 connect( job, SIGNAL( data(KIO::Job *, const QByteArray &) ), 00375 this, SLOT( slotData(KIO::Job *, const QByteArray &) ) ); 00376 connect( job, SIGNAL( dataReq(KIO::Job *, QByteArray &) ), 00377 this, SLOT( slotDataReq(KIO::Job *, QByteArray &) ) ); 00378 connect( job, SIGNAL( mimetype(KIO::Job *, const QString &) ), 00379 this, SLOT( slotMimetype(KIO::Job *, const QString &) ) ); 00380 connect( job, SIGNAL( canResume(KIO::Job *, KIO::filesize_t) ), 00381 this, SLOT( slotCanResume(KIO::Job *, KIO::filesize_t) ) ); 00382 } 00383 00385 00386 void ForwardingSlaveBase::slotResult(KIO::Job *job) 00387 { 00388 if ( job->error() != 0) 00389 { 00390 error( job->error(), job->errorText() ); 00391 } 00392 else 00393 { 00394 KIO::StatJob *stat_job = dynamic_cast<KIO::StatJob *>(job); 00395 if ( stat_job!=0L ) 00396 { 00397 KIO::UDSEntry entry = stat_job->statResult(); 00398 prepareUDSEntry(entry); 00399 statEntry( entry ); 00400 } 00401 finished(); 00402 } 00403 00404 qApp->eventLoop()->exitLoop(); 00405 } 00406 00407 void ForwardingSlaveBase::slotInfoMessage(KIO::Job* /*job*/, const QString &msg) 00408 { 00409 infoMessage(msg); 00410 } 00411 00412 void ForwardingSlaveBase::slotTotalSize(KIO::Job* /*job*/, KIO::filesize_t size) 00413 { 00414 totalSize(size); 00415 } 00416 00417 void ForwardingSlaveBase::slotProcessedSize(KIO::Job* /*job*/, KIO::filesize_t size) 00418 { 00419 processedSize(size); 00420 } 00421 00422 void ForwardingSlaveBase::slotSpeed(KIO::Job* /*job*/, unsigned long bytesPerSecond) 00423 { 00424 speed(bytesPerSecond); 00425 } 00426 00427 void ForwardingSlaveBase::slotRedirection(KIO::Job* /*job*/, const KURL &url) 00428 { 00429 redirection(url); 00430 } 00431 00432 void ForwardingSlaveBase::slotEntries(KIO::Job* /*job*/, 00433 const KIO::UDSEntryList &entries) 00434 { 00435 KIO::UDSEntryList final_entries = entries; 00436 00437 KIO::UDSEntryList::iterator it = final_entries.begin(); 00438 KIO::UDSEntryList::iterator end = final_entries.end(); 00439 00440 for(; it!=end; ++it) 00441 { 00442 prepareUDSEntry(*it, true); 00443 } 00444 00445 listEntries( final_entries ); 00446 } 00447 00448 void ForwardingSlaveBase::slotData(KIO::Job* /*job*/, const QByteArray &d) 00449 { 00450 data(d); 00451 } 00452 00453 void ForwardingSlaveBase::slotDataReq(KIO::Job* /*job*/, QByteArray &data) 00454 { 00455 dataReq(); 00456 readData(data); 00457 } 00458 00459 void ForwardingSlaveBase::slotMimetype (KIO::Job* /*job*/, const QString &type) 00460 { 00461 mimeType(type); 00462 } 00463 00464 void ForwardingSlaveBase::slotCanResume (KIO::Job* /*job*/, KIO::filesize_t offset) 00465 { 00466 canResume(offset); 00467 } 00468 00469 } 00470 00471 #include "forwardingslavebase.moc" 00472
KDE Logo
This file is part of the documentation for kio Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Apr 12 23:09:03 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003