kio Library API Documentation

netaccess.cpp

00001 /*  $Id: netaccess.cpp,v 1.39.2.2 2003/01/13 17:58:46 pfeiffer 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 = 0L;
00046 
00047 bool NetAccess::download(const KURL& u, QString & target)
00048 {
00049   if (u.isLocalFile()) {
00050     // file protocol. We do not need the network
00051     target = u.path();
00052     bool accessible = checkAccess(target, R_OK);
00053     if(!accessible)
00054     {
00055         if(!lastErrorMsg)
00056             lastErrorMsg = new QString;
00057         *lastErrorMsg = i18n("File '%1' is not readable").arg(target);
00058     }
00059     return accessible;
00060   }
00061 
00062   if (target.isEmpty())
00063   {
00064       KTempFile tmpFile;
00065       target = tmpFile.name();
00066       if (!tmpfiles)
00067       tmpfiles = new QStringList;
00068       tmpfiles->append(target);
00069   }
00070 
00071   NetAccess kioNet;
00072   KURL dest;
00073   dest.setPath( target );
00074   return kioNet.copyInternal( u, dest, true /*overwrite*/);
00075 }
00076 
00077 bool NetAccess::upload(const QString& src, const KURL& target)
00078 {
00079   if (target.isEmpty())
00080     return false;
00081 
00082   // If target is local... well, just copy. This can be useful
00083   // when the client code uses a temp file no matter what.
00084   // Let's make sure it's not the exact same file though
00085   if (target.isLocalFile() && target.path() == src)
00086     return true;
00087 
00088   NetAccess kioNet;
00089   KURL s;
00090   s.setPath(src);
00091   return kioNet.copyInternal( s, target, true /*overwrite*/ );
00092 }
00093 
00094 bool NetAccess::copy( const KURL & src, const KURL & target )
00095 {
00096   NetAccess kioNet;
00097   return kioNet.copyInternal( src, target, false /*not overwrite*/ );
00098 }
00099 
00100 bool NetAccess::dircopy( const KURL & src, const KURL & target )
00101 {
00102   NetAccess kioNet;
00103   return kioNet.dircopyInternal( src, target );
00104 }
00105 
00106 bool NetAccess::exists( const KURL & url )
00107 {
00108   if ( url.isLocalFile() )
00109     return QFile::exists( url.path() );
00110   NetAccess kioNet;
00111   return kioNet.statInternal( url, 0 /*no details*/, true /*source assumed*/ );
00112 }
00113 
00114 bool NetAccess::exists( const KURL & url, bool source )
00115 {
00116   if ( url.isLocalFile() )
00117     return QFile::exists( url.path() );
00118   NetAccess kioNet;
00119   return kioNet.statInternal( url, 0 /*no details*/, source );
00120 }
00121 
00122 bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry )
00123 {
00124   NetAccess kioNet;
00125   bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/ );
00126   if (ret)
00127     entry = kioNet.m_entry;
00128   return ret;
00129 }
00130 
00131 bool NetAccess::del( const KURL & url )
00132 {
00133   NetAccess kioNet;
00134   return kioNet.delInternal( url );
00135 }
00136 
00137 bool NetAccess::mkdir( const KURL & url, int permissions )
00138 {
00139   NetAccess kioNet;
00140   return kioNet.mkdirInternal( url, permissions );
00141 }
00142 
00143 QString NetAccess::mimetype( const KURL& url )
00144 {
00145   NetAccess kioNet;
00146   return kioNet.mimetypeInternal( url );
00147 }
00148 
00149 QStringList* NetAccess::tmpfiles = 0L;
00150 
00151 void NetAccess::removeTempFile(const QString& name)
00152 {
00153   if (!tmpfiles)
00154     return;
00155   if (tmpfiles->contains(name))
00156   {
00157     unlink(QFile::encodeName(name));
00158     tmpfiles->remove(name);
00159   }
00160 }
00161 
00162 bool NetAccess::copyInternal(const KURL& src, const KURL& target, bool overwrite)
00163 {
00164   bJobOK = true; // success unless further error occurs
00165 
00166   KIO::Scheduler::checkSlaveOnHold(true);
00167   KIO::Job * job = KIO::file_copy( src, target, -1, overwrite );
00168   connect( job, SIGNAL( result (KIO::Job *) ),
00169            this, SLOT( slotResult (KIO::Job *) ) );
00170 
00171   enter_loop();
00172   return bJobOK;
00173 }
00174 
00175 bool NetAccess::dircopyInternal(const KURL& src, const KURL& target)
00176 {
00177   bJobOK = true; // success unless further error occurs
00178 
00179   KIO::Job * job = KIO::copy( src, target );
00180   connect( job, SIGNAL( result (KIO::Job *) ),
00181            this, SLOT( slotResult (KIO::Job *) ) );
00182 
00183   enter_loop();
00184   return bJobOK;
00185 }
00186 
00187 bool NetAccess::statInternal( const KURL & url, int details, bool source )
00188 {
00189   bJobOK = true; // success unless further error occurs
00190   KIO::StatJob * job = KIO::stat( url, !url.isLocalFile() );
00191   job->setDetails( details );
00192   job->setSide( source );
00193   connect( job, SIGNAL( result (KIO::Job *) ),
00194            this, SLOT( slotResult (KIO::Job *) ) );
00195   enter_loop();
00196   return bJobOK;
00197 }
00198 
00199 bool NetAccess::delInternal( const KURL & url )
00200 {
00201   bJobOK = true; // success unless further error occurs
00202   KIO::Job * job = KIO::del( url );
00203   connect( job, SIGNAL( result (KIO::Job *) ),
00204            this, SLOT( slotResult (KIO::Job *) ) );
00205   enter_loop();
00206   return bJobOK;
00207 }
00208 
00209 bool NetAccess::mkdirInternal( const KURL & url, int permissions )
00210 {
00211   bJobOK = true; // success unless further error occurs
00212   KIO::Job * job = KIO::mkdir( url, permissions );
00213   connect( job, SIGNAL( result (KIO::Job *) ),
00214            this, SLOT( slotResult (KIO::Job *) ) );
00215   enter_loop();
00216   return bJobOK;
00217 }
00218 
00219 QString NetAccess::mimetypeInternal( const KURL & url )
00220 {
00221   bJobOK = true; // success unless further error occurs
00222   m_mimetype = QString::fromLatin1("unknown");
00223   KIO::Job * job = KIO::mimetype( url );
00224   connect( job, SIGNAL( result (KIO::Job *) ),
00225            this, SLOT( slotResult (KIO::Job *) ) );
00226   connect( job, SIGNAL( mimetype (KIO::Job *, const QString &) ),
00227            this, SLOT( slotMimetype (KIO::Job *, const QString &) ) );
00228   enter_loop();
00229   return m_mimetype;
00230 }
00231 
00232 void NetAccess::slotMimetype( KIO::Job *, const QString & type  )
00233 {
00234   m_mimetype = type;
00235 }
00236 
00237 // If a troll sees this, he kills me
00238 void qt_enter_modal( QWidget *widget );
00239 void qt_leave_modal( QWidget *widget );
00240 
00241 void NetAccess::enter_loop()
00242 {
00243   QWidget dummy(0,0,WType_Dialog | WShowModal);
00244   dummy.setFocusPolicy( QWidget::NoFocus );
00245   qt_enter_modal(&dummy);
00246   qApp->enter_loop();
00247   qt_leave_modal(&dummy);
00248 }
00249 
00250 void NetAccess::slotResult( KIO::Job * job )
00251 {
00252   bJobOK = !job->error();
00253   if ( !bJobOK )
00254   {
00255     if ( !lastErrorMsg )
00256       lastErrorMsg = new QString;
00257     *lastErrorMsg = job->errorString();
00258   }
00259   if ( job->isA("KIO::StatJob") )
00260     m_entry = static_cast<KIO::StatJob *>(job)->statResult();
00261   qApp->exit_loop();
00262 }
00263 
00264 #include "netaccess.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:33 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001