kio Library API Documentation

netaccess.cpp

00001 /*  $Id: netaccess.cpp 339765 2004-08-22 13:36:31Z zack $
00002 
00003     This file is part of the KDE libraries
00004     Copyright (C) 1997 Torben Weis (weis@kde.org)
00005     Copyright (C) 1998 Matthias Ettrich (ettrich@kde.org)
00006     Copyright (C) 1999 David Faure (faure@kde.org)
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021     Boston, MA 02111-1307, USA.
00022 */
00023 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <signal.h>
00027 #include <unistd.h>
00028 
00029 #include <cstring>
00030 
00031 #include <qstring.h>
00032 #include <qapplication.h>
00033 #include <qfile.h>
00034 #include <qmetaobject.h>
00035 
00036 #include <kapplication.h>
00037 #include <klocale.h>
00038 #include <ktempfile.h>
00039 #include <kdebug.h>
00040 #include <kurl.h>
00041 #include <kio/job.h>
00042 #include <kio/scheduler.h>
00043 
00044 #include "kio/netaccess.h"
00045 
00046 using namespace KIO;
00047 
00048 QString * NetAccess::lastErrorMsg;
00049 int NetAccess::lastErrorCode = 0;
00050 QStringList* NetAccess::tmpfiles;
00051 
00052 bool NetAccess::download(const KURL& u, QString & target)
00053 {
00054   return NetAccess::download (u, target, 0);
00055 }
00056 
00057 bool NetAccess::download(const KURL& u, QString & target, QWidget* window)
00058 {
00059   if (u.isLocalFile()) {
00060     // file protocol. We do not need the network
00061     target = u.path();
00062     bool accessible = checkAccess(target, R_OK);
00063     if(!accessible)
00064     {
00065         if(!lastErrorMsg)
00066             lastErrorMsg = new QString;
00067         *lastErrorMsg = i18n("File '%1' is not readable").arg(target);
00068         lastErrorCode = ERR_COULD_NOT_READ;
00069     }
00070     return accessible;
00071   }
00072 
00073   if (target.isEmpty())
00074   {
00075       KTempFile tmpFile;
00076       target = tmpFile.name();
00077       if (!tmpfiles)
00078           tmpfiles = new QStringList;
00079       tmpfiles->append(target);
00080   }
00081 
00082   NetAccess kioNet;
00083   KURL dest;
00084   dest.setPath( target );
00085   return kioNet.filecopyInternal( u, dest, -1, true /*overwrite*/,
00086                                   false, window, false /*copy*/);
00087 }
00088 
00089 bool NetAccess::upload(const QString& src, const KURL& target)
00090 {
00091   return NetAccess::upload(src, target, 0);
00092 }
00093 
00094 bool NetAccess::upload(const QString& src, const KURL& target, QWidget* window)
00095 {
00096   if (target.isEmpty())
00097     return false;
00098 
00099   // If target is local... well, just copy. This can be useful
00100   // when the client code uses a temp file no matter what.
00101   // Let's make sure it's not the exact same file though
00102   if (target.isLocalFile() && target.path() == src)
00103     return true;
00104 
00105   NetAccess kioNet;
00106   KURL s;
00107   s.setPath(src);
00108   return kioNet.filecopyInternal( s, target, -1, true /*overwrite*/,
00109                                   false, window, false /*copy*/ );
00110 }
00111 
00112 bool NetAccess::copy( const KURL & src, const KURL & target )
00113 {
00114   return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, 0L );
00115 }
00116 
00117 bool NetAccess::copy( const KURL & src, const KURL & target, QWidget* window )
00118 {
00119   return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, window );
00120 }
00121 
00122 bool NetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
00123                            bool overwrite, bool resume, QWidget* window )
00124 {
00125   NetAccess kioNet;
00126   return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
00127                                   window, false /*copy*/ );
00128 }
00129 
00130 
00131 bool NetAccess::file_move( const KURL& src, const KURL& target, int permissions,
00132                            bool overwrite, bool resume, QWidget* window )
00133 {
00134   NetAccess kioNet;
00135   return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
00136                                   window, true /*move*/ );
00137 }
00138 
00139 bool NetAccess::dircopy( const KURL & src, const KURL & target )
00140 {
00141   return NetAccess::dircopy( src, target, 0 );
00142 }
00143 
00144 bool NetAccess::dircopy( const KURL & src, const KURL & target, QWidget* window )
00145 {
00146   KURL::List srcList;
00147   srcList.append( src );
00148   return NetAccess::dircopy( srcList, target, window );
00149 }
00150 
00151 bool NetAccess::dircopy( const KURL::List & srcList, const KURL & target, QWidget* window )
00152 {
00153   NetAccess kioNet;
00154   return kioNet.dircopyInternal( srcList, target, window, false /*copy*/ );
00155 }
00156 
00157 bool NetAccess::move( const KURL& src, const KURL& target, QWidget* window )
00158 {
00159   KURL::List srcList;
00160   srcList.append( src );
00161   return NetAccess::move( srcList, target, window );
00162 }
00163 
00164 bool NetAccess::move( const KURL::List& srcList, const KURL& target, QWidget* window )
00165 {
00166   NetAccess kioNet;
00167   return kioNet.dircopyInternal( srcList, target, window, true /*move*/ );
00168 }
00169 
00170 bool NetAccess::exists( const KURL & url )
00171 {
00172   return NetAccess::exists( url, false, 0 );
00173 }
00174 
00175 bool NetAccess::exists( const KURL & url, QWidget* window )
00176 {
00177   return NetAccess::exists( url, false, window );
00178 }
00179 
00180 bool NetAccess::exists( const KURL & url, bool source )
00181 {
00182   return NetAccess::exists( url, source, 0 );
00183 }
00184 
00185 bool NetAccess::exists( const KURL & url, bool source, QWidget* window )
00186 {
00187   if ( url.isLocalFile() )
00188     return QFile::exists( url.path() );
00189   NetAccess kioNet;
00190   return kioNet.statInternal( url, 0 /*no details*/, source, window );
00191 }
00192 
00193 bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry )
00194 {
00195   return NetAccess::stat( url, entry, 0 );
00196 }
00197 
00198 bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry, QWidget* window )
00199 {
00200   NetAccess kioNet;
00201   bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
00202   if (ret)
00203     entry = kioNet.m_entry;
00204   return ret;
00205 }
00206 
00207 bool NetAccess::del( const KURL & url )
00208 {
00209   return NetAccess::del( url, 0 );
00210 }
00211 
00212 bool NetAccess::del( const KURL & url, QWidget* window )
00213 {
00214   NetAccess kioNet;
00215   return kioNet.delInternal( url, window );
00216 }
00217 
00218 bool NetAccess::mkdir( const KURL & url, int permissions )
00219 {
00220   return NetAccess::mkdir( url, 0, permissions );
00221 }
00222 
00223 bool NetAccess::mkdir( const KURL & url, QWidget* window, int permissions )
00224 {
00225   NetAccess kioNet;
00226   return kioNet.mkdirInternal( url, permissions, window );
00227 }
00228 
00229 QString NetAccess::fish_execute( const KURL & url, const QString command, QWidget* window )
00230 {
00231   NetAccess kioNet;
00232   return kioNet.fish_executeInternal( url, command, window );
00233 }
00234 
00235 bool NetAccess::synchronousRun( Job* job, QWidget* window, QByteArray* data,
00236                                 KURL* finalURL, QMap<QString, QString>* metaData )
00237 {
00238   NetAccess kioNet;
00239   return kioNet.synchronousRunInternal( job, window, data, finalURL, metaData );
00240 }
00241 
00242 QString NetAccess::mimetype( const KURL& url )
00243 {
00244   NetAccess kioNet;
00245   return kioNet.mimetypeInternal( url, 0 );
00246 }
00247 
00248 QString NetAccess::mimetype( const KURL& url, QWidget* window )
00249 {
00250   NetAccess kioNet;
00251   return kioNet.mimetypeInternal( url, window );
00252 }
00253 
00254 void NetAccess::removeTempFile(const QString& name)
00255 {
00256   if (!tmpfiles)
00257     return;
00258   if (tmpfiles->contains(name))
00259   {
00260     unlink(QFile::encodeName(name));
00261     tmpfiles->remove(name);
00262   }
00263 }
00264 
00265 bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
00266                                  bool overwrite, bool resume, QWidget* window, bool move)
00267 {
00268   bJobOK = true; // success unless further error occurs
00269 
00270   KIO::Scheduler::checkSlaveOnHold(true);
00271   KIO::Job * job = move
00272                    ? KIO::file_move( src, target, permissions, overwrite, resume )
00273                    : KIO::file_copy( src, target, permissions, overwrite, resume );
00274   job->setWindow (window);
00275   connect( job, SIGNAL( result (KIO::Job *) ),
00276            this, SLOT( slotResult (KIO::Job *) ) );
00277 
00278   enter_loop();
00279   return bJobOK;
00280 }
00281 
00282 bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
00283                                 QWidget* window, bool move)
00284 {
00285   bJobOK = true; // success unless further error occurs
00286 
00287   KIO::Job * job = move
00288                    ? KIO::move( src, target )
00289                    : KIO::copy( src, target );
00290   job->setWindow (window);
00291   connect( job, SIGNAL( result (KIO::Job *) ),
00292            this, SLOT( slotResult (KIO::Job *) ) );
00293 
00294   enter_loop();
00295   return bJobOK;
00296 }
00297 
00298 bool NetAccess::statInternal( const KURL & url, int details, bool source,
00299                               QWidget* window )
00300 {
00301   bJobOK = true; // success unless further error occurs
00302   KIO::StatJob * job = KIO::stat( url, !url.isLocalFile() );
00303   job->setWindow (window);
00304   job->setDetails( details );
00305   job->setSide( source );
00306   connect( job, SIGNAL( result (KIO::Job *) ),
00307            this, SLOT( slotResult (KIO::Job *) ) );
00308   enter_loop();
00309   return bJobOK;
00310 }
00311 
00312 bool NetAccess::delInternal( const KURL & url, QWidget* window )
00313 {
00314   bJobOK = true; // success unless further error occurs
00315   KIO::Job * job = KIO::del( url );
00316   job->setWindow (window);
00317   connect( job, SIGNAL( result (KIO::Job *) ),
00318            this, SLOT( slotResult (KIO::Job *) ) );
00319   enter_loop();
00320   return bJobOK;
00321 }
00322 
00323 bool NetAccess::mkdirInternal( const KURL & url, int permissions,
00324                                QWidget* window )
00325 {
00326   bJobOK = true; // success unless further error occurs
00327   KIO::Job * job = KIO::mkdir( url, permissions );
00328   job->setWindow (window);
00329   connect( job, SIGNAL( result (KIO::Job *) ),
00330            this, SLOT( slotResult (KIO::Job *) ) );
00331   enter_loop();
00332   return bJobOK;
00333 }
00334 
00335 QString NetAccess::mimetypeInternal( const KURL & url, QWidget* window )
00336 {
00337   bJobOK = true; // success unless further error occurs
00338   m_mimetype = QString::fromLatin1("unknown");
00339   KIO::Job * job = KIO::mimetype( url );
00340   job->setWindow (window);
00341   connect( job, SIGNAL( result (KIO::Job *) ),
00342            this, SLOT( slotResult (KIO::Job *) ) );
00343   connect( job, SIGNAL( mimetype (KIO::Job *, const QString &) ),
00344            this, SLOT( slotMimetype (KIO::Job *, const QString &) ) );
00345   enter_loop();
00346   return m_mimetype;
00347 }
00348 
00349 void NetAccess::slotMimetype( KIO::Job *, const QString & type  )
00350 {
00351   m_mimetype = type;
00352 }
00353 
00354 QString NetAccess::fish_executeInternal(const KURL & url, const QString command, QWidget* window)
00355 {
00356   QString target, remoteTempFileName, resultData;
00357   KURL tempPathUrl;
00358   KTempFile tmpFile;
00359   tmpFile.setAutoDelete( true );
00360 
00361   if( url.protocol() == "fish" )
00362   {
00363     // construct remote temp filename
00364     tempPathUrl = url;
00365     remoteTempFileName = tmpFile.name();
00366     // only need the filename KTempFile adds some KDE specific dirs
00367     // that probably does not exist on the remote side
00368     int pos = remoteTempFileName.findRev('/');
00369     remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
00370     tempPathUrl.setPath( remoteTempFileName );
00371     bJobOK = true; // success unless further error occurs
00372     QByteArray packedArgs;
00373     QDataStream stream( packedArgs, IO_WriteOnly );
00374 
00375     stream << int('X') << tempPathUrl << command;
00376 
00377     KIO::Job * job = KIO::special( tempPathUrl, packedArgs, true );
00378     job->setWindow( window );
00379     connect( job, SIGNAL( result (KIO::Job *) ),
00380              this, SLOT( slotResult (KIO::Job *) ) );
00381     enter_loop();
00382 
00383     // since the KIO::special does not provide feedback we need to download the result
00384     if( NetAccess::download( tempPathUrl, target, window ) )
00385     {
00386       QFile resultFile( target );
00387 
00388       if (resultFile.open( IO_ReadOnly ))
00389       {
00390         QTextStream ts( &resultFile );
00391         ts.setEncoding( QTextStream::Locale ); // Locale??
00392         resultData = ts.read();
00393         resultFile.close();
00394         NetAccess::del( tempPathUrl, window );
00395       }
00396     }
00397   }
00398   else
00399   {
00400     resultData = QString( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
00401   }
00402   return resultData;
00403 }
00404 
00405 bool NetAccess::synchronousRunInternal( Job* job, QWidget* window, QByteArray* data,
00406                                         KURL* finalURL, QMap<QString,QString>* metaData )
00407 {
00408   job->setWindow( window );
00409 
00410   m_metaData = metaData;
00411   if ( m_metaData ) {
00412       for ( QMap<QString, QString>::iterator it = m_metaData->begin(); it != m_metaData->end(); ++it ) {
00413           job->addMetaData( it.key(), it.data() );
00414       }
00415   }
00416 
00417   if ( finalURL ) {
00418       SimpleJob *sj = dynamic_cast<SimpleJob*>( job );
00419       if ( sj ) {
00420           m_url = sj->url();
00421       }
00422   }
00423 
00424   connect( job, SIGNAL( result (KIO::Job *) ),
00425            this, SLOT( slotResult (KIO::Job *) ) );
00426 
00427   QMetaObject *meta = job->metaObject();
00428 
00429   static const char dataSignal[] = "data(KIO::Job*,const QByteArray&)";
00430   if ( meta->findSignal( dataSignal ) != -1 ) {
00431       connect( job, SIGNAL(data(KIO::Job*,const QByteArray&)),
00432                this, SLOT(slotData(KIO::Job*,const QByteArray&)) );
00433   }
00434 
00435   static const char redirSignal[] = "redirection(KIO::Job*,const KURL&)";
00436   if ( meta->findSignal( redirSignal ) != -1 ) {
00437       connect( job, SIGNAL(redirection(KIO::Job*,const KURL&)),
00438                this, SLOT(slotRedirection(KIO::Job*, const KURL&)) );
00439   }
00440 
00441   enter_loop();
00442 
00443   if ( finalURL )
00444       *finalURL = m_url;
00445   if ( data )
00446       *data = m_data;
00447 
00448   return bJobOK;
00449 }
00450 
00451 // If a troll sees this, he kills me
00452 void qt_enter_modal( QWidget *widget );
00453 void qt_leave_modal( QWidget *widget );
00454 
00455 void NetAccess::enter_loop()
00456 {
00457   QWidget dummy(0,0,WType_Dialog | WShowModal);
00458   dummy.setFocusPolicy( QWidget::NoFocus );
00459   qt_enter_modal(&dummy);
00460   qApp->enter_loop();
00461   qt_leave_modal(&dummy);
00462 }
00463 
00464 void NetAccess::slotResult( KIO::Job * job )
00465 {
00466   lastErrorCode = job->error();
00467   bJobOK = !job->error();
00468   if ( !bJobOK )
00469   {
00470     if ( !lastErrorMsg )
00471       lastErrorMsg = new QString;
00472     *lastErrorMsg = job->errorString();
00473   }
00474   if ( job->isA("KIO::StatJob") )
00475     m_entry = static_cast<KIO::StatJob *>(job)->statResult();
00476 
00477   if ( m_metaData )
00478     *m_metaData = job->metaData();
00479 
00480   qApp->exit_loop();
00481 }
00482 
00483 void NetAccess::slotData( KIO::Job*, const QByteArray& data )
00484 {
00485   if ( data.isEmpty() )
00486     return;
00487 
00488   unsigned offset = m_data.size();
00489   m_data.resize( offset + data.size() );
00490   std::memcpy( m_data.data() + offset, data.data(), data.size() );
00491 }
00492 
00493 void NetAccess::slotRedirection( KIO::Job*, const KURL& url )
00494 {
00495   m_url = url;
00496 }
00497 
00498 #include "netaccess.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Jul 20 13:55:17 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003