netaccess.cpp
/* $Id: netaccess.cpp 580558 2006-09-03 21:46:23Z osterfeld $
This file is part of the KDE libraries
Copyright (C) 1997 Torben Weis (weis@kde.org)
Copyright (C) 1998 Matthias Ettrich (ettrich@kde.org)
Copyright (C) 1999 David Faure (faure@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <cstring>
#include <qstring.h>
#include <qapplication.h>
#include <qfile.h>
#include <qmetaobject.h>
#include <kapplication.h>
#include <klocale.h>
#include <ktempfile.h>
#include <kdebug.h>
#include <kurl.h>
#include <kio/job.h>
#include <kio/scheduler.h>
#include "kio/netaccess.h"
using namespace KIO;
QString * NetAccess::lastErrorMsg;
int NetAccess::lastErrorCode = 0;
QStringList* NetAccess::tmpfiles;
bool NetAccess::download(const KURL& u, QString & target)
{
return NetAccess::download (u, target, 0);
}
bool NetAccess::download(const KURL& u, QString & target, QWidget* window)
{
if (u.isLocalFile()) {
// file protocol. We do not need the network
target = u.path();
bool accessible = checkAccess(target, R_OK);
if(!accessible)
{
if(!lastErrorMsg)
lastErrorMsg = new QString;
*lastErrorMsg = i18n("File '%1' is not readable").arg(target);
lastErrorCode = ERR_COULD_NOT_READ;
}
return accessible;
}
if (target.isEmpty())
{
KTempFile tmpFile;
target = tmpFile.name();
if (!tmpfiles)
tmpfiles = new QStringList;
tmpfiles->append(target);
}
NetAccess kioNet;
KURL dest;
dest.setPath( target );
return kioNet.filecopyInternal( u, dest, -1, true /*overwrite*/,
false, window, false /*copy*/);
}
bool NetAccess::upload(const QString& src, const KURL& target)
{
return NetAccess::upload(src, target, 0);
}
bool NetAccess::upload(const QString& src, const KURL& target, QWidget* window)
{
if (target.isEmpty())
return false;
// If target is local... well, just copy. This can be useful
// when the client code uses a temp file no matter what.
// Let's make sure it's not the exact same file though
if (target.isLocalFile() && target.path() == src)
return true;
NetAccess kioNet;
KURL s;
s.setPath(src);
return kioNet.filecopyInternal( s, target, -1, true /*overwrite*/,
false, window, false /*copy*/ );
}
bool NetAccess::copy( const KURL & src, const KURL & target )
{
return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, 0L );
}
bool NetAccess::copy( const KURL & src, const KURL & target, QWidget* window )
{
return NetAccess::file_copy( src, target, -1, false /*not overwrite*/, false, window );
}
bool NetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
bool overwrite, bool resume, QWidget* window )
{
NetAccess kioNet;
return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
window, false /*copy*/ );
}
bool NetAccess::file_move( const KURL& src, const KURL& target, int permissions,
bool overwrite, bool resume, QWidget* window )
{
NetAccess kioNet;
return kioNet.filecopyInternal( src, target, permissions, overwrite, resume,
window, true /*move*/ );
}
bool NetAccess::dircopy( const KURL & src, const KURL & target )
{
return NetAccess::dircopy( src, target, 0 );
}
bool NetAccess::dircopy( const KURL & src, const KURL & target, QWidget* window )
{
KURL::List srcList;
srcList.append( src );
return NetAccess::dircopy( srcList, target, window );
}
bool NetAccess::dircopy( const KURL::List & srcList, const KURL & target, QWidget* window )
{
NetAccess kioNet;
return kioNet.dircopyInternal( srcList, target, window, false /*copy*/ );
}
bool NetAccess::move( const KURL& src, const KURL& target, QWidget* window )
{
KURL::List srcList;
srcList.append( src );
return NetAccess::move( srcList, target, window );
}
bool NetAccess::move( const KURL::List& srcList, const KURL& target, QWidget* window )
{
NetAccess kioNet;
return kioNet.dircopyInternal( srcList, target, window, true /*move*/ );
}
bool NetAccess::exists( const KURL & url )
{
return NetAccess::exists( url, false, 0 );
}
bool NetAccess::exists( const KURL & url, QWidget* window )
{
return NetAccess::exists( url, false, window );
}
bool NetAccess::exists( const KURL & url, bool source )
{
return NetAccess::exists( url, source, 0 );
}
bool NetAccess::exists( const KURL & url, bool source, QWidget* window )
{
if ( url.isLocalFile() )
return QFile::exists( url.path() );
NetAccess kioNet;
return kioNet.statInternal( url, 0 /*no details*/, source, window );
}
bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry )
{
return NetAccess::stat( url, entry, 0 );
}
bool NetAccess::stat( const KURL & url, KIO::UDSEntry & entry, QWidget* window )
{
NetAccess kioNet;
bool ret = kioNet.statInternal( url, 2 /*all details*/, true /*source*/, window );
if (ret)
entry = kioNet.m_entry;
return ret;
}
KURL NetAccess::mostLocalURL(const KURL & url, QWidget* window)
{
if ( url.isLocalFile() )
{
return url;
}
KIO::UDSEntry entry;
if (!stat(url, entry, window))
{
return url;
}
QString path;
// Extract the local path from the KIO::UDSEntry
KIO::UDSEntry::ConstIterator it = entry.begin();
const KIO::UDSEntry::ConstIterator end = entry.end();
for ( ; it != end; ++it )
{
if ( (*it).m_uds == KIO::UDS_LOCAL_PATH )
{
path = (*it).m_str;
break;
}
}
if ( !path.isEmpty() )
{
KURL new_url;
new_url.setPath(path);
return new_url;
}
return url;
}
bool NetAccess::del( const KURL & url )
{
return NetAccess::del( url, 0 );
}
bool NetAccess::del( const KURL & url, QWidget* window )
{
NetAccess kioNet;
return kioNet.delInternal( url, window );
}
bool NetAccess::mkdir( const KURL & url, int permissions )
{
return NetAccess::mkdir( url, 0, permissions );
}
bool NetAccess::mkdir( const KURL & url, QWidget* window, int permissions )
{
NetAccess kioNet;
return kioNet.mkdirInternal( url, permissions, window );
}
QString NetAccess::fish_execute( const KURL & url, const QString command, QWidget* window )
{
NetAccess kioNet;
return kioNet.fish_executeInternal( url, command, window );
}
bool NetAccess::synchronousRun( Job* job, QWidget* window, QByteArray* data,
KURL* finalURL, QMap<QString, QString>* metaData )
{
NetAccess kioNet;
return kioNet.synchronousRunInternal( job, window, data, finalURL, metaData );
}
QString NetAccess::mimetype( const KURL& url )
{
NetAccess kioNet;
return kioNet.mimetypeInternal( url, 0 );
}
QString NetAccess::mimetype( const KURL& url, QWidget* window )
{
NetAccess kioNet;
return kioNet.mimetypeInternal( url, window );
}
void NetAccess::removeTempFile(const QString& name)
{
if (!tmpfiles)
return;
if (tmpfiles->contains(name))
{
unlink(QFile::encodeName(name));
tmpfiles->remove(name);
}
}
bool NetAccess::filecopyInternal(const KURL& src, const KURL& target, int permissions,
bool overwrite, bool resume, QWidget* window, bool move)
{
bJobOK = true; // success unless further error occurs
KIO::Scheduler::checkSlaveOnHold(true);
KIO::Job * job = move
? KIO::file_move( src, target, permissions, overwrite, resume )
: KIO::file_copy( src, target, permissions, overwrite, resume );
job->setWindow (window);
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::dircopyInternal(const KURL::List& src, const KURL& target,
QWidget* window, bool move)
{
bJobOK = true; // success unless further error occurs
KIO::Job * job = move
? KIO::move( src, target )
: KIO::copy( src, target );
job->setWindow (window);
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::statInternal( const KURL & url, int details, bool source,
QWidget* window )
{
bJobOK = true; // success unless further error occurs
KIO::StatJob * job = KIO::stat( url, !url.isLocalFile() );
job->setWindow (window);
job->setDetails( details );
job->setSide( source );
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::delInternal( const KURL & url, QWidget* window )
{
bJobOK = true; // success unless further error occurs
KIO::Job * job = KIO::del( url );
job->setWindow (window);
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
enter_loop();
return bJobOK;
}
bool NetAccess::mkdirInternal( const KURL & url, int permissions,
QWidget* window )
{
bJobOK = true; // success unless further error occurs
KIO::Job * job = KIO::mkdir( url, permissions );
job->setWindow (window);
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
enter_loop();
return bJobOK;
}
QString NetAccess::mimetypeInternal( const KURL & url, QWidget* window )
{
bJobOK = true; // success unless further error occurs
m_mimetype = QString::fromLatin1("unknown");
KIO::Job * job = KIO::mimetype( url );
job->setWindow (window);
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
connect( job, SIGNAL( mimetype (KIO::Job *, const QString &) ),
this, SLOT( slotMimetype (KIO::Job *, const QString &) ) );
enter_loop();
return m_mimetype;
}
void NetAccess::slotMimetype( KIO::Job *, const QString & type )
{
m_mimetype = type;
}
QString NetAccess::fish_executeInternal(const KURL & url, const QString command, QWidget* window)
{
QString target, remoteTempFileName, resultData;
KURL tempPathUrl;
KTempFile tmpFile;
tmpFile.setAutoDelete( true );
if( url.protocol() == "fish" )
{
// construct remote temp filename
tempPathUrl = url;
remoteTempFileName = tmpFile.name();
// only need the filename KTempFile adds some KDE specific dirs
// that probably does not exist on the remote side
int pos = remoteTempFileName.findRev('/');
remoteTempFileName = "/tmp/fishexec_" + remoteTempFileName.mid(pos + 1);
tempPathUrl.setPath( remoteTempFileName );
bJobOK = true; // success unless further error occurs
QByteArray packedArgs;
QDataStream stream( packedArgs, IO_WriteOnly );
stream << int('X') << tempPathUrl << command;
KIO::Job * job = KIO::special( tempPathUrl, packedArgs, true );
job->setWindow( window );
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
enter_loop();
// since the KIO::special does not provide feedback we need to download the result
if( NetAccess::download( tempPathUrl, target, window ) )
{
QFile resultFile( target );
if (resultFile.open( IO_ReadOnly ))
{
QTextStream ts( &resultFile );
ts.setEncoding( QTextStream::Locale ); // Locale??
resultData = ts.read();
resultFile.close();
NetAccess::del( tempPathUrl, window );
}
}
}
else
{
resultData = i18n( "ERROR: Unknown protocol '%1'" ).arg( url.protocol() );
}
return resultData;
}
bool NetAccess::synchronousRunInternal( Job* job, QWidget* window, QByteArray* data,
KURL* finalURL, QMap<QString,QString>* metaData )
{
job->setWindow( window );
m_metaData = metaData;
if ( m_metaData ) {
for ( QMap<QString, QString>::iterator it = m_metaData->begin(); it != m_metaData->end(); ++it ) {
job->addMetaData( it.key(), it.data() );
}
}
if ( finalURL ) {
SimpleJob *sj = dynamic_cast<SimpleJob*>( job );
if ( sj ) {
m_url = sj->url();
}
}
connect( job, SIGNAL( result (KIO::Job *) ),
this, SLOT( slotResult (KIO::Job *) ) );
QMetaObject *meta = job->metaObject();
static const char dataSignal[] = "data(KIO::Job*,const QByteArray&)";
if ( meta->findSignal( dataSignal ) != -1 ) {
connect( job, SIGNAL(data(KIO::Job*,const QByteArray&)),
this, SLOT(slotData(KIO::Job*,const QByteArray&)) );
}
static const char redirSignal[] = "redirection(KIO::Job*,const KURL&)";
if ( meta->findSignal( redirSignal ) != -1 ) {
connect( job, SIGNAL(redirection(KIO::Job*,const KURL&)),
this, SLOT(slotRedirection(KIO::Job*, const KURL&)) );
}
enter_loop();
if ( finalURL )
*finalURL = m_url;
if ( data )
*data = m_data;
return bJobOK;
}
// If a troll sees this, he kills me
void qt_enter_modal( QWidget *widget );
void qt_leave_modal( QWidget *widget );
void NetAccess::enter_loop()
{
QWidget dummy(0,0,WType_Dialog | WShowModal);
dummy.setFocusPolicy( QWidget::NoFocus );
qt_enter_modal(&dummy);
qApp->enter_loop();
qt_leave_modal(&dummy);
}
void NetAccess::slotResult( KIO::Job * job )
{
lastErrorCode = job->error();
bJobOK = !job->error();
if ( !bJobOK )
{
if ( !lastErrorMsg )
lastErrorMsg = new QString;
*lastErrorMsg = job->errorString();
}
if ( job->isA("KIO::StatJob") )
m_entry = static_cast<KIO::StatJob *>(job)->statResult();
if ( m_metaData )
*m_metaData = job->metaData();
qApp->exit_loop();
}
void NetAccess::slotData( KIO::Job*, const QByteArray& data )
{
if ( data.isEmpty() )
return;
unsigned offset = m_data.size();
m_data.resize( offset + data.size() );
std::memcpy( m_data.data() + offset, data.data(), data.size() );
}
void NetAccess::slotRedirection( KIO::Job*, const KURL& url )
{
m_url = url;
}
#include "netaccess.moc"
KIO::NetAccess
Net Transparency.
Definition: netaccess.h:59
KIO::UDS_LOCAL_PATH
A local file path if the ioslave display files sitting on the local filesystem (but in another hierar...
Definition: global.h:335
KIO::Job::addMetaData
void addMetaData(const QString &key, const QString &value)
Add key/value pair to the meta data that is sent to the slave.
Definition: job.cpp:405
KIO::NetAccess::removeTempFile
static void removeTempFile(const QString &name)
Removes the specified file if and only if it was created by KIO::NetAccess as a temporary file for a ...
Definition: netaccess.cpp:292
KIO::SimpleJob::url
const KURL & url() const
Returns the SimpleJob's URL.
Definition: jobclasses.h:549
KIO::NetAccess::download
static bool download(const KURL &src, QString &target, QWidget *window)
Downloads a file from an arbitrary URL (src) to a temporary file on the local filesystem (target)...
Definition: netaccess.cpp:57
KIO::Scheduler::checkSlaveOnHold
static void checkSlaveOnHold(bool b)
When true, the next job will check whether KLauncher has a slave on hold that is suitable for the job...
Definition: scheduler.h:280
KIO::StatJob::setSide
void setSide(bool source)
A stat() can have two meanings.
Definition: jobclasses.h:709
KIO::StatJob
A KIO job that retrieves information about a file or directory.
Definition: jobclasses.h:688
KIO::NetAccess::file_move
static bool file_move(const KURL &src, const KURL &target, int permissions=-1, bool overwrite=false, bool resume=false, QWidget *window=0L)
Full-fledged equivalent of KIO::file_move.
Definition: netaccess.cpp:131
KIO::NetAccess::mostLocalURL
static KURL mostLocalURL(const KURL &url, QWidget *window)
Tries to map a local URL for the given URL.
Definition: netaccess.cpp:207
KIO::NetAccess::upload
static bool upload(const QString &src, const KURL &target, QWidget *window)
Uploads file src to URL target.
Definition: netaccess.cpp:94
KIO::Job::errorString
QString errorString() const
Converts an error code and a non-i18n error message into an error message in the current language...
Definition: global.cpp:208
KIO::move
KIO_EXPORT CopyJob * move(const KURL &src, const KURL &dest, bool showProgressInfo=true)
Moves a file or directory src to the given destination dest.
Definition: job.cpp:3908
KIO::mimetype
KIO_EXPORT MimetypeJob * mimetype(const KURL &url, bool showProgressInfo=true)
Find mimetype for one file or directory.
Definition: job.cpp:1509
KIO::NetAccess::synchronousRun
static bool synchronousRun(Job *job, QWidget *window, QByteArray *data=0, KURL *finalURL=0, QMap< QString, QString > *metaData=0)
This function executes a job in a synchronous way.
Definition: netaccess.cpp:273
KIO::NetAccess::mkdir
static bool mkdir(const KURL &url, QWidget *window, int permissions=-1)
Creates a directory in a synchronous way.
Definition: netaccess.cpp:261
KIO::NetAccess::file_copy
static bool file_copy(const KURL &src, const KURL &dest, int permissions=-1, bool overwrite=false, bool resume=false, QWidget *window=0L)
Full-fledged equivalent of KIO::file_copy.
Definition: netaccess.cpp:122
KIO::NetAccess::stat
static bool stat(const KURL &url, KIO::UDSEntry &entry, QWidget *window)
Tests whether a URL exists and return information on it.
Definition: netaccess.cpp:198
KIO::Job::metaData
MetaData metaData() const
Get meta data received from the slave.
Definition: job.cpp:388
KIO::Job::error
int error() const
Returns the error code, if there has been an error.
Definition: jobclasses.h:95
KIO::stat
KIO_EXPORT StatJob * stat(const KURL &url, bool showProgressInfo=true)
Find all details for one file or directory.
Definition: job.cpp:886
KIO::NetAccess::del
static bool del(const KURL &url, QWidget *window)
Deletes a file or a directory in a synchronous way.
Definition: netaccess.cpp:250
KIO::file_move
KIO_EXPORT FileCopyJob * file_move(const KURL &src, const KURL &dest, int permissions=-1, bool overwrite=false, bool resume=false, bool showProgressInfo=true)
Move a single file.
Definition: job.cpp:1969
KIO::NetAccess::dircopy
static bool dircopy(const KURL &src, const KURL &target, QWidget *window)
Alternative method for copying over the network.
Definition: netaccess.cpp:144
KIO::del
KIO_EXPORT DeleteJob * del(const KURL &src, bool shred=false, bool showProgressInfo=true)
Delete a file or directory.
Definition: job.cpp:4386
KIO::UDSEntry
QValueList< UDSAtom > UDSEntry
An entry is the list of atoms containing all the informations for a file or URL.
Definition: global.h:504
KIO::StatJob::setDetails
void setDetails(short int details)
Selects the level of details we want.
Definition: jobclasses.h:720
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:68
KIO::NetAccess::fish_execute
static QString fish_execute(const KURL &url, const QString command, QWidget *window)
Executes a remote process via the fish ioslave in a synchronous way.
Definition: netaccess.cpp:267
KIO::NetAccess::exists
static bool exists(const KURL &url, bool source, QWidget *window)
Tests whether a URL exists.
Definition: netaccess.cpp:185
KIO::NetAccess::move
static bool move(const KURL &src, const KURL &target, QWidget *window=0L)
Full-fledged equivalent of KIO::move.
Definition: netaccess.cpp:157
KIO::mkdir
KIO_EXPORT SimpleJob * mkdir(const KURL &url, int permissions=-1)
Creates a single directory.
Definition: job.cpp:751
KIO::NetAccess::copy
static bool copy(const KURL &src, const KURL &target, QWidget *window)
Alternative to upload for copying over the network.
Definition: netaccess.cpp:117
KIO::special
KIO_EXPORT SimpleJob * special(const KURL &url, const QByteArray &data, bool showProgressInfo=true)
Execute any command that is specific to one slave (protocol).
Definition: job.cpp:786
KIO::SimpleJob
A simple job (one url and one command).
Definition: jobclasses.h:528
KIO::Job::setWindow
void setWindow(QWidget *window)
Associate this job with a window given by window.
Definition: job.cpp:352

KDE Home | KDE Accessibility Home | Description of Access Keys