File indexing completed on 2024-04-28 11:40:54

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 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 78)
0129     /**
0130      * MIME type determined.
0131      * @param job the job that emitted this signal
0132      * @param mimeType the MIME type
0133      * @deprecated Since 5.78, use mimeTypeFound(KIO::Job *, const QString &)
0134      */
0135     KIOCORE_DEPRECATED_VERSION(5, 78, "Use KIO::TransferJob::mimeTypeFound(KIO::Job *, const QString &)")
0136     void mimetype(KIO::Job *job, const QString &mimeType); // clazy:exclude=overloaded-signal
0137 #endif
0138 
0139     /**
0140      * MIME type determined.
0141      * @param job the job that emitted this signal
0142      * @param mimeType the MIME type
0143      * @since 5.78
0144      */
0145     void mimeTypeFound(KIO::Job *job, const QString &mimeType);
0146 
0147     /**
0148      * File is open, metadata has been determined and the
0149      * file KIO worker is ready to receive commands.
0150      * @param job the job that emitted this signal
0151      */
0152     void open(KIO::Job *job);
0153 
0154     /**
0155      * \p written bytes were written to the file. Emitted after write().
0156      * @param job the job that emitted this signal
0157      * @param written bytes written.
0158      */
0159     void written(KIO::Job *job, KIO::filesize_t written);
0160 
0161 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 79)
0162     /**
0163      * Signals that the file is closed and will accept no more commands.
0164      *
0165      * @param job the job that emitted this signal
0166      *
0167      * @deprecated since 5.79, use KIO::FileJob::fileClosed(KIO::Job *)
0168      */
0169     KIOCORE_DEPRECATED_VERSION(5, 79, "Use KIO::FileJob::fileClosed(KIO::Job *)")
0170     void close(KIO::Job *job); // clazy:exclude=overloaded-signal
0171 #endif
0172 
0173     /**
0174      * Signals that the file is closed and will accept no more commands.
0175      *
0176      * @param job the job that emitted this signal
0177      *
0178      * @since 5.79
0179      */
0180     void fileClosed(KIO::Job *job);
0181 
0182     /**
0183      * The file has reached this position. Emitted after seek().
0184      * @param job the job that emitted this signal
0185      * @param offset the new position
0186      */
0187     void position(KIO::Job *job, KIO::filesize_t offset);
0188 
0189     /**
0190      * The file has been truncated to this point. Emitted after truncate().
0191      * @param job the job that emitted this signal
0192      * @param length the new length of the file
0193      * @since 5.66
0194      */
0195     void truncated(KIO::Job *job, KIO::filesize_t length);
0196 
0197 protected:
0198     KIOCORE_NO_EXPORT explicit FileJob(FileJobPrivate &dd);
0199 
0200 private:
0201     Q_DECLARE_PRIVATE(FileJob)
0202 };
0203 
0204 /**
0205  * Open ( random access I/O )
0206  *
0207  * The file-job emits open() when opened
0208  *
0209  * On error the open() signal is not emitted. To catch errors please
0210  * connect to the result() signal.
0211  *
0212  * @param url the URL of the file
0213  * @param mode the access privileges: see \ref OpenMode
0214  *
0215  * @return The file-handling job. It will never return 0. Errors are handled asynchronously
0216  * (emitted as signals).
0217  */
0218 KIOCORE_EXPORT FileJob *open(const QUrl &url, QIODevice::OpenMode mode);
0219 
0220 } // namespace
0221 
0222 #endif