File indexing completed on 2024-10-06 09:39:30

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 #if KIOCORE_ENABLE_DEPRECATED_SINCE(4, 3)
0063     /**
0064      * When enabled, the job reports the amount of data that has been sent,
0065      * instead of the amount of data that has been received.
0066      * @see slotProcessedSize
0067      * @see slotSpeed
0068      * @deprecated since 4.2.1, this is unnecessary (it is always false for
0069      *             KIO::get and true for KIO::put)
0070      */
0071     KIOCORE_DEPRECATED_VERSION(4, 3, "No longer needed")
0072     void setReportDataSent(bool enabled);
0073 #endif
0074 
0075 #if KIOCORE_ENABLE_DEPRECATED_SINCE(4, 3)
0076     /**
0077      *  Returns whether the job reports the amount of data that has been
0078      *  sent (true), or whether the job reports the amount of data that
0079      * has been received (false)
0080      * @deprecated since 4.2.1, this is unnecessary (it is always false for
0081      *             KIO::get and true for KIO::put)
0082      */
0083     KIOCORE_DEPRECATED_VERSION(4, 3, "No longer needed")
0084     bool reportDataSent() const;
0085 #endif
0086 
0087     /**
0088      * Call this in the slot connected to result,
0089      * and only after making sure no error happened.
0090      * @return the MIME type of the URL
0091      */
0092     QString mimetype() const;
0093 
0094     /**
0095      * After the job has finished, it will return the final url in case a redirection
0096      * has happened.
0097      * @return the final url that can be empty in case no redirection has happened.
0098      * @since 5.0
0099      */
0100     QUrl redirectUrl() const;
0101 
0102     /**
0103      * Set the total size of data that we are going to send
0104      * in a put job. Helps getting proper progress information.
0105      * @since 4.2.1
0106      */
0107     void setTotalSize(KIO::filesize_t bytes);
0108 
0109 protected:
0110     /**
0111      * Called when m_subJob finishes.
0112      * @param job the job that finished
0113      */
0114     void slotResult(KJob *job) override;
0115 
0116     /**
0117      * Reimplemented for internal reasons
0118      */
0119     bool doResume() override;
0120 
0121 Q_SIGNALS:
0122     /**
0123      * Data from the worker has arrived.
0124      * @param job the job that emitted this signal
0125      * @param data data received from the worker.
0126      *
0127      * End of data (EOD) has been reached if data.size() == 0, however, you
0128      * should not be certain of data.size() == 0 ever happening (e.g. in case
0129      * of an error), so you should rely on result() instead.
0130      */
0131     void data(KIO::Job *job, const QByteArray &data);
0132 
0133     /**
0134      * Request for data.
0135      * Please note, that you shouldn't put too large chunks
0136      * of data in it as this requires copies within the frame
0137      * work, so you should rather split the data you want
0138      * to pass here in reasonable chunks (about 1MB maximum)
0139      *
0140      * @param job the job that emitted this signal
0141      * @param data buffer to fill with data to send to the
0142      * worker. An empty buffer indicates end of data. (EOD)
0143      */
0144     void dataReq(KIO::Job *job, QByteArray &data);
0145 
0146     /**
0147      * Signals a redirection.
0148      * Use to update the URL shown to the user.
0149      * The redirection itself is handled internally.
0150      * @param job the job that emitted this signal
0151      * @param url the new URL
0152      */
0153     void redirection(KIO::Job *job, const QUrl &url);
0154 
0155     /**
0156      * Signals a permanent redirection.
0157      * The redirection itself is handled internally.
0158      * @param job the job that emitted this signal
0159      * @param fromUrl the original URL
0160      * @param toUrl the new URL
0161      */
0162     void permanentRedirection(KIO::Job *job, const QUrl &fromUrl, const QUrl &toUrl);
0163 
0164 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 78)
0165     /**
0166      * MIME type determined.
0167      * @param job the job that emitted this signal
0168      * @param mimeType the MIME type
0169      * @deprecated Since 5.78, use mimeTypeFound(KIO::Job *, const QString &)
0170      */
0171     KIOCORE_DEPRECATED_VERSION(5, 78, "Use KIO::TransferJob::mimeTypeFound(KIO::Job *, const QString &)")
0172     void mimetype(KIO::Job *job, const QString &mimeType); // clazy:exclude=overloaded-signal
0173 #endif
0174 
0175     /**
0176      * MIME type determined.
0177      * @param job the job that emitted this signal
0178      * @param mimeType the MIME type
0179      * @since 5.78
0180      */
0181     void mimeTypeFound(KIO::Job *job, const QString &mimeType);
0182 
0183     /**
0184      * @internal
0185      * Emitted if the "put" job found an existing partial file
0186      * (in which case offset is the size of that file)
0187      * and emitted by the "get" job if it supports resuming to
0188      * the given offset - in this case @p offset is unused)
0189      */
0190     void canResume(KIO::Job *job, KIO::filesize_t offset);
0191 
0192 protected Q_SLOTS:
0193     virtual void slotRedirection(const QUrl &url);
0194     void slotFinished() override;
0195     virtual void slotData(const QByteArray &data);
0196     virtual void slotDataReq();
0197     virtual void slotMimetype(const QString &mimetype);
0198 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 101) // override no longer needed
0199     void slotMetaData(const KIO::MetaData &_metaData) override;
0200 #endif
0201 
0202 protected:
0203     KIOCORE_NO_EXPORT explicit TransferJob(TransferJobPrivate &dd);
0204 
0205 private:
0206     Q_DECLARE_PRIVATE(TransferJob)
0207 
0208     // A FileCopyJob may control one or more TransferJobs
0209     friend class FileCopyJob;
0210     friend class FileCopyJobPrivate;
0211 };
0212 
0213 /**
0214  * Get (means: read).
0215  * This is the job to use in order to "download" a file into memory.
0216  * The worker emits the data through the data() signal.
0217  *
0218  * Special case: if you want to determine the MIME type of the file first,
0219  * and then read it with the appropriate component, you can still use
0220  * a KIO::get() directly. When that job emits the mimeType signal, (which is
0221  * guaranteed to happen before it emits any data), put the job on hold:
0222  *
0223  * @code
0224  *   job->putOnHold();
0225  *   KIO::Scheduler::publishSlaveOnHold();
0226  * @endcode
0227  *
0228  * and forget about the job. The next time someone does a KIO::get() on the
0229  * same URL (even in another process) this job will be resumed. This saves KIO
0230  * from doing two requests to the server.
0231  *
0232  * @param url the URL of the file
0233  * @param reload Reload to reload the file, NoReload if it can be taken from the cache
0234  * @param flags Can be HideProgressInfo here
0235  * @return the job handling the operation.
0236  */
0237 KIOCORE_EXPORT TransferJob *get(const QUrl &url, LoadType reload = NoReload, JobFlags flags = DefaultFlags);
0238 
0239 /**
0240  * Put (means: write)
0241  *
0242  * @param url Where to write data.
0243  * @param permissions May be -1. In this case no special permission mode is set.
0244  * @param flags Can be HideProgressInfo, Overwrite and Resume here. WARNING:
0245  * Setting Resume means that the data will be appended to @p dest if @p dest exists.
0246  * @return the job handling the operation.
0247  * @see multi_get()
0248  */
0249 KIOCORE_EXPORT TransferJob *put(const QUrl &url, int permissions, JobFlags flags = DefaultFlags);
0250 
0251 /**
0252  * HTTP POST (for form data).
0253  *
0254  * Example:
0255  * \code
0256  *    job = KIO::http_post( url, postData, KIO::HideProgressInfo );
0257  *    job->addMetaData("content-type", contentType );
0258  *    job->addMetaData("referrer", referrerURL);
0259  * \endcode
0260  *
0261  * @p postData is the data that you want to send and
0262  * @p contentType is the complete HTTP header line that
0263  * specifies the content's MIME type, for example
0264  * "Content-Type: text/xml".
0265  *
0266  * You MUST specify content-type!
0267  *
0268  * Often @p contentType is
0269  * "Content-Type: application/x-www-form-urlencoded" and
0270  * the @p postData is then an ASCII string (without null-termination!)
0271  * with characters like space, linefeed and percent escaped like %20,
0272  * %0A and %25.
0273  *
0274  * @param url Where to write the data.
0275  * @param postData Encoded data to post.
0276  * @param flags Can be HideProgressInfo here
0277  * @return the job handling the operation.
0278  */
0279 KIOCORE_EXPORT TransferJob *http_post(const QUrl &url, const QByteArray &postData, JobFlags flags = DefaultFlags);
0280 
0281 /**
0282  * HTTP POST.
0283  *
0284  * This function, unlike the one that accepts a QByteArray, accepts an IO device
0285  * from which to read the encoded data to be posted to the server in order to
0286  * to avoid holding the content of very large post requests, e.g. multimedia file
0287  * uploads, in memory.
0288  *
0289  * @param url Where to write the data.
0290  * @param device the device to read from
0291  * @param size Size of the encoded post data.
0292  * @param flags Can be HideProgressInfo here
0293  * @return the job handling the operation.
0294  *
0295  * @since 4.7
0296  */
0297 KIOCORE_EXPORT TransferJob *http_post(const QUrl &url, QIODevice *device, qint64 size = -1, JobFlags flags = DefaultFlags);
0298 
0299 /**
0300  * HTTP DELETE.
0301  *
0302  * Though this function servers the same purpose as KIO::file_delete, unlike
0303  * file_delete it accommodates HTTP specific actions such as redirections.
0304  *
0305  * @param url url resource to delete.
0306  * @param flags Can be HideProgressInfo here
0307  * @return the job handling the operation.
0308  *
0309  * @since 4.7.3
0310  */
0311 KIOCORE_EXPORT TransferJob *http_delete(const QUrl &url, JobFlags flags = DefaultFlags);
0312 
0313 }
0314 
0315 #endif