KIO

filejob.cpp
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));
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
67{
68 Q_D(FileJob);
69 if (!d->m_open) {
70 return;
71 }
72
73 KIO_ARGS << size;
74 d->m_worker->send(CMD_READ, 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_WRITE, _data);
85}
86
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_SEEK, packedArgs);
96}
97
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_TRUNCATE, packedArgs);
107}
108
110{
111 Q_D(FileJob);
112 if (!d->m_open) {
113 return;
114 }
115
116 d->m_worker->send(CMD_CLOSE);
117 // ### close?
118}
119
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(q, _data);
135}
136
137void FileJobPrivate::slotRedirection(const QUrl &url)
138{
139 Q_Q(FileJob);
140 // qDebug() << url;
141 Q_EMIT q->redirection(q, url);
142}
143
144void FileJobPrivate::slotMimetype(const QString &type)
145{
146 Q_Q(FileJob);
147 m_mimetype = type;
148 Q_EMIT q->mimeTypeFound(q, m_mimetype);
149}
150
151void FileJobPrivate::slotPosition(KIO::filesize_t pos)
152{
153 Q_Q(FileJob);
154 Q_EMIT q->position(q, pos);
155}
156
157void FileJobPrivate::slotTruncated(KIO::filesize_t length)
158{
159 Q_Q(FileJob);
160 Q_EMIT q->truncated(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(KJob::Bytes, m_size);
168}
169
170void FileJobPrivate::slotOpen()
171{
172 Q_Q(FileJob);
173 m_open = true;
174 Q_EMIT q->open(q);
175}
176
177void FileJobPrivate::slotWritten(KIO::filesize_t t_written)
178{
179 Q_Q(FileJob);
180 Q_EMIT q->written(q, 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(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(worker, &KIO::WorkerInterface::data, q, [this](const QByteArray &ba) {
201 slotData(ba);
202 });
203
204 q->connect(worker, &KIO::WorkerInterface::redirection, q, [this](const QUrl &url) {
205 slotRedirection(url);
206 });
207
208 q->connect(worker, &KIO::WorkerInterface::mimeType, q, [this](const QString &mimeType) {
209 slotMimetype(mimeType);
210 });
211
212 q->connect(worker, &KIO::WorkerInterface::open, q, [this]() {
213 slotOpen();
214 });
215
216 q->connect(worker, &KIO::WorkerInterface::finished, q, [this]() {
217 slotFinished();
218 });
219
220 q->connect(worker, &KIO::WorkerInterface::position, q, [this](KIO::filesize_t pos) {
221 slotPosition(pos);
222 });
223
224 q->connect(worker, &KIO::WorkerInterface::truncated, q, [this](KIO::filesize_t length) {
225 slotTruncated(length);
226 });
227
228 q->connect(worker, &KIO::WorkerInterface::written, q, [this](KIO::filesize_t dataWritten) {
229 slotWritten(dataWritten);
230 });
231
232 q->connect(worker, &KIO::WorkerInterface::totalSize, q, [this](KIO::filesize_t size) {
233 slotTotalSize(size);
234 });
235
236 SimpleJobPrivate::start(worker);
237}
238
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"
The file-job is an asynchronous version of normal file handling.
KIO::filesize_t size()
Size.
Definition filejob.cpp:120
void read(KIO::filesize_t size)
This function attempts to read up to size bytes from the URL passed to KIO::open() and returns the by...
Definition filejob.cpp:66
void write(const QByteArray &data)
This function attempts to write all the bytes in data to the URL passed to KIO::open() and returns th...
Definition filejob.cpp:77
void seek(KIO::filesize_t offset)
Seek.
Definition filejob.cpp:87
void truncate(KIO::filesize_t length)
Truncate.
Definition filejob.cpp:98
void close()
Closes the file KIO worker.
Definition filejob.cpp:109
A simple job (one url and one command).
void setUiDelegate(KJobUiDelegate *delegate)
Q_SCRIPTABLE Q_NOREPLY void start()
Type type(const QSqlDatabase &db)
A namespace for KIO globals.
KIOCORE_EXPORT FileJob * open(const QUrl &url, QIODevice::OpenMode mode)
Open ( random access I/O )
Definition filejob.cpp:239
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there's one, to create a delegate and return it.
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
Find MIME type for one file or directory.
qulonglong filesize_t
64-bit file size
Definition global.h:35
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.