File indexing completed on 2024-10-06 06:41:03
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2006 Allan Sandfeld Jensen <kde@carewolf.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #include "filejob.h" 0009 0010 #include "job_p.h" 0011 #include "worker_p.h" 0012 0013 class KIO::FileJobPrivate : public KIO::SimpleJobPrivate 0014 { 0015 public: 0016 FileJobPrivate(const QUrl &url, const QByteArray &packedArgs) 0017 : SimpleJobPrivate(url, CMD_OPEN, packedArgs) 0018 , m_open(false) 0019 , m_size(0) 0020 { 0021 } 0022 0023 bool m_open; 0024 QString m_mimetype; 0025 KIO::filesize_t m_size; 0026 0027 void slotRedirection(const QUrl &url); 0028 void slotData(const QByteArray &data); 0029 void slotMimetype(const QString &mimetype); 0030 void slotOpen(); 0031 void slotWritten(KIO::filesize_t); 0032 void slotFinished(); 0033 void slotPosition(KIO::filesize_t); 0034 void slotTruncated(KIO::filesize_t); 0035 void slotTotalSize(KIO::filesize_t); 0036 0037 /** 0038 * @internal 0039 * Called by the scheduler when a @p worker gets to 0040 * work on this job. 0041 * @param worker the worker that starts working on this job 0042 */ 0043 void start(Worker *worker) override; 0044 0045 Q_DECLARE_PUBLIC(FileJob) 0046 0047 static inline FileJob *newJob(const QUrl &url, const QByteArray &packedArgs) 0048 { 0049 FileJob *job = new FileJob(*new FileJobPrivate(url, packedArgs)); 0050 job->setUiDelegate(KIO::createDefaultJobUiDelegate()); 0051 return job; 0052 } 0053 }; 0054 0055 using namespace KIO; 0056 0057 FileJob::FileJob(FileJobPrivate &dd) 0058 : SimpleJob(dd) 0059 { 0060 } 0061 0062 FileJob::~FileJob() 0063 { 0064 } 0065 0066 void FileJob::read(KIO::filesize_t size) 0067 { 0068 Q_D(FileJob); 0069 if (!d->m_open) { 0070 return; 0071 } 0072 0073 KIO_ARGS << size; 0074 d->m_worker->send(CMD_READ, packedArgs); 0075 } 0076 0077 void FileJob::write(const QByteArray &_data) 0078 { 0079 Q_D(FileJob); 0080 if (!d->m_open) { 0081 return; 0082 } 0083 0084 d->m_worker->send(CMD_WRITE, _data); 0085 } 0086 0087 void FileJob::seek(KIO::filesize_t offset) 0088 { 0089 Q_D(FileJob); 0090 if (!d->m_open) { 0091 return; 0092 } 0093 0094 KIO_ARGS << KIO::filesize_t(offset); 0095 d->m_worker->send(CMD_SEEK, packedArgs); 0096 } 0097 0098 void FileJob::truncate(KIO::filesize_t length) 0099 { 0100 Q_D(FileJob); 0101 if (!d->m_open) { 0102 return; 0103 } 0104 0105 KIO_ARGS << KIO::filesize_t(length); 0106 d->m_worker->send(CMD_TRUNCATE, packedArgs); 0107 } 0108 0109 void FileJob::close() 0110 { 0111 Q_D(FileJob); 0112 if (!d->m_open) { 0113 return; 0114 } 0115 0116 d->m_worker->send(CMD_CLOSE); 0117 // ### close? 0118 } 0119 0120 KIO::filesize_t FileJob::size() 0121 { 0122 Q_D(FileJob); 0123 if (!d->m_open) { 0124 return 0; 0125 } 0126 0127 return d->m_size; 0128 } 0129 0130 // Worker sends data 0131 void FileJobPrivate::slotData(const QByteArray &_data) 0132 { 0133 Q_Q(FileJob); 0134 Q_EMIT q_func()->data(q, _data); 0135 } 0136 0137 void FileJobPrivate::slotRedirection(const QUrl &url) 0138 { 0139 Q_Q(FileJob); 0140 // qDebug() << url; 0141 Q_EMIT q->redirection(q, url); 0142 } 0143 0144 void FileJobPrivate::slotMimetype(const QString &type) 0145 { 0146 Q_Q(FileJob); 0147 m_mimetype = type; 0148 Q_EMIT q->mimeTypeFound(q, m_mimetype); 0149 } 0150 0151 void FileJobPrivate::slotPosition(KIO::filesize_t pos) 0152 { 0153 Q_Q(FileJob); 0154 Q_EMIT q->position(q, pos); 0155 } 0156 0157 void FileJobPrivate::slotTruncated(KIO::filesize_t length) 0158 { 0159 Q_Q(FileJob); 0160 Q_EMIT q->truncated(q, length); 0161 } 0162 0163 void FileJobPrivate::slotTotalSize(KIO::filesize_t t_size) 0164 { 0165 m_size = t_size; 0166 Q_Q(FileJob); 0167 q->setTotalAmount(KJob::Bytes, m_size); 0168 } 0169 0170 void FileJobPrivate::slotOpen() 0171 { 0172 Q_Q(FileJob); 0173 m_open = true; 0174 Q_EMIT q->open(q); 0175 } 0176 0177 void FileJobPrivate::slotWritten(KIO::filesize_t t_written) 0178 { 0179 Q_Q(FileJob); 0180 Q_EMIT q->written(q, t_written); 0181 } 0182 0183 void FileJobPrivate::slotFinished() 0184 { 0185 Q_Q(FileJob); 0186 // qDebug() << this << m_url; 0187 m_open = false; 0188 0189 Q_EMIT q->fileClosed(q); 0190 0191 // Return worker to the scheduler 0192 workerDone(); 0193 // Scheduler::doJob(this); 0194 q->emitResult(); 0195 } 0196 0197 void FileJobPrivate::start(Worker *worker) 0198 { 0199 Q_Q(FileJob); 0200 q->connect(worker, &KIO::WorkerInterface::data, q, [this](const QByteArray &ba) { 0201 slotData(ba); 0202 }); 0203 0204 q->connect(worker, &KIO::WorkerInterface::redirection, q, [this](const QUrl &url) { 0205 slotRedirection(url); 0206 }); 0207 0208 q->connect(worker, &KIO::WorkerInterface::mimeType, q, [this](const QString &mimeType) { 0209 slotMimetype(mimeType); 0210 }); 0211 0212 q->connect(worker, &KIO::WorkerInterface::open, q, [this]() { 0213 slotOpen(); 0214 }); 0215 0216 q->connect(worker, &KIO::WorkerInterface::finished, q, [this]() { 0217 slotFinished(); 0218 }); 0219 0220 q->connect(worker, &KIO::WorkerInterface::position, q, [this](KIO::filesize_t pos) { 0221 slotPosition(pos); 0222 }); 0223 0224 q->connect(worker, &KIO::WorkerInterface::truncated, q, [this](KIO::filesize_t length) { 0225 slotTruncated(length); 0226 }); 0227 0228 q->connect(worker, &KIO::WorkerInterface::written, q, [this](KIO::filesize_t dataWritten) { 0229 slotWritten(dataWritten); 0230 }); 0231 0232 q->connect(worker, &KIO::WorkerInterface::totalSize, q, [this](KIO::filesize_t size) { 0233 slotTotalSize(size); 0234 }); 0235 0236 SimpleJobPrivate::start(worker); 0237 } 0238 0239 FileJob *KIO::open(const QUrl &url, QIODevice::OpenMode mode) 0240 { 0241 // Send decoded path and encoded query 0242 KIO_ARGS << url << mode; 0243 return FileJobPrivate::newJob(url, packedArgs); 0244 } 0245 0246 #include "moc_filejob.cpp"