File indexing completed on 2024-05-05 16:13:25

0001 // -*- c++ -*-
0002 /*
0003     This file is part of the KDE libraries
0004     SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org>
0005     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 #ifndef KIO_SLAVE_H
0011 #define KIO_SLAVE_H
0012 
0013 #include "kio/slaveinterface.h"
0014 #include <QDateTime>
0015 #include <QObject>
0016 
0017 namespace KIO
0018 {
0019 
0020 class WorkerThread;
0021 class SlavePrivate;
0022 class SlaveKeeper;
0023 class SimpleJob;
0024 class Scheduler;
0025 class SchedulerPrivate;
0026 class DataProtocol;
0027 class ConnectedSlaveQueue;
0028 class ProtoQueue;
0029 class SimpleJobPrivate;
0030 class UserNotificationHandler;
0031 
0032 // Do not use this class directly, outside of KIO. Only use the Slave pointer
0033 // that is returned by the scheduler for passing it around.
0034 //
0035 // KF6 TODO: remove export macro, nothing uses this class outside kio anymore
0036 // (and rename this file to slave_p.h, and don't install it anymore)
0037 class KIOCORE_EXPORT Slave : public KIO::SlaveInterface
0038 {
0039     Q_OBJECT
0040 public:
0041     explicit Slave(const QString &protocol, QObject *parent = nullptr);
0042 
0043     ~Slave() override;
0044 
0045     /**
0046      * Sends the given command to the kioslave.
0047      * Called by the jobs.
0048      * @param cmd command id
0049      * @param arr byte array containing data
0050      */
0051     virtual void send(int cmd, const QByteArray &arr = QByteArray());
0052 
0053     /**
0054      * The actual protocol used to handle the request.
0055      *
0056      * This method will return a different protocol than
0057      * the one obtained by using protocol() if a
0058      * proxy-server is used for the given protocol.  This
0059      * usually means that this method will return "http"
0060      * when the actual request was to retrieve a resource
0061      * from an "ftp" server by going through a proxy server.
0062      *
0063      * @return the actual protocol (io-slave) that handled the request
0064      */
0065     QString slaveProtocol();
0066 
0067     /**
0068      * @return Host this slave is (was?) connected to
0069      */
0070     QString host();
0071 
0072     /**
0073      * @return port this slave is (was?) connected to
0074      */
0075     quint16 port();
0076 
0077     /**
0078      * @return User this slave is (was?) logged in as
0079      */
0080     QString user();
0081 
0082     /**
0083      * @return Passwd used to log in
0084      */
0085     QString passwd();
0086 
0087     /**
0088      * Creates a new slave.
0089      *
0090      * @param protocol the protocol
0091      * @param url is the url
0092      * @param error is the error code on failure and undefined else.
0093      * @param error_text is the error text on failure and undefined else.
0094      *
0095      * @return 0 on failure, or a pointer to a slave otherwise.
0096      */
0097     static Slave *createSlave(const QString &protocol, const QUrl &url, int &error, QString &error_text);
0098 
0099 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 88)
0100     /**
0101      * Requests a slave on hold for ths url, from klauncher, if there is such a job.
0102      * See hold()
0103      * @deprecated since 5.88 this method was internal anyway
0104      */
0105     KIOCORE_DEPRECATED_VERSION(5, 88, "Remove this call. The feature is gone")
0106     static Slave *holdSlave(const QString &protocol, const QUrl &url);
0107 #endif
0108 
0109 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 88)
0110     /**
0111      * Returns true if klauncher is holding a slave for @p url.
0112      * Used by klauncher only.
0113      * @since 4.7
0114      * @deprecated since 5.88 this method was internal anyway
0115      */
0116     KIOCORE_DEPRECATED_VERSION(5, 88, "Remove this call. The feature is gone")
0117     static bool checkForHeldSlave(const QUrl &url);
0118 #endif
0119 
0120     // == communication with connected kioslave ==
0121     // whenever possible prefer these methods over the respective
0122     // methods in connection()
0123     /**
0124      * Suspends the operation of the attached kioslave.
0125      */
0126     virtual void suspend();
0127 
0128     /**
0129      * Resumes the operation of the attached kioslave.
0130      */
0131     virtual void resume();
0132 
0133     /**
0134      * Tells whether the kioslave is suspended.
0135      * @return true if the kioslave is suspended.
0136      */
0137     virtual bool suspended();
0138 
0139     // == end communication with connected kioslave ==
0140 private:
0141     friend class Scheduler;
0142     friend class SchedulerPrivate;
0143     friend class DataProtocol;
0144     friend class SlaveKeeper;
0145     friend class ConnectedSlaveQueue;
0146     friend class ProtoQueue;
0147     friend class SimpleJobPrivate;
0148     friend class UserNotificationHandler;
0149 
0150     void setPID(qint64);
0151     qint64 slave_pid();
0152 
0153     void setJob(KIO::SimpleJob *job);
0154     KIO::SimpleJob *job() const;
0155 
0156     /**
0157      * Force termination
0158      */
0159     void kill();
0160 
0161     /**
0162      * @return true if the slave survived the last mission.
0163      */
0164     bool isAlive();
0165 
0166     /**
0167      * Set host for url
0168      * @param host to connect to.
0169      * @param port to connect to.
0170      * @param user to login as
0171      * @param passwd to login with
0172      */
0173     virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &passwd);
0174 
0175     /**
0176      * Clear host info.
0177      */
0178     void resetHost();
0179 
0180     /**
0181      * Configure slave
0182      */
0183     virtual void setConfig(const MetaData &config);
0184 
0185     /**
0186      * The protocol this slave handles.
0187      *
0188      * @return name of protocol handled by this slave, as seen by the user
0189      */
0190     QString protocol();
0191 
0192     void setProtocol(const QString &protocol);
0193 
0194     /**
0195      * Puts the kioslave associated with @p url at halt, and return it to klauncher, in order
0196      * to let another application connect to it and finish the job.
0197      * This is for the krunner case: type a URL in krunner, it will start downloading
0198      * to find the MIME type (KRun), and then hold the slave, publish the held slave using,
0199      * this method, and the final application can continue the same download by requesting
0200      * the same URL.
0201      */
0202     virtual void hold(const QUrl &url);
0203 
0204     /**
0205      * @return The number of seconds this slave has been idle.
0206      */
0207     int idleTime();
0208 
0209     /**
0210      * Marks this slave as idle.
0211      */
0212     void setIdle();
0213 
0214 #if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 103)
0215     /**
0216      * @returns Whether the slave is connected
0217      * (Connection oriented slaves only)
0218      * @deprecated Since 5.103. The connected slave feature will be removed.
0219      */
0220     KIOCORE_DEPRECATED_VERSION(5, 103, "The connected slave feature will be removed.")
0221     bool isConnected();
0222     KIOCORE_DEPRECATED_VERSION(5, 103, "The connected slave feature will be removed.")
0223     void setConnected(bool c);
0224 #endif
0225 
0226     void ref();
0227     void deref();
0228     void aboutToDelete();
0229 
0230     void setWorkerThread(WorkerThread *thread);
0231 
0232 public Q_SLOTS: // TODO KF6: make all three slots private
0233     void accept();
0234     void gotInput();
0235     void timeout();
0236 
0237 Q_SIGNALS:
0238     void slaveDied(KIO::Slave *slave);
0239 
0240 private:
0241     Q_DECLARE_PRIVATE(Slave)
0242 };
0243 
0244 }
0245 
0246 #endif