KIO

filejob.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2006 Allan Sandfeld Jensen <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  **/
20 
21 #include "filejob.h"
22 
23 #include "slavebase.h"
24 #include "scheduler.h"
25 #include "slave.h"
26 
27 
28 #include "job_p.h"
29 
30 class KIO::FileJobPrivate: public KIO::SimpleJobPrivate
31 {
32 public:
33  FileJobPrivate(const QUrl &url, const QByteArray &packedArgs)
34  : SimpleJobPrivate(url, CMD_OPEN, packedArgs), m_open(false), m_size(0)
35  {}
36 
37  bool m_open;
38  QString m_mimetype;
39  KIO::filesize_t m_size;
40 
41  void slotRedirection(const QUrl &url);
42  void slotData(const QByteArray &data);
43  void slotMimetype(const QString &mimetype);
44  void slotOpen();
45  void slotWritten(KIO::filesize_t);
46  void slotFinished();
47  void slotPosition(KIO::filesize_t);
48  void slotTruncated(KIO::filesize_t);
49  void slotTotalSize(KIO::filesize_t);
50 
57  void start(Slave *slave) override;
58 
59  Q_DECLARE_PUBLIC(FileJob)
60 
61  static inline FileJob *newJob(const QUrl &url, const QByteArray &packedArgs)
62  {
63  FileJob *job = new FileJob(*new FileJobPrivate(url, packedArgs));
65  return job;
66  }
67 };
68 
69 using namespace KIO;
70 
71 FileJob::FileJob(FileJobPrivate &dd)
72  : SimpleJob(dd)
73 {
74 }
75 
76 FileJob::~FileJob()
77 {
78 }
79 
81 {
82  Q_D(FileJob);
83  if (!d->m_open) {
84  return;
85  }
86 
87  KIO_ARGS << size;
88  d->m_slave->send(CMD_READ, packedArgs);
89 }
90 
91 void FileJob::write(const QByteArray &_data)
92 {
93  Q_D(FileJob);
94  if (!d->m_open) {
95  return;
96  }
97 
98  d->m_slave->send(CMD_WRITE, _data);
99 }
100 
102 {
103  Q_D(FileJob);
104  if (!d->m_open) {
105  return;
106  }
107 
108  KIO_ARGS << KIO::filesize_t(offset);
109  d->m_slave->send(CMD_SEEK, packedArgs);
110 }
111 
113 {
114  Q_D(FileJob);
115  if (!d->m_open) {
116  return;
117  }
118 
119  KIO_ARGS << KIO::filesize_t(length);
120  d->m_slave->send(CMD_TRUNCATE, packedArgs);
121 }
122 
124 {
125  Q_D(FileJob);
126  if (!d->m_open) {
127  return;
128  }
129 
130  d->m_slave->send(CMD_CLOSE);
131  // ### close?
132 }
133 
135 {
136  Q_D(FileJob);
137  if (!d->m_open) {
138  return 0;
139  }
140 
141  return d->m_size;
142 }
143 
144 // Slave sends data
145 void FileJobPrivate::slotData(const QByteArray &_data)
146 {
147  Q_Q(FileJob);
148  emit q_func()->data(q, _data);
149 }
150 
151 void FileJobPrivate::slotRedirection(const QUrl &url)
152 {
153  Q_Q(FileJob);
154  //qDebug() << url;
155  emit q->redirection(q, url);
156 }
157 
158 void FileJobPrivate::slotMimetype(const QString &type)
159 {
160  Q_Q(FileJob);
161  m_mimetype = type;
162  emit q->mimetype(q, m_mimetype);
163 }
164 
165 void FileJobPrivate::slotPosition(KIO::filesize_t pos)
166 {
167  Q_Q(FileJob);
168  emit q->position(q, pos);
169 }
170 
171 void FileJobPrivate::slotTruncated(KIO::filesize_t length)
172 {
173  Q_Q(FileJob);
174  emit q->truncated(q, length);
175 }
176 
177 void FileJobPrivate::slotTotalSize(KIO::filesize_t t_size)
178 {
179  m_size = t_size;
180  Q_Q(FileJob);
181  q->setTotalAmount(KJob::Bytes, m_size);
182 }
183 
184 void FileJobPrivate::slotOpen()
185 {
186  Q_Q(FileJob);
187  m_open = true;
188  emit q->open(q);
189 }
190 
191 void FileJobPrivate::slotWritten(KIO::filesize_t t_written)
192 {
193  Q_Q(FileJob);
194  emit q->written(q, t_written);
195 }
196 
197 void FileJobPrivate::slotFinished()
198 {
199  Q_Q(FileJob);
200  //qDebug() << this << m_url;
201  m_open = false;
202  emit q->close(q);
203  // Return slave to the scheduler
204  slaveDone();
205  // Scheduler::doJob(this);
206  q->emitResult();
207 }
208 
209 void FileJobPrivate::start(Slave *slave)
210 {
211  Q_Q(FileJob);
212  q->connect(slave, SIGNAL(data(QByteArray)),
213  SLOT(slotData(QByteArray)));
214 
215  q->connect(slave, SIGNAL(redirection(QUrl)),
216  SLOT(slotRedirection(QUrl)));
217 
218  q->connect(slave, SIGNAL(mimeType(QString)),
219  SLOT(slotMimetype(QString)));
220 
221  q->connect(slave, SIGNAL(open()),
222  SLOT(slotOpen()));
223 
224  q->connect(slave, SIGNAL(finished()),
225  SLOT(slotFinished()));
226 
227  q->connect(slave, SIGNAL(position(KIO::filesize_t)),
228  SLOT(slotPosition(KIO::filesize_t)));
229 
230  q->connect(slave, SIGNAL(truncated(KIO::filesize_t)),
231  SLOT(slotTruncated(KIO::filesize_t)));
232 
233  q->connect(slave, SIGNAL(written(KIO::filesize_t)),
234  SLOT(slotWritten(KIO::filesize_t)));
235 
236  q->connect(slave, SIGNAL(totalSize(KIO::filesize_t)),
237  SLOT(slotTotalSize(KIO::filesize_t)));
238 
239  SimpleJobPrivate::start(slave);
240 }
241 
243 {
244  // Send decoded path and encoded query
245  KIO_ARGS << url << mode;
246  return FileJobPrivate::newJob(url, packedArgs);
247 }
248 
249 #include "moc_filejob.cpp"
250 
void seek(KIO::filesize_t offset)
Seek.
Definition: filejob.cpp:101
qulonglong filesize_t
64-bit file size
Definition: global.h:51
void setUiDelegate(KJobUiDelegate *delegate)
A namespace for KIO globals.
Definition: authinfo.h:34
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:80
typedef OpenMode
void truncate(KIO::filesize_t length)
Truncate.
Definition: filejob.cpp:112
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:91
void finished(KJob *job)
void totalSize(KJob *job, qulonglong size)
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there&#39;s one, to create a delegate and return it...
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
Find mimetype for one file or directory.
Definition: mimetypejob.cpp:93
KIO::filesize_t size()
Size.
Definition: filejob.cpp:134
KIOCORE_EXPORT FileJob * open(const QUrl &url, QIODevice::OpenMode mode)
Open ( random access I/O )
Definition: filejob.cpp:242
The file-job is an asynchronous version of normal file handling.
Definition: filejob.h:40
void close()
Closes the file-slave.
Definition: filejob.cpp:123
A simple job (one url and one command).
Definition: simplejob.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 23:00:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.