KIO

kprotocolmanager.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999 Torben Weis <weis@kde.org>
4 SPDX-FileCopyrightText: 2000 Waldo Bastain <bastain@kde.org>
5 SPDX-FileCopyrightText: 2000 Dawit Alemayehu <adawit@kde.org>
6 SPDX-FileCopyrightText: 2008 Jarosław Staniek <staniek@kde.org>
7 SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
8
9 SPDX-License-Identifier: LGPL-2.0-only
10*/
11
12#include "kprotocolmanager.h"
13#include "kprotocolinfo_p.h"
14#include "kprotocolmanager_p.h"
15
16#include <config-kiocore.h>
17
18#include <QCoreApplication>
19#include <QUrl>
20
21#include <KConfigGroup>
22#include <KSharedConfig>
23
24#include <kprotocolinfofactory_p.h>
25
26#include "ioworker_defaults.h"
27#include "workerconfig.h"
28
29Q_GLOBAL_STATIC(KProtocolManagerPrivate, kProtocolManagerPrivate)
30
31static void syncOnExit()
32{
33 if (kProtocolManagerPrivate.exists()) {
34 kProtocolManagerPrivate()->sync();
35 }
36}
37
38KProtocolManagerPrivate::KProtocolManagerPrivate()
39{
40 // post routine since KConfig::sync() breaks if called too late
41 qAddPostRoutine(syncOnExit);
42}
43
44KProtocolManagerPrivate::~KProtocolManagerPrivate()
45{
46}
47
48void KProtocolManagerPrivate::sync()
49{
50 QMutexLocker lock(&mutex);
51 if (configPtr) {
52 configPtr->sync();
53 }
54}
55
57{
58 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
59 QMutexLocker lock(&d->mutex);
60 if (d->configPtr) {
61 d->configPtr->reparseConfiguration();
62 }
63 lock.unlock();
64
65 // Force the slave config to re-read its config...
66 KIO::WorkerConfig::self()->reset();
67}
68
69static KSharedConfig::Ptr config()
70{
71 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
72 Q_ASSERT(!d->mutex.tryLock()); // the caller must have locked the mutex
73 if (!d->configPtr) {
74 d->configPtr = KSharedConfig::openConfig(QStringLiteral("kioslaverc"), KConfig::NoGlobals);
75 }
76 return d->configPtr;
77}
78
79QMap<QString, QString> KProtocolManager::entryMap(const QString &group)
80{
81 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
82 QMutexLocker lock(&d->mutex);
83 return config()->entryMap(group);
84}
85
86/*=============================== TIMEOUT SETTINGS ==========================*/
87
89{
90 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
91 QMutexLocker lock(&d->mutex);
92 KConfigGroup cg(config(), QString());
93 int val = cg.readEntry("ReadTimeout", DEFAULT_READ_TIMEOUT);
94 return qMax(MIN_TIMEOUT_VALUE, val);
95}
96
98{
99 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
100 QMutexLocker lock(&d->mutex);
101 KConfigGroup cg(config(), QString());
102 int val = cg.readEntry("ConnectTimeout", DEFAULT_CONNECT_TIMEOUT);
103 return qMax(MIN_TIMEOUT_VALUE, val);
104}
105
107{
108 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
109 QMutexLocker lock(&d->mutex);
110 KConfigGroup cg(config(), QString());
111 int val = cg.readEntry("ProxyConnectTimeout", DEFAULT_PROXY_CONNECT_TIMEOUT);
112 return qMax(MIN_TIMEOUT_VALUE, val);
113}
114
116{
117 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
118 QMutexLocker lock(&d->mutex);
119 KConfigGroup cg(config(), QString());
120 int val = cg.readEntry("ResponseTimeout", DEFAULT_RESPONSE_TIMEOUT);
121 return qMax(MIN_TIMEOUT_VALUE, val);
122}
123
124/*==================================== OTHERS ===============================*/
125
127{
128 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
129 QMutexLocker lock(&d->mutex);
130 return config()->group(QString()).readEntry("MarkPartial", true);
131}
132
134{
135 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
136 QMutexLocker lock(&d->mutex);
137 return config()->group(QString()).readEntry("MinimumKeepSize",
138 DEFAULT_MINIMUM_KEEP_SIZE); // 5000 byte
139}
140
142{
143 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
144 QMutexLocker lock(&d->mutex);
145 return config()->group(QString()).readEntry("AutoResume", false);
146}
147
148/* =========================== PROTOCOL CAPABILITIES ============== */
149
150static KProtocolInfoPrivate *findProtocol(const QUrl &url)
151{
152 if (!url.isValid()) {
153 return nullptr;
154 }
155 QString protocol = url.scheme();
156 return KProtocolInfoFactory::self()->findProtocol(protocol);
157}
158
160{
161 KProtocolInfoPrivate *prot = findProtocol(url);
162 if (!prot) {
164 }
165
166 return prot->m_inputType;
167}
168
170{
171 KProtocolInfoPrivate *prot = findProtocol(url);
172 if (!prot) {
174 }
175
176 return prot->m_outputType;
177}
178
180{
181 KProtocolInfoPrivate *prot = findProtocol(url);
182 if (!prot) {
183 return false;
184 }
185
186 return prot->m_isSourceProtocol;
187}
188
190{
191 KProtocolInfoPrivate *prot = findProtocol(url);
192 if (!prot) {
193 return false;
194 }
195
196 return prot->m_supportsListing;
197}
198
200{
201 KProtocolInfoPrivate *prot = findProtocol(url);
202 if (!prot) {
203 return QStringList();
204 }
205
206 return prot->m_listing;
207}
208
210{
211 KProtocolInfoPrivate *prot = findProtocol(url);
212 if (!prot) {
213 return false;
214 }
215
216 return prot->m_supportsReading;
217}
218
220{
221 KProtocolInfoPrivate *prot = findProtocol(url);
222 if (!prot) {
223 return false;
224 }
225
226 return prot->m_supportsWriting;
227}
228
230{
231 KProtocolInfoPrivate *prot = findProtocol(url);
232 if (!prot) {
233 return false;
234 }
235
236 return prot->m_supportsMakeDir;
237}
238
240{
241 KProtocolInfoPrivate *prot = findProtocol(url);
242 if (!prot) {
243 return false;
244 }
245
246 return prot->m_supportsDeleting;
247}
248
250{
251 KProtocolInfoPrivate *prot = findProtocol(url);
252 if (!prot) {
253 return false;
254 }
255
256 return prot->m_supportsLinking;
257}
258
260{
261 KProtocolInfoPrivate *prot = findProtocol(url);
262 if (!prot) {
263 return false;
264 }
265
266 return prot->m_supportsMoving;
267}
268
270{
271 KProtocolInfoPrivate *prot = findProtocol(url);
272 if (!prot) {
273 return false;
274 }
275
276 return prot->m_supportsOpening;
277}
278
280{
281 KProtocolInfoPrivate *prot = findProtocol(url);
282 if (!prot) {
283 return false;
284 }
285
286 return prot->m_supportsTruncating;
287}
288
290{
291 KProtocolInfoPrivate *prot = findProtocol(url);
292 if (!prot) {
293 return false;
294 }
295
296 return prot->m_canCopyFromFile;
297}
298
300{
301 KProtocolInfoPrivate *prot = findProtocol(url);
302 if (!prot) {
303 return false;
304 }
305
306 return prot->m_canCopyToFile;
307}
308
310{
311 KProtocolInfoPrivate *prot = findProtocol(url);
312 if (!prot) {
313 return false;
314 }
315
316 return prot->m_canRenameFromFile;
317}
318
320{
321 KProtocolInfoPrivate *prot = findProtocol(url);
322 if (!prot) {
323 return false;
324 }
325
326 return prot->m_canRenameToFile;
327}
328
330{
331 KProtocolInfoPrivate *prot = findProtocol(url);
332 if (!prot) {
333 return false;
334 }
335
336 return prot->m_canDeleteRecursive;
337}
338
339KProtocolInfo::FileNameUsedForCopying KProtocolManager::fileNameUsedForCopying(const QUrl &url)
340{
341 KProtocolInfoPrivate *prot = findProtocol(url);
342 if (!prot) {
343 return KProtocolInfo::FromUrl;
344 }
345
346 return prot->m_fileNameUsedForCopying;
347}
348
350{
351 KProtocolInfoPrivate *prot = findProtocol(url);
352 if (!prot) {
353 return QString();
354 }
355
356 return prot->m_defaultMimetype;
357}
358
360{
361 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
362 QMutexLocker lock(&d->mutex);
363 if (d->protocolForArchiveMimetypes.isEmpty()) {
364 const QList<KProtocolInfoPrivate *> allProtocols = KProtocolInfoFactory::self()->allProtocols();
365 for (KProtocolInfoPrivate *allProtocol : allProtocols) {
366 const QStringList archiveMimetypes = allProtocol->m_archiveMimeTypes;
367 for (const QString &mime : archiveMimetypes) {
368 d->protocolForArchiveMimetypes.insert(mime, allProtocol->m_name);
369 }
370 }
371 }
372 return d->protocolForArchiveMimetypes.value(mimeType);
373}
374
376{
377 return KIO::WorkerConfig::self()->configData(url.scheme(), url.host(), QStringLiteral("Charset"));
378}
379
381{
382 KProtocolInfoPrivate *prot = findProtocol(url);
383 if (!prot) {
384 return true;
385 }
386
387 return prot->m_supportsPermissions;
388}
389
390#undef PRIVATE_DATA
391
392#include "moc_kprotocolmanager_p.cpp"
QString readEntry(const char *key, const char *aDefault=nullptr) const
void reset()
Undo any changes made by calls to setConfigData.
MetaData configData(const QString &protocol, const QString &host)
Query worker configuration for workers of type protocol when dealing with host.
Type
Describes the type of a protocol.
@ T_NONE
no information about the type available
static int proxyConnectTimeout()
Returns the preferred timeout value for proxy connections in seconds.
static bool supportsOpening(const QUrl &url)
Returns whether the protocol can be opened using KIO::open(const QUrl&).
static KProtocolInfo::Type outputType(const QUrl &url)
Returns whether the protocol should be treated as a filesystem or as a stream when writing to it.
static bool supportsPermissions(const QUrl &url)
Returns whether the protocol suppports KIO/POSIX permissions handling.
static QString charsetFor(const QUrl &url)
Returns the charset to use for the specified url.
static QStringList listing(const QUrl &url)
Returns the list of fields this protocol returns when listing The current possibilities are Name,...
static bool canRenameToFile(const QUrl &url)
Returns whether the protocol can rename (i.e.
static bool supportsReading(const QUrl &url)
Returns whether the protocol can retrieve data from URLs.
static bool canCopyFromFile(const QUrl &url)
Returns whether the protocol can copy files/objects directly from the filesystem itself.
static QString defaultMimetype(const QUrl &url)
Returns default MIME type for this URL based on the protocol.
static bool canRenameFromFile(const QUrl &url)
Returns whether the protocol can rename (i.e.
static KProtocolInfo::Type inputType(const QUrl &url)
Returns whether the protocol should be treated as a filesystem or as a stream when reading from it.
static bool supportsListing(const QUrl &url)
Returns whether the protocol can list files/objects.
static bool supportsDeleting(const QUrl &url)
Returns whether the protocol can delete files/objects.
static bool supportsLinking(const QUrl &url)
Returns whether the protocol can create links between files/objects.
static KProtocolInfo::FileNameUsedForCopying fileNameUsedForCopying(const QUrl &url)
This setting defines the strategy to use for generating a filename, when copying a file or directory ...
static QString protocolForArchiveMimetype(const QString &mimeType)
Returns which protocol handles this MIME type, if it's an archive MIME type.
static bool supportsTruncating(const QUrl &url)
Returns whether the protocol can be truncated with FileJob::truncate(KIO::filesize_t length).
static int connectTimeout()
Returns the preferred timeout value for remote connections in seconds.
static bool autoResume()
Returns true if partial downloads should be automatically resumed.
static bool markPartial()
Returns true if partial downloads should be marked with a ".part" extension.
static bool isSourceProtocol(const QUrl &url)
Returns whether the protocol can act as a source protocol.
static bool supportsMoving(const QUrl &url)
Returns whether the protocol can move files/objects between different locations.
static bool canCopyToFile(const QUrl &url)
Returns whether the protocol can copy files/objects directly to the filesystem itself.
static int minimumKeepSize()
Returns the minimum file size for keeping aborted downloads.
static bool supportsMakeDir(const QUrl &url)
Returns whether the protocol can create directories/folders.
static bool canDeleteRecursive(const QUrl &url)
Returns whether the protocol can recursively delete directories by itself.
static void reparseConfiguration()
Force a reload of the general config file of KIO workers ( kioslaverc).
static bool supportsWriting(const QUrl &url)
Returns whether the protocol can store data to URLs.
static int responseTimeout()
Returns the preferred response timeout value for remote connecting in seconds.
static int readTimeout()
Returns the preferred timeout value for reading from remote connections in seconds.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString host(ComponentFormattingOptions options) const const
bool isValid() const const
QString scheme() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 11:56:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.