File indexing completed on 2024-10-06 03:39:25
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 #ifndef KIO_FILEJOB_H 0009 #define KIO_FILEJOB_H 0010 0011 #include "kiocore_export.h" 0012 #include "simplejob.h" 0013 0014 namespace KIO 0015 { 0016 class FileJobPrivate; 0017 /** 0018 * @class KIO::FileJob filejob.h <KIO/FileJob> 0019 * 0020 * The file-job is an asynchronous version of normal file handling. 0021 * It allows block-wise reading and writing, and allows seeking and truncation. Results are returned through signals. 0022 * 0023 * Should always be created using KIO::open(const QUrl&, QIODevice::OpenMode). 0024 */ 0025 0026 class KIOCORE_EXPORT FileJob : public SimpleJob 0027 { 0028 Q_OBJECT 0029 0030 public: 0031 ~FileJob() override; 0032 0033 /** 0034 * This function attempts to read up to \p size bytes from the URL passed to 0035 * KIO::open() and returns the bytes received via the data() signal. 0036 * 0037 * The read operation commences at the current file offset, and the file 0038 * offset is incremented by the number of bytes read, but this change in the 0039 * offset does not result in the position() signal being emitted. 0040 * 0041 * If the current file offset is at or past the end of file (i.e. EOD), no 0042 * bytes are read, and the data() signal returns an empty QByteArray. 0043 * 0044 * On error the data() signal is not emitted. To catch errors please connect 0045 * to the result() signal. 0046 * 0047 * @param size the requested amount of data to read 0048 * 0049 */ 0050 void read(KIO::filesize_t size); 0051 0052 /** 0053 * This function attempts to write all the bytes in \p data to the URL 0054 * passed to KIO::open() and returns the bytes written received via the 0055 * written() signal. 0056 * 0057 * The write operation commences at the current file offset, and the file 0058 * offset is incremented by the number of bytes read, but this change in the 0059 * offset does not result in the position() being emitted. 0060 * 0061 * On error the written() signal is not emitted. To catch errors please 0062 * connect to the result() signal. 0063 * 0064 * @param data the data to write 0065 */ 0066 void write(const QByteArray &data); 0067 0068 /** 0069 * Closes the file KIO worker. 0070 * 0071 * The worker emits close() and result(). 0072 */ 0073 void close(); 0074 0075 /** 0076 * Seek 0077 * 0078 * The worker emits position() on successful seek to the specified \p offset. 0079 * 0080 * On error the position() signal is not emitted. To catch errors please 0081 * connect to the result() signal. 0082 * 0083 * @param offset the position from start to go to 0084 */ 0085 void seek(KIO::filesize_t offset); 0086 0087 /** 0088 * Truncate 0089 * 0090 * The worker emits truncated() on successful truncation to the specified \p length. 0091 * 0092 * On error the truncated() signal is not emitted. To catch errors please 0093 * connect to the result() signal. 0094 * 0095 * @param length the desired length to truncate to 0096 * @since 5.66 0097 */ 0098 void truncate(KIO::filesize_t length); 0099 0100 /** 0101 * Size 0102 * 0103 * @return the file size 0104 */ 0105 KIO::filesize_t size(); 0106 0107 Q_SIGNALS: 0108 /** 0109 * Data from the worker has arrived. Emitted after read(). 0110 * 0111 * Unless a read() request was sent for 0 bytes, End of data (EOD) has been 0112 * reached if data.size() == 0 0113 * 0114 * @param job the job that emitted this signal 0115 * @param data data received from the worker. 0116 * 0117 */ 0118 void data(KIO::Job *job, const QByteArray &data); 0119 0120 /** 0121 * Signals the file is a redirection. 0122 * Follow this url manually to reach data 0123 * @param job the job that emitted this signal 0124 * @param url the new URL 0125 */ 0126 void redirection(KIO::Job *job, const QUrl &url); 0127 0128 /** 0129 * MIME type determined. 0130 * @param job the job that emitted this signal 0131 * @param mimeType the MIME type 0132 * @since 5.78 0133 */ 0134 void mimeTypeFound(KIO::Job *job, const QString &mimeType); 0135 0136 /** 0137 * File is open, metadata has been determined and the 0138 * file KIO worker is ready to receive commands. 0139 * @param job the job that emitted this signal 0140 */ 0141 void open(KIO::Job *job); 0142 0143 /** 0144 * \p written bytes were written to the file. Emitted after write(). 0145 * @param job the job that emitted this signal 0146 * @param written bytes written. 0147 */ 0148 void written(KIO::Job *job, KIO::filesize_t written); 0149 0150 /** 0151 * Signals that the file is closed and will accept no more commands. 0152 * 0153 * @param job the job that emitted this signal 0154 * 0155 * @since 5.79 0156 */ 0157 void fileClosed(KIO::Job *job); 0158 0159 /** 0160 * The file has reached this position. Emitted after seek(). 0161 * @param job the job that emitted this signal 0162 * @param offset the new position 0163 */ 0164 void position(KIO::Job *job, KIO::filesize_t offset); 0165 0166 /** 0167 * The file has been truncated to this point. Emitted after truncate(). 0168 * @param job the job that emitted this signal 0169 * @param length the new length of the file 0170 * @since 5.66 0171 */ 0172 void truncated(KIO::Job *job, KIO::filesize_t length); 0173 0174 protected: 0175 KIOCORE_NO_EXPORT explicit FileJob(FileJobPrivate &dd); 0176 0177 private: 0178 Q_DECLARE_PRIVATE(FileJob) 0179 }; 0180 0181 /** 0182 * Open ( random access I/O ) 0183 * 0184 * The file-job emits open() when opened 0185 * 0186 * On error the open() signal is not emitted. To catch errors please 0187 * connect to the result() signal. 0188 * 0189 * @param url the URL of the file 0190 * @param mode the access privileges: see \ref OpenMode 0191 * 0192 * @return The file-handling job. It will never return 0. Errors are handled asynchronously 0193 * (emitted as signals). 0194 */ 0195 KIOCORE_EXPORT FileJob *open(const QUrl &url, QIODevice::OpenMode mode); 0196 0197 } // namespace 0198 0199 #endif