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