2
This file is part of the KDE libraries
3
Copyright (C) 1997 Torben Weis (weis@kde.org)
4
Copyright (C) 1998 Matthias Ettrich (ettrich@kde.org)
5
Copyright (C) 1999 David Faure (faure@kde.org)
7
This library is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Library General Public
9
License as published by the Free Software Foundation; either
10
version 2 of the License, or (at your option) any later version.
12
This library is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
Library General Public License for more details.
17
You should have received a copy of the GNU Library General Public License
18
along with this library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
Boston, MA 02111-1307, USA.
31
#include <qapplication.h>
33
#include <qmetaobject.h>
35
#include <kapplication.h>
37
#include <ktempfile.h>
41
#include <kio/scheduler.h>
43
#include "netaccess.h"
45
using namespace KIO_COMPAT;
47
QString * NetAccess::lastErrorMsg;
48
int NetAccess::lastErrorCode = 0;
49
QStringList* NetAccess::tmpfiles;
51
bool NetAccess::download(const KURL& u, QString & target)
53
return NetAccess::download (u, target, 0);
56
bool NetAccess::download(const KURL& u, QString & target, QWidget* window)
58
if (u.isLocalFile()) {
59
// file protocol. We do not need the network
61
bool accessible = checkAccess(target, R_OK);
65
lastErrorMsg = new QString;
66
*lastErrorMsg = i18n("File '%1' is not readable").arg(target);
67
lastErrorCode = KIO::ERR_COULD_NOT_READ;
75
target = tmpFile.name();
77
tmpfiles = new QStringList;
78
tmpfiles->append(target);
83
dest.setPath( target );
84
return kioNet.filecopyInternal( u, dest, -1, true /*overwrite*/,
85
false, window, false /*copy*/);
88
bool NetAccess::upload(const QString& src, const KURL& target)
90
return NetAccess::upload(src, target, 0);
93
bool NetAccess::upload(const QString& src, const KURL& target, QWidget* window)
98
// If target is local... well, just copy. This can be useful
99
// when the client code uses a temp file no matter what.
100
// Let's make sure it's not the exact same file though
101
if (target.isLocalFile() && target.path() == src)
107
return kioNet.filecopyInternal( s, target, -1, true /*overwrite*/,
108
false, window, false /*copy*/ );
111
bool NetAccess::copy( const KURL & src, const KURL & target )
113
return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, 0L );
116
bool NetAccess::copy( const KURL & src, const KURL & target, QWidget* window )
118
return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, window );
121
bool NetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
122
bool overwrite, bool resume, QWidget* window )
125
return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
126
window, false /*copy*/ );
130
bool NetAccess::file_move( const KURL& src, const KURL& target, int permissions,
131
bool overwrite, bool resume, QWidget* window )
134
return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
135
window, true /*move*/ );
138
bool NetAccess::dircopy( const KURL & src, const KURL & target )
140
return NetAccess::dircopy( src, target, 0 );
143
bool NetAccess::dircopy( const KURL & src, const KURL & target, QWidget* window )
146
srcList.append( src );
147
return NetAccess::dircopy( srcList, target, window );
150
bool NetAccess::dircopy( const KURL::List & srcList, const KURL & target, QWidget* window )
153
return kioNet.dircopyInternal( srcList, target, window, false /*copy*/ );
156
bool NetAccess::move( const KURL& src, const KURL& target, QWidget* window )
159
srcList.append( src );
160
return NetAccess::move( srcList, target, window );
163
bool NetAccess::move( const KURL::List& srcList, const KURL& target, QWidget* window )
166
return kioNet.dircopyInternal( srcList, target, window, true /*move*/ );
169
bool NetAccess::exists( const KURL & url )
171
return NetAccess::exists( url, false, 0 );
174
bool NetAccess::exists( const KURL & url, QWidget* window )
176
return NetAccess::exists( url, false, window );
179
bool NetAccess::exists( const KURL & url, bool source )
181
return NetAccess::exists( url, source, 0 );
184
bool NetAccess::exists( const KURL & url, bool source, QWidget* window )
186
if ( url.isLocalFile() )
187
return QFile::exists( url.path() );
189
return kioNet.statInternal( url, 0 /*no details*/, source, window );
192
bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry )
194
return NetAccess::stat( url, entry, 0 );
197
bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry, QWidget* window )
200
bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
202
entry = kioNet.m_entry;
206
bool NetAccess::del( const KURL & url )
208
return NetAccess::del( url, 0 );
211
bool NetAccess::del( const KURL & url, QWidget* window )
214
return kioNet.delInternal( url, window );
217
bool NetAccess::mkdir( const KURL & url, int permissions )
219
return NetAccess::mkdir( url, 0, permissions );
222
bool NetAccess::mkdir( const KURL & url, QWidget* window, int permissions )
225
return kioNet.mkdirInternal( url, permissions, window );
228
QString NetAccess::fish_execute( const KURL & url, const QString command, QWidget* window )
231
return kioNet.fish_executeInternal( url, command, window );
234
bool NetAccess::synchronousRun( KIO::Job* job, QWidget* window, QByteArray* data,
235
KURL* finalURL, QMap<QString, QString>* metaData )
238
return kioNet.synchronousRunInternal( job, window, data, finalURL, metaData );
241
QString NetAccess::mimetype( const KURL& url )
244
return kioNet.mimetypeInternal( url, 0 );
247
QString NetAccess::mimetype( const KURL& url, QWidget* window )
250
return kioNet.mimetypeInternal( url, window );
253
void NetAccess::removeTempFile(const QString& name)
257
if (tmpfiles->contains(name))
259
unlink(QFile::encodeName(name));
260
tmpfiles->remove(name);
264
bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
265
bool overwrite, bool resume, QWidget* window, bool move)
267
bJobOK = true; // success unless further error occurs
269
KIO::Scheduler::checkSlaveOnHold(true);
270
KIO::Job * job = move
271
? KIO::file_move( src, target, permissions, overwrite, resume )
272
: KIO::file_copy( src, target, permissions, overwrite, resume );
273
job->setWindow (window);
274
connect( job, SIGNAL( result (KIO::Job *) ),
275
this, SLOT( slotResult (KIO::Job *) ) );
281
bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
282
QWidget* window, bool move)
284
bJobOK = true; // success unless further error occurs
286
KIO::Job * job = move
287
? KIO::move( src, target )
288
: KIO::copy( src, target );
289
job->setWindow (window);
290
connect( job, SIGNAL( result (KIO::Job *) ),
291
this, SLOT( slotResult (KIO::Job *) ) );
297
bool NetAccess::statInternal( const KURL & url, int details, bool source,
300
bJobOK = true; // success unless further error occurs
301
KIO::StatJob * job = KIO::stat( url, !url.isLocalFile() );
302
job->setWindow (window);
303
job->setDetails( details );
304
job->setSide( source );
305
connect( job, SIGNAL( result (KIO::Job *) ),
306
this, SLOT( slotResult (KIO::Job *) ) );
311
bool NetAccess::delInternal( const KURL & url, QWidget* window )
313
bJobOK = true; // success unless further error occurs
314
KIO::Job * job = KIO::del( url );
315
job->setWindow (window);
316
connect( job, SIGNAL( result (KIO::Job *) ),
317
this, SLOT( slotResult (KIO::Job *) ) );
322
bool NetAccess::mkdirInternal( const KURL & url, int permissions,
325
bJobOK = true; // success unless further error occurs
326
KIO::Job * job = KIO::mkdir( url, permissions );
327
job->setWindow (window);
328
connect( job, SIGNAL( result (KIO::Job *) ),
329
this, SLOT( slotResult (KIO::Job *) ) );
334
QString NetAccess::mimetypeInternal( const KURL & url, QWidget* window )
336
bJobOK = true; // success unless further error occurs
337
m_mimetype = QString::fromLatin1("unknown");
338
KIO::Job * job = KIO::mimetype( url );
339
job->setWindow (window);
340
connect( job, SIGNAL( result (KIO::Job *) ),
341
this, SLOT( slotResult (KIO::Job *) ) );
342
connect( job, SIGNAL( mimetype (KIO::Job *, const QString &) ),
343
this, SLOT( slotMimetype (KIO::Job *, const QString &) ) );
348
void NetAccess::slotMimetype( KIO::Job *, const QString & type )
353
QString NetAccess::fish_executeInternal(const KURL & url, const QString command, QWidget* window)
355
QString target, remoteTempFileName, resultData;
358
tmpFile.setAutoDelete( true );
360
if( url.protocol() == "fish" )
362
// construct remote temp filename
364
remoteTempFileName = tmpFile.name();
365
// only need the filename KTempFile adds some KDE specific dirs
366
// that probably does not exist on the remote side
367
int pos = remoteTempFileName.findRev('/');
368
remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
369
tempPathUrl.setPath( remoteTempFileName );
370
bJobOK = true; // success unless further error occurs
371
QByteArray packedArgs;
372
QDataStream stream( packedArgs, IO_WriteOnly );
374
stream << int('X') << tempPathUrl << command;
376
KIO::Job * job = KIO::special( tempPathUrl, packedArgs, true );
377
job->setWindow( window );
378
connect( job, SIGNAL( result (KIO::Job *) ),
379
this, SLOT( slotResult (KIO::Job *) ) );
382
// since the KIO::special does not provide feedback we need to download the result
383
if( NetAccess::download( tempPathUrl, target, window ) )
385
QFile resultFile( target );
387
if (resultFile.open( IO_ReadOnly ))
389
QTextStream ts( &resultFile );
390
ts.setEncoding( QTextStream::Locale ); // Locale??
391
resultData = ts.read();
393
NetAccess::del( tempPathUrl, window );
399
resultData = QString( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
404
bool NetAccess::synchronousRunInternal( KIO::Job* job, QWidget* window, QByteArray* data,
405
KURL* finalURL, QMap<QString,QString>* metaData )
407
job->setWindow( window );
409
m_metaData = metaData;
411
for ( QMap<QString, QString>::iterator it = m_metaData->begin(); it != m_metaData->end(); ++it ) {
412
job->addMetaData( it.key(), it.data() );
417
KIO::SimpleJob *sj = dynamic_cast<KIO::SimpleJob*>( job );
423
connect( job, SIGNAL( result (KIO::Job *) ),
424
this, SLOT( slotResult (KIO::Job *) ) );
426
QMetaObject *meta = job->metaObject();
428
static const char dataSignal[] = "data(KIO::Job*,const QByteArray&)";
429
if ( meta->findSignal( dataSignal ) != -1 ) {
430
connect( job, SIGNAL(data(KIO::Job*,const QByteArray&)),
431
this, SLOT(slotData(KIO::Job*,const QByteArray&)) );
434
static const char redirSignal[] = "redirection(KIO::Job*,const KURL&)";
435
if ( meta->findSignal( redirSignal ) != -1 ) {
436
connect( job, SIGNAL(redirection(KIO::Job*,const KURL&)),
437
this, SLOT(slotRedirection(KIO::Job*, const KURL&)) );
450
// If a troll sees this, he kills me
451
void qt_enter_modal( QWidget *widget );
452
void qt_leave_modal( QWidget *widget );
454
void NetAccess::enter_loop()
456
QWidget dummy(0,0,WType_Dialog | WShowModal);
457
dummy.setFocusPolicy( QWidget::NoFocus );
458
qt_enter_modal(&dummy);
460
qt_leave_modal(&dummy);
463
void NetAccess::slotResult( KIO::Job * job )
465
lastErrorCode = job->error();
466
bJobOK = !job->error();
470
lastErrorMsg = new QString;
471
*lastErrorMsg = job->errorString();
473
if ( job->isA("KIO::StatJob") )
474
m_entry = static_cast<KIO::StatJob *>(job)->statResult();
477
*m_metaData = job->metaData();
482
void NetAccess::slotData( KIO::Job*, const QByteArray& data )
484
if ( data.isEmpty() )
487
unsigned offset = m_data.size();
488
m_data.resize( offset + data.size() );
489
std::memcpy( m_data.data() + offset, data.data(), data.size() );
492
void NetAccess::slotRedirection( KIO::Job*, const KURL& url )
497
#include "netaccess.moc"