• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • 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 <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 url_found = false;
00073     QString name;
00074     KURL url;
00075 
00076     KIO::UDSEntry::iterator it = entry.begin();
00077     KIO::UDSEntry::iterator end = entry.end();
00078 
00079     for(; it!=end; ++it)
00080     {
00081         KURL new_url = m_requestedURL;
00082 
00083         switch( (*it).m_uds )
00084         {
00085         case KIO::UDS_NAME:
00086             name = (*it).m_str;
00087             kdDebug() << "Name = " << name << endl;
00088         break;
00089         case KIO::UDS_URL:
00090             url_found = true;
00091             url = (*it).m_str;
00092         if (listing)
00093             {
00094                 new_url.addPath(url.fileName());
00095             }
00096             (*it).m_str = new_url.url();
00097             kdDebug() << "URL = " << url << endl;
00098             kdDebug() << "New URL = " << (*it).m_str << endl;
00099             break;
00100         }
00101     }
00102 
00103     if ( m_processedURL.isLocalFile() )
00104     {
00105         KURL new_url = m_processedURL;
00106         if (listing)
00107         {
00108             new_url.addPath( name );
00109         }
00110 
00111         KIO::UDSAtom atom;
00112         atom.m_uds = KIO::UDS_LOCAL_PATH;
00113         atom.m_long = 0;
00114         atom.m_str = new_url.path();
00115         entry.append(atom);
00116     }
00117 }
00118 
00119 void ForwardingSlaveBase::get(const KURL &url)
00120 {
00121     kdDebug() << "ForwardingSlaveBase::get: " << url << endl;
00122 
00123     KURL new_url;
00124     if ( internalRewriteURL(url, new_url) )
00125     {
00126         KIO::TransferJob *job = KIO::get(new_url, false, false);
00127         connectTransferJob(job);
00128 
00129         qApp->eventLoop()->enterLoop();
00130     }
00131 }
00132 
00133 void ForwardingSlaveBase::put(const KURL &url, int permissions,
00134                               bool overwrite, bool resume )
00135 {
00136     kdDebug() << "ForwardingSlaveBase::put: " << url << endl;
00137 
00138     KURL new_url;
00139     if ( internalRewriteURL(url, new_url) )
00140     {
00141         KIO::TransferJob *job = KIO::put(new_url, permissions, overwrite,
00142                                          resume, false);
00143         connectTransferJob(job);
00144 
00145         qApp->eventLoop()->enterLoop();
00146     }
00147 }
00148 
00149 void ForwardingSlaveBase::stat(const KURL &url)
00150 {
00151     kdDebug() << "ForwardingSlaveBase::stat: " << url << endl;
00152 
00153     KURL new_url;
00154     if ( internalRewriteURL(url, new_url) )
00155     {
00156         KIO::SimpleJob *job = KIO::stat(new_url, false);
00157         connectSimpleJob(job);
00158 
00159         qApp->eventLoop()->enterLoop();
00160     }
00161 }
00162 
00163 void ForwardingSlaveBase::mimetype(const KURL &url)
00164 {
00165     kdDebug() << "ForwardingSlaveBase::mimetype: " << url << endl;
00166 
00167     KURL new_url;
00168     if ( internalRewriteURL(url, new_url) )
00169     {
00170         KIO::TransferJob *job = KIO::mimetype(new_url, false);
00171         connectTransferJob(job);
00172 
00173         qApp->eventLoop()->enterLoop();
00174     }
00175 }
00176 
00177 void ForwardingSlaveBase::listDir(const KURL &url)
00178 {
00179     kdDebug() << "ForwardingSlaveBase::listDir: " << url << endl;
00180 
00181     KURL new_url;
00182     if ( internalRewriteURL(url, new_url) )
00183     {
00184         KIO::ListJob *job = KIO::listDir(new_url, false);
00185         connectListJob(job);
00186 
00187         qApp->eventLoop()->enterLoop();
00188     }
00189 }
00190 
00191 void ForwardingSlaveBase::mkdir(const KURL &url, int permissions)
00192 {
00193     kdDebug() << "ForwardingSlaveBase::mkdir: " << url << endl;
00194 
00195     KURL new_url;
00196     if ( internalRewriteURL(url, new_url) )
00197     {
00198         KIO::SimpleJob *job = KIO::mkdir(new_url, permissions);
00199         connectSimpleJob(job);
00200 
00201         qApp->eventLoop()->enterLoop();
00202     }
00203 }
00204 
00205 void ForwardingSlaveBase::rename(const KURL &src, const KURL &dest,
00206                                  bool overwrite)
00207 {
00208     kdDebug() << "ForwardingSlaveBase::rename: " << src << ", " << dest << endl;
00209 
00210     KURL new_src, new_dest;
00211     if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) )
00212     {
00213         KIO::Job *job = KIO::rename(new_src, new_dest, overwrite);
00214         connectJob(job);
00215 
00216         qApp->eventLoop()->enterLoop();
00217     }
00218 }
00219 
00220 void ForwardingSlaveBase::symlink(const QString &target, const KURL &dest,
00221                                   bool overwrite)
00222 {
00223     kdDebug() << "ForwardingSlaveBase::symlink: " << target << ", " << dest << endl;
00224 
00225     KURL new_dest;
00226     if ( internalRewriteURL(dest, new_dest) )
00227     {
00228         KIO::SimpleJob *job = KIO::symlink(target, new_dest, overwrite, false);
00229         connectSimpleJob(job);
00230 
00231         qApp->eventLoop()->enterLoop();
00232     }
00233 }
00234 
00235 void ForwardingSlaveBase::chmod(const KURL &url, int permissions)
00236 {
00237     kdDebug() << "ForwardingSlaveBase::chmod: " << url << endl;
00238 
00239     KURL new_url;
00240     if ( internalRewriteURL(url, new_url) )
00241     {
00242         KIO::SimpleJob *job = KIO::chmod(new_url, permissions);
00243         connectSimpleJob(job);
00244 
00245         qApp->eventLoop()->enterLoop();
00246     }
00247 }
00248 
00249 void ForwardingSlaveBase::copy(const KURL &src, const KURL &dest,
00250                                int permissions, bool overwrite)
00251 {
00252     kdDebug() << "ForwardingSlaveBase::copy: " << src << ", " << dest << endl;
00253 
00254     KURL new_src, new_dest;
00255     if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) )
00256     {
00257         KIO::Job *job = KIO::file_copy(new_src, new_dest, permissions,
00258                                        overwrite, false);
00259         connectJob(job);
00260 
00261         qApp->eventLoop()->enterLoop();
00262     }
00263 }
00264 
00265 void ForwardingSlaveBase::del(const KURL &url, bool isfile)
00266 {
00267     kdDebug() << "ForwardingSlaveBase::del: " << url << endl;
00268 
00269     KURL new_url;
00270     if ( internalRewriteURL(url, new_url) )
00271     {
00272         if (isfile)
00273         {
00274             KIO::DeleteJob *job = KIO::del(new_url, false, false);
00275             connectJob(job);
00276         }
00277         else
00278         {
00279             KIO::SimpleJob *job = KIO::rmdir(new_url);
00280             connectSimpleJob(job);
00281         }
00282 
00283         qApp->eventLoop()->enterLoop();
00284     }
00285 }
00286 
00287 
00289 
00290 void ForwardingSlaveBase::connectJob(KIO::Job *job)
00291 {
00292     // We will forward the warning message, no need to let the job
00293     // display it itself
00294     job->setInteractive(false);
00295 
00296     // Forward metadata (e.g. modification time for put())
00297     job->setMetaData( allMetaData() );
00298 #if 0 // debug code
00299     kdDebug() << k_funcinfo << "transferring metadata:" << endl;
00300     const MetaData md = allMetaData();
00301     for ( MetaData::const_iterator it = md.begin(); it != md.end(); ++it )
00302         kdDebug() << it.key() << " = " << it.data() << endl;
00303 #endif
00304 
00305     connect( job, SIGNAL( result(KIO::Job *) ),
00306              this, SLOT( slotResult(KIO::Job *) ) );
00307     connect( job, SIGNAL( warning(KIO::Job *, const QString &) ),
00308              this, SLOT( slotWarning(KIO::Job *, const QString &) ) );
00309     connect( job, SIGNAL( infoMessage(KIO::Job *, const QString &) ),
00310              this, SLOT( slotInfoMessage(KIO::Job *, const QString &) ) );
00311     connect( job, SIGNAL( totalSize(KIO::Job *, KIO::filesize_t) ),
00312              this, SLOT( slotTotalSize(KIO::Job *, KIO::filesize_t) ) );
00313     connect( job, SIGNAL( processedSize(KIO::Job *, KIO::filesize_t) ),
00314              this, SLOT( slotProcessedSize(KIO::Job *, KIO::filesize_t) ) );
00315     connect( job, SIGNAL( speed(KIO::Job *, unsigned long) ),
00316              this, SLOT( slotSpeed(KIO::Job *, unsigned long) ) );
00317 }
00318 
00319 void ForwardingSlaveBase::connectSimpleJob(KIO::SimpleJob *job)
00320 {
00321     connectJob(job);
00322     connect( job, SIGNAL( redirection(KIO::Job *, const KURL &) ),
00323              this, SLOT( slotRedirection(KIO::Job *, const KURL &) ) );
00324 }
00325 
00326 void ForwardingSlaveBase::connectListJob(KIO::ListJob *job)
00327 {
00328     connectSimpleJob(job);
00329     connect( job, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList &) ),
00330              this, SLOT( slotEntries(KIO::Job *, const KIO::UDSEntryList &) ) );
00331 }
00332 
00333 void ForwardingSlaveBase::connectTransferJob(KIO::TransferJob *job)
00334 {
00335     connectSimpleJob(job);
00336     connect( job, SIGNAL( data(KIO::Job *, const QByteArray &) ),
00337              this, SLOT( slotData(KIO::Job *, const QByteArray &) ) );
00338     connect( job, SIGNAL( dataReq(KIO::Job *, QByteArray &) ),
00339              this, SLOT( slotDataReq(KIO::Job *, QByteArray &) ) );
00340     connect( job, SIGNAL( mimetype(KIO::Job *, const QString &) ),
00341              this, SLOT( slotMimetype(KIO::Job *, const QString &) ) );
00342     connect( job, SIGNAL( canResume(KIO::Job *, KIO::filesize_t) ),
00343              this, SLOT( slotCanResume(KIO::Job *, KIO::filesize_t) ) );
00344 }
00345 
00347 
00348 void ForwardingSlaveBase::slotResult(KIO::Job *job)
00349 {
00350     if ( job->error() != 0)
00351     {
00352         error( job->error(), job->errorText() );
00353     }
00354     else
00355     {
00356         KIO::StatJob *stat_job = dynamic_cast<KIO::StatJob *>(job);
00357         if ( stat_job!=0L )
00358         {
00359             KIO::UDSEntry entry = stat_job->statResult();
00360         prepareUDSEntry(entry);
00361             statEntry( entry );
00362         }
00363         finished();
00364     }
00365 
00366     qApp->eventLoop()->exitLoop();
00367 }
00368 
00369 void ForwardingSlaveBase::slotWarning(KIO::Job* /*job*/, const QString &msg)
00370 {
00371     warning(msg);
00372 }
00373 
00374 void ForwardingSlaveBase::slotInfoMessage(KIO::Job* /*job*/, const QString &msg)
00375 {
00376     infoMessage(msg);
00377 }
00378 
00379 void ForwardingSlaveBase::slotTotalSize(KIO::Job* /*job*/, KIO::filesize_t size)
00380 {
00381     totalSize(size);
00382 }
00383 
00384 void ForwardingSlaveBase::slotProcessedSize(KIO::Job* /*job*/, KIO::filesize_t size)
00385 {
00386     processedSize(size);
00387 }
00388 
00389 void ForwardingSlaveBase::slotSpeed(KIO::Job* /*job*/, unsigned long bytesPerSecond)
00390 {
00391     speed(bytesPerSecond);
00392 }
00393 
00394 void ForwardingSlaveBase::slotRedirection(KIO::Job *job, const KURL &url)
00395 {
00396     redirection(url);
00397 
00398     // We've been redirected stop everything.
00399     job->kill( true );
00400     finished();
00401 
00402     qApp->eventLoop()->exitLoop();
00403 }
00404 
00405 void ForwardingSlaveBase::slotEntries(KIO::Job* /*job*/,
00406                                       const KIO::UDSEntryList &entries)
00407 {
00408     KIO::UDSEntryList final_entries = entries;
00409 
00410     KIO::UDSEntryList::iterator it = final_entries.begin();
00411     KIO::UDSEntryList::iterator end = final_entries.end();
00412 
00413     for(; it!=end; ++it)
00414     {
00415         prepareUDSEntry(*it, true);
00416     }
00417 
00418     listEntries( final_entries );
00419 }
00420 
00421 void ForwardingSlaveBase::slotData(KIO::Job* /*job*/, const QByteArray &d)
00422 {
00423     data(d);
00424 }
00425 
00426 void ForwardingSlaveBase::slotDataReq(KIO::Job* /*job*/, QByteArray &data)
00427 {
00428     dataReq();
00429     readData(data);
00430 }
00431 
00432 void ForwardingSlaveBase::slotMimetype (KIO::Job* /*job*/, const QString &type)
00433 {
00434     mimeType(type);
00435 }
00436 
00437 void ForwardingSlaveBase::slotCanResume (KIO::Job* /*job*/, KIO::filesize_t offset)
00438 {
00439     canResume(offset);
00440 }
00441 
00442 }
00443 
00444 #include "forwardingslavebase.moc"
00445 

kio

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
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