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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0004     SPDX-FileCopyrightText: 2000-2009 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KIO_JOB_BASE_H
0010 #define KIO_JOB_BASE_H
0011 
0012 #include <KCompositeJob>
0013 #include <kio/metadata.h>
0014 
0015 namespace KIO
0016 {
0017 class JobUiDelegateExtension;
0018 
0019 class JobPrivate;
0020 /**
0021  * @class KIO::Job job_base.h <KIO/Job>
0022  *
0023  * The base class for all jobs.
0024  * For all jobs created in an application, the code looks like
0025  *
0026  * \code
0027  *   KIO::Job* job = KIO::someoperation(some parameters);
0028  *   connect(job, &KJob::result, this, &MyClass::slotResult);
0029  * \endcode
0030  *   (other connects, specific to the job)
0031  *
0032  * And slotResult is usually at least:
0033  *
0034  * \code
0035  * void MyClass::slotResult(KJob *job)
0036  * {
0037  *   if (job->error()) {
0038  *     job->uiDelegate()->showErrorMessage();
0039  *   }
0040  * }
0041  * \endcode
0042  * @see KIO::Scheduler
0043  */
0044 class KIOCORE_EXPORT Job : public KCompositeJob
0045 {
0046     Q_OBJECT
0047 
0048 protected:
0049     Job();
0050     // used also from KIOGui's PreviewJob, so needs to be exported
0051     explicit Job(JobPrivate &dd);
0052 
0053 public:
0054     ~Job() override;
0055     void start() override
0056     {
0057     } // Since KIO autostarts its jobs
0058 
0059     /**
0060      * Retrieves the UI delegate extension used by this job.
0061      * @since 5.0
0062      */
0063     JobUiDelegateExtension *uiDelegateExtension() const;
0064 
0065     /**
0066      * Sets the UI delegate extension to be used by this job.
0067      * The default UI delegate extension is KIO::defaultJobUiDelegateExtension()
0068      */
0069     void setUiDelegateExtension(JobUiDelegateExtension *extension);
0070 
0071 protected:
0072     /**
0073      * Abort this job.
0074      * This kills all subjobs and deletes the job.
0075      *
0076      */
0077     bool doKill() override;
0078 
0079     /**
0080      * Suspend this job
0081      * @see resume
0082      */
0083     bool doSuspend() override;
0084 
0085     /**
0086      * Resume this job
0087      * @see suspend
0088      */
0089     bool doResume() override;
0090 
0091 public:
0092     /**
0093      * Converts an error code and a non-i18n error message into an
0094      * error message in the current language. The low level (non-i18n)
0095      * error message (usually a url) is put into the translated error
0096      * message using %1.
0097      *
0098      * Example for errid == ERR_CANNOT_OPEN_FOR_READING:
0099      * \code
0100      *   i18n("Could not read\n%1", errortext);
0101      * \endcode
0102      * Use this to display the error yourself, but for a dialog box
0103      * use uiDelegate()->showErrorMessage(). Do not call it if error()
0104      * is not 0.
0105      * @return the error message and if there is no error, a message
0106      *         telling the user that the app is broken, so check with
0107      *         error() whether there is an error
0108      */
0109     QString errorString() const override;
0110 
0111     /**
0112      * Converts an error code and a non-i18n error message into i18n
0113      * strings suitable for presentation in a detailed error message box.
0114      *
0115      * @param reqUrl the request URL that generated this error message
0116      * @param method the method that generated this error message
0117      * (unimplemented)
0118      * @return the following strings: title, error + description,
0119      *         causes+solutions
0120      */
0121     QStringList detailedErrorStrings(const QUrl *reqUrl = nullptr, int method = -1) const;
0122 
0123     /**
0124      * Set the parent Job.
0125      * One example use of this is when FileCopyJob calls RenameDialog::open,
0126      * it must pass the correct progress ID of the parent CopyJob
0127      * (to hide the progress dialog).
0128      * You can set the parent job only once. By default a job does not
0129      * have a parent job.
0130      * @param parentJob the new parent job
0131      */
0132     void setParentJob(Job *parentJob);
0133 
0134     /**
0135      * Returns the parent job, if there is one.
0136      * @return the parent job, or @c nullptr if there is none
0137      * @see setParentJob
0138      */
0139     Job *parentJob() const;
0140 
0141     /**
0142      * Set meta data to be sent to the worker, replacing existing
0143      * meta data.
0144      * @param metaData the meta data to set
0145      * @see addMetaData()
0146      * @see mergeMetaData()
0147      */
0148     void setMetaData(const KIO::MetaData &metaData);
0149 
0150     /**
0151      * Add key/value pair to the meta data that is sent to the worker.
0152      * @param key the key of the meta data
0153      * @param value the value of the meta data
0154      * @see setMetaData()
0155      * @see mergeMetaData()
0156      */
0157     void addMetaData(const QString &key, const QString &value);
0158 
0159     /**
0160      * Add key/value pairs to the meta data that is sent to the worker.
0161      * If a certain key already existed, it will be overridden.
0162      * @param values the meta data to add
0163      * @see setMetaData()
0164      * @see mergeMetaData()
0165      */
0166     void addMetaData(const QMap<QString, QString> &values);
0167 
0168     /**
0169      * Add key/value pairs to the meta data that is sent to the worker.
0170      * If a certain key already existed, it will remain unchanged.
0171      * @param values the meta data to merge
0172      * @see setMetaData()
0173      * @see addMetaData()
0174      */
0175     void mergeMetaData(const QMap<QString, QString> &values);
0176 
0177     /**
0178      * @internal. For the scheduler. Do not use.
0179      */
0180     MetaData outgoingMetaData() const;
0181 
0182     /**
0183      * Get meta data received from the worker.
0184      * (Valid when first data is received and/or worker is finished)
0185      * @return the job's meta data
0186      */
0187     MetaData metaData() const;
0188 
0189     /**
0190      * Query meta data received from the worker.
0191      * (Valid when first data is received and/or worker is finished)
0192      * @param key the key of the meta data to retrieve
0193      * @return the value of the meta data, or QString() if the
0194      *         @p key does not exist
0195      */
0196     QString queryMetaData(const QString &key);
0197 
0198 protected:
0199 Q_SIGNALS:
0200     /**
0201      * Emitted when the worker successfully connected to the host.
0202      * There is no guarantee the worker will send this, and this is
0203      * currently unused (in the applications).
0204      * @param job the job that emitted this signal
0205      */
0206     void connected(KIO::Job *job);
0207 
0208 protected:
0209     /**
0210      * Add a job that has to be finished before a result
0211      * is emitted. This has obviously to be called before
0212      * the finish signal is emitted by the worker.
0213      *
0214      * @param job the subjob to add
0215      */
0216     bool addSubjob(KJob *job) override;
0217 
0218     /**
0219      * Mark a sub job as being done.
0220      *
0221      * Note that this does not terminate the parent job, even if @p job
0222      * is the last subjob.  emitResult must be called to indicate that
0223      * the job is complete.
0224      *
0225      * @param job the subjob to remove
0226      */
0227     bool removeSubjob(KJob *job) override;
0228 
0229 protected:
0230     JobPrivate *const d_ptr;
0231 
0232 private:
0233     Q_DECLARE_PRIVATE(Job)
0234 };
0235 
0236 /**
0237  * Flags for the job properties.
0238  * Not all flags are supported in all cases. Please see documentation of
0239  * the calling function!
0240  * @see JobFlags
0241  */
0242 enum JobFlag {
0243     /**
0244      * Show the progress info GUI, no Resume and no Overwrite
0245      */
0246     DefaultFlags = 0,
0247 
0248     /**
0249      * Hide progress information dialog, i.e.\ don't show a GUI.
0250      */
0251     HideProgressInfo = 1,
0252 
0253     /**
0254      * When set, automatically append to the destination file if it exists already.
0255      * WARNING: this is NOT the builtin support for offering the user to resume a previous
0256      * partial download. The Resume option is much less used, it allows to append
0257      * to an existing file.
0258      * This is used by KIO::put(), KIO::file_copy(), KIO::file_move().
0259      */
0260     Resume = 2,
0261 
0262     /**
0263      * When set, automatically overwrite the destination if it exists already.
0264      * This is used by KIO::rename(), KIO::put(), KIO::file_copy(), KIO::file_move(), KIO::symlink().
0265      * Otherwise the operation will fail with ERR_FILE_ALREADY_EXIST or ERR_DIR_ALREADY_EXIST.
0266      */
0267     Overwrite = 4,
0268 
0269     /**
0270      * When set, notifies the worker that application/job does not want privilege execution.
0271      * So in case of failure due to insufficient privileges show an error without attempting
0272      * to run the operation as root first.
0273      *
0274      * @since 5.43
0275      */
0276     NoPrivilegeExecution = 8,
0277 };
0278 /**
0279  * Stores a combination of #JobFlag values.
0280  */
0281 Q_DECLARE_FLAGS(JobFlags, JobFlag)
0282 Q_DECLARE_OPERATORS_FOR_FLAGS(JobFlags)
0283 
0284 enum LoadType { Reload, NoReload };
0285 
0286 }
0287 
0288 #endif