1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2006 Allan Sandfeld Jensen <kde@carewolf.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include "filejob.h"
9
10#include "job_p.h"
11#include "worker_p.h"
12
13class KIO::FileJobPrivate : public KIO::SimpleJobPrivate
14{
15public:
16 FileJobPrivate(const QUrl &url, const QByteArray &packedArgs)
17 : SimpleJobPrivate(url, CMD_OPEN, packedArgs)
18 , m_open(false)
19 , m_size(0)
20 {
21 }
22
23 bool m_open;
24 QString m_mimetype;
25 KIO::filesize_t m_size;
26
27 void slotRedirection(const QUrl &url);
28 void slotData(const QByteArray &data);
29 void slotMimetype(const QString &mimetype);
30 void slotOpen();
31 void slotWritten(KIO::filesize_t);
32 void slotFinished();
33 void slotPosition(KIO::filesize_t);
34 void slotTruncated(KIO::filesize_t);
35 void slotTotalSize(KIO::filesize_t);
36
37 /**
38 * @internal
39 * Called by the scheduler when a @p worker gets to
40 * work on this job.
41 * @param worker the worker that starts working on this job
42 */
43 void start(Worker *worker) override;
44
45 Q_DECLARE_PUBLIC(FileJob)
46
47 static inline FileJob *newJob(const QUrl &url, const QByteArray &packedArgs)
48 {
49 FileJob *job = new FileJob(*new FileJobPrivate(url, packedArgs));
50 job->setUiDelegate(KIO::createDefaultJobUiDelegate());
51 return job;
52 }
53};
54
55using namespace KIO;
56
57FileJob::FileJob(FileJobPrivate &dd)
58 : SimpleJob(dd)
59{
60}
61
62FileJob::~FileJob()
63{
64}
65
66void FileJob::read(KIO::filesize_t size)
67{
68 Q_D(FileJob);
69 if (!d->m_open) {
70 return;
71 }
72
73 KIO_ARGS << size;
74 d->m_worker->send(cmd: CMD_READ, arr: packedArgs);
75}
76
77void FileJob::write(const QByteArray &_data)
78{
79 Q_D(FileJob);
80 if (!d->m_open) {
81 return;
82 }
83
84 d->m_worker->send(cmd: CMD_WRITE, arr: _data);
85}
86
87void FileJob::seek(KIO::filesize_t offset)
88{
89 Q_D(FileJob);
90 if (!d->m_open) {
91 return;
92 }
93
94 KIO_ARGS << KIO::filesize_t(offset);
95 d->m_worker->send(cmd: CMD_SEEK, arr: packedArgs);
96}
97
98void FileJob::truncate(KIO::filesize_t length)
99{
100 Q_D(FileJob);
101 if (!d->m_open) {
102 return;
103 }
104
105 KIO_ARGS << KIO::filesize_t(length);
106 d->m_worker->send(cmd: CMD_TRUNCATE, arr: packedArgs);
107}
108
109void FileJob::close()
110{
111 Q_D(FileJob);
112 if (!d->m_open) {
113 return;
114 }
115
116 d->m_worker->send(cmd: CMD_CLOSE);
117 // ### close?
118}
119
120KIO::filesize_t FileJob::size()
121{
122 Q_D(FileJob);
123 if (!d->m_open) {
124 return 0;
125 }
126
127 return d->m_size;
128}
129
130// Worker sends data
131void FileJobPrivate::slotData(const QByteArray &_data)
132{
133 Q_Q(FileJob);
134 Q_EMIT q_func()->data(job: q, data: _data);
135}
136
137void FileJobPrivate::slotRedirection(const QUrl &url)
138{
139 Q_Q(FileJob);
140 // qDebug() << url;
141 Q_EMIT q->redirection(job: q, url);
142}
143
144void FileJobPrivate::slotMimetype(const QString &type)
145{
146 Q_Q(FileJob);
147 m_mimetype = type;
148 Q_EMIT q->mimeTypeFound(job: q, mimeType: m_mimetype);
149}
150
151void FileJobPrivate::slotPosition(KIO::filesize_t pos)
152{
153 Q_Q(FileJob);
154 Q_EMIT q->position(job: q, offset: pos);
155}
156
157void FileJobPrivate::slotTruncated(KIO::filesize_t length)
158{
159 Q_Q(FileJob);
160 Q_EMIT q->truncated(job: q, length);
161}
162
163void FileJobPrivate::slotTotalSize(KIO::filesize_t t_size)
164{
165 m_size = t_size;
166 Q_Q(FileJob);
167 q->setTotalAmount(unit: KJob::Bytes, amount: m_size);
168}
169
170void FileJobPrivate::slotOpen()
171{
172 Q_Q(FileJob);
173 m_open = true;
174 Q_EMIT q->open(job: q);
175}
176
177void FileJobPrivate::slotWritten(KIO::filesize_t t_written)
178{
179 Q_Q(FileJob);
180 Q_EMIT q->written(job: q, written: t_written);
181}
182
183void FileJobPrivate::slotFinished()
184{
185 Q_Q(FileJob);
186 // qDebug() << this << m_url;
187 m_open = false;
188
189 Q_EMIT q->fileClosed(job: q);
190
191 // Return worker to the scheduler
192 workerDone();
193 // Scheduler::doJob(this);
194 q->emitResult();
195}
196
197void FileJobPrivate::start(Worker *worker)
198{
199 Q_Q(FileJob);
200 q->connect(sender: worker, signal: &KIO::WorkerInterface::data, context: q, slot: [this](const QByteArray &ba) {
201 slotData(data: ba);
202 });
203
204 q->connect(sender: worker, signal: &KIO::WorkerInterface::redirection, context: q, slot: [this](const QUrl &url) {
205 slotRedirection(url);
206 });
207
208 q->connect(sender: worker, signal: &KIO::WorkerInterface::mimeType, context: q, slot: [this](const QString &mimeType) {
209 slotMimetype(type: mimeType);
210 });
211
212 q->connect(sender: worker, signal: &KIO::WorkerInterface::open, context: q, slot: [this]() {
213 slotOpen();
214 });
215
216 q->connect(sender: worker, signal: &KIO::WorkerInterface::finished, context: q, slot: [this]() {
217 slotFinished();
218 });
219
220 q->connect(sender: worker, signal: &KIO::WorkerInterface::position, context: q, slot: [this](KIO::filesize_t pos) {
221 slotPosition(pos);
222 });
223
224 q->connect(sender: worker, signal: &KIO::WorkerInterface::truncated, context: q, slot: [this](KIO::filesize_t length) {
225 slotTruncated(length);
226 });
227
228 q->connect(sender: worker, signal: &KIO::WorkerInterface::written, context: q, slot: [this](KIO::filesize_t dataWritten) {
229 slotWritten(t_written: dataWritten);
230 });
231
232 q->connect(sender: worker, signal: &KIO::WorkerInterface::totalSize, context: q, slot: [this](KIO::filesize_t size) {
233 slotTotalSize(t_size: size);
234 });
235
236 SimpleJobPrivate::start(worker);
237}
238
239FileJob *KIO::open(const QUrl &url, QIODevice::OpenMode mode)
240{
241 // Send decoded path and encoded query
242 KIO_ARGS << url << mode;
243 return FileJobPrivate::newJob(url, packedArgs);
244}
245
246#include "moc_filejob.cpp"
247

source code of kio/src/core/filejob.cpp