kio Library API Documentation

netaccess.cpp

00001 /*  $Id: netaccess.cpp,v 1.47 2003/08/28 20:50:44 faure Exp $
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 <qstring.h>
00030 #include <qapplication.h>
00031 #include <qfile.h>
00032 
00033 #include <kapplication.h>
00034 #include <klocale.h>
00035 #include <ktempfile.h>
00036 #include <kdebug.h>
00037 #include <kurl.h>
00038 #include <kio/job.h>
00039 #include <kio/scheduler.h>
00040 
00041 #include "kio/netaccess.h"
00042 
00043 using namespace KIO;
00044 
00045 QString * NetAccess::lastErrorMsg;
00046 QStringList* NetAccess::tmpfiles;
00047 
00048 bool NetAccess::download(const KURL& u, QString & target)
00049 {
00050   return NetAccess::download (u, target, 0);
00051 }
00052 
00053 bool NetAccess::download(const KURL& u, QString & target, QWidget* window)
00054 {
00055   if (u.isLocalFile()) {
00056     // file protocol. We do not need the network
00057     target = u.path();
00058     bool accessible = checkAccess(target, R_OK);
00059     if(!accessible)
00060     {
00061         if(!lastErrorMsg)
00062             lastErrorMsg = new QString;
00063         *lastErrorMsg = i18n("File '%1' is not readable").arg(target);
00064     }
00065     return accessible;
00066   }
00067 
00068   if (target.isEmpty())
00069   {
00070       KTempFile tmpFile;
00071       target = tmpFile.name();
00072       if (!tmpfiles)
00073           tmpfiles = new QStringList;
00074       tmpfiles->append(target);
00075   }
00076 
00077   NetAccess kioNet;
00078   KURL dest;
00079   dest.setPath( target );
00080   return kioNet.filecopyInternal( u, dest, -1, true /*overwrite*/,
00081                                   false, window, false /*copy*/);
00082 }
00083 
00084 bool NetAccess::upload(const QString& src, const KURL& target)
00085 {
00086   return NetAccess::upload(src, target, 0);
00087 }
00088 
00089 bool NetAccess::upload(const QString& src, const KURL& target, QWidget* window)
00090 {
00091   if (target.isEmpty())
00092     return false;
00093 
00094   // If target is local... well, just copy. This can be useful
00095   // when the client code uses a temp file no matter what.
00096   // Let's make sure it's not the exact same file though
00097   if (target.isLocalFile() && target.path() == src)
00098     return true;
00099 
00100   NetAccess kioNet;
00101   KURL s;
00102   s.setPath(src);
00103   return kioNet.filecopyInternal( s, target, -1, true /*overwrite*/,
00104                                   false, window, false /*copy*/ );
00105 }
00106 
00107 bool NetAccess::copy( const KURL & src, const KURL & target )
00108 {
00109   return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, 0L );
00110 }
00111 
00112 bool NetAccess::copy( const KURL & src, const KURL & target, QWidget* window )
00113 {
00114   return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, window );
00115 }
00116 
00117 bool NetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
00118                            bool overwrite, bool resume, QWidget* window )
00119 {
00120   NetAccess kioNet;
00121   return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
00122                                   window, false /*copy*/ );
00123 }
00124 
00125 
00126 bool NetAccess::file_move( const KURL& src, const KURL& target, int permissions,
00127                            bool overwrite, bool resume, QWidget* window )
00128 {
00129   NetAccess kioNet;
00130   return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
00131                                   window, true /*move*/ );
00132 }
00133 
00134 bool NetAccess::dircopy( const KURL & src, const KURL & target )
00135 {
00136   return NetAccess::dircopy( src, target, 0 );
00137 }
00138 
00139 bool NetAccess::dircopy( const KURL & src, const KURL & target, QWidget* window )
00140 {
00141   KURL::List srcList;
00142   srcList.append( src );
00143   return NetAccess::dircopy( srcList, target, window );
00144 }
00145 
00146 bool NetAccess::dircopy( const KURL::List & srcList, const KURL & target, QWidget* window )
00147 {
00148   NetAccess kioNet;
00149   return kioNet.dircopyInternal( srcList, target, window, false /*copy*/ );
00150 }
00151 
00152 bool NetAccess::move( const KURL& src, const KURL& target, QWidget* window )
00153 {
00154   KURL::List srcList;
00155   srcList.append( src );
00156   return NetAccess::move( srcList, target, window );
00157 }
00158 
00159 bool NetAccess::move( const KURL::List& srcList, const KURL& target, QWidget* window )
00160 {
00161   NetAccess kioNet;
00162   return kioNet.dircopyInternal( srcList, target, window, true /*move*/ );
00163 }
00164 
00165 bool NetAccess::exists( const KURL & url )
00166 {
00167   return NetAccess::exists( url, false, 0 );
00168 }
00169 
00170 bool NetAccess::exists( const KURL & url, QWidget* window )
00171 {
00172   return NetAccess::exists( url, false, window );
00173 }
00174 
00175 bool NetAccess::exists( const KURL & url, bool source )
00176 {
00177   return NetAccess::exists( url, source, 0 );
00178 }
00179 
00180 bool NetAccess::exists( const KURL & url, bool source, QWidget* window )
00181 {
00182   if ( url.isLocalFile() )
00183     return QFile::exists( url.path() );
00184   NetAccess kioNet;
00185   return kioNet.statInternal( url, 0 /*no details*/, source, window );
00186 }
00187 
00188 bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry )
00189 {
00190   return NetAccess::stat( url, entry, 0 );
00191 }
00192 
00193 bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry, QWidget* window )
00194 {
00195   NetAccess kioNet;
00196   bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
00197   if (ret)
00198     entry = kioNet.m_entry;
00199   return ret;
00200 }
00201 
00202 bool NetAccess::del( const KURL & url )
00203 {
00204   return NetAccess::del( url, 0 );
00205 }
00206 
00207 bool NetAccess::del( const KURL & url, QWidget* window )
00208 {
00209   NetAccess kioNet;
00210   return kioNet.delInternal( url, window );
00211 }
00212 
00213 bool NetAccess::mkdir( const KURL & url, int permissions )
00214 {
00215   return NetAccess::mkdir( url, 0, permissions );
00216 }
00217 
00218 bool NetAccess::mkdir( const KURL & url, QWidget* window, int permissions )
00219 {
00220   NetAccess kioNet;
00221   return kioNet.mkdirInternal( url, permissions, window );
00222 }
00223 
00224 QString NetAccess::fish_execute( const KURL & url, const QString command, QWidget* window )
00225 {
00226   NetAccess kioNet;
00227   return kioNet.fish_executeInternal( url, command, window );
00228 }
00229 
00230 QString NetAccess::mimetype( const KURL& url )
00231 {
00232   NetAccess kioNet;
00233   return kioNet.mimetypeInternal( url, 0 );
00234 }
00235 
00236 QString NetAccess::mimetype( const KURL& url, QWidget* window )
00237 {
00238   NetAccess kioNet;
00239   return kioNet.mimetypeInternal( url, window );
00240 }
00241 
00242 void NetAccess::removeTempFile(const QString& name)
00243 {
00244   if (!tmpfiles)
00245     return;
00246   if (tmpfiles->contains(name))
00247   {
00248     unlink(QFile::encodeName(name));
00249     tmpfiles->remove(name);
00250   }
00251 }
00252 
00253 bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
00254                                  bool overwrite, bool resume, QWidget* window, bool move)
00255 {
00256   bJobOK = true; // success unless further error occurs
00257 
00258   KIO::Scheduler::checkSlaveOnHold(true);
00259   KIO::Job * job = move
00260                    ? KIO::file_move( src, target, permissions, overwrite, resume )
00261                    : KIO::file_copy( src, target, permissions, overwrite, resume );
00262   job->setWindow (window);
00263   connect( job, SIGNAL( result (KIO::Job *) ),
00264            this, SLOT( slotResult (KIO::Job *) ) );
00265 
00266   enter_loop();
00267   return bJobOK;
00268 }
00269 
00270 bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
00271                                 QWidget* window, bool move)
00272 {
00273   bJobOK = true; // success unless further error occurs
00274 
00275   KIO::Job * job = move
00276                    ? KIO::move( src, target )
00277                    : KIO::copy( src, target );
00278   job->setWindow (window);
00279   connect( job, SIGNAL( result (KIO::Job *) ),
00280            this, SLOT( slotResult (KIO::Job *) ) );
00281 
00282   enter_loop();
00283   return bJobOK;
00284 }
00285 
00286 bool NetAccess::statInternal( const KURL & url, int details, bool source,
00287                               QWidget* window )
00288 {
00289   bJobOK = true; // success unless further error occurs
00290   KIO::StatJob * job = KIO::stat( url, !url.isLocalFile() );
00291   job->setWindow (window);
00292   job->setDetails( details );
00293   job->setSide( source );
00294   connect( job, SIGNAL( result (KIO::Job *) ),
00295            this, SLOT( slotResult (KIO::Job *) ) );
00296   enter_loop();
00297   return bJobOK;
00298 }
00299 
00300 bool NetAccess::delInternal( const KURL & url, QWidget* window )
00301 {
00302   bJobOK = true; // success unless further error occurs
00303   KIO::Job * job = KIO::del( url );
00304   job->setWindow (window);
00305   connect( job, SIGNAL( result (KIO::Job *) ),
00306            this, SLOT( slotResult (KIO::Job *) ) );
00307   enter_loop();
00308   return bJobOK;
00309 }
00310 
00311 bool NetAccess::mkdirInternal( const KURL & url, int permissions,
00312                                QWidget* window )
00313 {
00314   bJobOK = true; // success unless further error occurs
00315   KIO::Job * job = KIO::mkdir( url, permissions );
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 QString NetAccess::mimetypeInternal( const KURL & url, QWidget* window )
00324 {
00325   bJobOK = true; // success unless further error occurs
00326   m_mimetype = QString::fromLatin1("unknown");
00327   KIO::Job * job = KIO::mimetype( url );
00328   job->setWindow (window);
00329   connect( job, SIGNAL( result (KIO::Job *) ),
00330            this, SLOT( slotResult (KIO::Job *) ) );
00331   connect( job, SIGNAL( mimetype (KIO::Job *, const QString &) ),
00332            this, SLOT( slotMimetype (KIO::Job *, const QString &) ) );
00333   enter_loop();
00334   return m_mimetype;
00335 }
00336 
00337 void NetAccess::slotMimetype( KIO::Job *, const QString & type  )
00338 {
00339   m_mimetype = type;
00340 }
00341 
00342 QString NetAccess::fish_executeInternal(const KURL & url, const QString command, QWidget* window)
00343 {
00344   QString target, remoteTempFileName, resultData;
00345   KURL tempPathUrl;
00346   KTempFile tmpFile;
00347   tmpFile.setAutoDelete( true );
00348 
00349   if( url.protocol() == "fish" )
00350   {
00351     // construct remote temp filename
00352     tempPathUrl = url;
00353     remoteTempFileName = tmpFile.name();
00354     // only need the filename KTempFile adds some KDE specific dirs
00355     // that probably does not exist on the remote side
00356     int pos = remoteTempFileName.findRev('/');
00357     remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
00358     tempPathUrl.setPath( remoteTempFileName );
00359     bJobOK = true; // success unless further error occurs
00360     QByteArray packedArgs;
00361     QDataStream stream( packedArgs, IO_WriteOnly );
00362 
00363     stream << int('X') << tempPathUrl << command;
00364 
00365     KIO::Job * job = KIO::special( tempPathUrl, packedArgs, true );
00366     job->setWindow( window );
00367     connect( job, SIGNAL( result (KIO::Job *) ),
00368              this, SLOT( slotResult (KIO::Job *) ) );
00369     enter_loop();
00370 
00371     // since the KIO::special does not provide feedback we need to download the result
00372     if( NetAccess::download( tempPathUrl, target, window ) )
00373     {
00374       QFile resultFile( target );
00375 
00376       if (resultFile.open( IO_ReadOnly ))
00377       {
00378         QTextStream ts( &resultFile );
00379         ts.setEncoding( QTextStream::Locale ); // Locale??
00380         resultData = ts.read();
00381         resultFile.close();
00382         NetAccess::del( tempPathUrl, window );
00383       }
00384     }
00385   }
00386   else
00387   {
00388     resultData = QString( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
00389   }
00390   return resultData;
00391 }
00392 
00393 // If a troll sees this, he kills me
00394 void qt_enter_modal( QWidget *widget );
00395 void qt_leave_modal( QWidget *widget );
00396 
00397 void NetAccess::enter_loop()
00398 {
00399   QWidget dummy(0,0,WType_Dialog | WShowModal);
00400   dummy.setFocusPolicy( QWidget::NoFocus );
00401   qt_enter_modal(&dummy);
00402   qApp->enter_loop();
00403   qt_leave_modal(&dummy);
00404 }
00405 
00406 void NetAccess::slotResult( KIO::Job * job )
00407 {
00408   bJobOK = !job->error();
00409   if ( !bJobOK )
00410   {
00411     if ( !lastErrorMsg )
00412       lastErrorMsg = new QString;
00413     *lastErrorMsg = job->errorString();
00414   }
00415   if ( job->isA("KIO::StatJob") )
00416     m_entry = static_cast<KIO::StatJob *>(job)->statResult();
00417   qApp->exit_loop();
00418 }
00419 
00420 #include "netaccess.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 12 09:06:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003