forwardingslavebase.cpp
/* This file is part of the KDE project
Copyright (c) 2004 Kevin Ottens <ervin ipsquad net>
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 <kdebug.h>
#include <kio/job.h>
#include <kmimetype.h>
#include <kprotocolinfo.h>
#include <qapplication.h>
#include <qeventloop.h>
#include "forwardingslavebase.h"
namespace KIO
{
class ForwardingSlaveBasePrivate
{
};
ForwardingSlaveBase::ForwardingSlaveBase(const QCString &protocol,
const QCString &poolSocket,
const QCString &appSocket)
: QObject(), SlaveBase(protocol, poolSocket, appSocket)
{
}
ForwardingSlaveBase::~ForwardingSlaveBase()
{
}
bool ForwardingSlaveBase::internalRewriteURL(const KURL &url, KURL &newURL)
{
bool result = true;
if ( url.protocol().ascii()==mProtocol )
{
result = rewriteURL(url, newURL);
}
else
{
newURL = url;
}
m_processedURL = newURL;
m_requestedURL = url;
return result;
}
void ForwardingSlaveBase::prepareUDSEntry(KIO::UDSEntry &entry,
bool listing) const
{
kdDebug() << "ForwardingSlaveBase::prepareUDSEntry: listing=="
<< listing << endl;
bool url_found = false;
QString name;
KURL url;
KIO::UDSEntry::iterator it = entry.begin();
KIO::UDSEntry::iterator end = entry.end();
for(; it!=end; ++it)
{
KURL new_url = m_requestedURL;
switch( (*it).m_uds )
{
case KIO::UDS_NAME:
name = (*it).m_str;
kdDebug() << "Name = " << name << endl;
break;
case KIO::UDS_URL:
url_found = true;
url = (*it).m_str;
if (listing)
{
new_url.addPath(url.fileName());
}
(*it).m_str = new_url.url();
kdDebug() << "URL = " << url << endl;
kdDebug() << "New URL = " << (*it).m_str << endl;
break;
}
}
if ( m_processedURL.isLocalFile() )
{
KURL new_url = m_processedURL;
if (listing)
{
new_url.addPath( name );
}
KIO::UDSAtom atom;
atom.m_uds = KIO::UDS_LOCAL_PATH;
atom.m_long = 0;
atom.m_str = new_url.path();
entry.append(atom);
}
}
void ForwardingSlaveBase::get(const KURL &url)
{
kdDebug() << "ForwardingSlaveBase::get: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::TransferJob *job = KIO::get(new_url, false, false);
connectTransferJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::put(const KURL &url, int permissions,
bool overwrite, bool resume )
{
kdDebug() << "ForwardingSlaveBase::put: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::TransferJob *job = KIO::put(new_url, permissions, overwrite,
resume, false);
connectTransferJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::stat(const KURL &url)
{
kdDebug() << "ForwardingSlaveBase::stat: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::SimpleJob *job = KIO::stat(new_url, false);
connectSimpleJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::mimetype(const KURL &url)
{
kdDebug() << "ForwardingSlaveBase::mimetype: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::TransferJob *job = KIO::mimetype(new_url, false);
connectTransferJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::listDir(const KURL &url)
{
kdDebug() << "ForwardingSlaveBase::listDir: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::ListJob *job = KIO::listDir(new_url, false);
connectListJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::mkdir(const KURL &url, int permissions)
{
kdDebug() << "ForwardingSlaveBase::mkdir: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::SimpleJob *job = KIO::mkdir(new_url, permissions);
connectSimpleJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::rename(const KURL &src, const KURL &dest,
bool overwrite)
{
kdDebug() << "ForwardingSlaveBase::rename: " << src << ", " << dest << endl;
KURL new_src, new_dest;
if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) )
{
KIO::Job *job = KIO::rename(new_src, new_dest, overwrite);
connectJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::symlink(const QString &target, const KURL &dest,
bool overwrite)
{
kdDebug() << "ForwardingSlaveBase::symlink: " << target << ", " << dest << endl;
KURL new_dest;
if ( internalRewriteURL(dest, new_dest) )
{
KIO::SimpleJob *job = KIO::symlink(target, new_dest, overwrite, false);
connectSimpleJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::chmod(const KURL &url, int permissions)
{
kdDebug() << "ForwardingSlaveBase::chmod: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
KIO::SimpleJob *job = KIO::chmod(new_url, permissions);
connectSimpleJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::copy(const KURL &src, const KURL &dest,
int permissions, bool overwrite)
{
kdDebug() << "ForwardingSlaveBase::copy: " << src << ", " << dest << endl;
KURL new_src, new_dest;
if ( internalRewriteURL(src, new_src) && internalRewriteURL(dest, new_dest) )
{
KIO::Job *job = KIO::file_copy(new_src, new_dest, permissions,
overwrite, false);
connectJob(job);
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::del(const KURL &url, bool isfile)
{
kdDebug() << "ForwardingSlaveBase::del: " << url << endl;
KURL new_url;
if ( internalRewriteURL(url, new_url) )
{
if (isfile)
{
KIO::DeleteJob *job = KIO::del(new_url, false, false);
connectJob(job);
}
else
{
KIO::SimpleJob *job = KIO::rmdir(new_url);
connectSimpleJob(job);
}
qApp->eventLoop()->enterLoop();
}
}
void ForwardingSlaveBase::connectJob(KIO::Job *job)
{
// We will forward the warning message, no need to let the job
// display it itself
job->setInteractive(false);
// Forward metadata (e.g. modification time for put())
job->setMetaData( allMetaData() );
#if 0 // debug code
kdDebug() << k_funcinfo << "transferring metadata:" << endl;
const MetaData md = allMetaData();
for ( MetaData::const_iterator it = md.begin(); it != md.end(); ++it )
kdDebug() << it.key() << " = " << it.data() << endl;
#endif
connect( job, SIGNAL( result(KIO::Job *) ),
this, SLOT( slotResult(KIO::Job *) ) );
connect( job, SIGNAL( warning(KIO::Job *, const QString &) ),
this, SLOT( slotWarning(KIO::Job *, const QString &) ) );
connect( job, SIGNAL( infoMessage(KIO::Job *, const QString &) ),
this, SLOT( slotInfoMessage(KIO::Job *, const QString &) ) );
connect( job, SIGNAL( totalSize(KIO::Job *, KIO::filesize_t) ),
this, SLOT( slotTotalSize(KIO::Job *, KIO::filesize_t) ) );
connect( job, SIGNAL( processedSize(KIO::Job *, KIO::filesize_t) ),
this, SLOT( slotProcessedSize(KIO::Job *, KIO::filesize_t) ) );
connect( job, SIGNAL( speed(KIO::Job *, unsigned long) ),
this, SLOT( slotSpeed(KIO::Job *, unsigned long) ) );
}
void ForwardingSlaveBase::connectSimpleJob(KIO::SimpleJob *job)
{
connectJob(job);
connect( job, SIGNAL( redirection(KIO::Job *, const KURL &) ),
this, SLOT( slotRedirection(KIO::Job *, const KURL &) ) );
}
void ForwardingSlaveBase::connectListJob(KIO::ListJob *job)
{
connectSimpleJob(job);
connect( job, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList &) ),
this, SLOT( slotEntries(KIO::Job *, const KIO::UDSEntryList &) ) );
}
void ForwardingSlaveBase::connectTransferJob(KIO::TransferJob *job)
{
connectSimpleJob(job);
connect( job, SIGNAL( data(KIO::Job *, const QByteArray &) ),
this, SLOT( slotData(KIO::Job *, const QByteArray &) ) );
connect( job, SIGNAL( dataReq(KIO::Job *, QByteArray &) ),
this, SLOT( slotDataReq(KIO::Job *, QByteArray &) ) );
connect( job, SIGNAL( mimetype(KIO::Job *, const QString &) ),
this, SLOT( slotMimetype(KIO::Job *, const QString &) ) );
connect( job, SIGNAL( canResume(KIO::Job *, KIO::filesize_t) ),
this, SLOT( slotCanResume(KIO::Job *, KIO::filesize_t) ) );
}
void ForwardingSlaveBase::slotResult(KIO::Job *job)
{
if ( job->error() != 0)
{
error( job->error(), job->errorText() );
}
else
{
KIO::StatJob *stat_job = dynamic_cast<KIO::StatJob *>(job);
if ( stat_job!=0L )
{
KIO::UDSEntry entry = stat_job->statResult();
prepareUDSEntry(entry);
statEntry( entry );
}
finished();
}
qApp->eventLoop()->exitLoop();
}
void ForwardingSlaveBase::slotWarning(KIO::Job* /*job*/, const QString &msg)
{
warning(msg);
}
void ForwardingSlaveBase::slotInfoMessage(KIO::Job* /*job*/, const QString &msg)
{
infoMessage(msg);
}
void ForwardingSlaveBase::slotTotalSize(KIO::Job* /*job*/, KIO::filesize_t size)
{
totalSize(size);
}
void ForwardingSlaveBase::slotProcessedSize(KIO::Job* /*job*/, KIO::filesize_t size)
{
processedSize(size);
}
void ForwardingSlaveBase::slotSpeed(KIO::Job* /*job*/, unsigned long bytesPerSecond)
{
speed(bytesPerSecond);
}
void ForwardingSlaveBase::slotRedirection(KIO::Job *job, const KURL &url)
{
redirection(url);
// We've been redirected stop everything.
job->kill( true );
finished();
qApp->eventLoop()->exitLoop();
}
void ForwardingSlaveBase::slotEntries(KIO::Job* /*job*/,
const KIO::UDSEntryList &entries)
{
KIO::UDSEntryList final_entries = entries;
KIO::UDSEntryList::iterator it = final_entries.begin();
KIO::UDSEntryList::iterator end = final_entries.end();
for(; it!=end; ++it)
{
prepareUDSEntry(*it, true);
}
listEntries( final_entries );
}
void ForwardingSlaveBase::slotData(KIO::Job* /*job*/, const QByteArray &d)
{
data(d);
}
void ForwardingSlaveBase::slotDataReq(KIO::Job* /*job*/, QByteArray &data)
{
dataReq();
readData(data);
}
void ForwardingSlaveBase::slotMimetype (KIO::Job* /*job*/, const QString &type)
{
mimeType(type);
}
void ForwardingSlaveBase::slotCanResume (KIO::Job* /*job*/, KIO::filesize_t offset)
{
canResume(offset);
}
}
#include "forwardingslavebase.moc"
KIO::SlaveBase::finished
void finished()
Call to signal successful completion of any command (besides openConnection and closeConnection) ...
Definition: slavebase.cpp:453
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::put
KIO_EXPORT TransferJob * put(const KURL &url, int permissions, bool overwrite, bool resume, bool showProgressInfo=true)
Put (a.k.a.
Definition: job.cpp:1380
KIO::Job::setMetaData
void setMetaData(const KIO::MetaData &metaData)
Set meta data to be sent to the slave, replacing existing meta data.
Definition: job.cpp:400
KIO::ForwardingSlaveBase::del
virtual void del(const KURL &url, bool isfile)
Delete a file or directory.
Definition: forwardingslavebase.cpp:265
KIO::SlaveBase::mProtocol
QCString mProtocol
Name of the protocol supported by this slave.
Definition: slavebase.h:803
KIO::SlaveBase::warning
void warning(const QString &msg)
Call to signal a warning, to be displayed in a dialog box.
Definition: slavebase.cpp:618
KIO::Job::errorText
const QString & errorText() const
Returns the error text if there has been an error.
Definition: jobclasses.h:111
KIO::symlink
KIO_EXPORT SimpleJob * symlink(const QString &target, const KURL &dest, bool overwrite, bool showProgressInfo=true)
Create or move a symlink.
Definition: job.cpp:779
KIO::ForwardingSlaveBase::listDir
virtual void listDir(const KURL &url)
Lists the contents of url.
Definition: forwardingslavebase.cpp:177
KIO::ListJob
A ListJob is allows you to get the get the content of a directory.
Definition: jobclasses.h:1392
KIO::ForwardingSlaveBase::rename
virtual void rename(const KURL &src, const KURL &dest, bool overwrite)
Rename oldname into newname.
Definition: forwardingslavebase.cpp:205
KIO::Job::kill
virtual void kill(bool quietly=true)
Abort this job.
Definition: job.cpp:239
KIO::ForwardingSlaveBase::copy
virtual void copy(const KURL &src, const KURL &dest, int permissions, bool overwrite)
Copy src into dest.
Definition: forwardingslavebase.cpp:249
KIO::StatJob
A KIO job that retrieves information about a file or directory.
Definition: jobclasses.h:688
KIO::file_copy
KIO_EXPORT FileCopyJob * file_copy(const KURL &src, const KURL &dest, int permissions=-1, bool overwrite=false, bool resume=false, bool showProgressInfo=true)
Copy a single file.
Definition: job.cpp:1963
KIO::SlaveBase::dataReq
void dataReq()
Asks for data from the job.
Definition: slavebase.cpp:422
KIO::rmdir
KIO_EXPORT SimpleJob * rmdir(const KURL &url)
Removes a single directory.
Definition: job.cpp:758
KIO::MetaData
MetaData is a simple map of key/value strings.
Definition: global.h:512
KIO::SlaveBase::mimeType
void mimeType(const QString &_type)
Call this in mimetype() and in get(), when you know the mimetype.
Definition: slavebase.cpp:575
KIO::SlaveBase::totalSize
void totalSize(KIO::filesize_t _bytes)
Call this in get and copy, to give the total size of the file Call in listDir too, when you know the total number of items.
Definition: slavebase.cpp:486
KIO::ForwardingSlaveBase::mimetype
virtual void mimetype(const KURL &url)
Finds mimetype for one file or directory.
Definition: forwardingslavebase.cpp:163
KIO::listDir
KIO_EXPORT ListJob * listDir(const KURL &url, bool showProgressInfo=true, bool includeHidden=true)
List the contents of url, which is assumed to be a directory.
Definition: job.cpp:2152
KIO::mimetype
KIO_EXPORT MimetypeJob * mimetype(const KURL &url, bool showProgressInfo=true)
Find mimetype for one file or directory.
Definition: job.cpp:1509
KIO::ForwardingSlaveBase::prepareUDSEntry
virtual void prepareUDSEntry(KIO::UDSEntry &entry, bool listing=false) const
Allow to modify a UDSEntry before it's sent to the ioslave enpoint.
Definition: forwardingslavebase.cpp:66
KIO::SlaveBase::infoMessage
void infoMessage(const QString &msg)
Call to signal a message, to be displayed if the application wants to, for instance in a status bar...
Definition: slavebase.cpp:624
KIO::SlaveBase::processedSize
void processedSize(KIO::filesize_t _bytes)
Call this during get and copy, once in a while, to give some info about the current state...
Definition: slavebase.cpp:502
KIO::Job::setInteractive
void setInteractive(bool enable)
Enable or disable the message display from the job.
Definition: job.cpp:342
KIO::chmod
KIO_EXPORT ChmodJob * chmod(const KFileItemList &lstItems, int permissions, int mask, QString newOwner, QString newGroup, bool recursive, bool showProgressInfo=true)
Creates a job that changes permissions/ownership on several files or directories, optionally recursiv...
Definition: chmodjob.cpp:230
KIO::DeleteJob
A more complex Job to delete files and directories.
Definition: jobclasses.h:1763
KIO::UDS_NAME
Filename - as displayed in directory listings etc.
Definition: global.h:332
KIO::ForwardingSlaveBase::chmod
virtual void chmod(const KURL &url, int permissions)
Change permissions on path The slave emits ERR_DOES_NOT_EXIST or ERR_CANNOT_CHMOD.
Definition: forwardingslavebase.cpp:235
KIO::SlaveBase::statEntry
void statEntry(const UDSEntry &_entry)
Call this from stat() to express details about an object, the UDSEntry customarily contains the atoms...
Definition: slavebase.cpp:651
KIO::rename
KIO_EXPORT SimpleJob * rename(const KURL &src, const KURL &dest, bool overwrite)
Rename a file or directory.
Definition: job.cpp:772
KIO::ForwardingSlaveBase::symlink
virtual void symlink(const QString &target, const KURL &dest, bool overwrite)
Creates a symbolic link named dest, pointing to target, which may be a relative or an absolute path...
Definition: forwardingslavebase.cpp:220
KIO::SlaveBase::data
void data(const QByteArray &data)
Sends data in the slave to the job (i.e.
Definition: slavebase.cpp:413
KIO::ForwardingSlaveBase::put
virtual void put(const KURL &url, int permissions, bool overwrite, bool resume)
put, i.e.
Definition: forwardingslavebase.cpp:133
KIO::filesize_t
Q_ULLONG filesize_t
64-bit file size
Definition: global.h:39
KIO::Job::error
int error() const
Returns the error code, if there has been an error.
Definition: jobclasses.h:95
KIO::SlaveBase::error
void error(int _errid, const QString &_text)
Call to signal an error.
Definition: slavebase.cpp:433
KIO::ForwardingSlaveBase::mkdir
virtual void mkdir(const KURL &url, int permissions)
Create a directory.
Definition: forwardingslavebase.cpp:191
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::ForwardingSlaveBase::get
virtual void get(const KURL &url)
get, aka read.
Definition: forwardingslavebase.cpp:119
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::StatJob::statResult
const UDSEntry & statResult() const
Call this in the slot connected to result, and only after making sure no error happened.
Definition: jobclasses.h:727
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::ForwardingSlaveBase::rewriteURL
virtual bool rewriteURL(const KURL &url, KURL &newURL)=0
Rewrite an url to it's forwarded counterpart.
KIO::SlaveBase::speed
void speed(unsigned long _bytes_per_second)
Call this in get and copy, to give the current transfer speed, but only if it can't be calculated out...
Definition: slavebase.cpp:544
KIO::ForwardingSlaveBase::stat
virtual void stat(const KURL &url)
Finds all details for one file or directory.
Definition: forwardingslavebase.cpp:149
KIO::Job
The base class for all jobs.
Definition: jobclasses.h:68
KIO::UDS_URL
An alternative URL (If different from the caption)
Definition: global.h:368
KIO::SlaveBase::redirection
void redirection(const KURL &_url)
Call this to signal a redirection The job will take care of going to that url.
Definition: slavebase.cpp:552
KIO::TransferJob
The transfer job pumps data into and/or out of a Slave.
Definition: jobclasses.h:875
KIO::mkdir
KIO_EXPORT SimpleJob * mkdir(const KURL &url, int permissions=-1)
Creates a single directory.
Definition: job.cpp:751
KIO::SlaveBase::listEntries
void listEntries(const UDSEntryList &_entry)
Call this in listDir, each time you have a bunch of entries to report.
Definition: slavebase.cpp:701
KIO::SlaveBase::readData
int readData(QByteArray &buffer)
Read data send by the job, after a dataReq.
Definition: slavebase.cpp:964
KIO::get
KIO_EXPORT TransferJob * get(const KURL &url, bool reload=false, bool showProgressInfo=true)
Get (a.k.a.
Definition: job.cpp:1220
KIO::SlaveBase::canResume
bool canResume(KIO::filesize_t offset)
Call this at the beginning of put(), to give the size of the existing partial file, if there is one.
Definition: slavebase.cpp:914
KIO::SimpleJob
A simple job (one url and one command).
Definition: jobclasses.h:528

KDE Home | KDE Accessibility Home | Description of Access Keys