File indexing completed on 2024-09-15 11:59:48
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 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) 0060 /** 0061 * Retrieves the UI delegate of this job. 0062 * 0063 * @return the delegate used by the job to communicate with the UI 0064 * 0065 * @deprecated since 5.0, can now be replaced with uiDelegate() 0066 */ 0067 KIOCORE_DEPRECATED_VERSION(5, 0, "Use KJob::uiDelegate()") 0068 KJobUiDelegate *ui() const; 0069 #endif 0070 0071 /** 0072 * Retrieves the UI delegate extension used by this job. 0073 * @since 5.0 0074 */ 0075 JobUiDelegateExtension *uiDelegateExtension() const; 0076 0077 /** 0078 * Sets the UI delegate extension to be used by this job. 0079 * The default UI delegate extension is KIO::defaultJobUiDelegateExtension() 0080 */ 0081 void setUiDelegateExtension(JobUiDelegateExtension *extension); 0082 0083 protected: 0084 /** 0085 * Abort this job. 0086 * This kills all subjobs and deletes the job. 0087 * 0088 */ 0089 bool doKill() override; 0090 0091 /** 0092 * Suspend this job 0093 * @see resume 0094 */ 0095 bool doSuspend() override; 0096 0097 /** 0098 * Resume this job 0099 * @see suspend 0100 */ 0101 bool doResume() override; 0102 0103 public: 0104 /** 0105 * Converts an error code and a non-i18n error message into an 0106 * error message in the current language. The low level (non-i18n) 0107 * error message (usually a url) is put into the translated error 0108 * message using %1. 0109 * 0110 * Example for errid == ERR_CANNOT_OPEN_FOR_READING: 0111 * \code 0112 * i18n("Could not read\n%1", errortext); 0113 * \endcode 0114 * Use this to display the error yourself, but for a dialog box 0115 * use uiDelegate()->showErrorMessage(). Do not call it if error() 0116 * is not 0. 0117 * @return the error message and if there is no error, a message 0118 * telling the user that the app is broken, so check with 0119 * error() whether there is an error 0120 */ 0121 QString errorString() const override; 0122 0123 /** 0124 * Converts an error code and a non-i18n error message into i18n 0125 * strings suitable for presentation in a detailed error message box. 0126 * 0127 * @param reqUrl the request URL that generated this error message 0128 * @param method the method that generated this error message 0129 * (unimplemented) 0130 * @return the following strings: title, error + description, 0131 * causes+solutions 0132 */ 0133 QStringList detailedErrorStrings(const QUrl *reqUrl = nullptr, int method = -1) const; 0134 0135 /** 0136 * Set the parent Job. 0137 * One example use of this is when FileCopyJob calls RenameDialog::open, 0138 * it must pass the correct progress ID of the parent CopyJob 0139 * (to hide the progress dialog). 0140 * You can set the parent job only once. By default a job does not 0141 * have a parent job. 0142 * @param parentJob the new parent job 0143 */ 0144 void setParentJob(Job *parentJob); 0145 0146 /** 0147 * Returns the parent job, if there is one. 0148 * @return the parent job, or @c nullptr if there is none 0149 * @see setParentJob 0150 */ 0151 Job *parentJob() const; 0152 0153 /** 0154 * Set meta data to be sent to the worker, replacing existing 0155 * meta data. 0156 * @param metaData the meta data to set 0157 * @see addMetaData() 0158 * @see mergeMetaData() 0159 */ 0160 void setMetaData(const KIO::MetaData &metaData); 0161 0162 /** 0163 * Add key/value pair to the meta data that is sent to the worker. 0164 * @param key the key of the meta data 0165 * @param value the value of the meta data 0166 * @see setMetaData() 0167 * @see mergeMetaData() 0168 */ 0169 void addMetaData(const QString &key, const QString &value); 0170 0171 /** 0172 * Add key/value pairs to the meta data that is sent to the worker. 0173 * If a certain key already existed, it will be overridden. 0174 * @param values the meta data to add 0175 * @see setMetaData() 0176 * @see mergeMetaData() 0177 */ 0178 void addMetaData(const QMap<QString, QString> &values); 0179 0180 /** 0181 * Add key/value pairs to the meta data that is sent to the worker. 0182 * If a certain key already existed, it will remain unchanged. 0183 * @param values the meta data to merge 0184 * @see setMetaData() 0185 * @see addMetaData() 0186 */ 0187 void mergeMetaData(const QMap<QString, QString> &values); 0188 0189 /** 0190 * @internal. For the scheduler. Do not use. 0191 */ 0192 MetaData outgoingMetaData() const; 0193 0194 /** 0195 * Get meta data received from the worker. 0196 * (Valid when first data is received and/or worker is finished) 0197 * @return the job's meta data 0198 */ 0199 MetaData metaData() const; 0200 0201 /** 0202 * Query meta data received from the worker. 0203 * (Valid when first data is received and/or worker is finished) 0204 * @param key the key of the meta data to retrieve 0205 * @return the value of the meta data, or QString() if the 0206 * @p key does not exist 0207 */ 0208 QString queryMetaData(const QString &key); 0209 0210 protected: 0211 Q_SIGNALS: 0212 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 0) 0213 /** 0214 * Emitted when the job is canceled. 0215 * Signal result() is emitted as well, and error() is, 0216 * in this case, ERR_USER_CANCELED. 0217 * @param job the job that emitted this signal 0218 * @deprecated Since 5.0. Don't use ! 0219 */ 0220 KIOCORE_DEPRECATED_VERSION(5, 0, "Do not use") 0221 void canceled(KJob *job); 0222 #endif 0223 0224 /** 0225 * Emitted when the worker successfully connected to the host. 0226 * There is no guarantee the worker will send this, and this is 0227 * currently unused (in the applications). 0228 * @param job the job that emitted this signal 0229 */ 0230 void connected(KIO::Job *job); 0231 0232 protected: 0233 /** 0234 * Add a job that has to be finished before a result 0235 * is emitted. This has obviously to be called before 0236 * the finish signal is emitted by the worker. 0237 * 0238 * @param job the subjob to add 0239 */ 0240 bool addSubjob(KJob *job) override; 0241 0242 /** 0243 * Mark a sub job as being done. 0244 * 0245 * Note that this does not terminate the parent job, even if @p job 0246 * is the last subjob. emitResult must be called to indicate that 0247 * the job is complete. 0248 * 0249 * @param job the subjob to remove 0250 */ 0251 bool removeSubjob(KJob *job) override; 0252 0253 protected: 0254 JobPrivate *const d_ptr; 0255 0256 private: 0257 Q_DECLARE_PRIVATE(Job) 0258 }; 0259 0260 /** 0261 * Flags for the job properties. 0262 * Not all flags are supported in all cases. Please see documentation of 0263 * the calling function! 0264 * @see JobFlags 0265 */ 0266 enum JobFlag { 0267 /** 0268 * Show the progress info GUI, no Resume and no Overwrite 0269 */ 0270 DefaultFlags = 0, 0271 0272 /** 0273 * Hide progress information dialog, i.e.\ don't show a GUI. 0274 */ 0275 HideProgressInfo = 1, 0276 0277 /** 0278 * When set, automatically append to the destination file if it exists already. 0279 * WARNING: this is NOT the builtin support for offering the user to resume a previous 0280 * partial download. The Resume option is much less used, it allows to append 0281 * to an existing file. 0282 * This is used by KIO::put(), KIO::file_copy(), KIO::file_move(). 0283 */ 0284 Resume = 2, 0285 0286 /** 0287 * When set, automatically overwrite the destination if it exists already. 0288 * This is used by KIO::rename(), KIO::put(), KIO::file_copy(), KIO::file_move(), KIO::symlink(). 0289 * Otherwise the operation will fail with ERR_FILE_ALREADY_EXIST or ERR_DIR_ALREADY_EXIST. 0290 */ 0291 Overwrite = 4, 0292 0293 /** 0294 * When set, notifies the worker that application/job does not want privilege execution. 0295 * So in case of failure due to insufficient privileges show an error without attempting 0296 * to run the operation as root first. 0297 * 0298 * @since 5.43 0299 */ 0300 NoPrivilegeExecution = 8, 0301 }; 0302 /** 0303 * Stores a combination of #JobFlag values. 0304 */ 0305 Q_DECLARE_FLAGS(JobFlags, JobFlag) 0306 Q_DECLARE_OPERATORS_FOR_FLAGS(JobFlags) 0307 0308 enum LoadType { Reload, NoReload }; 0309 0310 } 0311 0312 #endif