• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

forwardingslavebase.cpp

Go to the documentation of this file.
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 "forwardingslavebase.h"
00021 
00022 #include "deletejob.h"
00023 #include "job.h"
00024 
00025 #include <kdebug.h>
00026 #include <kmimetype.h>
00027 #include <kprotocolinfo.h>
00028 
00029 #include <QtGui/QApplication>
00030 #include <QtCore/QEventLoop>
00031 
00032 namespace KIO
00033 {
00034 
00035 class ForwardingSlaveBasePrivate
00036 {
00037 public:
00038     ForwardingSlaveBasePrivate(QObject * eventLoopParent) :
00039         eventLoop(eventLoopParent)
00040     {}
00041     ForwardingSlaveBase *q;
00042 
00043     KUrl m_processedURL;
00044     KUrl m_requestedURL;
00045     QEventLoop eventLoop;
00046 
00047     bool internalRewriteUrl(const KUrl &url, KUrl &newURL);
00048 
00049     void connectJob(Job *job);
00050     void connectSimpleJob(SimpleJob *job);
00051     void connectListJob(ListJob *job);
00052     void connectTransferJob(TransferJob *job);
00053 
00054     void _k_slotResult(KJob *job);
00055     void _k_slotWarning(KJob *job, const QString &msg);
00056     void _k_slotInfoMessage(KJob *job, const QString &msg);
00057     void _k_slotTotalSize(KJob *job, qulonglong size);
00058     void _k_slotProcessedSize(KJob *job, qulonglong size);
00059     void _k_slotSpeed(KJob *job, unsigned long bytesPerSecond);
00060 
00061     // KIO::SimpleJob subclasses
00062     void _k_slotRedirection(KIO::Job *job, const KUrl &url);
00063 
00064     // KIO::ListJob
00065     void _k_slotEntries(KIO::Job *job, const KIO::UDSEntryList &entries);
00066 
00067     // KIO::TransferJob
00068     void _k_slotData(KIO::Job *job, const QByteArray &data);
00069     void _k_slotDataReq(KIO::Job *job, QByteArray &data);
00070     void _k_slotMimetype (KIO::Job *job, const QString &type);
00071     void _k_slotCanResume (KIO::Job *job, KIO::filesize_t offset);
00072 };
00073 
00074 ForwardingSlaveBase::ForwardingSlaveBase(const QByteArray &protocol,
00075                                          const QByteArray &poolSocket,
00076                                          const QByteArray &appSocket)
00077     : QObject(), SlaveBase(protocol, poolSocket, appSocket),
00078       d( new ForwardingSlaveBasePrivate(this) )
00079 {
00080     d->q = this;
00081 }
00082 
00083 ForwardingSlaveBase::~ForwardingSlaveBase()
00084 {
00085     delete d;
00086 }
00087 
00088 bool ForwardingSlaveBasePrivate::internalRewriteUrl(const KUrl &url, KUrl &newURL)
00089 {
00090     bool result = true;
00091 
00092     if ( url.protocol() == q->mProtocol )
00093     {
00094         result = q->rewriteUrl(url, newURL);
00095     }
00096     else
00097     {
00098         newURL = url;
00099     }
00100 
00101     m_processedURL = newURL;
00102     m_requestedURL = url;
00103     return result;
00104 }
00105 
00106 void ForwardingSlaveBase::prepareUDSEntry(KIO::UDSEntry &entry,
00107                                           bool listing) const
00108 {
00109     kDebug() << "ForwardingSlaveBase::prepareUDSEntry: listing=="
00110               << listing << endl;
00111 
00112     const QString name = entry.stringValue( KIO::UDSEntry::UDS_NAME );
00113     QString mimetype = entry.stringValue( KIO::UDSEntry::UDS_MIME_TYPE );
00114     KUrl url;
00115     const QString urlStr = entry.stringValue( KIO::UDSEntry::UDS_URL );
00116     const bool url_found = !urlStr.isEmpty();
00117     if ( url_found )
00118     {
00119         url = urlStr;
00120         KUrl new_url = d->m_requestedURL;
00121         if (listing)
00122             new_url.addPath(url.fileName());
00123         // ## Didn't find a way to use an iterator instead of re-doing a key lookup
00124         entry.insert( KIO::UDSEntry::UDS_URL, new_url.url() );
00125         kDebug() << "URL = " << url;
00126         kDebug() << "New URL = " << urlStr;
00127     }
00128 
00129     if (mimetype.isEmpty())
00130     {
00131         KUrl new_url = d->m_processedURL;
00132         if (url_found && listing)
00133         {
00134             new_url.addPath( url.fileName() );
00135         }
00136         else if (listing)
00137         {
00138             new_url.addPath( name );
00139         }
00140 
00141         mimetype = KMimeType::findByUrl(new_url)->name();
00142 
00143         entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, mimetype );
00144 
00145         kDebug() << "New Mimetype = " << mimetype;
00146     }
00147 
00148     if ( d->m_processedURL.isLocalFile() )
00149     {
00150         KUrl new_url = d->m_processedURL;
00151         if (listing)
00152         {
00153             new_url.addPath( name );
00154         }
00155 
00156         entry.insert( KIO::UDSEntry::UDS_LOCAL_PATH, new_url.path() );
00157     }
00158 }
00159 
00160 KUrl ForwardingSlaveBase::processedUrl() const
00161 {
00162     return d->m_processedURL;
00163 }
00164 
00165 KUrl ForwardingSlaveBase::requestedUrl() const
00166 {
00167     return d->m_requestedURL;
00168 }
00169 
00170 void ForwardingSlaveBase::get(const KUrl &url)
00171 {
00172     kDebug() << "ForwardingSlaveBase::get: " << url;
00173 
00174     KUrl new_url;
00175     if ( d->internalRewriteUrl(url, new_url) )
00176     {
00177         KIO::TransferJob *job = KIO::get(new_url, NoReload, HideProgressInfo);
00178         d->connectTransferJob(job);
00179 
00180         d->eventLoop.exec();
00181     }
00182 }
00183 
00184 void ForwardingSlaveBase::put(const KUrl &url, int permissions,
00185                               JobFlags flags)
00186 {
00187     kDebug() << "ForwardingSlaveBase::put: " << url;
00188 
00189     KUrl new_url;
00190     if ( d->internalRewriteUrl(url, new_url) )
00191     {
00192         KIO::TransferJob *job = KIO::put(new_url, permissions,
00193                                          flags | HideProgressInfo);
00194         d->connectTransferJob(job);
00195 
00196         d->eventLoop.exec();
00197     }
00198 }
00199 
00200 void ForwardingSlaveBase::stat(const KUrl &url)
00201 {
00202     kDebug() << "ForwardingSlaveBase::stat: " << url;
00203 
00204     KUrl new_url;
00205     if ( d->internalRewriteUrl(url, new_url) )
00206     {
00207         KIO::SimpleJob *job = KIO::stat(new_url, KIO::HideProgressInfo);
00208         d->connectSimpleJob(job);
00209 
00210         d->eventLoop.exec();
00211     }
00212 }
00213 
00214 void ForwardingSlaveBase::mimetype(const KUrl &url)
00215 {
00216     kDebug() << "ForwardingSlaveBase::mimetype: " << url;
00217 
00218     KUrl new_url;
00219     if ( d->internalRewriteUrl(url, new_url) )
00220     {
00221         KIO::TransferJob *job = KIO::mimetype(new_url, KIO::HideProgressInfo);
00222         d->connectTransferJob(job);
00223 
00224         d->eventLoop.exec();
00225     }
00226 }
00227 
00228 void ForwardingSlaveBase::listDir(const KUrl &url)
00229 {
00230     kDebug() << "ForwardingSlaveBase::listDir: " << url;
00231 
00232     KUrl new_url;
00233     if ( d->internalRewriteUrl(url, new_url) )
00234     {
00235         KIO::ListJob *job = KIO::listDir(new_url, KIO::HideProgressInfo);
00236         d->connectListJob(job);
00237 
00238         d->eventLoop.exec();
00239     }
00240 }
00241 
00242 void ForwardingSlaveBase::mkdir(const KUrl &url, int permissions)
00243 {
00244     kDebug() << "ForwardingSlaveBase::mkdir: " << url;
00245 
00246     KUrl new_url;
00247     if ( d->internalRewriteUrl(url, new_url) )
00248     {
00249         KIO::SimpleJob *job = KIO::mkdir(new_url, permissions);
00250         d->connectSimpleJob(job);
00251 
00252         d->eventLoop.exec();
00253     }
00254 }
00255 
00256 void ForwardingSlaveBase::rename(const KUrl &src, const KUrl &dest,
00257                                  JobFlags flags)
00258 {
00259     kDebug() << "ForwardingSlaveBase::rename: " << src << ", " << dest;
00260 
00261     KUrl new_src, new_dest;
00262     if ( d->internalRewriteUrl(src, new_src) && d->internalRewriteUrl(dest, new_dest) )
00263     {
00264         KIO::Job *job = KIO::rename(new_src, new_dest, flags);
00265         d->connectJob(job);
00266 
00267         d->eventLoop.exec();
00268     }
00269 }
00270 
00271 void ForwardingSlaveBase::symlink(const QString &target, const KUrl &dest,
00272                                   JobFlags flags)
00273 {
00274     kDebug() << "ForwardingSlaveBase::symlink: " << target << ", " << dest;
00275 
00276     KUrl new_dest;
00277     if ( d->internalRewriteUrl(dest, new_dest) )
00278     {
00279         KIO::SimpleJob *job = KIO::symlink(target, new_dest, flags & HideProgressInfo);
00280         d->connectSimpleJob(job);
00281 
00282         d->eventLoop.exec();
00283     }
00284 }
00285 
00286 void ForwardingSlaveBase::chmod(const KUrl &url, int permissions)
00287 {
00288     kDebug() << "ForwardingSlaveBase::chmod: " << url;
00289 
00290     KUrl new_url;
00291     if ( d->internalRewriteUrl(url, new_url) )
00292     {
00293         KIO::SimpleJob *job = KIO::chmod(new_url, permissions);
00294         d->connectSimpleJob(job);
00295 
00296         d->eventLoop.exec();
00297     }
00298 }
00299 
00300 void ForwardingSlaveBase::setModificationTime(const KUrl& url, const QDateTime& mtime)
00301 {
00302     kDebug() << "ForwardingSlaveBase::setModificationTime: " << url;
00303 
00304     KUrl new_url;
00305     if ( d->internalRewriteUrl(url, new_url) )
00306     {
00307         KIO::SimpleJob *job = KIO::setModificationTime(new_url, mtime);
00308         d->connectSimpleJob(job);
00309 
00310         d->eventLoop.exec();
00311     }
00312 }
00313 
00314 void ForwardingSlaveBase::copy(const KUrl &src, const KUrl &dest,
00315                                int permissions, JobFlags flags)
00316 {
00317     kDebug() << "ForwardingSlaveBase::copy: " << src << ", " << dest;
00318 
00319     KUrl new_src, new_dest;
00320     if ( d->internalRewriteUrl(src, new_src) && d->internalRewriteUrl(dest, new_dest) )
00321     {
00322       // Are you sure you want to display here a ProgressInfo ???
00323         KIO::Job *job = KIO::file_copy(new_src, new_dest, permissions,
00324                                        (flags & (~Overwrite) & (~HideProgressInfo)) );
00325         d->connectJob(job);
00326 
00327         d->eventLoop.exec();
00328     }
00329 }
00330 
00331 void ForwardingSlaveBase::del(const KUrl &url, bool isfile)
00332 {
00333     kDebug() << "ForwardingSlaveBase::del: " << url;
00334 
00335     KUrl new_url;
00336     if ( d->internalRewriteUrl(url, new_url) )
00337     {
00338         if (isfile)
00339         {
00340             KIO::DeleteJob *job = KIO::del(new_url, HideProgressInfo);
00341             d->connectJob(job);
00342         }
00343         else
00344         {
00345             KIO::SimpleJob *job = KIO::rmdir(new_url);
00346             d->connectSimpleJob(job);
00347         }
00348 
00349         d->eventLoop.exec();
00350     }
00351 }
00352 
00353 
00355 
00356 void ForwardingSlaveBasePrivate::connectJob(KIO::Job *job)
00357 {
00358     // We will forward the warning message, no need to let the job
00359     // display it itself
00360     job->setUiDelegate( 0 );
00361 
00362     // Forward metadata (e.g. modification time for put())
00363     job->setMetaData( q->allMetaData() );
00364 #if 0 // debug code
00365     kDebug() << "transferring metadata:";
00366     const MetaData md = allMetaData();
00367     for ( MetaData::const_iterator it = md.begin(); it != md.end(); ++it )
00368         kDebug() << it.key() << " = " << it.data();
00369 #endif
00370 
00371     q->connect( job, SIGNAL( result(KJob *) ),
00372                 SLOT( _k_slotResult(KJob *) ) );
00373     q->connect( job, SIGNAL( warning(KJob *, const QString &, const QString &) ),
00374                 SLOT( _k_slotWarning(KJob *, const QString &) ) );
00375     q->connect( job, SIGNAL( infoMessage(KJob *, const QString &, const QString &) ),
00376                 SLOT( _k_slotInfoMessage(KJob *, const QString &) ) );
00377     q->connect( job, SIGNAL( totalSize(KJob *, qulonglong) ),
00378                 SLOT( _k_slotTotalSize(KJob *, qulonglong) ) );
00379     q->connect( job, SIGNAL( processedSize(KJob *, qulonglong) ),
00380                 SLOT( _k_slotProcessedSize(KJob *, qulonglong) ) );
00381     q->connect( job, SIGNAL( speed(KJob *, unsigned long) ),
00382                 SLOT( _k_slotSpeed(KJob *, unsigned long) ) );
00383 }
00384 
00385 void ForwardingSlaveBasePrivate::connectSimpleJob(KIO::SimpleJob *job)
00386 {
00387     connectJob(job);
00388     q->connect( job, SIGNAL( redirection(KIO::Job *, const KUrl &) ),
00389                 SLOT( _k_slotRedirection(KIO::Job *, const KUrl &) ) );
00390 }
00391 
00392 void ForwardingSlaveBasePrivate::connectListJob(KIO::ListJob *job)
00393 {
00394     connectSimpleJob(job);
00395     q->connect( job, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList &) ),
00396                 SLOT( _k_slotEntries(KIO::Job *, const KIO::UDSEntryList &) ) );
00397 }
00398 
00399 void ForwardingSlaveBasePrivate::connectTransferJob(KIO::TransferJob *job)
00400 {
00401     connectSimpleJob(job);
00402     q->connect( job, SIGNAL( data(KIO::Job *, const QByteArray &) ),
00403                 SLOT( _k_slotData(KIO::Job *, const QByteArray &) ) );
00404     q->connect( job, SIGNAL( dataReq(KIO::Job *, QByteArray &) ),
00405                 SLOT( _k_slotDataReq(KIO::Job *, QByteArray &) ) );
00406     q->connect( job, SIGNAL( mimetype(KIO::Job *, const QString &) ),
00407                 SLOT( _k_slotMimetype(KIO::Job *, const QString &) ) );
00408     q->connect( job, SIGNAL( canResume(KIO::Job *, KIO::filesize_t) ),
00409                 SLOT( _k_slotCanResume(KIO::Job *, KIO::filesize_t) ) );
00410 }
00411 
00413 
00414 void ForwardingSlaveBasePrivate::_k_slotResult(KJob *job)
00415 {
00416     if ( job->error() != 0)
00417     {
00418         q->error( job->error(), job->errorText() );
00419     }
00420     else
00421     {
00422         KIO::StatJob *stat_job = qobject_cast<KIO::StatJob *>(job);
00423         if ( stat_job!=0L )
00424         {
00425             KIO::UDSEntry entry = stat_job->statResult();
00426         q->prepareUDSEntry(entry);
00427             q->statEntry( entry );
00428         }
00429         q->finished();
00430     }
00431 
00432     eventLoop.exit();
00433 }
00434 
00435 void ForwardingSlaveBasePrivate::_k_slotWarning(KJob* /*job*/, const QString &msg)
00436 {
00437     q->warning(msg);
00438 }
00439 
00440 void ForwardingSlaveBasePrivate::_k_slotInfoMessage(KJob* /*job*/, const QString &msg)
00441 {
00442     q->infoMessage(msg);
00443 }
00444 
00445 void ForwardingSlaveBasePrivate::_k_slotTotalSize(KJob* /*job*/, qulonglong size)
00446 {
00447     q->totalSize(size);
00448 }
00449 
00450 void ForwardingSlaveBasePrivate::_k_slotProcessedSize(KJob* /*job*/, qulonglong size)
00451 {
00452     q->processedSize(size);
00453 }
00454 
00455 void ForwardingSlaveBasePrivate::_k_slotSpeed(KJob* /*job*/, unsigned long bytesPerSecond)
00456 {
00457     q->speed(bytesPerSecond);
00458 }
00459 
00460 void ForwardingSlaveBasePrivate::_k_slotRedirection(KIO::Job *job, const KUrl &url)
00461 {
00462     q->redirection(url);
00463 
00464     // We've been redirected stop everything.
00465     job->kill( KJob::Quietly );
00466     q->finished();
00467 
00468     eventLoop.exit();
00469 }
00470 
00471 void ForwardingSlaveBasePrivate::_k_slotEntries(KIO::Job* /*job*/,
00472                                       const KIO::UDSEntryList &entries)
00473 {
00474     KIO::UDSEntryList final_entries = entries;
00475 
00476     KIO::UDSEntryList::iterator it = final_entries.begin();
00477     const KIO::UDSEntryList::iterator end = final_entries.end();
00478 
00479     for(; it!=end; ++it)
00480     {
00481         q->prepareUDSEntry(*it, true);
00482     }
00483 
00484     q->listEntries( final_entries );
00485 }
00486 
00487 void ForwardingSlaveBasePrivate::_k_slotData(KIO::Job* /*job*/, const QByteArray &_data)
00488 {
00489     q->data(_data);
00490 }
00491 
00492 void ForwardingSlaveBasePrivate::_k_slotDataReq(KIO::Job* /*job*/, QByteArray &data)
00493 {
00494     q->dataReq();
00495     q->readData(data);
00496 }
00497 
00498 void ForwardingSlaveBasePrivate::_k_slotMimetype (KIO::Job* /*job*/, const QString &type)
00499 {
00500     q->mimeType(type);
00501 }
00502 
00503 void ForwardingSlaveBasePrivate::_k_slotCanResume (KIO::Job* /*job*/, KIO::filesize_t offset)
00504 {
00505     q->canResume(offset);
00506 }
00507 
00508 }
00509 
00510 #include "forwardingslavebase.moc"
00511 

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal