1 | // -*- c++ -*- |
2 | /* |
3 | This file is part of the KDE libraries |
4 | SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org> |
5 | SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org> |
6 | |
7 | SPDX-License-Identifier: LGPL-2.0-only |
8 | */ |
9 | |
10 | #ifndef KIO_WORKER_P_H |
11 | #define KIO_WORKER_P_H |
12 | |
13 | #include "workerinterface_p.h" |
14 | |
15 | #include <QDateTime> |
16 | #include <QElapsedTimer> |
17 | #include <QObject> |
18 | |
19 | namespace KIO |
20 | { |
21 | |
22 | class WorkerThread; |
23 | class WorkerManager; |
24 | class SimpleJob; |
25 | class Scheduler; |
26 | class SchedulerPrivate; |
27 | class DataProtocol; |
28 | class ProtoQueue; |
29 | class SimpleJobPrivate; |
30 | class UserNotificationHandler; |
31 | |
32 | // Do not use this class directly, outside of KIO. Only use the Worker pointer |
33 | // that is returned by the scheduler for passing it around. |
34 | class Worker : public KIO::WorkerInterface |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | explicit Worker(const QString &protocol, QObject *parent = nullptr); |
39 | |
40 | ~Worker() override; |
41 | |
42 | /** |
43 | * Sends the given command to the KIO worker. |
44 | * Called by the jobs. |
45 | * @param cmd command id |
46 | * @param arr byte array containing data |
47 | */ |
48 | virtual void send(int cmd, const QByteArray &arr = QByteArray()); |
49 | |
50 | /** |
51 | * The actual protocol used to handle the request. |
52 | * |
53 | * This method will return a different protocol than |
54 | * the one obtained by using protocol() if a |
55 | * proxy-server is used for the given protocol. This |
56 | * usually means that this method will return "http" |
57 | * when the actual request was to retrieve a resource |
58 | * from an "ftp" server by going through a proxy server. |
59 | * |
60 | * @return the actual protocol (KIO worker) that handled the request |
61 | */ |
62 | QString workerProtocol() const; |
63 | |
64 | /** |
65 | * @return Host this worker is (was?) connected to |
66 | */ |
67 | QString host() const; |
68 | |
69 | /** |
70 | * @return port this worker is (was?) connected to |
71 | */ |
72 | quint16 port() const; |
73 | |
74 | /** |
75 | * @return User this worker is (was?) logged in as |
76 | */ |
77 | QString user() const; |
78 | |
79 | /** |
80 | * @return Passwd used to log in |
81 | */ |
82 | QString passwd() const; |
83 | |
84 | /** |
85 | * Creates a new worker. |
86 | * |
87 | * @param protocol the protocol |
88 | * @param url is the url |
89 | * @param error is the error code on failure and undefined else. |
90 | * @param error_text is the error text on failure and undefined else. |
91 | * |
92 | * @return 0 on failure, or a pointer to a worker otherwise. |
93 | */ |
94 | static Worker *createWorker(const QString &protocol, const QUrl &url, int &error, QString &error_text); |
95 | |
96 | // == communication with connected kioworker == |
97 | // whenever possible prefer these methods over the respective |
98 | // methods in connection() |
99 | /** |
100 | * Suspends the operation of the attached kioworker. |
101 | */ |
102 | virtual void suspend(); |
103 | |
104 | /** |
105 | * Resumes the operation of the attached kioworker. |
106 | */ |
107 | virtual void resume(); |
108 | |
109 | /** |
110 | * Tells whether the kioworker is suspended. |
111 | * @return true if the kioworker is suspended. |
112 | */ |
113 | virtual bool suspended(); |
114 | |
115 | // == end communication with connected kioworker == |
116 | private: |
117 | friend class Scheduler; |
118 | friend class SchedulerPrivate; |
119 | friend class DataProtocol; |
120 | friend class WorkerManager; |
121 | friend class ProtoQueue; |
122 | friend class SimpleJobPrivate; |
123 | friend class UserNotificationHandler; |
124 | |
125 | void setPID(qint64); |
126 | qint64 worker_pid() const; |
127 | |
128 | void setJob(KIO::SimpleJob *job); |
129 | KIO::SimpleJob *job() const; |
130 | |
131 | /** |
132 | * Force termination |
133 | */ |
134 | void kill(); |
135 | |
136 | /** |
137 | * @return true if the worker survived the last mission. |
138 | */ |
139 | bool isAlive() const; |
140 | |
141 | /** |
142 | * Set host for url |
143 | * @param host to connect to. |
144 | * @param port to connect to. |
145 | * @param user to login as |
146 | * @param passwd to login with |
147 | */ |
148 | virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &passwd); |
149 | |
150 | /** |
151 | * Clear host info. |
152 | */ |
153 | void resetHost(); |
154 | |
155 | /** |
156 | * Configure worker |
157 | */ |
158 | virtual void setConfig(const MetaData &config); |
159 | |
160 | /** |
161 | * The protocol this worker handles. |
162 | * |
163 | * @return name of protocol handled by this worker, as seen by the user |
164 | */ |
165 | QString protocol() const; |
166 | |
167 | void setProtocol(const QString &protocol); |
168 | |
169 | /** |
170 | * @return The number of seconds this worker has been idle. |
171 | */ |
172 | int idleTime() const; |
173 | |
174 | /** |
175 | * Marks this worker as idle. |
176 | */ |
177 | void setIdle(); |
178 | |
179 | void ref(); |
180 | void deref(); |
181 | void aboutToDelete(); |
182 | |
183 | void setWorkerThread(WorkerThread *thread); |
184 | |
185 | public Q_SLOTS: // TODO KF6: make all three slots private |
186 | void accept(); |
187 | void gotInput(); |
188 | void timeout(); |
189 | |
190 | Q_SIGNALS: |
191 | void workerDied(KIO::Worker *worker); |
192 | |
193 | private: |
194 | WorkerThread *m_workerThread = nullptr; // only set for in-process workers |
195 | QString m_protocol; |
196 | QString m_workerProtocol; |
197 | QString m_host; |
198 | QString m_user; |
199 | QString m_passwd; |
200 | KIO::ConnectionServer *m_workerConnServer; |
201 | KIO::SimpleJob *m_job = nullptr; |
202 | qint64 m_pid = 0; // only set for out-of-process workers |
203 | quint16 m_port = 0; |
204 | bool m_dead = false; |
205 | QElapsedTimer m_contact_started; |
206 | QElapsedTimer m_idleSince; |
207 | int m_refCount = 1; |
208 | }; |
209 | |
210 | } |
211 | |
212 | #endif |
213 | |