File indexing completed on 2024-04-21 07:42:08

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_STATJOB_H
0010 #define KIO_STATJOB_H
0011 
0012 #include "global.h"
0013 #include "simplejob.h"
0014 #include <kio/udsentry.h>
0015 
0016 namespace KIO
0017 {
0018 class StatJobPrivate;
0019 /**
0020  * @class KIO::StatJob statjob.h <KIO/StatJob>
0021  *
0022  * A KIO job that retrieves information about a file or directory.
0023  * @see KIO::stat()
0024  */
0025 class KIOCORE_EXPORT StatJob : public SimpleJob
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     enum StatSide {
0031         SourceSide,
0032         DestinationSide,
0033     };
0034 
0035     ~StatJob() override;
0036 
0037     /**
0038      * A stat() can have two meanings. Either we want to read from this URL,
0039      * or to check if we can write to it. First case is "source", second is "dest".
0040      * It is necessary to know what the StatJob is for, to tune the KIO worker's behavior
0041      * (e.g. with FTP).
0042      * By default it is SourceSide.
0043      * @param side SourceSide or DestinationSide
0044      */
0045     void setSide(StatSide side);
0046 
0047     /**
0048      * Selects the level of @p details we want.
0049      * @since 5.69
0050      */
0051     void setDetails(KIO::StatDetails details);
0052 
0053     /**
0054      * @brief Result of the stat operation.
0055      * Call this in the slot connected to result,
0056      * and only after making sure no error happened.
0057      * @return the result of the stat
0058      */
0059     const UDSEntry &statResult() const;
0060 
0061     /**
0062      * @brief most local URL
0063      *
0064      * Since this method depends on UDSEntry::UDS_LOCAL_PATH having been previously set
0065      * by a KIO worker, ideally you should first check that the protocol Class of the URL
0066      * being stat'ed is ":local" before creating the StatJob at all. Typically only ":local"
0067      * KIO workers set UDS_LOCAL_PATH. See KProtocolInfo::protocolClass().
0068      *
0069      * Call this in a slot connected to the result signal, and only after making sure no error
0070      * happened.
0071      *
0072      * @return the most local URL for the URL we were stat'ing
0073      *
0074      * Sample usage:
0075      *
0076      * @code
0077      * auto *job = KIO::mostLocalUrl("desktop:/foo");
0078      * job->uiDelegate()->setWindow(this);
0079      * connect(job, &KJob::result, this, &MyClass::slotMostLocalUrlResult);
0080      * [...]
0081      * // and in slotMostLocalUrlResult(KJob *job)
0082      * if (job->error()) {
0083      *    [...] // doesn't exist
0084      * } else {
0085      *    const QUrl localUrl = job->mostLocalUrl();
0086      *    // localUrl = file:///$HOME/Desktop/foo
0087      *    [...]
0088      * }
0089      * @endcode
0090      *
0091      * \since 4.4
0092      */
0093     QUrl mostLocalUrl() const;
0094 
0095 Q_SIGNALS:
0096     /**
0097      * Signals a redirection.
0098      * Use to update the URL shown to the user.
0099      * The redirection itself is handled internally.
0100      * @param job the job that is redirected
0101      * @param url the new url
0102      */
0103     void redirection(KIO::Job *job, const QUrl &url);
0104 
0105     /**
0106      * Signals a permanent redirection.
0107      * The redirection itself is handled internally.
0108      * @param job the job that is redirected
0109      * @param fromUrl the original URL
0110      * @param toUrl the new URL
0111      */
0112     void permanentRedirection(KIO::Job *job, const QUrl &fromUrl, const QUrl &toUrl);
0113 
0114 protected Q_SLOTS:
0115     void slotFinished() override;
0116 
0117 protected:
0118     KIOCORE_NO_EXPORT explicit StatJob(StatJobPrivate &dd);
0119 
0120 private:
0121     Q_DECLARE_PRIVATE(StatJob)
0122     friend KIOCORE_EXPORT StatJob *mostLocalUrl(const QUrl &url, JobFlags flags);
0123 };
0124 
0125 /**
0126  * Find all details for one file or directory.
0127  *
0128  * @param url the URL of the file
0129  * @param flags Can be HideProgressInfo here
0130  * @return the job handling the operation.
0131  */
0132 KIOCORE_EXPORT StatJob *stat(const QUrl &url, JobFlags flags = DefaultFlags);
0133 /**
0134  * Find all details for one file or directory.
0135  * This version of the call includes two additional booleans, @p sideIsSource and @p details.
0136  *
0137  * @param url the URL of the file
0138  * @param side is SourceSide when stating a source file (we will do a get on it if
0139  * the stat works) and DestinationSide when stating a destination file (target of a copy).
0140  * The reason for this parameter is that in some cases the KIO worker might not
0141  * be able to determine a file's existence (e.g. HTTP doesn't allow it, FTP
0142  * has issues with case-sensitivity on some systems).
0143  * When the worker can't reliably determine the existence of a file, it will:
0144  * @li be optimistic if SourceSide, i.e. it will assume the file exists,
0145  * and if it doesn't this will appear when actually trying to download it
0146  * @li be pessimistic if DestinationSide, i.e. it will assume the file
0147  * doesn't exist, to prevent showing "about to overwrite" errors to the user.
0148  * If you simply want to check for existence without downloading/uploading afterwards,
0149  * then you should use DestinationSide.
0150  *
0151  * @param details selects the level of details we want.
0152  * You should minimize the detail level for better performance.
0153  * @param flags Can be HideProgressInfo here
0154  * @return the job handling the operation.
0155  * @since 6.0
0156  */
0157 KIOCORE_EXPORT StatJob *stat(const QUrl &url, KIO::StatJob::StatSide side, KIO::StatDetails details = KIO::StatDefaultDetails, JobFlags flags = DefaultFlags);
0158 
0159 /**
0160  * Tries to map a local URL for the given URL, using a KIO job. This only makes sense for
0161  * protocols that have Class ":local" (such protocols most likely have KIO workers that set
0162  * UDSEntry::UDS_LOCAL_PATH); ideally you should check the URL protocol Class before creating
0163  * a StatJob. See KProtocolInfo::protocolClass().
0164  *
0165  * Starts a (stat) job for determining the "most local URL" for a given URL.
0166  * Retrieve the result with StatJob::mostLocalUrl in the result slot.
0167  *
0168  * @param url The URL we are stat'ing.
0169  *
0170  *
0171  * Sample usage:
0172  *
0173  * Here the KIO worker name is "foo", which for example could be:
0174  *  - "desktop", "fonts", "kdeconnect", these have class ":local"
0175  *  - "ftp", "sftp", "smb", these have class ":internet"
0176  *
0177  * @code
0178  *
0179  * QUrl url(QStringLiteral("foo://bar/");
0180  * if (url.isLocalFile()) { // If it's a local URL, there is no need to stat
0181  *    [...]
0182  * } else if (KProtocolInfo::protocolClass(url.scheme()) == QLatin1String(":local")) {
0183  *     // Not a local URL, but if the protocol class is ":local", we may be able to stat
0184  *     // and get a "most local URL"
0185  *     auto *job = KIO::mostLocalUrl(url);
0186  *     job->uiDelegate()->setWindow(this);
0187  *     connect(job, &KJob::result, this, &MyClass::slotMostLocalUrlResult);
0188  *     [...]
0189  *     // And in slotMostLocalUrlResult(KJob *job)
0190  *     if (job->error()) {
0191  *         [...] // Doesn't exist, ideally show an error message
0192  *     } else {
0193  *         const QUrl localUrl = job->mostLocalUrl();
0194  *         // localUrl = file:///local/path/to/bar/
0195  *         [...]
0196  *     }
0197  * }
0198  *
0199  * @endcode
0200  *
0201  */
0202 KIOCORE_EXPORT StatJob *mostLocalUrl(const QUrl &url, JobFlags flags = DefaultFlags);
0203 
0204 }
0205 
0206 #endif