KIO
filejob.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (c) 2006 Allan Sandfeld Jensen <kde@carewolf.com> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License version 2 as published by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 * 00019 **/ 00020 00021 #include "filejob.h" 00022 00023 #include "slavebase.h" 00024 #include "connection.h" 00025 #include "scheduler.h" 00026 #include "slave.h" 00027 00028 #include <QTimer> 00029 #include <kdebug.h> 00030 00031 #include "job_p.h" 00032 00033 class KIO::FileJobPrivate: public KIO::SimpleJobPrivate 00034 { 00035 public: 00036 FileJobPrivate(const KUrl& url, const QByteArray &packedArgs) 00037 : SimpleJobPrivate(url, CMD_OPEN, packedArgs), m_open(false), m_size(0) 00038 {} 00039 00040 bool m_open; 00041 QString m_mimetype; 00042 KIO::filesize_t m_size; 00043 00044 void slotRedirection( const KUrl &url ); 00045 void slotData( const QByteArray &data ); 00046 void slotMimetype( const QString &mimetype ); 00047 void slotOpen( ); 00048 void slotWritten( KIO::filesize_t ); 00049 void slotFinished( ); 00050 void slotPosition( KIO::filesize_t ); 00051 void slotTotalSize( KIO::filesize_t ); 00052 00059 virtual void start(Slave *slave); 00060 00061 Q_DECLARE_PUBLIC(FileJob) 00062 00063 static inline FileJob *newJob(const KUrl &url, const QByteArray &packedArgs) 00064 { 00065 FileJob *job = new FileJob(*new FileJobPrivate(url, packedArgs)); 00066 job->setUiDelegate(new JobUiDelegate); 00067 return job; 00068 } 00069 }; 00070 00071 using namespace KIO; 00072 00073 FileJob::FileJob(FileJobPrivate &dd) 00074 : SimpleJob(dd) 00075 { 00076 } 00077 00078 FileJob::~FileJob() 00079 { 00080 } 00081 00082 void FileJob::read(KIO::filesize_t size) 00083 { 00084 Q_D(FileJob); 00085 if (!d->m_open) return; 00086 00087 KIO_ARGS << size; 00088 d->m_slave->send( CMD_READ, packedArgs ); 00089 } 00090 00091 00092 void FileJob::write(const QByteArray &_data) 00093 { 00094 Q_D(FileJob); 00095 if (!d->m_open) return; 00096 00097 d->m_slave->send( CMD_WRITE, _data ); 00098 } 00099 00100 void FileJob::seek(KIO::filesize_t offset) 00101 { 00102 Q_D(FileJob); 00103 if (!d->m_open) return; 00104 00105 KIO_ARGS << KIO::filesize_t(offset); 00106 d->m_slave->send( CMD_SEEK, packedArgs) ; 00107 } 00108 00109 void FileJob::close() 00110 { 00111 Q_D(FileJob); 00112 if (!d->m_open) return; 00113 00114 d->m_slave->send( CMD_CLOSE ); 00115 // ### close? 00116 } 00117 00118 KIO::filesize_t FileJob::size() 00119 { 00120 Q_D(FileJob); 00121 if (!d->m_open) return 0; 00122 00123 return d->m_size; 00124 } 00125 00126 // Slave sends data 00127 void FileJobPrivate::slotData( const QByteArray &_data) 00128 { 00129 Q_Q(FileJob); 00130 emit q_func()->data(q, _data); 00131 } 00132 00133 void FileJobPrivate::slotRedirection( const KUrl &url) 00134 { 00135 Q_Q(FileJob); 00136 kDebug(7007) << url; 00137 emit q->redirection(q, url); 00138 } 00139 00140 void FileJobPrivate::slotMimetype( const QString& type ) 00141 { 00142 Q_Q(FileJob); 00143 m_mimetype = type; 00144 emit q->mimetype(q, m_mimetype); 00145 } 00146 00147 void FileJobPrivate::slotPosition( KIO::filesize_t pos ) 00148 { 00149 Q_Q(FileJob); 00150 emit q->position(q, pos); 00151 } 00152 00153 void FileJobPrivate::slotTotalSize( KIO::filesize_t t_size ) 00154 { 00155 m_size = t_size; 00156 Q_Q(FileJob); 00157 q->setTotalAmount(KJob::Bytes, m_size); 00158 } 00159 00160 void FileJobPrivate::slotOpen( ) 00161 { 00162 Q_Q(FileJob); 00163 m_open = true; 00164 emit q->open( q ); 00165 } 00166 00167 void FileJobPrivate::slotWritten( KIO::filesize_t t_written ) 00168 { 00169 Q_Q(FileJob); 00170 emit q->written(q, t_written); 00171 } 00172 00173 void FileJobPrivate::slotFinished() 00174 { 00175 Q_Q(FileJob); 00176 kDebug(7007) << this << m_url; 00177 emit q->close( q ); 00178 // Return slave to the scheduler 00179 slaveDone(); 00180 // Scheduler::doJob(this); 00181 q->emitResult(); 00182 } 00183 00184 void FileJobPrivate::start(Slave *slave) 00185 { 00186 Q_Q(FileJob); 00187 q->connect( slave, SIGNAL( data( const QByteArray & ) ), 00188 SLOT( slotData( const QByteArray & ) ) ); 00189 00190 q->connect( slave, SIGNAL( redirection(const KUrl &) ), 00191 SLOT( slotRedirection(const KUrl &) ) ); 00192 00193 q->connect( slave, SIGNAL(mimeType( const QString& ) ), 00194 SLOT( slotMimetype( const QString& ) ) ); 00195 00196 q->connect( slave, SIGNAL(open() ), 00197 SLOT( slotOpen() ) ); 00198 00199 q->connect( slave, SIGNAL(position(KIO::filesize_t) ), 00200 SLOT( slotPosition(KIO::filesize_t) ) ); 00201 00202 q->connect( slave, SIGNAL(written(KIO::filesize_t) ), 00203 SLOT( slotWritten(KIO::filesize_t) ) ); 00204 00205 q->connect( slave, SIGNAL(totalSize(KIO::filesize_t) ), 00206 SLOT( slotTotalSize(KIO::filesize_t) ) ); 00207 00208 SimpleJobPrivate::start(slave); 00209 } 00210 00211 FileJob *KIO::open(const KUrl &url, QIODevice::OpenMode mode) 00212 { 00213 // Send decoded path and encoded query 00214 KIO_ARGS << url << mode; 00215 return FileJobPrivate::newJob(url, packedArgs); 00216 } 00217 00218 #include "filejob.moc" 00219
KDE 4.6 API Reference