00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
int NetAccess::lastErrorCode = 0;
00047
QStringList* NetAccess::tmpfiles;
00048
00049 bool NetAccess::download(
const KURL& u,
QString & target)
00050 {
00051
return NetAccess::download (u, target, 0);
00052 }
00053
00054 bool NetAccess::download(
const KURL& u,
QString & target,
QWidget* window)
00055 {
00056
if (u.
isLocalFile()) {
00057
00058 target = u.
path();
00059
bool accessible = checkAccess(target, R_OK);
00060
if(!accessible)
00061 {
00062
if(!lastErrorMsg)
00063 lastErrorMsg =
new QString;
00064 *lastErrorMsg = i18n(
"File '%1' is not readable").arg(target);
00065 lastErrorCode = ERR_COULD_NOT_READ;
00066 }
00067
return accessible;
00068 }
00069
00070
if (target.isEmpty())
00071 {
00072
KTempFile tmpFile;
00073 target = tmpFile.
name();
00074
if (!tmpfiles)
00075 tmpfiles =
new QStringList;
00076 tmpfiles->append(target);
00077 }
00078
00079
NetAccess kioNet;
00080
KURL dest;
00081 dest.
setPath( target );
00082
return kioNet.
filecopyInternal( u, dest, -1,
true ,
00083
false, window,
false );
00084 }
00085
00086 bool NetAccess::upload(
const QString& src,
const KURL& target)
00087 {
00088
return NetAccess::upload(src, target, 0);
00089 }
00090
00091 bool NetAccess::upload(
const QString& src,
const KURL& target,
QWidget* window)
00092 {
00093
if (target.
isEmpty())
00094
return false;
00095
00096
00097
00098
00099
if (target.
isLocalFile() && target.
path() == src)
00100
return true;
00101
00102
NetAccess kioNet;
00103
KURL s;
00104 s.
setPath(src);
00105
return kioNet.
filecopyInternal( s, target, -1,
true ,
00106
false, window,
false );
00107 }
00108
00109 bool NetAccess::copy(
const KURL & src,
const KURL & target )
00110 {
00111
return NetAccess::file_copy( src, target, -1,
false ,
false, 0L );
00112 }
00113
00114 bool NetAccess::copy(
const KURL & src,
const KURL & target,
QWidget* window )
00115 {
00116
return NetAccess::file_copy( src, target, -1,
false ,
false, window );
00117 }
00118
00119 bool NetAccess::file_copy(
const KURL& src,
const KURL& target,
int permissions,
00120
bool overwrite,
bool resume,
QWidget* window )
00121 {
00122
NetAccess kioNet;
00123
return kioNet.
filecopyInternal( src, target, permissions, overwrite, resume,
00124 window,
false );
00125 }
00126
00127
00128 bool NetAccess::file_move(
const KURL& src,
const KURL& target,
int permissions,
00129
bool overwrite,
bool resume,
QWidget* window )
00130 {
00131
NetAccess kioNet;
00132
return kioNet.
filecopyInternal( src, target, permissions, overwrite, resume,
00133 window,
true );
00134 }
00135
00136 bool NetAccess::dircopy(
const KURL & src,
const KURL & target )
00137 {
00138
return NetAccess::dircopy( src, target, 0 );
00139 }
00140
00141 bool NetAccess::dircopy(
const KURL & src,
const KURL & target,
QWidget* window )
00142 {
00143
KURL::List srcList;
00144 srcList.append( src );
00145
return NetAccess::dircopy( srcList, target, window );
00146 }
00147
00148 bool NetAccess::dircopy(
const KURL::List & srcList,
const KURL & target,
QWidget* window )
00149 {
00150
NetAccess kioNet;
00151
return kioNet.
dircopyInternal( srcList, target, window,
false );
00152 }
00153
00154 bool NetAccess::move(
const KURL& src,
const KURL& target,
QWidget* window )
00155 {
00156
KURL::List srcList;
00157 srcList.append( src );
00158
return NetAccess::move( srcList, target, window );
00159 }
00160
00161 bool NetAccess::move(
const KURL::List& srcList,
const KURL& target,
QWidget* window )
00162 {
00163
NetAccess kioNet;
00164
return kioNet.
dircopyInternal( srcList, target, window,
true );
00165 }
00166
00167 bool NetAccess::exists(
const KURL & url )
00168 {
00169
return NetAccess::exists( url,
false, 0 );
00170 }
00171
00172 bool NetAccess::exists(
const KURL & url,
QWidget* window )
00173 {
00174
return NetAccess::exists( url,
false, window );
00175 }
00176
00177 bool NetAccess::exists(
const KURL & url,
bool source )
00178 {
00179
return NetAccess::exists( url, source, 0 );
00180 }
00181
00182 bool NetAccess::exists(
const KURL & url,
bool source,
QWidget* window )
00183 {
00184
if ( url.
isLocalFile() )
00185
return QFile::exists( url.
path() );
00186
NetAccess kioNet;
00187
return kioNet.
statInternal( url, 0 , source, window );
00188 }
00189
00190 bool NetAccess::stat(
const KURL & url,
KIO::UDSEntry & entry )
00191 {
00192
return NetAccess::stat( url, entry, 0 );
00193 }
00194
00195 bool NetAccess::stat(
const KURL & url,
KIO::UDSEntry & entry,
QWidget* window )
00196 {
00197
NetAccess kioNet;
00198
bool ret = kioNet.
statInternal( url, 2 ,
true , window );
00199
if (ret)
00200 entry = kioNet.
m_entry;
00201
return ret;
00202 }
00203
00204 bool NetAccess::del(
const KURL & url )
00205 {
00206
return NetAccess::del( url, 0 );
00207 }
00208
00209 bool NetAccess::del(
const KURL & url,
QWidget* window )
00210 {
00211
NetAccess kioNet;
00212
return kioNet.
delInternal( url, window );
00213 }
00214
00215 bool NetAccess::mkdir(
const KURL & url,
int permissions )
00216 {
00217
return NetAccess::mkdir( url, 0, permissions );
00218 }
00219
00220 bool NetAccess::mkdir(
const KURL & url,
QWidget* window,
int permissions )
00221 {
00222
NetAccess kioNet;
00223
return kioNet.
mkdirInternal( url, permissions, window );
00224 }
00225
00226 QString NetAccess::fish_execute(
const KURL & url,
const QString command,
QWidget* window )
00227 {
00228
NetAccess kioNet;
00229
return kioNet.
fish_executeInternal( url, command, window );
00230 }
00231
00232 QString NetAccess::mimetype(
const KURL& url )
00233 {
00234
NetAccess kioNet;
00235
return kioNet.
mimetypeInternal( url, 0 );
00236 }
00237
00238
QString NetAccess::mimetype(
const KURL& url,
QWidget* window )
00239 {
00240
NetAccess kioNet;
00241
return kioNet.
mimetypeInternal( url, window );
00242 }
00243
00244 void NetAccess::removeTempFile(
const QString& name)
00245 {
00246
if (!tmpfiles)
00247
return;
00248
if (tmpfiles->contains(name))
00249 {
00250 unlink(QFile::encodeName(name));
00251 tmpfiles->remove(name);
00252 }
00253 }
00254
00255
bool NetAccess::filecopyInternal(
const KURL& src,
const KURL& target,
int permissions,
00256
bool overwrite,
bool resume,
QWidget* window,
bool move)
00257 {
00258 bJobOK =
true;
00259
00260
KIO::Scheduler::checkSlaveOnHold(
true);
00261
KIO::Job * job =
move
00262 ?
KIO::file_move( src, target, permissions, overwrite, resume )
00263 : KIO::file_copy( src, target, permissions, overwrite, resume );
00264 job->
setWindow (window);
00265 connect( job, SIGNAL( result (
KIO::Job *) ),
00266
this, SLOT( slotResult (KIO::Job *) ) );
00267
00268 enter_loop();
00269
return bJobOK;
00270 }
00271
00272
bool NetAccess::dircopyInternal(
const KURL::List& src,
const KURL& target,
00273
QWidget* window,
bool move)
00274 {
00275 bJobOK =
true;
00276
00277 KIO::Job * job =
move
00278 ?
KIO::move( src, target )
00279 : KIO::copy( src, target );
00280 job->
setWindow (window);
00281 connect( job, SIGNAL( result (KIO::Job *) ),
00282
this, SLOT( slotResult (KIO::Job *) ) );
00283
00284 enter_loop();
00285
return bJobOK;
00286 }
00287
00288
bool NetAccess::statInternal(
const KURL & url,
int details,
bool source,
00289
QWidget* window )
00290 {
00291 bJobOK =
true;
00292
KIO::StatJob * job =
KIO::stat( url, !url.
isLocalFile() );
00293 job->
setWindow (window);
00294 job->
setDetails( details );
00295 job->
setSide( source );
00296 connect( job, SIGNAL( result (KIO::Job *) ),
00297
this, SLOT( slotResult (KIO::Job *) ) );
00298 enter_loop();
00299
return bJobOK;
00300 }
00301
00302
bool NetAccess::delInternal(
const KURL & url,
QWidget* window )
00303 {
00304 bJobOK =
true;
00305 KIO::Job * job =
KIO::del( url );
00306 job->
setWindow (window);
00307 connect( job, SIGNAL( result (KIO::Job *) ),
00308
this, SLOT( slotResult (KIO::Job *) ) );
00309 enter_loop();
00310
return bJobOK;
00311 }
00312
00313
bool NetAccess::mkdirInternal(
const KURL & url,
int permissions,
00314
QWidget* window )
00315 {
00316 bJobOK =
true;
00317 KIO::Job * job =
KIO::mkdir( url, permissions );
00318 job->
setWindow (window);
00319 connect( job, SIGNAL( result (KIO::Job *) ),
00320
this, SLOT( slotResult (KIO::Job *) ) );
00321 enter_loop();
00322
return bJobOK;
00323 }
00324
00325
QString NetAccess::mimetypeInternal(
const KURL & url,
QWidget* window )
00326 {
00327 bJobOK =
true;
00328 m_mimetype = QString::fromLatin1(
"unknown");
00329 KIO::Job * job =
KIO::mimetype( url );
00330 job->
setWindow (window);
00331 connect( job, SIGNAL( result (KIO::Job *) ),
00332
this, SLOT( slotResult (KIO::Job *) ) );
00333 connect( job, SIGNAL( mimetype (KIO::Job *,
const QString &) ),
00334
this, SLOT( slotMimetype (KIO::Job *,
const QString &) ) );
00335 enter_loop();
00336
return m_mimetype;
00337 }
00338
00339
void NetAccess::slotMimetype( KIO::Job *,
const QString & type )
00340 {
00341 m_mimetype = type;
00342 }
00343
00344
QString NetAccess::fish_executeInternal(
const KURL & url,
const QString command,
QWidget* window)
00345 {
00346
QString target, remoteTempFileName, resultData;
00347
KURL tempPathUrl;
00348
KTempFile tmpFile;
00349 tmpFile.
setAutoDelete(
true );
00350
00351
if( url.
protocol() ==
"fish" )
00352 {
00353
00354 tempPathUrl = url;
00355 remoteTempFileName = tmpFile.
name();
00356
00357
00358
int pos = remoteTempFileName.findRev(
'/');
00359 remoteTempFileName =
"/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
00360 tempPathUrl.
setPath( remoteTempFileName );
00361 bJobOK =
true;
00362
QByteArray packedArgs;
00363
QDataStream stream( packedArgs, IO_WriteOnly );
00364
00365 stream << int(
'X') << tempPathUrl << command;
00366
00367 KIO::Job * job =
KIO::special( tempPathUrl, packedArgs,
true );
00368 job->
setWindow( window );
00369 connect( job, SIGNAL( result (KIO::Job *) ),
00370
this, SLOT( slotResult (KIO::Job *) ) );
00371 enter_loop();
00372
00373
00374
if(
NetAccess::download( tempPathUrl, target, window ) )
00375 {
00376
QFile resultFile( target );
00377
00378
if (resultFile.open( IO_ReadOnly ))
00379 {
00380
QTextStream ts( &resultFile );
00381 ts.setEncoding( QTextStream::Locale );
00382 resultData = ts.read();
00383 resultFile.close();
00384
NetAccess::del( tempPathUrl, window );
00385 }
00386 }
00387 }
00388
else
00389 {
00390 resultData =
QString(
"ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
00391 }
00392
return resultData;
00393 }
00394
00395
00396
void qt_enter_modal(
QWidget *widget );
00397
void qt_leave_modal(
QWidget *widget );
00398
00399
void NetAccess::enter_loop()
00400 {
00401
QWidget dummy(0,0,WType_Dialog | WShowModal);
00402 dummy.setFocusPolicy( QWidget::NoFocus );
00403 qt_enter_modal(&dummy);
00404 qApp->enter_loop();
00405 qt_leave_modal(&dummy);
00406 }
00407
00408
void NetAccess::slotResult( KIO::Job * job )
00409 {
00410 lastErrorCode = job->
error();
00411 bJobOK = !job->
error();
00412
if ( !bJobOK )
00413 {
00414
if ( !lastErrorMsg )
00415 lastErrorMsg =
new QString;
00416 *lastErrorMsg = job->
errorString();
00417 }
00418
if ( job->isA(
"KIO::StatJob") )
00419 m_entry = static_cast<KIO::StatJob *>(job)->statResult();
00420 qApp->exit_loop();
00421 }
00422
00423
#include "netaccess.moc"