File indexing completed on 2024-04-28 15:26:17

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2004 Kevin Ottens <ervin@ipsquad.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef _FORWARDING_SLAVE_BASE_H_
0009 #define _FORWARDING_SLAVE_BASE_H_
0010 
0011 #include "job_base.h" // JobFlags
0012 #include "kiocore_export.h"
0013 #include <kio/slavebase.h>
0014 
0015 #include <QEventLoop>
0016 #include <QObject>
0017 
0018 #include <memory>
0019 
0020 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 101)
0021 
0022 namespace KIO
0023 {
0024 class ForwardingSlaveBasePrivate;
0025 
0026 /**
0027  * @class KIO::ForwardingSlaveBase forwardingslavebase.h <KIO/ForwardingSlaveBase>
0028  *
0029  * This class should be used as a base for ioslaves acting as a
0030  * forwarder to other ioslaves. It has been designed to support only
0031  * local filesystem like ioslaves.
0032  *
0033  * If the resulting ioslave should be a simple proxy, you only need
0034  * to implement the ForwardingSlaveBase::rewriteUrl() method.
0035  *
0036  * For more advanced behavior, the classic ioslave methods should
0037  * be reimplemented, because their default behavior in this class
0038  * is to forward using the ForwardingSlaveBase::rewriteUrl() method.
0039  *
0040  * A possible code snippet for an advanced stat() behavior would look
0041  * like this in the child class:
0042  *
0043  * \code
0044  *     void ChildProtocol::stat(const QUrl &url)
0045  *     {
0046  *         bool is_special = false;
0047  *
0048  *         // Process the URL to see if it should have
0049  *         // a special treatment
0050  *
0051  *         if ( is_special )
0052  *         {
0053  *             // Handle the URL ourselves
0054  *             KIO::UDSEntry entry;
0055  *             // Fill entry with UDSAtom instances
0056  *             statEntry(entry);
0057  *             finished();
0058  *         }
0059  *         else
0060  *         {
0061  *             // Setup the ioslave internal state if
0062  *             // required by ChildProtocol::rewriteUrl()
0063  *             ForwardingSlaveBase::stat(url);
0064  *         }
0065  *     }
0066  * \endcode
0067  *
0068  * Of course in this case, you surely need to reimplement listDir()
0069  * and get() accordingly.
0070  *
0071  * If you want view on directories to be correctly refreshed when
0072  * something changes on a forwarded URL, you'll need a companion kded
0073  * module to emit the KDirNotify Files*() D-Bus signals.
0074  *
0075  * This class was initially used for media:/ ioslave. This ioslave code
0076  * and the MediaDirNotify class of its companion kded module can be a
0077  * good source of inspiration.
0078  *
0079  * @see ForwardingSlaveBase::rewriteUrl()
0080  * @author Kevin Ottens <ervin@ipsquad.net>
0081  *
0082  * @deprecated Since 5.101, use ForwardingWorkerBase.
0083  */
0084 class KIOCORE_EXPORT ForwardingSlaveBase : public QObject, public SlaveBase
0085 {
0086     Q_OBJECT
0087 public:
0088     KIOCORE_DEPRECATED_VERSION(5, 101, "Use ForwardingWorkerBase")
0089     ForwardingSlaveBase(const QByteArray &protocol, const QByteArray &poolSocket, const QByteArray &appSocket);
0090     ~ForwardingSlaveBase() override;
0091 
0092     void get(const QUrl &url) override;
0093 
0094     void put(const QUrl &url, int permissions, JobFlags flags) override;
0095 
0096     void stat(const QUrl &url) override;
0097 
0098     void mimetype(const QUrl &url) override;
0099 
0100     void listDir(const QUrl &url) override;
0101 
0102     void mkdir(const QUrl &url, int permissions) override;
0103 
0104     void rename(const QUrl &src, const QUrl &dest, JobFlags flags) override;
0105 
0106     void symlink(const QString &target, const QUrl &dest, JobFlags flags) override;
0107 
0108     void chmod(const QUrl &url, int permissions) override;
0109 
0110     void setModificationTime(const QUrl &url, const QDateTime &mtime) override;
0111 
0112     void copy(const QUrl &src, const QUrl &dest, int permissions, JobFlags flags) override;
0113 
0114     void del(const QUrl &url, bool isfile) override;
0115 
0116 protected:
0117     /**
0118      * Rewrite an url to its forwarded counterpart. It should return
0119      * true if everything was ok, and false otherwise.
0120      *
0121      * If a problem is detected it's up to this method to trigger error()
0122      * before returning. Returning false silently cancels the current
0123      * slave operation.
0124      *
0125      * @param url The URL as given during the slave call
0126      * @param newURL The new URL to forward the slave call to
0127      * @return true if the given url could be correctly rewritten
0128      */
0129     virtual bool rewriteUrl(const QUrl &url, QUrl &newURL) = 0;
0130 
0131     /**
0132      * Allow to modify a UDSEntry before it's sent to the ioslave endpoint.
0133      * This is the default implementation working in most cases, but sometimes
0134      * you could make use of more forwarding black magic (for example
0135      * dynamically transform any desktop file into a fake directory...)
0136      *
0137      * @param entry the UDSEntry to post-process
0138      * @param listing indicate if this entry it created during a listDir
0139      *                operation
0140      */
0141     virtual void prepareUDSEntry(KIO::UDSEntry &entry, bool listing = false) const;
0142 
0143     /**
0144      * Return the URL being processed by the ioslave
0145      * Only access it inside prepareUDSEntry()
0146      */
0147     QUrl processedUrl() const;
0148 
0149     /**
0150      * Return the URL asked to the ioslave
0151      * Only access it inside prepareUDSEntry()
0152      */
0153     QUrl requestedUrl() const;
0154 
0155 private:
0156     // KIO::Job
0157     Q_PRIVATE_SLOT(d, void _k_slotResult(KJob *job))
0158     Q_PRIVATE_SLOT(d, void _k_slotWarning(KJob *job, const QString &msg))
0159     Q_PRIVATE_SLOT(d, void _k_slotInfoMessage(KJob *job, const QString &msg))
0160     Q_PRIVATE_SLOT(d, void _k_slotTotalSize(KJob *job, qulonglong size))
0161     Q_PRIVATE_SLOT(d, void _k_slotProcessedSize(KJob *job, qulonglong size))
0162     Q_PRIVATE_SLOT(d, void _k_slotSpeed(KJob *job, unsigned long bytesPerSecond))
0163 
0164     // KIO::SimpleJob subclasses
0165     Q_PRIVATE_SLOT(d, void _k_slotRedirection(KIO::Job *job, const QUrl &url))
0166 
0167     // KIO::ListJob
0168     Q_PRIVATE_SLOT(d, void _k_slotEntries(KIO::Job *job, const KIO::UDSEntryList &entries))
0169 
0170     // KIO::TransferJob
0171     Q_PRIVATE_SLOT(d, void _k_slotData(KIO::Job *job, const QByteArray &data))
0172     Q_PRIVATE_SLOT(d, void _k_slotDataReq(KIO::Job *job, QByteArray &data))
0173     Q_PRIVATE_SLOT(d, void _k_slotMimetype(KIO::Job *job, const QString &type))
0174     Q_PRIVATE_SLOT(d, void _k_slotCanResume(KIO::Job *job, KIO::filesize_t offset))
0175 
0176     friend class ForwardingSlaveBasePrivate;
0177     std::unique_ptr<ForwardingSlaveBasePrivate> const d;
0178 };
0179 
0180 }
0181 
0182 #endif
0183 
0184 #endif