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
88#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
90{
91 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
92 QMutexLocker lock(&d->mutex);
93 KConfigGroup cg(config(), QString());
94 int val = cg.readEntry("ReadTimeout", DEFAULT_READ_TIMEOUT);
95 return qMax(MIN_TIMEOUT_VALUE, val);
96}
97#endif
98
99#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
101{
102 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
103 QMutexLocker lock(&d->mutex);
104 KConfigGroup cg(config(), QString());
105 int val = cg.readEntry("ConnectTimeout", DEFAULT_CONNECT_TIMEOUT);
106 return qMax(MIN_TIMEOUT_VALUE, val);
107}
108#endif
109
110#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
112{
113 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
114 QMutexLocker lock(&d->mutex);
115 KConfigGroup cg(config(), QString());
116 int val = cg.readEntry("ProxyConnectTimeout", DEFAULT_PROXY_CONNECT_TIMEOUT);
117 return qMax(MIN_TIMEOUT_VALUE, val);
118}
119#endif
120
121#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 11)
123{
124 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
125 QMutexLocker lock(&d->mutex);
126 KConfigGroup cg(config(), QString());
127 int val = cg.readEntry("ResponseTimeout", DEFAULT_RESPONSE_TIMEOUT);
128 return qMax(MIN_TIMEOUT_VALUE, val);
129}
130#endif
131
132/*==================================== OTHERS ===============================*/
133
135{
136 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
137 QMutexLocker lock(&d->mutex);
138 return config()->group(QString()).readEntry("MarkPartial", true);
139}
140
142{
143 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
144 QMutexLocker lock(&d->mutex);
145 return config()->group(QString()).readEntry("MinimumKeepSize",
146 DEFAULT_MINIMUM_KEEP_SIZE); // 5000 byte
147}
148
150{
151 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
152 QMutexLocker lock(&d->mutex);
153 return config()->group(QString()).readEntry("AutoResume", false);
154}
155
156/* =========================== PROTOCOL CAPABILITIES ============== */
157
158static KProtocolInfoPrivate *findProtocol(const QUrl &url)
159{
160 if (!url.isValid()) {
161 return nullptr;
162 }
163 QString protocol = url.scheme();
164 return KProtocolInfoFactory::self()->findProtocol(protocol);
165}
166
168{
169 KProtocolInfoPrivate *prot = findProtocol(url);
170 if (!prot) {
172 }
173
174 return prot->m_inputType;
175}
176
178{
179 KProtocolInfoPrivate *prot = findProtocol(url);
180 if (!prot) {
182 }
183
184 return prot->m_outputType;
185}
186
188{
189 KProtocolInfoPrivate *prot = findProtocol(url);
190 if (!prot) {
191 return false;
192 }
193
194 return prot->m_isSourceProtocol;
195}
196
198{
199 KProtocolInfoPrivate *prot = findProtocol(url);
200 if (!prot) {
201 return false;
202 }
203
204 return prot->m_supportsListing;
205}
206
208{
209 KProtocolInfoPrivate *prot = findProtocol(url);
210 if (!prot) {
211 return QStringList();
212 }
213
214 return prot->m_listing;
215}
216
218{
219 KProtocolInfoPrivate *prot = findProtocol(url);
220 if (!prot) {
221 return false;
222 }
223
224 return prot->m_supportsReading;
225}
226
228{
229 KProtocolInfoPrivate *prot = findProtocol(url);
230 if (!prot) {
231 return false;
232 }
233
234 return prot->m_supportsWriting;
235}
236
238{
239 KProtocolInfoPrivate *prot = findProtocol(url);
240 if (!prot) {
241 return false;
242 }
243
244 return prot->m_supportsMakeDir;
245}
246
248{
249 KProtocolInfoPrivate *prot = findProtocol(url);
250 if (!prot) {
251 return false;
252 }
253
254 return prot->m_supportsDeleting;
255}
256
258{
259 KProtocolInfoPrivate *prot = findProtocol(url);
260 if (!prot) {
261 return false;
262 }
263
264 return prot->m_supportsLinking;
265}
266
268{
269 KProtocolInfoPrivate *prot = findProtocol(url);
270 if (!prot) {
271 return false;
272 }
273
274 return prot->m_supportsMoving;
275}
276
278{
279 KProtocolInfoPrivate *prot = findProtocol(url);
280 if (!prot) {
281 return false;
282 }
283
284 return prot->m_supportsOpening;
285}
286
288{
289 KProtocolInfoPrivate *prot = findProtocol(url);
290 if (!prot) {
291 return false;
292 }
293
294 return prot->m_supportsTruncating;
295}
296
298{
299 KProtocolInfoPrivate *prot = findProtocol(url);
300 if (!prot) {
301 return false;
302 }
303
304 return prot->m_canCopyFromFile;
305}
306
308{
309 KProtocolInfoPrivate *prot = findProtocol(url);
310 if (!prot) {
311 return false;
312 }
313
314 return prot->m_canCopyToFile;
315}
316
318{
319 KProtocolInfoPrivate *prot = findProtocol(url);
320 if (!prot) {
321 return false;
322 }
323
324 return prot->m_canRenameFromFile;
325}
326
328{
329 KProtocolInfoPrivate *prot = findProtocol(url);
330 if (!prot) {
331 return false;
332 }
333
334 return prot->m_canRenameToFile;
335}
336
338{
339 KProtocolInfoPrivate *prot = findProtocol(url);
340 if (!prot) {
341 return false;
342 }
343
344 return prot->m_canDeleteRecursive;
345}
346
347KProtocolInfo::FileNameUsedForCopying KProtocolManager::fileNameUsedForCopying(const QUrl &url)
348{
349 KProtocolInfoPrivate *prot = findProtocol(url);
350 if (!prot) {
351 return KProtocolInfo::FromUrl;
352 }
353
354 return prot->m_fileNameUsedForCopying;
355}
356
358{
359 KProtocolInfoPrivate *prot = findProtocol(url);
360 if (!prot) {
361 return QString();
362 }
363
364 return prot->m_defaultMimetype;
365}
366
368{
369 KProtocolManagerPrivate *d = kProtocolManagerPrivate();
370 QMutexLocker lock(&d->mutex);
371 if (d->protocolForArchiveMimetypes.isEmpty()) {
372 const QList<KProtocolInfoPrivate *> allProtocols = KProtocolInfoFactory::self()->allProtocols();
373 for (KProtocolInfoPrivate *allProtocol : allProtocols) {
374 const QStringList archiveMimetypes = allProtocol->m_archiveMimeTypes;
375 for (const QString &mime : archiveMimetypes) {
376 d->protocolForArchiveMimetypes.insert(mime, allProtocol->m_name);
377 }
378 }
379 }
380 return d->protocolForArchiveMimetypes.value(mimeType);
381}
382
384{
385 return KIO::WorkerConfig::self()->configData(url.scheme(), url.host(), QStringLiteral("Charset"));
386}
387
389{
390 KProtocolInfoPrivate *prot = findProtocol(url);
391 if (!prot) {
392 return true;
393 }
394
395 return prot->m_supportsPermissions;
396}
397
398#undef PRIVATE_DATA
399
400#include "moc_kprotocolmanager_p.cpp"
QString readEntry(const char *key, const char *aDefault=nullptr) const
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-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:36 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.