File indexing completed on 2024-04-21 03:55:14

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0004     SPDX-FileCopyrightText: 2000-2013 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KIO_TRANSFERJOB_H
0010 #define KIO_TRANSFERJOB_H
0011 
0012 #include "simplejob.h"
0013 
0014 namespace KIO
0015 {
0016 class TransferJobPrivate;
0017 /**
0018  * @class KIO::TransferJob transferjob.h <KIO/TransferJob>
0019  *
0020  * The transfer job pumps data into and/or out of a KIO worker.
0021  * Data is sent to the worker on request of the worker ( dataReq).
0022  * If data coming from the worker can not be handled, the
0023  * reading of data from the worker should be suspended.
0024  */
0025 class KIOCORE_EXPORT TransferJob : public SimpleJob
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     ~TransferJob() override;
0031 
0032     /**
0033      * Sets the modification time of the file to be created (by KIO::put)
0034      * Note that some KIO workers might ignore this.
0035      */
0036     void setModificationTime(const QDateTime &mtime);
0037 
0038     /**
0039      * Checks whether we got an error page. This currently only happens
0040      * with HTTP urls. Call this from your slot connected to result().
0041      *
0042      * @return true if we got an (HTML) error page from the server
0043      * instead of what we asked for.
0044      */
0045     bool isErrorPage() const;
0046 
0047     /**
0048      * Enable the async data mode.
0049      * When async data is enabled, data should be provided to the job by
0050      * calling sendAsyncData() instead of returning data in the
0051      * dataReq() signal.
0052      */
0053     void setAsyncDataEnabled(bool enabled);
0054 
0055     /**
0056      * Provide data to the job when async data is enabled.
0057      * Should be called exactly once after receiving a dataReq signal
0058      * Sending an empty block indicates end of data.
0059      */
0060     void sendAsyncData(const QByteArray &data);
0061 
0062     /**
0063      * Call this in the slot connected to result,
0064      * and only after making sure no error happened.
0065      * @return the MIME type of the URL
0066      */
0067     QString mimetype() const;
0068 
0069     /**
0070      * After the job has finished, it will return the final url in case a redirection
0071      * has happened.
0072      * @return the final url that can be empty in case no redirection has happened.
0073      * @since 5.0
0074      */
0075     QUrl redirectUrl() const;
0076 
0077     /**
0078      * Set the total size of data that we are going to send
0079      * in a put job. Helps getting proper progress information.
0080      * @since 4.2.1
0081      */
0082     void setTotalSize(KIO::filesize_t bytes);
0083 
0084 protected:
0085     /**
0086      * Reimplemented for internal reasons
0087      */
0088     bool doResume() override;
0089 
0090 Q_SIGNALS:
0091     /**
0092      * Data from the worker has arrived.
0093      * @param job the job that emitted this signal
0094      * @param data data received from the worker.
0095      *
0096      * End of data (EOD) has been reached if data.size() == 0, however, you
0097      * should not be certain of data.size() == 0 ever happening (e.g. in case
0098      * of an error), so you should rely on result() instead.
0099      */
0100     void data(KIO::Job *job, const QByteArray &data);
0101 
0102     /**
0103      * Request for data.
0104      * Please note, that you shouldn't put too large chunks
0105      * of data in it as this requires copies within the frame
0106      * work, so you should rather split the data you want
0107      * to pass here in reasonable chunks (about 1MB maximum)
0108      *
0109      * @param job the job that emitted this signal
0110      * @param data buffer to fill with data to send to the
0111      * worker. An empty buffer indicates end of data. (EOD)
0112      */
0113     void dataReq(KIO::Job *job, QByteArray &data);
0114 
0115     /**
0116      * Signals a redirection.
0117      * Use to update the URL shown to the user.
0118      * The redirection itself is handled internally.
0119      * @param job the job that emitted this signal
0120      * @param url the new URL
0121      */
0122     void redirection(KIO::Job *job, const QUrl &url);
0123 
0124     /**
0125      * Signals a permanent redirection.
0126      * The redirection itself is handled internally.
0127      * @param job the job that emitted this signal
0128      * @param fromUrl the original URL
0129      * @param toUrl the new URL
0130      */
0131     void permanentRedirection(KIO::Job *job, const QUrl &fromUrl, const QUrl &toUrl);
0132 
0133     /**
0134      * MIME type determined.
0135      * @param job the job that emitted this signal
0136      * @param mimeType the MIME type
0137      * @since 5.78
0138      */
0139     void mimeTypeFound(KIO::Job *job, const QString &mimeType);
0140 
0141     /**
0142      * @internal
0143      * Emitted if the "put" job found an existing partial file
0144      * (in which case offset is the size of that file)
0145      * and emitted by the "get" job if it supports resuming to
0146      * the given offset - in this case @p offset is unused)
0147      */
0148     void canResume(KIO::Job *job, KIO::filesize_t offset);
0149 
0150 protected Q_SLOTS:
0151     virtual void slotRedirection(const QUrl &url);
0152     void slotFinished() override;
0153     virtual void slotData(const QByteArray &data);
0154     virtual void slotDataReq();
0155     virtual void slotMimetype(const QString &mimetype);
0156 
0157 protected:
0158     KIOCORE_NO_EXPORT explicit TransferJob(TransferJobPrivate &dd);
0159 
0160 private:
0161     Q_DECLARE_PRIVATE(TransferJob)
0162 
0163     // A FileCopyJob may control one or more TransferJobs
0164     friend class FileCopyJob;
0165     friend class FileCopyJobPrivate;
0166 };
0167 
0168 /**
0169  * Get (means: read).
0170  * This is the job to use in order to "download" a file into memory.
0171  * The worker emits the data through the data() signal.
0172  *
0173  * Special case: if you want to determine the MIME type of the file first,
0174  * and then read it with the appropriate component, you can still use
0175  * a KIO::get() directly. When that job emits the mimeType signal, (which is
0176  * guaranteed to happen before it emits any data), put the job on hold:
0177  *
0178  * @code
0179  *   job->putOnHold();
0180  * @endcode
0181  *
0182  * and forget about the job. The next time someone does a KIO::get() on the
0183  * same URL (even in another process) this job will be resumed. This saves KIO
0184  * from doing two requests to the server.
0185  *
0186  * @param url the URL of the file
0187  * @param reload Reload to reload the file, NoReload if it can be taken from the cache
0188  * @param flags Can be HideProgressInfo here
0189  * @return the job handling the operation.
0190  */
0191 KIOCORE_EXPORT TransferJob *get(const QUrl &url, LoadType reload = NoReload, JobFlags flags = DefaultFlags);
0192 
0193 /**
0194  * Put (means: write)
0195  *
0196  * @param url Where to write data.
0197  * @param permissions May be -1. In this case no special permission mode is set.
0198  * @param flags Can be HideProgressInfo, Overwrite and Resume here. WARNING:
0199  * Setting Resume means that the data will be appended to @p dest if @p dest exists.
0200  * @return the job handling the operation.
0201  * @see multi_get()
0202  */
0203 KIOCORE_EXPORT TransferJob *put(const QUrl &url, int permissions, JobFlags flags = DefaultFlags);
0204 
0205 /**
0206  * HTTP POST (for form data).
0207  *
0208  * Example:
0209  * \code
0210  *    job = KIO::http_post( url, postData, KIO::HideProgressInfo );
0211  *    job->addMetaData("content-type", contentType );
0212  * \endcode
0213  *
0214  * @p postData is the data that you want to send and
0215  * @p contentType is the complete HTTP header line that
0216  * specifies the content's MIME type, for example
0217  * "Content-Type: text/xml".
0218  *
0219  * You MUST specify content-type!
0220  *
0221  * Often @p contentType is
0222  * "Content-Type: application/x-www-form-urlencoded" and
0223  * the @p postData is then an ASCII string (without null-termination!)
0224  * with characters like space, linefeed and percent escaped like %20,
0225  * %0A and %25.
0226  *
0227  * @param url Where to write the data.
0228  * @param postData Encoded data to post.
0229  * @param flags Can be HideProgressInfo here
0230  * @return the job handling the operation.
0231  */
0232 KIOCORE_EXPORT TransferJob *http_post(const QUrl &url, const QByteArray &postData, JobFlags flags = DefaultFlags);
0233 
0234 /**
0235  * HTTP POST.
0236  *
0237  * This function, unlike the one that accepts a QByteArray, accepts an IO device
0238  * from which to read the encoded data to be posted to the server in order to
0239  * to avoid holding the content of very large post requests, e.g. multimedia file
0240  * uploads, in memory.
0241  *
0242  * @param url Where to write the data.
0243  * @param device the device to read from
0244  * @param size Size of the encoded post data.
0245  * @param flags Can be HideProgressInfo here
0246  * @return the job handling the operation.
0247  *
0248  */
0249 KIOCORE_EXPORT TransferJob *http_post(const QUrl &url, QIODevice *device, qint64 size = -1, JobFlags flags = DefaultFlags);
0250 
0251 /**
0252  * HTTP DELETE.
0253  *
0254  * Though this function servers the same purpose as KIO::file_delete, unlike
0255  * file_delete it accommodates HTTP specific actions such as redirections.
0256  *
0257  * @param url url resource to delete.
0258  * @param flags Can be HideProgressInfo here
0259  * @return the job handling the operation.
0260  *
0261  * @since 4.7.3
0262  */
0263 KIOCORE_EXPORT TransferJob *http_delete(const QUrl &url, JobFlags flags = DefaultFlags);
0264 
0265 }
0266 
0267 #endif