File indexing completed on 2024-04-28 03:55:18

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_WORKER_P_H
0011 #define KIO_WORKER_P_H
0012 
0013 #include "workerinterface_p.h"
0014 
0015 #include <QDateTime>
0016 #include <QElapsedTimer>
0017 #include <QObject>
0018 
0019 namespace KIO
0020 {
0021 
0022 class WorkerThread;
0023 class WorkerManager;
0024 class SimpleJob;
0025 class Scheduler;
0026 class SchedulerPrivate;
0027 class DataProtocol;
0028 class ProtoQueue;
0029 class SimpleJobPrivate;
0030 class UserNotificationHandler;
0031 
0032 // Do not use this class directly, outside of KIO. Only use the Worker pointer
0033 // that is returned by the scheduler for passing it around.
0034 class Worker : public KIO::WorkerInterface
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit Worker(const QString &protocol, QObject *parent = nullptr);
0039 
0040     ~Worker() override;
0041 
0042     /**
0043      * Sends the given command to the KIO worker.
0044      * Called by the jobs.
0045      * @param cmd command id
0046      * @param arr byte array containing data
0047      */
0048     virtual void send(int cmd, const QByteArray &arr = QByteArray());
0049 
0050     /**
0051      * The actual protocol used to handle the request.
0052      *
0053      * This method will return a different protocol than
0054      * the one obtained by using protocol() if a
0055      * proxy-server is used for the given protocol.  This
0056      * usually means that this method will return "http"
0057      * when the actual request was to retrieve a resource
0058      * from an "ftp" server by going through a proxy server.
0059      *
0060      * @return the actual protocol (KIO worker) that handled the request
0061      */
0062     QString workerProtocol() const;
0063 
0064     /**
0065      * @return Host this worker is (was?) connected to
0066      */
0067     QString host() const;
0068 
0069     /**
0070      * @return port this worker is (was?) connected to
0071      */
0072     quint16 port() const;
0073 
0074     /**
0075      * @return User this worker is (was?) logged in as
0076      */
0077     QString user() const;
0078 
0079     /**
0080      * @return Passwd used to log in
0081      */
0082     QString passwd() const;
0083 
0084     /**
0085      * Creates a new worker.
0086      *
0087      * @param protocol the protocol
0088      * @param url is the url
0089      * @param error is the error code on failure and undefined else.
0090      * @param error_text is the error text on failure and undefined else.
0091      *
0092      * @return 0 on failure, or a pointer to a worker otherwise.
0093      */
0094     static Worker *createWorker(const QString &protocol, const QUrl &url, int &error, QString &error_text);
0095 
0096     // == communication with connected kioworker ==
0097     // whenever possible prefer these methods over the respective
0098     // methods in connection()
0099     /**
0100      * Suspends the operation of the attached kioworker.
0101      */
0102     virtual void suspend();
0103 
0104     /**
0105      * Resumes the operation of the attached kioworker.
0106      */
0107     virtual void resume();
0108 
0109     /**
0110      * Tells whether the kioworker is suspended.
0111      * @return true if the kioworker is suspended.
0112      */
0113     virtual bool suspended();
0114 
0115     // == end communication with connected kioworker ==
0116 private:
0117     friend class Scheduler;
0118     friend class SchedulerPrivate;
0119     friend class DataProtocol;
0120     friend class WorkerManager;
0121     friend class ProtoQueue;
0122     friend class SimpleJobPrivate;
0123     friend class UserNotificationHandler;
0124 
0125     void setPID(qint64);
0126     qint64 worker_pid() const;
0127 
0128     void setJob(KIO::SimpleJob *job);
0129     KIO::SimpleJob *job() const;
0130 
0131     /**
0132      * Force termination
0133      */
0134     void kill();
0135 
0136     /**
0137      * @return true if the worker survived the last mission.
0138      */
0139     bool isAlive() const;
0140 
0141     /**
0142      * Set host for url
0143      * @param host to connect to.
0144      * @param port to connect to.
0145      * @param user to login as
0146      * @param passwd to login with
0147      */
0148     virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &passwd);
0149 
0150     /**
0151      * Clear host info.
0152      */
0153     void resetHost();
0154 
0155     /**
0156      * Configure worker
0157      */
0158     virtual void setConfig(const MetaData &config);
0159 
0160     /**
0161      * The protocol this worker handles.
0162      *
0163      * @return name of protocol handled by this worker, as seen by the user
0164      */
0165     QString protocol() const;
0166 
0167     void setProtocol(const QString &protocol);
0168 
0169     /**
0170      * @return The number of seconds this worker has been idle.
0171      */
0172     int idleTime() const;
0173 
0174     /**
0175      * Marks this worker as idle.
0176      */
0177     void setIdle();
0178 
0179     void ref();
0180     void deref();
0181     void aboutToDelete();
0182 
0183     void setWorkerThread(WorkerThread *thread);
0184 
0185 public Q_SLOTS: // TODO KF6: make all three slots private
0186     void accept();
0187     void gotInput();
0188     void timeout();
0189 
0190 Q_SIGNALS:
0191     void workerDied(KIO::Worker *worker);
0192 
0193 private:
0194     WorkerThread *m_workerThread = nullptr; // only set for in-process workers
0195     QString m_protocol;
0196     QString m_workerProtocol;
0197     QString m_host;
0198     QString m_user;
0199     QString m_passwd;
0200     KIO::ConnectionServer *m_workerConnServer;
0201     KIO::SimpleJob *m_job = nullptr;
0202     qint64 m_pid = 0; // only set for out-of-process workers
0203     quint16 m_port = 0;
0204     bool m_dead = false;
0205     QElapsedTimer m_contact_started;
0206     QElapsedTimer m_idleSince;
0207     int m_refCount = 1;
0208 };
0209 
0210 }
0211 
0212 #endif