• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

netaccess.cpp
00001 /*  $Id$
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., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, 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 <tqstring.h>
00032 #include <tqapplication.h>
00033 #include <tqfile.h>
00034 #include <tqmetaobject.h>
00035 
00036 #include <tdeapplication.h>
00037 #include <tdelocale.h>
00038 #include <tdetempfile.h>
00039 #include <kdebug.h>
00040 #include <kurl.h>
00041 #include <tdeio/job.h>
00042 #include <tdeio/scheduler.h>
00043 
00044 #include "tdeio/netaccess.h"
00045 
00046 using namespace TDEIO;
00047 
00048 TQString * NetAccess::lastErrorMsg;
00049 int NetAccess::lastErrorCode = 0;
00050 TQStringList* NetAccess::tmpfiles;
00051 
00052 bool NetAccess::download(const KURL& u, TQString & target)
00053 {
00054   return NetAccess::download (u, target, 0);
00055 }
00056 
00057 bool NetAccess::download(const KURL& u, TQString & target, TQWidget* 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 TQString;
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 TQStringList;
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 TQString& src, const KURL& target)
00090 {
00091   return NetAccess::upload(src, target, 0);
00092 }
00093 
00094 bool NetAccess::upload(const TQString& src, const KURL& target, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* 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, TQWidget* window )
00186 {
00187   if ( url.isLocalFile() )
00188     return TQFile::exists( url.path() );
00189   NetAccess kioNet;
00190   return kioNet.statInternal( url, 0 /*no details*/, source, window );
00191 }
00192 
00193 KURL NetAccess::localURL(const KURL& url, TQWidget* window)
00194 {
00195   NetAccess kioNet;
00196   return kioNet.localURLInternal( url, window );
00197 }
00198 
00199 bool NetAccess::stat( const KURL & url, TDEIO::UDSEntry & entry )
00200 {
00201   return NetAccess::stat( url, entry, 0 );
00202 }
00203 
00204 bool NetAccess::stat( const KURL & url, TDEIO::UDSEntry & entry, TQWidget* window )
00205 {
00206   NetAccess kioNet;
00207   bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
00208   if (ret)
00209     entry = kioNet.m_entry;
00210   return ret;
00211 }
00212 
00213 KURL NetAccess::mostLocalURL(const KURL & url, TQWidget* window)
00214 {
00215   if ( url.isLocalFile() )
00216   {
00217     return url;
00218   }
00219 
00220   TDEIO::UDSEntry entry;
00221   if (!stat(url, entry, window))
00222   {
00223     return url;
00224   }
00225 
00226   TQString path;
00227 
00228   // Extract the local path from the TDEIO::UDSEntry
00229   TDEIO::UDSEntry::ConstIterator it = entry.begin();
00230   const TDEIO::UDSEntry::ConstIterator end = entry.end();
00231   for ( ; it != end; ++it )
00232   {
00233     if ( (*it).m_uds == TDEIO::UDS_LOCAL_PATH )
00234     {
00235       path = (*it).m_str;
00236       break;
00237     }
00238   }
00239 
00240   if ( !path.isEmpty() )
00241   {
00242     KURL new_url;
00243     new_url.setPath(path);
00244     return new_url;
00245   }
00246 
00247   return url;
00248 }
00249 
00250 
00251 bool NetAccess::del( const KURL & url )
00252 {
00253   return NetAccess::del( url, 0 );
00254 }
00255 
00256 bool NetAccess::del( const KURL & url, TQWidget* window )
00257 {
00258   NetAccess kioNet;
00259   return kioNet.delInternal( url, window );
00260 }
00261 
00262 bool NetAccess::mkdir( const KURL & url, int permissions )
00263 {
00264   return NetAccess::mkdir( url, 0, permissions );
00265 }
00266 
00267 bool NetAccess::mkdir( const KURL & url, TQWidget* window, int permissions )
00268 {
00269   NetAccess kioNet;
00270   return kioNet.mkdirInternal( url, permissions, window );
00271 }
00272 
00273 TQString NetAccess::fish_execute( const KURL & url, const TQString command, TQWidget* window )
00274 {
00275   NetAccess kioNet;
00276   return kioNet.fish_executeInternal( url, command, window );
00277 }
00278 
00279 bool NetAccess::synchronousRun( Job* job, TQWidget* window, TQByteArray* data,
00280                                 KURL* finalURL, TQMap<TQString, TQString>* metaData )
00281 {
00282   NetAccess kioNet;
00283   return kioNet.synchronousRunInternal( job, window, data, finalURL, metaData );
00284 }
00285 
00286 TQString NetAccess::mimetype( const KURL& url )
00287 {
00288   NetAccess kioNet;
00289   return kioNet.mimetypeInternal( url, 0 );
00290 }
00291 
00292 TQString NetAccess::mimetype( const KURL& url, TQWidget* window )
00293 {
00294   NetAccess kioNet;
00295   return kioNet.mimetypeInternal( url, window );
00296 }
00297 
00298 void NetAccess::removeTempFile(const TQString& name)
00299 {
00300   if (!tmpfiles)
00301     return;
00302   if (tmpfiles->contains(name))
00303   {
00304     unlink(TQFile::encodeName(name));
00305     tmpfiles->remove(name);
00306   }
00307 }
00308 
00309 bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
00310                                  bool overwrite, bool resume, TQWidget* window, bool move)
00311 {
00312   bJobOK = true; // success unless further error occurs
00313 
00314   TDEIO::Scheduler::checkSlaveOnHold(true);
00315   TDEIO::Job * job = move
00316                    ? TDEIO::file_move( src, target, permissions, overwrite, resume )
00317                    : TDEIO::file_copy( src, target, permissions, overwrite, resume );
00318   job->setWindow (window);
00319   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00320            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00321 
00322   enter_loop();
00323   return bJobOK;
00324 }
00325 
00326 bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
00327                                 TQWidget* window, bool move)
00328 {
00329   bJobOK = true; // success unless further error occurs
00330 
00331   TDEIO::Job * job = move
00332                    ? TDEIO::move( src, target )
00333                    : TDEIO::copy( src, target );
00334   job->setWindow (window);
00335   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00336            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00337 
00338   enter_loop();
00339   return bJobOK;
00340 }
00341 
00342 bool NetAccess::statInternal( const KURL & url, int details, bool source,
00343                               TQWidget* window )
00344 {
00345   bJobOK = true; // success unless further error occurs
00346   TDEIO::StatJob * job = TDEIO::stat( url, !url.isLocalFile() );
00347   job->setWindow (window);
00348   job->setDetails( details );
00349   job->setSide( source );
00350   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00351            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00352   enter_loop();
00353   return bJobOK;
00354 }
00355 
00356 KURL NetAccess::localURLInternal( const KURL & url, TQWidget* window )
00357 {
00358   m_localURL = url;
00359   TDEIO::LocalURLJob* job = TDEIO::localURL(url);
00360   job->setWindow (window);
00361   connect(job, TQT_SIGNAL( localURL(TDEIO::Job*, const KURL&, bool) ),
00362            this, TQT_SLOT( slotLocalURL(TDEIO::Job*, const KURL&, bool) ));
00363   enter_loop();
00364   return m_localURL;
00365 }
00366 
00367 bool NetAccess::delInternal( const KURL & url, TQWidget* window )
00368 {
00369   bJobOK = true; // success unless further error occurs
00370   TDEIO::Job * job = TDEIO::del( url );
00371   job->setWindow (window);
00372   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00373            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00374   enter_loop();
00375   return bJobOK;
00376 }
00377 
00378 bool NetAccess::mkdirInternal( const KURL & url, int permissions,
00379                                TQWidget* window )
00380 {
00381   bJobOK = true; // success unless further error occurs
00382   TDEIO::Job * job = TDEIO::mkdir( url, permissions );
00383   job->setWindow (window);
00384   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00385            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00386   enter_loop();
00387   return bJobOK;
00388 }
00389 
00390 TQString NetAccess::mimetypeInternal( const KURL & url, TQWidget* window )
00391 {
00392   bJobOK = true; // success unless further error occurs
00393   m_mimetype = TQString::fromLatin1("unknown");
00394   TDEIO::Job * job = TDEIO::mimetype( url );
00395   job->setWindow (window);
00396   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00397            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00398   connect( job, TQT_SIGNAL( mimetype (TDEIO::Job *, const TQString &) ),
00399            this, TQT_SLOT( slotMimetype (TDEIO::Job *, const TQString &) ) );
00400   enter_loop();
00401   return m_mimetype;
00402 }
00403 
00404 void NetAccess::slotMimetype( TDEIO::Job *, const TQString & type  )
00405 {
00406   m_mimetype = type;
00407 }
00408 
00409 void NetAccess::slotLocalURL(TDEIO::Job*, const KURL & url, bool)
00410 {
00411   m_localURL = url;
00412   tqApp->exit_loop();
00413 }
00414 
00415 TQString NetAccess::fish_executeInternal(const KURL & url, const TQString command, TQWidget* window)
00416 {
00417   TQString target, remoteTempFileName, resultData;
00418   KURL tempPathUrl;
00419   KTempFile tmpFile;
00420   tmpFile.setAutoDelete( true );
00421 
00422   if( url.protocol() == "fish" )
00423   {
00424     // construct remote temp filename
00425     tempPathUrl = url;
00426     remoteTempFileName = tmpFile.name();
00427     // only need the filename KTempFile adds some KDE specific dirs
00428     // that probably does not exist on the remote side
00429     int pos = remoteTempFileName.findRev('/');
00430     remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
00431     tempPathUrl.setPath( remoteTempFileName );
00432     bJobOK = true; // success unless further error occurs
00433     TQByteArray packedArgs;
00434     TQDataStream stream( packedArgs, IO_WriteOnly );
00435 
00436     stream << int('X') << tempPathUrl << command;
00437 
00438     TDEIO::Job * job = TDEIO::special( tempPathUrl, packedArgs, true );
00439     job->setWindow( window );
00440     connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00441              this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00442     enter_loop();
00443 
00444     // since the TDEIO::special does not provide feedback we need to download the result
00445     if( NetAccess::download( tempPathUrl, target, window ) )
00446     {
00447       TQFile resultFile( target );
00448 
00449       if (resultFile.open( IO_ReadOnly ))
00450       {
00451         TQTextStream ts( &resultFile );
00452         ts.setEncoding( TQTextStream::Locale ); // Locale??
00453         resultData = ts.read();
00454         resultFile.close();
00455         NetAccess::del( tempPathUrl, window );
00456       }
00457     }
00458   }
00459   else
00460   {
00461     resultData = i18n( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
00462   }
00463   return resultData;
00464 }
00465 
00466 bool NetAccess::synchronousRunInternal( Job* job, TQWidget* window, TQByteArray* data,
00467                                         KURL* finalURL, TQMap<TQString,TQString>* metaData )
00468 {
00469   job->setWindow( window );
00470 
00471   m_metaData = metaData;
00472   if ( m_metaData ) {
00473       for ( TQMap<TQString, TQString>::iterator it = m_metaData->begin(); it != m_metaData->end(); ++it ) {
00474           job->addMetaData( it.key(), it.data() );
00475       }
00476   }
00477 
00478   if ( finalURL ) {
00479       SimpleJob *sj = dynamic_cast<SimpleJob*>( job );
00480       if ( sj ) {
00481           m_url = sj->url();
00482       }
00483   }
00484 
00485   connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
00486            this, TQT_SLOT( slotResult (TDEIO::Job *) ) );
00487 
00488   TQMetaObject *meta = job->metaObject();
00489 
00490   static const char dataSignal[] = "data(TDEIO::Job*,const " TQBYTEARRAY_OBJECT_NAME_STRING "&)";
00491   if ( meta->findSignal( dataSignal ) != -1 ) {
00492       connect( job, TQT_SIGNAL(data(TDEIO::Job*,const TQByteArray&)),
00493                this, TQT_SLOT(slotData(TDEIO::Job*,const TQByteArray&)) );
00494   }
00495 
00496   static const char redirSignal[] = "redirection(TDEIO::Job*,const KURL&)";
00497   if ( meta->findSignal( redirSignal ) != -1 ) {
00498       connect( job, TQT_SIGNAL(redirection(TDEIO::Job*,const KURL&)),
00499                this, TQT_SLOT(slotRedirection(TDEIO::Job*, const KURL&)) );
00500   }
00501 
00502   enter_loop();
00503 
00504   if ( finalURL )
00505       *finalURL = m_url;
00506   if ( data )
00507       *data = m_data;
00508 
00509   return bJobOK;
00510 }
00511 
00512 // If a troll sees this, he kills me
00513 void tqt_enter_modal( TQWidget *widget );
00514 void tqt_leave_modal( TQWidget *widget );
00515 
00516 void NetAccess::enter_loop()
00517 {
00518   TQWidget dummy(0,0,(WFlags)(WType_Dialog | WShowModal));
00519   dummy.setFocusPolicy( TQ_NoFocus );
00520   tqt_enter_modal(&dummy);
00521   tqApp->enter_loop();
00522   tqt_leave_modal(&dummy);
00523 }
00524 
00525 void NetAccess::slotResult( TDEIO::Job * job )
00526 {
00527   lastErrorCode = job->error();
00528   bJobOK = !job->error();
00529   if ( !bJobOK ) {
00530     if ( !lastErrorMsg ) {
00531       lastErrorMsg = new TQString;
00532     }
00533     *lastErrorMsg = job->errorString();
00534   }
00535   if ( job->isA("TDEIO::StatJob") ) {
00536     m_entry = static_cast<TDEIO::StatJob *>(job)->statResult();
00537   }
00538 
00539   if ( m_metaData ) {
00540     *m_metaData = job->metaData();
00541   }
00542 
00543   tqApp->exit_loop();
00544 }
00545 
00546 void NetAccess::slotData( TDEIO::Job*, const TQByteArray& data )
00547 {
00548   if ( data.isEmpty() ) {
00549     return;
00550   }
00551 
00552   unsigned offset = m_data.size();
00553   m_data.resize( offset + data.size() );
00554   std::memcpy( m_data.data() + offset, data.data(), data.size() );
00555 }
00556 
00557 void NetAccess::slotRedirection( TDEIO::Job*, const KURL& url )
00558 {
00559   m_url = url;
00560 }
00561 
00562 #include "netaccess.moc"

tdeio/tdeio

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

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.7.6.1
This website is maintained by Timothy Pearson.