KIO

workerbase.cpp
1/*
2 SPDX-License-Identifier: LGPL-2.0-or-later
3 SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org>
4 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
5 SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
6 SPDX-FileCopyrightText: 2007 Thiago Macieira <thiago@kde.org>
7 SPDX-FileCopyrightText: 2019-2022 Harald Sitter <sitter@kde.org>
8*/
9
10#include "workerbase.h"
11#include "workerbase_p.h"
12
13#include <commands_p.h>
14
15namespace KIO
16{
17
18WorkerBase::WorkerBase(const QByteArray &protocol, const QByteArray &poolSocket, const QByteArray &appSocket)
19 : d(new WorkerBasePrivate(protocol, poolSocket, appSocket, this))
20{
21}
22
23WorkerBase::~WorkerBase() = default;
24
26{
27 d->bridge.dispatchLoop();
28}
29
31{
32 d->bridge.connectSlave(address);
33}
34
35void WorkerBase::disconnectWorker()
36{
37 d->bridge.disconnectSlave();
38}
39
40void WorkerBase::setMetaData(const QString &key, const QString &value)
41{
42 d->bridge.setMetaData(key, value);
43}
44
46{
47 return d->bridge.metaData(key);
48}
49
51{
52 return d->bridge.allMetaData();
53}
54
55bool WorkerBase::hasMetaData(const QString &key) const
56{
57 return d->bridge.hasMetaData(key);
58}
59
61{
62 return d->bridge.mapConfig();
63}
64
65bool WorkerBase::configValue(const QString &key, bool defaultValue) const
66{
67 return d->bridge.configValue(key, defaultValue);
68}
69
70int WorkerBase::configValue(const QString &key, int defaultValue) const
71{
72 return d->bridge.configValue(key, defaultValue);
73}
74
75QString WorkerBase::configValue(const QString &key, const QString &defaultValue) const
76{
77 return d->bridge.configValue(key, defaultValue);
78}
79
81{
82 return d->bridge.config();
83}
84
86{
87 d->bridge.sendMetaData();
88}
89
91{
92 d->bridge.sendAndKeepMetaData();
93}
94
96{
97 return d->bridge.remoteEncoding();
98}
99
101{
102 d->bridge.data(data);
103}
104
106{
107 d->bridge.dataReq();
108}
109
110void WorkerBase::workerStatus(const QString &host, bool connected)
111{
112 d->bridge.slaveStatus(host, connected);
113}
114
116{
117 d->bridge.canResume();
118}
119
121{
122 d->bridge.totalSize(_bytes);
123}
124
126{
127 d->bridge.processedSize(_bytes);
128}
129
130void WorkerBase::written(KIO::filesize_t _bytes)
131{
132 d->bridge.written(_bytes);
133}
134
135void WorkerBase::position(KIO::filesize_t _pos)
136{
137 d->bridge.position(_pos);
138}
139
140void WorkerBase::truncated(KIO::filesize_t _length)
141{
142 d->bridge.truncated(_length);
143}
144
145void WorkerBase::speed(unsigned long _bytes_per_second)
146{
147 d->bridge.speed(_bytes_per_second);
148}
149
151{
152 d->bridge.redirection(_url);
153}
154
155#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 3)
157{
158}
159#endif
160
162{
163 d->bridge.mimeType(_type);
164}
165
167{
168 d->bridge.exit();
169}
170
172{
173 d->bridge.warning(_msg);
174}
175
177{
178 d->bridge.infoMessage(_msg);
179}
180
182{
183 d->bridge.statEntry(entry);
184}
185
187{
188 d->bridge.listEntry(entry);
189}
190
192{
193 d->bridge.listEntries(list);
194}
195
197{
198} // No response!
199
200void WorkerBase::setHost(QString const &, quint16, QString const &, QString const &)
201{
202} // No response!
203
205{
206 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_CONNECT));
207}
208
210{
211} // No response!
212
214{
215 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_STAT));
216}
217
219{
220 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_PUT));
221}
222
224{
225 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_SPECIAL));
226}
227
229{
230 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_LISTDIR));
231}
232
234{
235 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_GET));
236}
237
239{
240 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_OPEN));
241}
242
244{
245 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_READ));
246}
247
249{
250 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_WRITE));
251}
252
254{
255 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_SEEK));
256}
257
259{
260 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_TRUNCATE));
261}
262
264{
265 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_CLOSE));
266}
267
269{
270 return get(url);
271}
272
274{
275 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_RENAME));
276}
277
279{
280 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_SYMLINK));
281}
282
284{
285 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_COPY));
286}
287
289{
290 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_DEL));
291}
292
294{
295 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_MKDIR));
296}
297
299{
300 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_CHMOD));
301}
302
304{
305 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_SETMODIFICATIONTIME));
306}
307
309{
310 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_CHOWN));
311}
312
314{
315 return WorkerResult::fail(ERR_UNSUPPORTED_ACTION, unsupportedActionErrorString(d->protocolName(), CMD_FILESYSTEMFREESPACE));
316}
317
319{
320 workerStatus(QString(), false);
321}
322
324{
325 // base implementation called by bridge
326}
327
329{
330 return d->bridge.openPasswordDialogV2(info, errorMsg);
331}
332
333int WorkerBase::messageBox(MessageBoxType type, const QString &text, const QString &title, const QString &primaryActionText, const QString &secondaryActionText)
334{
335 return messageBox(text, type, title, primaryActionText, secondaryActionText, QString());
336}
337
339 MessageBoxType type,
340 const QString &title,
341 const QString &primaryActionText,
342 const QString &secondaryActionText,
343 const QString &dontAskAgainName)
344{
345 return d->bridge.messageBox(text, static_cast<SlaveBase::MessageBoxType>(type), title, primaryActionText, secondaryActionText, dontAskAgainName);
346}
347
348int WorkerBase::sslError(const QVariantMap &sslData)
349{
350 return d->bridge.sslError(sslData);
351}
352
354{
355 return d->bridge.canResume(offset);
356}
357
358int WorkerBase::waitForAnswer(int expected1, int expected2, QByteArray &data, int *pCmd)
359{
360 return d->bridge.waitForAnswer(expected1, expected2, data, pCmd);
361}
362
364{
365 return d->bridge.readData(buffer);
366}
367
369{
370 d->bridge.setTimeoutSpecialCommand(timeout, data);
371}
372
374{
375 return d->bridge.checkCachedAuthentication(info);
376}
377
379{
380 return d->bridge.cacheAuthentication(info);
381}
382
383#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
385{
386 return d->bridge.connectTimeout();
387}
388#endif
389
390#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
392{
393 return d->bridge.proxyConnectTimeout();
394}
395#endif
396
397#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
399{
400 return d->bridge.responseTimeout();
401}
402#endif
403
404#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
406{
407 return d->bridge.readTimeout();
408}
409#endif
410
412{
413 return d->bridge.wasKilled();
414}
415
417{
418 return d->bridge.lookupHost(host);
419}
420
422{
423 return d->bridge.waitForHostInfo(info);
424}
425
427{
428 return d->bridge.requestPrivilegeOperation(operationDetails);
429}
430
432{
433 d->bridge.addTemporaryAuthorization(action);
434}
435
436class WorkerResultPrivate
437{
438public:
439 bool success;
440 int error;
441 QString errorString;
442};
443
444WorkerResult::~WorkerResult() = default;
445
446WorkerResult::WorkerResult(const WorkerResult &rhs)
447 : d(std::make_unique<WorkerResultPrivate>(*rhs.d))
448{
449}
450
451WorkerResult &WorkerResult::operator=(const WorkerResult &rhs)
452{
453 if (this == &rhs) {
454 return *this;
455 }
456 d = std::make_unique<WorkerResultPrivate>(*rhs.d);
457 return *this;
458}
459
460WorkerResult::WorkerResult(WorkerResult &&) noexcept = default;
461WorkerResult &WorkerResult::operator=(WorkerResult &&) noexcept = default;
462
464{
465 return d->success;
466}
467
469{
470 return d->error;
471}
472
474{
475 return d->errorString;
476}
477
478Q_REQUIRED_RESULT WorkerResult WorkerResult::fail(int _error, const QString &_errorString)
479{
480 return WorkerResult(std::make_unique<WorkerResultPrivate>(WorkerResultPrivate{false, _error, _errorString}));
481}
482
484{
485 return WorkerResult(std::make_unique<WorkerResultPrivate>(WorkerResultPrivate{true, 0, QString()}));
486}
487
488WorkerResult::WorkerResult(std::unique_ptr<WorkerResultPrivate> &&dptr)
489 : d(std::move(dptr))
490{
491}
492
494{
495 d->bridge.setIncomingMetaData(metaData);
496}
497} // namespace KIO
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
Definition authinfo.h:50
MetaData is a simple map of key/value strings.
Definition metadata.h:23
MessageBoxType
Type of message box.
Definition slavebase.h:250
Universal Directory Service.
Definition udsentry.h:79
virtual WorkerResult del(const QUrl &url, bool isfile)
Delete a file or directory.
virtual WorkerResult mimetype(const QUrl &url)
Finds MIME type for one file or directory.
virtual void reparseConfiguration()
Called by the scheduler to tell the worker that the configuration changed (i.e. proxy settings) .
void errorPage()
Tell that we will only get an error page here.
virtual WorkerResult write(const QByteArray &data)
write.
virtual WorkerResult close()
close.
bool wasKilled() const
If your ioworker was killed by a signal, wasKilled() returns true.
virtual void worker_status()
Called to get the status of the worker.
KConfigGroup * config()
Returns a configuration object to query config/meta-data information from.
KRemoteEncoding * remoteEncoding()
Returns an object that can translate remote filenames into proper Unicode forms.
void redirection(const QUrl &_url)
Call this to signal a redirection.
int messageBox(MessageBoxType type, const QString &text, const QString &title=QString(), const QString &primaryActionText=QString(), const QString &secondaryActionText=QString())
Call this to show a message box from the worker.
void setTimeoutSpecialCommand(int timeout, const QByteArray &data=QByteArray())
This function sets a timeout of timeout seconds and calls special(data) when the timeout occurs as if...
void totalSize(KIO::filesize_t _bytes)
Call this in get and copy, to give the total size of the file.
void statEntry(const UDSEntry &_entry)
Call this from stat() to express details about an object, the UDSEntry customarily contains the atoms...
virtual WorkerResult special(const QByteArray &data)
Used for any command that is specific to this worker (protocol).
virtual WorkerResult mkdir(const QUrl &url, int permissions)
Create a directory.
virtual WorkerResult stat(const QUrl &url)
Finds all details for one file or directory.
bool configValue(const QString &key, bool defaultValue) const
Returns a bool from the config/meta-data information.
void lookupHost(const QString &host)
Internally used.
MetaData allMetaData() const
void listEntry(const UDSEntry &entry)
It collects entries and emits them via listEntries when enough of them are there or a certain time fr...
void addTemporaryAuthorization(const QString &action)
Adds action to the list of PolicyKit actions which the worker is authorized to perform.
bool checkCachedAuthentication(AuthInfo &info)
Checks for cached authentication based on parameters given by info.
void warning(const QString &msg)
Call to signal a warning, to be displayed in a dialog box.
void workerStatus(const QString &host, bool connected)
Used to report the status of the worker.
void infoMessage(const QString &msg)
Call to signal a message, to be displayed if the application wants to, for instance in a status bar.
virtual WorkerResult symlink(const QString &target, const QUrl &dest, JobFlags flags)
Creates a symbolic link named dest, pointing to target, which may be a relative or an absolute path.
virtual WorkerResult read(KIO::filesize_t size)
read.
virtual WorkerResult chmod(const QUrl &url, int permissions)
Change permissions on url.
virtual WorkerResult listDir(const QUrl &url)
Lists the contents of url.
void listEntries(const UDSEntryList &_entry)
Call this in listDir, each time you have a bunch of entries to report.
virtual WorkerResult openConnection()
Opens the connection (forced).
int openPasswordDialog(KIO::AuthInfo &info, const QString &errorMsg=QString())
Prompt the user for Authorization info (login & password).
int waitForAnswer(int expected1, int expected2, QByteArray &data, int *pCmd=nullptr)
Wait for an answer to our request, until we get expected1 or expected2.
virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &pass)
Set the host.
bool hasMetaData(const QString &key) const
Queries for the existence of a certain config/meta-data entry send by the application to the worker.
MessageBoxType
Type of message box.
Definition workerbase.h:223
virtual WorkerResult fileSystemFreeSpace(const QUrl &url)
Get a filesystem's total and available space.
void dataReq()
Asks for data from the job.
void setMetaData(const QString &key, const QString &value)
Sets meta-data to be send to the application before the first data() or finished() signal.
bool canResume(KIO::filesize_t offset)
Call this at the beginning of put(), to give the size of the existing partial file,...
void setIncomingMetaData(const KIO::MetaData &metaData)
Set the Incoming Meta Data This is only really useful if your worker wants to overwrite the metadata ...
bool cacheAuthentication(const AuthInfo &info)
Caches info in a persistent storage like KWallet.
virtual WorkerResult seek(KIO::filesize_t offset)
seek.
int waitForHostInfo(QHostInfo &info)
Internally used.
QMap< QString, QVariant > mapConfig() const
Returns a map to query config/meta-data information from.
virtual WorkerResult chown(const QUrl &url, const QString &owner, const QString &group)
Change ownership of url.
QString metaData(const QString &key) const
Queries for config/meta-data send by the application to the worker.
void sendAndKeepMetaData()
Internal function to transmit meta data to the application.
virtual void closeConnection()
Closes the connection (forced).
PrivilegeOperationStatus requestPrivilegeOperation(const QString &operationDetails)
Checks with job if privilege operation is allowed.
void speed(unsigned long _bytes_per_second)
Call this in get and copy, to give the current transfer speed, but only if it can't be calculated out...
int readData(QByteArray &buffer)
Read data sent by the job, after a dataReq.
void mimeType(const QString &_type)
Call this in mimetype() and in get(), when you know the MIME type.
virtual WorkerResult get(const QUrl &url)
get, aka read.
void data(const QByteArray &data)
Sends data in the worker to the job (i.e. in get).
void sendMetaData()
Internal function to transmit meta data to the application.
virtual WorkerResult copy(const QUrl &src, const QUrl &dest, int permissions, JobFlags flags)
Copy src into dest.
int proxyConnectTimeout()
virtual WorkerResult rename(const QUrl &src, const QUrl &dest, JobFlags flags)
Rename oldname into newname.
virtual void appConnectionMade()
Application connected to the worker.
void processedSize(KIO::filesize_t _bytes)
Call this during get and copy, once in a while, to give some info about the current state.
virtual WorkerResult setModificationTime(const QUrl &url, const QDateTime &mtime)
Sets the modification time for @url.
virtual WorkerResult put(const QUrl &url, int permissions, JobFlags flags)
put, i.e. write data into a file.
virtual WorkerResult truncate(KIO::filesize_t size)
truncate
virtual WorkerResult open(const QUrl &url, QIODevice::OpenMode mode)
open.
void connectWorker(const QString &path)
internal function to connect a worker to/ disconnect from either the worker pool or the application
The result of a worker call When using the Result type always mark the function Q_REQUIRED_RESULT to ...
Definition workerbase.h:36
bool success() const
Whether or not the result was a success.
int error() const
The error code (or ERR_UNKNOWN) of the result.
QString errorString() const
The localized error description if applicable.
static WorkerResult pass()
Constructs a success result.
static WorkerResult fail(int _error=KIO::ERR_UNKNOWN, const QString &_errorString=QString())
Constructs a failure results.
WorkerResult()=delete
Use fail() or pass();.
Allows encoding and decoding properly remote filenames into Unicode.
A namespace for KIO globals.
KIOCORE_EXPORT QString unsupportedActionErrorString(const QString &protocol, int cmd)
Returns an appropriate error message if the given command cmd is an unsupported action (ERR_UNSUPPORT...
KIOCORE_EXPORT CopyJob * move(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Moves a file or directory src to the given destination dest.
Definition copyjob.cpp:2657
PrivilegeOperationStatus
Specifies privilege file operation status.
Definition global.h:239
QList< UDSEntry > UDSEntryList
A directory listing is a list of UDSEntry instances.
Definition udsentry.h:379
QFlags< JobFlag > JobFlags
Stores a combination of JobFlag values.
Definition job_base.h:281
qulonglong filesize_t
64-bit file size
Definition global.h:35
typedef OpenMode
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:37 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.