File indexing completed on 2024-10-06 12:21:37
0001 /* 0002 SPDX-License-Identifier: LGPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org> 0004 SPDX-FileCopyrightText: 2019-2022 Harald Sitter <sitter@kde.org> 0005 */ 0006 0007 #ifndef WORKERBASE_H 0008 #define WORKERBASE_H 0009 0010 #include <memory> 0011 0012 #include "slavebase.h" // for KIO::JobFlags 0013 0014 namespace KIO 0015 { 0016 class WorkerBasePrivate; 0017 class WorkerResultPrivate; 0018 0019 /** 0020 * @brief The result of a worker call 0021 * When using the Result type always mark the function Q_REQUIRED_RESULT to enforce handling of the Result. 0022 */ 0023 class KIOCORE_EXPORT WorkerResult 0024 { 0025 public: 0026 /// Use fail() or pass(); 0027 WorkerResult() = delete; 0028 ~WorkerResult(); 0029 WorkerResult(const WorkerResult &); 0030 WorkerResult &operator=(const WorkerResult &); 0031 WorkerResult(WorkerResult &&) noexcept; 0032 WorkerResult &operator=(WorkerResult &&) noexcept; 0033 0034 /// Whether or not the result was a success. 0035 bool success() const; 0036 /// The error code (or ERR_UNKNOWN) of the result. 0037 int error() const; 0038 /// The localized error description if applicable. 0039 QString errorString() const; 0040 0041 /// Constructs a failure results. 0042 Q_REQUIRED_RESULT static WorkerResult fail(int _error = KIO::ERR_UNKNOWN, const QString &_errorString = QString()); 0043 /// Constructs a success result. 0044 Q_REQUIRED_RESULT static WorkerResult pass(); 0045 0046 private: 0047 KIOCORE_NO_EXPORT explicit WorkerResult(std::unique_ptr<WorkerResultPrivate> &&dptr); 0048 std::unique_ptr<WorkerResultPrivate> d; 0049 }; 0050 0051 /** 0052 * @class KIO::WorkerBase workerbase.h <KIO/WorkerBase> 0053 * 0054 * WorkerBase is the class to use to implement a worker - simply inherit WorkerBase in your worker. 0055 * 0056 * A call to foo() results in a call to slotFoo() on the other end. 0057 * 0058 * Note that a kioworker doesn't have a Qt event loop. When idle, it's waiting for a command 0059 * on the socket that connects it to the application. So don't expect a kioworker to react 0060 * to D-Bus signals for instance. KIOWorkers are short-lived anyway, so any kind of watching 0061 * or listening for notifications should be done elsewhere, for instance in a kded module 0062 * (see kio_desktop's desktopnotifier.cpp for an example). 0063 * 0064 * If a kioworker needs a Qt event loop within the implementation of one method, e.g. to 0065 * wait for an asynchronous operation to finish, that is possible, using QEventLoop. 0066 * 0067 * @since 5.96 0068 */ 0069 class KIOCORE_EXPORT WorkerBase 0070 { 0071 public: 0072 WorkerBase(const QByteArray &protocol, const QByteArray &poolSocket, const QByteArray &appSocket); 0073 virtual ~WorkerBase(); 0074 0075 /** 0076 * @internal 0077 * Terminate the worker by calling the destructor and then ::exit() 0078 */ 0079 void exit(); 0080 0081 /** 0082 * @internal 0083 */ 0084 void dispatchLoop(); 0085 0086 /////////// 0087 // Message Signals to send to the job 0088 /////////// 0089 0090 /** 0091 * Sends data in the worker to the job (i.e.\ in get). 0092 * 0093 * To signal end of data, simply send an empty 0094 * QByteArray(). 0095 * 0096 * @param data the data read by the worker 0097 */ 0098 void data(const QByteArray &data); 0099 0100 /** 0101 * Asks for data from the job. 0102 * @see readData 0103 */ 0104 void dataReq(); 0105 0106 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 103) 0107 /** 0108 * Call to signal that data from the sub-URL is needed 0109 * @deprecated Since 5.96, feature no longer exists. 0110 */ 0111 KIOCORE_DEPRECATED_VERSION_BELATED(5, 103, 5, 96, "Feature no longer exists.") 0112 void needSubUrlData(); 0113 #endif 0114 0115 /** 0116 * Used to report the status of the worker. 0117 * @param host the worker is currently connected to. (Should be 0118 * empty if not connected) 0119 * @param connected Whether an actual network connection exists. 0120 **/ 0121 void workerStatus(const QString &host, bool connected); 0122 0123 /** 0124 * Call this from stat() to express details about an object, the 0125 * UDSEntry customarily contains the atoms describing file name, size, 0126 * MIME type, etc. 0127 * @param _entry The UDSEntry containing all of the object attributes. 0128 */ 0129 void statEntry(const UDSEntry &_entry); 0130 0131 /** 0132 * Call this in listDir, each time you have a bunch of entries 0133 * to report. 0134 * @param _entry The UDSEntry containing all of the object attributes. 0135 */ 0136 void listEntries(const UDSEntryList &_entry); 0137 0138 /** 0139 * Call this at the beginning of put(), to give the size of the existing 0140 * partial file, if there is one. The @p offset argument notifies the 0141 * other job (the one that gets the data) about the offset to use. 0142 * In this case, the boolean returns whether we can indeed resume or not 0143 * (we can't if the protocol doing the get() doesn't support setting an offset) 0144 */ 0145 bool canResume(KIO::filesize_t offset); 0146 0147 /** 0148 * Call this at the beginning of get(), if the "range-start" metadata was set 0149 * and returning byte ranges is implemented by this protocol. 0150 */ 0151 void canResume(); 0152 0153 /////////// 0154 // Info Signals to send to the job 0155 /////////// 0156 0157 /** 0158 * Call this in get and copy, to give the total size 0159 * of the file. 0160 */ 0161 void totalSize(KIO::filesize_t _bytes); 0162 /** 0163 * Call this during get and copy, once in a while, 0164 * to give some info about the current state. 0165 * Don't emit it in listDir, listEntries speaks for itself. 0166 */ 0167 void processedSize(KIO::filesize_t _bytes); 0168 0169 void position(KIO::filesize_t _pos); 0170 0171 void written(KIO::filesize_t _bytes); 0172 0173 void truncated(KIO::filesize_t _length); 0174 0175 /** 0176 * Call this in get and copy, to give the current transfer 0177 * speed, but only if it can't be calculated out of the size you 0178 * passed to processedSize (in most cases you don't want to call it) 0179 */ 0180 void speed(unsigned long _bytes_per_second); 0181 0182 /** 0183 * Call this to signal a redirection. 0184 * The job will take care of going to that url. 0185 */ 0186 void redirection(const QUrl &_url); 0187 0188 /** 0189 * Tell that we will only get an error page here. 0190 * This means: the data you'll get isn't the data you requested, 0191 * but an error page (usually HTML) that describes an error. 0192 */ 0193 void errorPage(); 0194 0195 /** 0196 * Call this in mimetype() and in get(), when you know the MIME type. 0197 * See mimetype() about other ways to implement it. 0198 */ 0199 void mimeType(const QString &_type); 0200 0201 /** 0202 * Call to signal a warning, to be displayed in a dialog box. 0203 */ 0204 void warning(const QString &msg); 0205 0206 /** 0207 * Call to signal a message, to be displayed if the application wants to, 0208 * for instance in a status bar. Usual examples are "connecting to host xyz", etc. 0209 */ 0210 void infoMessage(const QString &msg); 0211 0212 /** 0213 * Type of message box. Should be kept in sync with KMessageBox::DialogType. 0214 */ 0215 enum MessageBoxType { 0216 QuestionTwoActions = 1, ///< @since 5.100 0217 WarningTwoActions = 2, ///< @since 5.100 0218 WarningContinueCancel = 3, 0219 WarningTwoActionsCancel = 4, ///< @since 5.100 0220 Information = 5, 0221 SSLMessageBox = 6, 0222 // In KMessageBox::DialogType; Sorry = 7, Error = 8, QuestionTwoActionsCancel = 9 0223 WarningContinueCancelDetailed = 10, 0224 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 100) 0225 QuestionYesNo ///< @deprecated Since 5.100, use QuestionTwoActions. 0226 KIOCORE_ENUMERATOR_DEPRECATED_VERSION(5, 100, "Use QuestionTwoActions.") = QuestionTwoActions, 0227 WarningYesNo ///< @deprecated Since 5.100, use WarningTwoActions. 0228 KIOCORE_ENUMERATOR_DEPRECATED_VERSION(5, 100, "Use WarningTwoActions.") = WarningTwoActions, 0229 WarningYesNoCancel ///< @deprecated Since 5.100, use WarningTwoActionsCancel. 0230 KIOCORE_ENUMERATOR_DEPRECATED_VERSION(5, 100, "Use WarningTwoActionsCancel.") = WarningTwoActionsCancel, 0231 #endif 0232 }; 0233 0234 /** 0235 * Button codes. Should be kept in sync with KMessageBox::ButtonCode 0236 */ 0237 enum ButtonCode { 0238 Ok = 1, 0239 Cancel = 2, 0240 PrimaryAction = 3, ///< @since 5.100 0241 SecondaryAction = 4, ///< @since 5.100 0242 Continue = 5, 0243 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 100) 0244 Yes ///< @deprecated Since 5.100, use PrimaryAction. 0245 KIOCORE_ENUMERATOR_DEPRECATED_VERSION(5, 100, "Use PrimaryAction.") = PrimaryAction, 0246 No ///< @deprecated Since 5.100, use SecondaryAction. 0247 KIOCORE_ENUMERATOR_DEPRECATED_VERSION(5, 100, "Use SecondaryAction.") = SecondaryAction, 0248 #endif 0249 }; 0250 0251 /** 0252 * Call this to show a message box from the worker 0253 * @param type type of message box 0254 * @param text Message string. May contain newlines. 0255 * @param title Message box title. 0256 * @param primaryActionText the text for the first button. 0257 * Ignored for @p type Information & SSLMessageBox. 0258 * The (deprecated since 5.100) default is i18n("&Yes"). 0259 * @param secondaryActionText the text for the second button. 0260 * Ignored for @p type WarningContinueCancel, WarningContinueCancelDetailed, 0261 * Information & SSLMessageBox. 0262 * The (deprecated since 5.100) default is i18n("&No"). 0263 * @return a button code, as defined in ButtonCode, or 0 on communication error. 0264 */ 0265 int messageBox(MessageBoxType type, 0266 const QString &text, 0267 const QString &title = QString(), 0268 const QString &primaryActionText = QString(), 0269 const QString &secondaryActionText = QString()); 0270 0271 /** 0272 * Call this to show a message box from the worker 0273 * @param text Message string. May contain newlines. 0274 * @param type type of message box 0275 * @param title Message box title. 0276 * @param primaryActionText the text for the first button. 0277 * Ignored for @p type Information & SSLMessageBox. 0278 * The (deprecated since 5.100) default is i18n("&Yes"). 0279 * @param secondaryActionText the text for the second button. 0280 * Ignored for @p type WarningContinueCancel, WarningContinueCancelDetailed, 0281 * Information & SSLMessageBox. 0282 * The (deprecated since 5.100) default is i18n("&No"). 0283 * @param dontAskAgainName the name used to store result from 'Do not ask again' checkbox. 0284 * @return a button code, as defined in ButtonCode, or 0 on communication error. 0285 */ 0286 int messageBox(const QString &text, 0287 MessageBoxType type, 0288 const QString &title = QString(), 0289 const QString &primaryActionText = QString(), 0290 const QString &secondaryActionText = QString(), 0291 const QString &dontAskAgainName = QString()); 0292 0293 /** 0294 * Sets meta-data to be send to the application before the first 0295 * data() or finished() signal. 0296 */ 0297 void setMetaData(const QString &key, const QString &value); 0298 0299 /** 0300 * Queries for the existence of a certain config/meta-data entry 0301 * send by the application to the worker. 0302 */ 0303 bool hasMetaData(const QString &key) const; 0304 0305 /** 0306 * Queries for config/meta-data send by the application to the worker. 0307 */ 0308 QString metaData(const QString &key) const; 0309 0310 /** 0311 * @internal for ForwardingSlaveBase 0312 * Contains all metadata (but no config) sent by the application to the worker. 0313 */ 0314 MetaData allMetaData() const; 0315 0316 /** 0317 * Returns a map to query config/meta-data information from. 0318 * 0319 * The application provides the worker with all configuration information 0320 * relevant for the current protocol and host. 0321 * 0322 * Use configValue() as shortcut. 0323 */ 0324 QMap<QString, QVariant> mapConfig() const; 0325 0326 /** 0327 * Returns a bool from the config/meta-data information. 0328 */ 0329 bool configValue(const QString &key, bool defaultValue) const; 0330 0331 /** 0332 * Returns an int from the config/meta-data information. 0333 */ 0334 int configValue(const QString &key, int defaultValue) const; 0335 0336 /** 0337 * Returns a QString from the config/meta-data information. 0338 */ 0339 QString configValue(const QString &key, const QString &defaultValue = QString()) const; 0340 0341 /** 0342 * Returns a configuration object to query config/meta-data information 0343 * from. 0344 * 0345 * The application provides the worker with all configuration information 0346 * relevant for the current protocol and host. 0347 * 0348 * @note Since 5.64 prefer to use mapConfig() or one of the configValue(...) overloads. 0349 * @todo Find replacements for the other current usages of this method. 0350 */ 0351 KConfigGroup *config(); 0352 // KF6: perhaps rename mapConfig() to config() when removing this 0353 0354 /** 0355 * Returns an object that can translate remote filenames into proper 0356 * Unicode forms. This encoding can be set by the user. 0357 */ 0358 KRemoteEncoding *remoteEncoding(); 0359 0360 /////////// 0361 // Commands sent by the job, the worker has to 0362 // override what it wants to implement 0363 /////////// 0364 0365 /** 0366 * @brief Application connected to the worker. 0367 * 0368 * Called when an application has connected to the worker. Mostly only useful 0369 * when you want to e.g. send metadata to the application once it connects. 0370 */ 0371 virtual void appConnectionMade(); 0372 0373 /** 0374 * Set the host 0375 * 0376 * Called directly by createWorker and not via the interface. 0377 * 0378 * This method is called whenever a change in host, port or user occurs. 0379 */ 0380 virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &pass); 0381 0382 /** 0383 * Opens the connection (forced). 0384 * When this function gets called the worker is operating in 0385 * connection-oriented mode. 0386 * When a connection gets lost while the worker operates in 0387 * connection oriented mode, the worker should report 0388 * ERR_CONNECTION_BROKEN instead of reconnecting. The user is 0389 * expected to disconnect the worker in the error handler. 0390 */ 0391 Q_REQUIRED_RESULT virtual WorkerResult openConnection(); 0392 0393 /** 0394 * Closes the connection (forced). 0395 * Called when the application disconnects the worker to close 0396 * any open network connections. 0397 * 0398 * When the worker was operating in connection-oriented mode, 0399 * it should reset itself to connectionless (default) mode. 0400 */ 0401 virtual void closeConnection(); 0402 0403 /** 0404 * get, aka read. 0405 * @param url the full url for this request. Host, port and user of the URL 0406 * can be assumed to be the same as in the last setHost() call. 0407 * 0408 * The worker should first "emit" the MIME type by calling mimeType(), 0409 * and then "emit" the data using the data() method. 0410 * 0411 * The reason why we need get() to emit the MIME type is: 0412 * when pasting a URL in krunner, or konqueror's location bar, 0413 * we have to find out what is the MIME type of that URL. 0414 * Rather than doing it with a call to mimetype(), then the app or part 0415 * would have to do a second request to the same server, this is done 0416 * like this: get() is called, and when it emits the MIME type, the job 0417 * is put on hold and the right app or part is launched. When that app 0418 * or part calls get(), the worker is magically reused, and the download 0419 * can now happen. All with a single call to get() in the worker. 0420 * This mechanism is also described in KIO::get(). 0421 */ 0422 Q_REQUIRED_RESULT virtual WorkerResult get(const QUrl &url); 0423 0424 /** 0425 * open. 0426 * @param url the full url for this request. Host, port and user of the URL 0427 * can be assumed to be the same as in the last setHost() call. 0428 * @param mode see \ref QIODevice::OpenMode 0429 */ 0430 Q_REQUIRED_RESULT virtual WorkerResult open(const QUrl &url, QIODevice::OpenMode mode); 0431 0432 /** 0433 * read. 0434 * @param size the requested amount of data to read 0435 * @see KIO::FileJob::read() 0436 */ 0437 Q_REQUIRED_RESULT virtual WorkerResult read(KIO::filesize_t size); 0438 /** 0439 * write. 0440 * @param data the data to write 0441 * @see KIO::FileJob::write() 0442 */ 0443 Q_REQUIRED_RESULT virtual WorkerResult write(const QByteArray &data); 0444 /** 0445 * seek. 0446 * @param offset the requested amount of data to read 0447 * @see KIO::FileJob::read() 0448 */ 0449 Q_REQUIRED_RESULT virtual WorkerResult seek(KIO::filesize_t offset); 0450 /** 0451 * truncate 0452 * @param size size to truncate the file to 0453 * @see KIO::FileJob::truncate() 0454 */ 0455 Q_REQUIRED_RESULT virtual WorkerResult truncate(KIO::filesize_t size); 0456 /** 0457 * close. 0458 * @see KIO::FileJob::close() 0459 */ 0460 Q_REQUIRED_RESULT virtual WorkerResult close(); 0461 0462 /** 0463 * put, i.e.\ write data into a file. 0464 * 0465 * @param url where to write the file 0466 * @param permissions may be -1. In this case no special permission mode is set. 0467 * @param flags We support Overwrite here. Hopefully, we're going to 0468 * support Resume in the future, too. 0469 * If the file indeed already exists, the worker should NOT apply the 0470 * permissions change to it. 0471 * The support for resuming using .part files is done by calling canResume(). 0472 * 0473 * IMPORTANT: Use the "modified" metadata in order to set the modification time of the file. 0474 * 0475 * @see canResume() 0476 */ 0477 Q_REQUIRED_RESULT virtual WorkerResult put(const QUrl &url, int permissions, JobFlags flags); 0478 0479 /** 0480 * Finds all details for one file or directory. 0481 * The information returned is the same as what listDir returns, 0482 * but only for one file or directory. 0483 * Call statEntry() after creating the appropriate UDSEntry for this 0484 * url. 0485 * 0486 * You can use the "details" metadata to optimize this method to only 0487 * do as much work as needed by the application. 0488 * By default details is 2 (all details wanted, including modification time, size, etc.), 0489 * details==1 is used when deleting: we don't need all the information if it takes 0490 * too much time, no need to follow symlinks etc. 0491 * details==0 is used for very simple probing: we'll only get the answer 0492 * "it's a file or a directory (or a symlink), or it doesn't exist". 0493 */ 0494 Q_REQUIRED_RESULT virtual WorkerResult stat(const QUrl &url); 0495 0496 /** 0497 * Finds MIME type for one file or directory. 0498 * 0499 * This method should either emit 'mimeType' or it 0500 * should send a block of data big enough to be able 0501 * to determine the MIME type. 0502 * 0503 * If the worker doesn't reimplement it, a get will 0504 * be issued, i.e.\ the whole file will be downloaded before 0505 * determining the MIME type on it - this is obviously not a 0506 * good thing in most cases. 0507 */ 0508 Q_REQUIRED_RESULT virtual WorkerResult mimetype(const QUrl &url); 0509 0510 /** 0511 * Lists the contents of @p url. 0512 * The worker should emit ERR_CANNOT_ENTER_DIRECTORY if it doesn't exist, 0513 * if we don't have enough permissions. 0514 * You should not list files if the path in @p url is empty, but redirect 0515 * to a non-empty path instead. 0516 */ 0517 Q_REQUIRED_RESULT virtual WorkerResult listDir(const QUrl &url); 0518 0519 /** 0520 * Create a directory 0521 * @param url path to the directory to create 0522 * @param permissions the permissions to set after creating the directory 0523 * (-1 if no permissions to be set) 0524 * The worker emits ERR_CANNOT_MKDIR if failure. 0525 */ 0526 Q_REQUIRED_RESULT virtual WorkerResult mkdir(const QUrl &url, int permissions); 0527 0528 /** 0529 * Rename @p oldname into @p newname. 0530 * If the worker returns an error ERR_UNSUPPORTED_ACTION, the job will 0531 * ask for copy + del instead. 0532 * 0533 * Important: the worker must implement the logic "if the destination already 0534 * exists, error ERR_DIR_ALREADY_EXIST or ERR_FILE_ALREADY_EXIST". 0535 * For performance reasons no stat is done in the destination before hand, 0536 * the worker must do it. 0537 * 0538 * By default, rename() is only called when renaming (moving) from 0539 * yourproto://host/path to yourproto://host/otherpath. 0540 * 0541 * If you set renameFromFile=true then rename() will also be called when 0542 * moving a file from file:///path to yourproto://host/otherpath. 0543 * Otherwise such a move would have to be done the slow way (copy+delete). 0544 * See KProtocolManager::canRenameFromFile() for more details. 0545 * 0546 * If you set renameToFile=true then rename() will also be called when 0547 * moving a file from yourproto: to file:. 0548 * See KProtocolManager::canRenameToFile() for more details. 0549 * 0550 * @param src where to move the file from 0551 * @param dest where to move the file to 0552 * @param flags We support Overwrite here 0553 */ 0554 Q_REQUIRED_RESULT virtual WorkerResult rename(const QUrl &src, const QUrl &dest, JobFlags flags); 0555 0556 /** 0557 * Creates a symbolic link named @p dest, pointing to @p target, which 0558 * may be a relative or an absolute path. 0559 * @param target The string that will become the "target" of the link (can be relative) 0560 * @param dest The symlink to create. 0561 * @param flags We support Overwrite here 0562 */ 0563 Q_REQUIRED_RESULT virtual WorkerResult symlink(const QString &target, const QUrl &dest, JobFlags flags); 0564 0565 /** 0566 * Change permissions on @p url. 0567 * The worker emits ERR_DOES_NOT_EXIST or ERR_CANNOT_CHMOD 0568 */ 0569 Q_REQUIRED_RESULT virtual WorkerResult chmod(const QUrl &url, int permissions); 0570 0571 /** 0572 * Change ownership of @p url. 0573 * The worker emits ERR_DOES_NOT_EXIST or ERR_CANNOT_CHOWN 0574 */ 0575 Q_REQUIRED_RESULT virtual WorkerResult chown(const QUrl &url, const QString &owner, const QString &group); 0576 0577 /** 0578 * Sets the modification time for @url. 0579 * For instance this is what CopyJob uses to set mtime on dirs at the end of a copy. 0580 * It could also be used to set the mtime on any file, in theory. 0581 * The usual implementation on unix is to call utime(path, &myutimbuf). 0582 * The worker emits ERR_DOES_NOT_EXIST or ERR_CANNOT_SETTIME 0583 */ 0584 Q_REQUIRED_RESULT virtual WorkerResult setModificationTime(const QUrl &url, const QDateTime &mtime); 0585 0586 /** 0587 * Copy @p src into @p dest. 0588 * 0589 * By default, copy() is only called when copying a file from 0590 * yourproto://host/path to yourproto://host/otherpath. 0591 * 0592 * If you set copyFromFile=true then copy() will also be called when 0593 * moving a file from file:///path to yourproto://host/otherpath. 0594 * Otherwise such a copy would have to be done the slow way (get+put). 0595 * See also KProtocolManager::canCopyFromFile(). 0596 * 0597 * If you set copyToFile=true then copy() will also be called when 0598 * moving a file from yourproto: to file:. 0599 * See also KProtocolManager::canCopyToFile(). 0600 * 0601 * If the worker returns an error ERR_UNSUPPORTED_ACTION, the job will 0602 * ask for get + put instead. 0603 * 0604 * If the worker returns an error ERR_FILE_ALREADY_EXIST, the job will 0605 * ask for a different destination filename. 0606 * 0607 * @param src where to copy the file from (decoded) 0608 * @param dest where to copy the file to (decoded) 0609 * @param permissions may be -1. In this case no special permission mode is set, 0610 * and the owner and group permissions are not preserved. 0611 * @param flags We support Overwrite here 0612 * 0613 * Don't forget to set the modification time of @p dest to be the modification time of @p src. 0614 */ 0615 Q_REQUIRED_RESULT virtual WorkerResult copy(const QUrl &src, const QUrl &dest, int permissions, JobFlags flags); 0616 0617 /** 0618 * Delete a file or directory. 0619 * @param url file/directory to delete 0620 * @param isfile if true, a file should be deleted. 0621 * if false, a directory should be deleted. 0622 * 0623 * By default, del() on a directory should FAIL if the directory is not empty. 0624 * However, if metadata("recurse") == "true", then the worker can do a recursive deletion. 0625 * This behavior is only invoked if the worker specifies deleteRecursive=true in its protocol file. 0626 */ 0627 Q_REQUIRED_RESULT virtual WorkerResult del(const QUrl &url, bool isfile); 0628 0629 /** 0630 * Used for any command that is specific to this worker (protocol). 0631 * Examples are : HTTP POST, mount and unmount (kio_file) 0632 * 0633 * @param data packed data; the meaning is completely dependent on the 0634 * worker, but usually starts with an int for the command number. 0635 * Document your worker's commands, at least in its header file. 0636 */ 0637 Q_REQUIRED_RESULT virtual WorkerResult special(const QByteArray &data); 0638 0639 /** 0640 * Used for multiple get. Currently only used for HTTP pipelining 0641 * support. 0642 * 0643 * @param data packed data; Contains number of URLs to fetch, and for 0644 * each URL the URL itself and its associated MetaData. 0645 */ 0646 Q_REQUIRED_RESULT virtual WorkerResult multiGet(const QByteArray &data); 0647 0648 /** 0649 * Get a filesystem's total and available space. 0650 * 0651 * @param url Url to the filesystem 0652 */ 0653 Q_REQUIRED_RESULT virtual WorkerResult fileSystemFreeSpace(const QUrl &url); 0654 0655 /** 0656 * Called to get the status of the worker. Worker should respond 0657 * by calling workerStatus(...) 0658 */ 0659 virtual void worker_status(); 0660 0661 /** 0662 * Called by the scheduler to tell the worker that the configuration 0663 * changed (i.e.\ proxy settings) . 0664 */ 0665 virtual void reparseConfiguration(); 0666 0667 /** 0668 * @return timeout value for connecting to remote host. 0669 */ 0670 int connectTimeout(); 0671 0672 /** 0673 * @return timeout value for connecting to proxy in secs. 0674 */ 0675 int proxyConnectTimeout(); 0676 0677 /** 0678 * @return timeout value for read from first data from 0679 * remote host in seconds. 0680 */ 0681 int responseTimeout(); 0682 0683 /** 0684 * @return timeout value for read from subsequent data from 0685 * remote host in secs. 0686 */ 0687 int readTimeout(); 0688 0689 /** 0690 * This function sets a timeout of @p timeout seconds and calls 0691 * special(data) when the timeout occurs as if it was called by the 0692 * application. 0693 * 0694 * A timeout can only occur when the worker is waiting for a command 0695 * from the application. 0696 * 0697 * Specifying a negative timeout cancels a pending timeout. 0698 * 0699 * Only one timeout at a time is supported, setting a timeout 0700 * cancels any pending timeout. 0701 */ 0702 void setTimeoutSpecialCommand(int timeout, const QByteArray &data = QByteArray()); 0703 0704 /** 0705 * Read data sent by the job, after a dataReq 0706 * 0707 * @param buffer buffer where data is stored 0708 * @return 0 on end of data, 0709 * > 0 bytes read 0710 * < 0 error 0711 **/ 0712 int readData(QByteArray &buffer); 0713 0714 /** 0715 * It collects entries and emits them via listEntries 0716 * when enough of them are there or a certain time 0717 * frame exceeded (to make sure the app gets some 0718 * items in time but not too many items one by one 0719 * as this will cause a drastic performance penalty). 0720 * @param entry The UDSEntry containing all of the object attributes. 0721 */ 0722 void listEntry(const UDSEntry &entry); 0723 0724 /** 0725 * internal function to connect a worker to/ disconnect from 0726 * either the worker pool or the application 0727 */ 0728 void connectWorker(const QString &path); 0729 void disconnectWorker(); 0730 0731 /** 0732 * Prompt the user for Authorization info (login & password). 0733 * 0734 * Use this function to request authorization information from 0735 * the end user. You can also pass an error message which explains 0736 * why a previous authorization attempt failed. Here is a very 0737 * simple example: 0738 * 0739 * \code 0740 * KIO::AuthInfo authInfo; 0741 * int errorCode = openPasswordDialogV2(authInfo); 0742 * if (!errorCode) { 0743 * qDebug() << QLatin1String("User: ") << authInfo.username; 0744 * qDebug() << QLatin1String("Password: not displayed here!"); 0745 * } else { 0746 * error(errorCode, QString()); 0747 * } 0748 * \endcode 0749 * 0750 * You can also preset some values like the username, caption or 0751 * comment as follows: 0752 * 0753 * \code 0754 * KIO::AuthInfo authInfo; 0755 * authInfo.caption = i18n("Acme Password Dialog"); 0756 * authInfo.username = "Wile E. Coyote"; 0757 * QString errorMsg = i18n("You entered an incorrect password."); 0758 * int errorCode = openPasswordDialogV2(authInfo, errorMsg); 0759 * [...] 0760 * \endcode 0761 * 0762 * \note You should consider using checkCachedAuthentication() to 0763 * see if the password is available in kpasswdserver before calling 0764 * this function. 0765 * 0766 * \note A call to this function can fail and return @p false, 0767 * if the password server could not be started for whatever reason. 0768 * 0769 * \note This function does not store the password information 0770 * automatically (and has not since kdelibs 4.7). If you want to 0771 * store the password information in a persistent storage like 0772 * KWallet, then you MUST call @ref cacheAuthentication. 0773 * 0774 * @see checkCachedAuthentication 0775 * @param info See AuthInfo. 0776 * @param errorMsg Error message to show 0777 * @return a KIO error code: NoError (0), KIO::USER_CANCELED, or other error codes. 0778 */ 0779 int openPasswordDialog(KIO::AuthInfo &info, const QString &errorMsg = QString()); 0780 0781 /** 0782 * Checks for cached authentication based on parameters 0783 * given by @p info. 0784 * 0785 * Use this function to check if any cached password exists 0786 * for the URL given by @p info. If @p AuthInfo::realmValue 0787 * and/or @p AuthInfo::verifyPath flag is specified, then 0788 * they will also be factored in determining the presence 0789 * of a cached password. Note that @p Auth::url is a required 0790 * parameter when attempting to check for cached authorization 0791 * info. Here is a simple example: 0792 * 0793 * \code 0794 * AuthInfo info; 0795 * info.url = QUrl("https://www.foobar.org/foo/bar"); 0796 * info.username = "somename"; 0797 * info.verifyPath = true; 0798 * if ( !checkCachedAuthentication( info ) ) 0799 * { 0800 * int errorCode = openPasswordDialogV2(info); 0801 * .... 0802 * } 0803 * \endcode 0804 * 0805 * @param info See AuthInfo. 0806 * @return @p true if cached Authorization is found, false otherwise. 0807 */ 0808 bool checkCachedAuthentication(AuthInfo &info); 0809 0810 /** 0811 * Caches @p info in a persistent storage like KWallet. 0812 * 0813 * Note that calling openPasswordDialogV2 does not store passwords 0814 * automatically for you (and has not since kdelibs 4.7). 0815 * 0816 * Here is a simple example of how to use cacheAuthentication: 0817 * 0818 * \code 0819 * AuthInfo info; 0820 * info.url = QUrl("https://www.foobar.org/foo/bar"); 0821 * info.username = "somename"; 0822 * info.verifyPath = true; 0823 * if ( !checkCachedAuthentication( info ) ) { 0824 * int errorCode = openPasswordDialogV2(info); 0825 * if (!errorCode) { 0826 * if (info.keepPassword) { // user asked password be save/remembered 0827 * cacheAuthentication(info); 0828 * } 0829 * } 0830 * } 0831 * \endcode 0832 * 0833 * @param info See AuthInfo. 0834 * @return @p true if @p info was successfully cached. 0835 */ 0836 bool cacheAuthentication(const AuthInfo &info); 0837 0838 /** 0839 * Wait for an answer to our request, until we get @p expected1 or @p expected2 0840 * @return the result from readData, as well as the cmd in *pCmd if set, and the data in @p data 0841 */ 0842 int waitForAnswer(int expected1, int expected2, QByteArray &data, int *pCmd = nullptr); 0843 0844 /** 0845 * Internal function to transmit meta data to the application. 0846 * m_outgoingMetaData will be cleared; this means that if the worker is for 0847 * example put on hold and picked up by a different KIO::Job later the new 0848 * job will not see the metadata sent before. 0849 * See kio/DESIGN.krun for an overview of the state 0850 * progression of a job/worker. 0851 * @warning calling this method may seriously interfere with the operation 0852 * of KIO which relies on the presence of some metadata at some points in time. 0853 * You should not use it if you are not familiar with KIO and not before 0854 * the worker is connected to the last job before returning to idle state. 0855 */ 0856 void sendMetaData(); 0857 0858 /** 0859 * Internal function to transmit meta data to the application. 0860 * Like sendMetaData() but m_outgoingMetaData will not be cleared. 0861 * This method is mainly useful in code that runs before the worker is connected 0862 * to its final job. 0863 */ 0864 void sendAndKeepMetaData(); 0865 0866 /** If your ioworker was killed by a signal, wasKilled() returns true. 0867 Check it regularly in lengthy functions (e.g. in get();) and return 0868 as fast as possible from this function if wasKilled() returns true. 0869 This will ensure that your worker destructor will be called correctly. 0870 */ 0871 bool wasKilled() const; 0872 0873 /** Internally used 0874 * @internal 0875 */ 0876 void lookupHost(const QString &host); 0877 0878 /** Internally used 0879 * @internal 0880 */ 0881 int waitForHostInfo(QHostInfo &info); 0882 0883 /** 0884 * Checks with job if privilege operation is allowed. 0885 * @return privilege operation status. 0886 * @see PrivilegeOperationStatus 0887 */ 0888 PrivilegeOperationStatus requestPrivilegeOperation(const QString &operationDetails); 0889 0890 /** 0891 * Adds @p action to the list of PolicyKit actions which the 0892 * worker is authorized to perform. 0893 * 0894 * @param action the PolicyKit action 0895 */ 0896 void addTemporaryAuthorization(const QString &action); 0897 0898 /** 0899 * @brief Set the Incoming Meta Data 0900 * This is only really useful if your worker wants to overwrite the 0901 * metadata for consumption in other worker functions; this overwrites 0902 * existing metadata set by the client! 0903 * 0904 * @param metaData metadata to set 0905 * @since 5.99 0906 */ 0907 void setIncomingMetaData(const KIO::MetaData &metaData); 0908 0909 private: 0910 std::unique_ptr<WorkerBasePrivate> d; 0911 Q_DISABLE_COPY_MOVE(WorkerBase) 0912 friend class WorkerSlaveBaseBridge; 0913 friend class WorkerThread; 0914 }; 0915 0916 } // namespace KIO 0917 0918 #endif