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 <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_requestedURL;
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 ( m_processedURL.isLocalFile() )
00136     {
00137         KURL new_url = m_processedURL;
00138         if (listing)
00139         {
00140             new_url.addPath( name );
00141         }
00142 
00143         KIO::UDSAtom atom;
00144         atom.m_uds = KIO::UDS_LOCAL_PATH;
00145         atom.m_long = 0;
00146         atom.m_str = new_url.path();
00147         entry.append(atom);
00148     }
00149 }
00150 
00151 void ForwardingSlaveBase::get(const KURL &url)
00152 {
00153     kdDebug() << "ForwardingSlaveBase::get: " << url << endl;
00154 
00155     KURL new_url;
00156     if ( internalRewriteURL(url, new_url) )
00157     {
00158         KIO::TransferJob *job = KIO::get(new_url, false, false);
00159         connectTransferJob(job);
00160 
00161         qApp->eventLoop()->enterLoop();
00162     }
00163 }
00164 
00165 void ForwardingSlaveBase::put(const KURL &url, int permissions,
00166                               bool overwrite, bool resume )
00167 {
00168     kdDebug() << "ForwardingSlaveBase::put: " << url << endl;
00169 
00170     KURL new_url;
00171     if ( internalRewriteURL(url, new_url) )
00172     {
00173         KIO::TransferJob *job = KIO::put(new_url, permissions, overwrite,
00174                                          resume, false);
00175         connectTransferJob(job);
00176 
00177         qApp->eventLoop()->enterLoop();
00178     }
00179 }
00180 
00181 void ForwardingSlaveBase::stat(const KURL &url)
00182 {
00183     kdDebug() << "ForwardingSlaveBase::stat: " << url << endl;
00184 
00185     KURL new_url;
00186     if ( internalRewriteURL(url, new_url) )
00187     {
00188         KIO::SimpleJob *job = KIO::stat(new_url, false);
00189         connectSimpleJob(job);
00190 
00191         qApp->eventLoop()->enterLoop();
00192     }
00193 }
00194 
00195 void ForwardingSlaveBase::mimetype(const KURL &url)
00196 {
00197     kdDebug() << "ForwardingSlaveBase::mimetype: " << url << endl;
00198 
00199     KURL new_url;
00200     if ( internalRewriteURL(url, new_url) )
00201     {
00202         KIO::TransferJob *job = KIO::mimetype(new_url, false);
00203         connectTransferJob(job);
00204 
00205         qApp->eventLoop()->enterLoop();
00206     }
00207 }
00208 
00209 void ForwardingSlaveBase::listDir(const KURL &url)
00210 {
00211     kdDebug() << "ForwardingSlaveBase::listDir: " << url << endl;
00212 
00213     KURL new_url;
00214     if ( internalRewriteURL(url, new_url) )
00215     {
00216         KIO::ListJob *job = KIO::listDir(new_url, false);
00217         connectListJob(job);
00218 
00219         qApp->eventLoop()->enterLoop();
00220     }
00221 }
00222 
00223 void ForwardingSlaveBase::mkdir(const KURL &url, int permissions)
00224 {
00225     kdDebug() << "ForwardingSlaveBase::mkdir: " << url << endl;
00226 
00227     KURL new_url;
00228     if ( internalRewriteURL(url, new_url) )
00229     {
00230         KIO::SimpleJob *job = KIO::mkdir(new_url, permissions);
00231         connectSimpleJob(job);
00232 
00233         qApp->eventLoop()->enterLoop();
00234     }
00235 }
00236 
00237 void ForwardingSlaveBase::rename(const KURL &src, const KURL &dest,
00238                                  bool overwrite)
00239 {
00240     kdDebug() << "ForwardingSlaveBase::rename: " << src << ", " << dest << endl;
00241 
00242     KURL new_src, new_dest;
00243     if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) )
00244     {
00245         KIO::Job *job = KIO::rename(new_src, new_dest, overwrite);
00246         connectJob(job);
00247 
00248         qApp->eventLoop()->enterLoop();
00249     }
00250 }
00251 
00252 void ForwardingSlaveBase::symlink(const QString &target, const KURL &dest,
00253                                   bool overwrite)
00254 {
00255     kdDebug() << "ForwardingSlaveBase::symlink: " << target << ", " << dest << endl;
00256 
00257     KURL new_dest;
00258     if ( internalRewriteURL(dest, new_dest) )
00259     {
00260         KIO::SimpleJob *job = KIO::symlink(target, new_dest, overwrite, false);
00261         connectSimpleJob(job);
00262 
00263         qApp->eventLoop()->enterLoop();
00264     }
00265 }
00266 
00267 void ForwardingSlaveBase::chmod(const KURL &url, int permissions)
00268 {
00269     kdDebug() << "ForwardingSlaveBase::chmod: " << url << endl;
00270 
00271     KURL new_url;
00272     if ( internalRewriteURL(url, new_url) )
00273     {
00274         KIO::SimpleJob *job = KIO::chmod(new_url, permissions);
00275         connectSimpleJob(job);
00276 
00277         qApp->eventLoop()->enterLoop();
00278     }
00279 }
00280 
00281 void ForwardingSlaveBase::copy(const KURL &src, const KURL &dest,
00282                                int permissions, bool overwrite)
00283 {
00284     kdDebug() << "ForwardingSlaveBase::copy: " << src << ", " << dest << endl;
00285 
00286     KURL new_src, new_dest;
00287     if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) )
00288     {
00289         KIO::Job *job = KIO::file_copy(new_src, new_dest, permissions,
00290                                        overwrite, false);
00291         connectJob(job);
00292 
00293         qApp->eventLoop()->enterLoop();
00294     }
00295 }
00296 
00297 void ForwardingSlaveBase::del(const KURL &url, bool isfile)
00298 {
00299     kdDebug() << "ForwardingSlaveBase::del: " << url << endl;
00300 
00301     KURL new_url;
00302     if ( internalRewriteURL(url, new_url) )
00303     {
00304         if (isfile)
00305         {
00306             KIO::DeleteJob *job = KIO::del(new_url, false, false);
00307             connectJob(job);
00308         }
00309         else
00310         {
00311             KIO::SimpleJob *job = KIO::rmdir(new_url);
00312             connectSimpleJob(job);
00313         }
00314 
00315         qApp->eventLoop()->enterLoop();
00316     }
00317 }
00318 
00319 
00321 
00322 void ForwardingSlaveBase::connectJob(KIO::Job *job)
00323 {
00324     // We will forward the warning message, no need to let the job
00325     // display it itself
00326     job->setInteractive(false);
00327 
00328     // Forward metadata (e.g. modification time for put())
00329     job->setMetaData( allMetaData() );
00330 #if 0 // debug code
00331     kdDebug() << k_funcinfo << "transferring metadata:" << endl;
00332     const MetaData md = allMetaData();
00333     for ( MetaData::const_iterator it = md.begin(); it != md.end(); ++it )
00334         kdDebug() << it.key() << " = " << it.data() << endl;
00335 #endif
00336 
00337     connect( job, SIGNAL( result(KIO::Job *) ),
00338              this, SLOT( slotResult(KIO::Job *) ) );
00339     connect( job, SIGNAL( warning(KIO::Job *, const QString &) ),
00340              this, SLOT( slotWarning(KIO::Job *, const QString &) ) );
00341     connect( job, SIGNAL( infoMessage(KIO::Job *, const QString &) ),
00342              this, SLOT( slotInfoMessage(KIO::Job *, const QString &) ) );
00343     connect( job, SIGNAL( totalSize(KIO::Job *, KIO::filesize_t) ),
00344              this, SLOT( slotTotalSize(KIO::Job *, KIO::filesize_t) ) );
00345     connect( job, SIGNAL( processedSize(KIO::Job *, KIO::filesize_t) ),
00346              this, SLOT( slotProcessedSize(KIO::Job *, KIO::filesize_t) ) );
00347     connect( job, SIGNAL( speed(KIO::Job *, unsigned long) ),
00348              this, SLOT( slotSpeed(KIO::Job *, unsigned long) ) );
00349 }
00350 
00351 void ForwardingSlaveBase::connectSimpleJob(KIO::SimpleJob *job)
00352 {
00353     connectJob(job);
00354     connect( job, SIGNAL( redirection(KIO::Job *, const KURL &) ),
00355              this, SLOT( slotRedirection(KIO::Job *, const KURL &) ) );
00356 }
00357 
00358 void ForwardingSlaveBase::connectListJob(KIO::ListJob *job)
00359 {
00360     connectSimpleJob(job);
00361     connect( job, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList &) ),
00362              this, SLOT( slotEntries(KIO::Job *, const KIO::UDSEntryList &) ) );
00363 }
00364 
00365 void ForwardingSlaveBase::connectTransferJob(KIO::TransferJob *job)
00366 {
00367     connectSimpleJob(job);
00368     connect( job, SIGNAL( data(KIO::Job *, const QByteArray &) ),
00369              this, SLOT( slotData(KIO::Job *, const QByteArray &) ) );
00370     connect( job, SIGNAL( dataReq(KIO::Job *, QByteArray &) ),
00371              this, SLOT( slotDataReq(KIO::Job *, QByteArray &) ) );
00372     connect( job, SIGNAL( mimetype(KIO::Job *, const QString &) ),
00373              this, SLOT( slotMimetype(KIO::Job *, const QString &) ) );
00374     connect( job, SIGNAL( canResume(KIO::Job *, KIO::filesize_t) ),
00375              this, SLOT( slotCanResume(KIO::Job *, KIO::filesize_t) ) );
00376 }
00377 
00379 
00380 void ForwardingSlaveBase::slotResult(KIO::Job *job)
00381 {
00382     if ( job->error() != 0)
00383     {
00384         error( job->error(), job->errorText() );
00385     }
00386     else
00387     {
00388         KIO::StatJob *stat_job = dynamic_cast<KIO::StatJob *>(job);
00389         if ( stat_job!=0L )
00390         {
00391             KIO::UDSEntry entry = stat_job->statResult();
00392         prepareUDSEntry(entry);
00393             statEntry( entry );
00394         }
00395         finished();
00396     }
00397 
00398     qApp->eventLoop()->exitLoop();
00399 }
00400 
00401 void ForwardingSlaveBase::slotWarning(KIO::Job* /*job*/, const QString &msg)
00402 {
00403     warning(msg);
00404 }
00405 
00406 void ForwardingSlaveBase::slotInfoMessage(KIO::Job* /*job*/, const QString &msg)
00407 {
00408     infoMessage(msg);
00409 }
00410 
00411 void ForwardingSlaveBase::slotTotalSize(KIO::Job* /*job*/, KIO::filesize_t size)
00412 {
00413     totalSize(size);
00414 }
00415 
00416 void ForwardingSlaveBase::slotProcessedSize(KIO::Job* /*job*/, KIO::filesize_t size)
00417 {
00418     processedSize(size);
00419 }
00420 
00421 void ForwardingSlaveBase::slotSpeed(KIO::Job* /*job*/, unsigned long bytesPerSecond)
00422 {
00423     speed(bytesPerSecond);
00424 }
00425 
00426 void ForwardingSlaveBase::slotRedirection(KIO::Job *job, const KURL &url)
00427 {
00428     redirection(url);
00429 
00430     // We've been redirected stop everything.
00431     job->kill( true );
00432     finished();
00433 
00434     qApp->eventLoop()->exitLoop();
00435 }
00436 
00437 void ForwardingSlaveBase::slotEntries(KIO::Job* /*job*/,
00438                                       const KIO::UDSEntryList &entries)
00439 {
00440     KIO::UDSEntryList final_entries = entries;
00441 
00442     KIO::UDSEntryList::iterator it = final_entries.begin();
00443     KIO::UDSEntryList::iterator end = final_entries.end();
00444 
00445     for(; it!=end; ++it)
00446     {
00447         prepareUDSEntry(*it, true);
00448     }
00449 
00450     listEntries( final_entries );
00451 }
00452 
00453 void ForwardingSlaveBase::slotData(KIO::Job* /*job*/, const QByteArray &d)
00454 {
00455     data(d);
00456 }
00457 
00458 void ForwardingSlaveBase::slotDataReq(KIO::Job* /*job*/, QByteArray &data)
00459 {
00460     dataReq();
00461     readData(data);
00462 }
00463 
00464 void ForwardingSlaveBase::slotMimetype (KIO::Job* /*job*/, const QString &type)
00465 {
00466     mimeType(type);
00467 }
00468 
00469 void ForwardingSlaveBase::slotCanResume (KIO::Job* /*job*/, KIO::filesize_t offset)
00470 {
00471     canResume(offset);
00472 }
00473 
00474 }
00475 
00476 #include "forwardingslavebase.moc"
00477 
KDE Home | KDE Accessibility Home | Description of Access Keys