KIO

kprotocolinfo.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1999 Torben Weis <[email protected]>
4  SPDX-FileCopyrightText: 2003 Waldo Bastian <[email protected]>
5  SPDX-FileCopyrightText: 2012 David Faure <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-only
8 */
9 
10 #include "kprotocolinfo.h"
11 #include "kprotocolinfo_p.h"
12 #include "kprotocolinfofactory_p.h"
13 
14 #include <KConfig>
15 #include <KSharedConfig>
16 #include <KConfigGroup>
17 #include <QUrl>
18 #include <KPluginMetaData>
19 
20 //
21 // Internal functions:
22 //
23 KProtocolInfoPrivate::KProtocolInfoPrivate(const QString &path)
24 {
25  KConfig sconfig(path, KConfig::SimpleConfig);
26  KConfigGroup config(&sconfig, "Protocol");
27 
28  m_name = config.readEntry("protocol");
29  m_exec = config.readPathEntry("exec", QString());
30  m_isSourceProtocol = config.readEntry("source", true);
31  m_isHelperProtocol = config.readEntry("helper", false);
32  m_supportsReading = config.readEntry("reading", false);
33  m_supportsWriting = config.readEntry("writing", false);
34  m_supportsMakeDir = config.readEntry("makedir", false);
35  m_supportsDeleting = config.readEntry("deleting", false);
36  m_supportsLinking = config.readEntry("linking", false);
37  m_supportsMoving = config.readEntry("moving", false);
38  m_supportsOpening = config.readEntry("opening", false);
39  m_supportsTruncating = config.readEntry("truncating", false);
40  m_canCopyFromFile = config.readEntry("copyFromFile", false);
41  m_canCopyToFile = config.readEntry("copyToFile", false);
42  m_canRenameFromFile = config.readEntry("renameFromFile", false);
43  m_canRenameToFile = config.readEntry("renameToFile", false);
44  m_canDeleteRecursive = config.readEntry("deleteRecursive", false);
45  const QString fnu = config.readEntry("fileNameUsedForCopying", "FromURL");
46  m_fileNameUsedForCopying = KProtocolInfo::FromUrl;
47  if (fnu == QLatin1String("Name")) {
48  m_fileNameUsedForCopying = KProtocolInfo::Name;
49  } else if (fnu == QLatin1String("DisplayName")) {
50  m_fileNameUsedForCopying = KProtocolInfo::DisplayName;
51  }
52 
53  m_listing = config.readEntry("listing", QStringList());
54  // Many .protocol files say "Listing=false" when they really mean "Listing=" (i.e. unsupported)
55  if (m_listing.count() == 1 && m_listing.first() == QLatin1String("false")) {
56  m_listing.clear();
57  }
58  m_supportsListing = (m_listing.count() > 0);
59  m_defaultMimetype = config.readEntry("defaultMimetype");
60  m_determineMimetypeFromExtension = config.readEntry("determineMimetypeFromExtension", true);
61  m_archiveMimeTypes = config.readEntry("archiveMimetype", QStringList());
62  m_icon = config.readEntry("Icon");
63  m_config = config.readEntry("config", m_name);
64  m_maxSlaves = config.readEntry("maxInstances", 1);
65  m_maxSlavesPerHost = config.readEntry("maxInstancesPerHost", 0);
66 
67  QString tmp = config.readEntry("input");
68  if (tmp == QLatin1String("filesystem")) {
69  m_inputType = KProtocolInfo::T_FILESYSTEM;
70  } else if (tmp == QLatin1String("stream")) {
71  m_inputType = KProtocolInfo::T_STREAM;
72  } else {
73  m_inputType = KProtocolInfo::T_NONE;
74  }
75 
76  tmp = config.readEntry("output");
77  if (tmp == QLatin1String("filesystem")) {
78  m_outputType = KProtocolInfo::T_FILESYSTEM;
79  } else if (tmp == QLatin1String("stream")) {
80  m_outputType = KProtocolInfo::T_STREAM;
81  } else {
82  m_outputType = KProtocolInfo::T_NONE;
83  }
84 
85  m_docPath = config.readPathEntry("X-DocPath", QString());
86  if (m_docPath.isEmpty()) {
87  m_docPath = config.readPathEntry("DocPath", QString());
88  }
89  m_protClass = config.readEntry("Class").toLower();
90  if (!m_protClass.startsWith(QLatin1Char(':'))) {
91  m_protClass.prepend(QLatin1Char(':'));
92  }
93 
94  const QStringList extraNames = config.readEntry("ExtraNames", QStringList());
95  const QStringList extraTypes = config.readEntry("ExtraTypes", QStringList());
96  QStringList::const_iterator it = extraNames.begin();
97  QStringList::const_iterator typeit = extraTypes.begin();
98  for (; it != extraNames.end() && typeit != extraTypes.end(); ++it, ++typeit) {
99  QVariant::Type type = QVariant::nameToType((*typeit).toLatin1().constData());
100  // currently QVariant::Type and ExtraField::Type use the same subset of values, so we can just cast.
101  m_extraFields.append(KProtocolInfo::ExtraField(*it, static_cast<KProtocolInfo::ExtraField::Type>(type)));
102  }
103 
104  m_showPreviews = config.readEntry("ShowPreviews", m_protClass == QLatin1String(":local"));
105 
106  m_capabilities = config.readEntry("Capabilities", QStringList());
107 
108  m_slaveHandlesNotify = config.readEntry("slaveHandlesNotify", QStringList());
109 
110  m_proxyProtocol = config.readEntry("ProxiedBy");
111 }
112 
113 KProtocolInfoPrivate::KProtocolInfoPrivate(const QString &name, const QString &exec, const QJsonObject &json)
114  : m_name(name)
115  , m_exec(exec)
116 {
117  // source has fallback true if not set
118  m_isSourceProtocol = json.value(QStringLiteral("source")).toBool(true);
119 
120  // other bools are fine with default false by toBool
121  m_isHelperProtocol = json.value(QStringLiteral("helper")).toBool();
122  m_supportsReading = json.value(QStringLiteral("reading")).toBool();
123  m_supportsWriting = json.value(QStringLiteral("writing")).toBool();
124  m_supportsMakeDir = json.value(QStringLiteral("makedir")).toBool();
125  m_supportsDeleting = json.value(QStringLiteral("deleting")).toBool();
126  m_supportsLinking = json.value(QStringLiteral("linking")).toBool();
127  m_supportsMoving = json.value(QStringLiteral("moving")).toBool();
128  m_supportsOpening = json.value(QStringLiteral("opening")).toBool();
129  m_supportsTruncating = json.value(QStringLiteral("truncating")).toBool();
130  m_canCopyFromFile = json.value(QStringLiteral("copyFromFile")).toBool();
131  m_canCopyToFile = json.value(QStringLiteral("copyToFile")).toBool();
132  m_canRenameFromFile = json.value(QStringLiteral("renameFromFile")).toBool();
133  m_canRenameToFile = json.value(QStringLiteral("renameToFile")).toBool();
134  m_canDeleteRecursive = json.value(QStringLiteral("deleteRecursive")).toBool();
135 
136  // default is "FromURL"
137  const QString fnu = json.value(QStringLiteral("fileNameUsedForCopying")).toString();
138  m_fileNameUsedForCopying = KProtocolInfo::FromUrl;
139  if (fnu == QLatin1String("Name")) {
140  m_fileNameUsedForCopying = KProtocolInfo::Name;
141  } else if (fnu == QLatin1String("DisplayName")) {
142  m_fileNameUsedForCopying = KProtocolInfo::DisplayName;
143  }
144 
145  m_listing = json.value(QStringLiteral("listing")).toVariant().toStringList();
146  // Many .protocol files say "Listing=false" when they really mean "Listing=" (i.e. unsupported)
147  if (m_listing.count() == 1 && m_listing.first() == QLatin1String("false")) {
148  m_listing.clear();
149  }
150  m_supportsListing = (m_listing.count() > 0);
151 
152  m_defaultMimetype = json.value(QStringLiteral("defaultMimetype")).toString();
153 
154  // determineMimetypeFromExtension has fallback true if not set
155  m_determineMimetypeFromExtension = json.value(QStringLiteral("determineMimetypeFromExtension")).toBool(true);
156 
157  m_archiveMimeTypes = json.value(QStringLiteral("archiveMimetype")).toVariant().toStringList();
158 
159  m_icon = json.value(QStringLiteral("Icon")).toString();
160 
161  // config has fallback to name if not set
162  m_config = json.value(QStringLiteral("config")).toString(m_name);
163 
164  // max slaves has fallback to 1 if not set
165  m_maxSlaves = json.value(QStringLiteral("maxInstances")).toInt(1);
166 
167  m_maxSlavesPerHost = json.value(QStringLiteral("maxInstancesPerHost")).toInt();
168 
169  QString tmp = json.value(QStringLiteral("input")).toString();
170  if (tmp == QLatin1String("filesystem")) {
171  m_inputType = KProtocolInfo::T_FILESYSTEM;
172  } else if (tmp == QLatin1String("stream")) {
173  m_inputType = KProtocolInfo::T_STREAM;
174  } else {
175  m_inputType = KProtocolInfo::T_NONE;
176  }
177 
178  tmp = json.value(QStringLiteral("output")).toString();
179  if (tmp == QLatin1String("filesystem")) {
180  m_outputType = KProtocolInfo::T_FILESYSTEM;
181  } else if (tmp == QLatin1String("stream")) {
182  m_outputType = KProtocolInfo::T_STREAM;
183  } else {
184  m_outputType = KProtocolInfo::T_NONE;
185  }
186 
187  m_docPath = json.value(QStringLiteral("X-DocPath")).toString();
188  if (m_docPath.isEmpty()) {
189  m_docPath = json.value(QStringLiteral("DocPath")).toString();
190  }
191 
192  m_protClass = json.value(QStringLiteral("Class")).toString().toLower();
193  if (m_protClass[0] != QLatin1Char(':')) {
194  m_protClass.prepend(QLatin1Char(':'));
195  }
196 
197  // ExtraNames is a translated value, use the KCoreAddons helper to read it
198  const QStringList extraNames = KPluginMetaData::readTranslatedValue(json, QStringLiteral("ExtraNames")).toVariant().toStringList();
199 
200  const QStringList extraTypes = json.value(QStringLiteral("ExtraTypes")).toVariant().toStringList();
201  QStringList::const_iterator it = extraNames.begin();
202  QStringList::const_iterator typeit = extraTypes.begin();
203  for (; it != extraNames.end() && typeit != extraTypes.end(); ++it, ++typeit) {
204  QVariant::Type type = QVariant::nameToType((*typeit).toLatin1().constData());
205  // currently QVariant::Type and ExtraField::Type use the same subset of values, so we can just cast.
206  m_extraFields.append(KProtocolInfo::ExtraField(*it, static_cast<KProtocolInfo::ExtraField::Type>(type)));
207  }
208 
209  // fallback based on class
210  m_showPreviews = json.value(QStringLiteral("ShowPreviews")).toBool(m_protClass == QLatin1String(":local"));
211 
212  m_capabilities = json.value(QStringLiteral("Capabilities")).toVariant().toStringList();
213 
214  m_slaveHandlesNotify = json.value(QStringLiteral("slaveHandlesNotify")).toVariant().toStringList();
215 
216  m_proxyProtocol = json.value(QStringLiteral("ProxiedBy")).toString();
217 }
218 
219 //
220 // Static functions:
221 //
222 
224 {
225  return KProtocolInfoFactory::self()->protocols();
226 }
227 
229 {
230  // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings.
231  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
232  if (!prot) {
233  return false;
234  }
235 
236  return !prot->m_isSourceProtocol;
237 }
238 
240 {
241  // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings.
242  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
243  if (!prot) {
244  return QString();
245  }
246 
247  return prot->m_icon;
248 }
249 
251 {
252  // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings.
253  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
254  if (!prot) {
255  return QString();
256  }
257 
258  return QStringLiteral("kio_%1rc").arg(prot->m_config);
259 }
260 
261 int KProtocolInfo::maxSlaves(const QString &_protocol)
262 {
263  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
264  if (!prot) {
265  return 1;
266  }
267 
268  return prot->m_maxSlaves;
269 }
270 
272 {
273  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
274  if (!prot) {
275  return 0;
276  }
277 
278  return prot->m_maxSlavesPerHost;
279 }
280 
282 {
283  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
284  if (!prot) {
285  return true;
286  }
287 
288  return prot->m_determineMimetypeFromExtension;
289 }
290 
292 {
293  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol);
294  if (!prot) {
295  return QString();
296  }
297  return prot->m_exec;
298 }
299 
301 {
302  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(url.scheme());
303  if (!prot) {
304  return ExtraFieldList();
305  }
306 
307  return prot->m_extraFields;
308 }
309 
311 {
312  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
313  if (!prot) {
314  return QString();
315  }
316 
317  return prot->m_defaultMimetype;
318 }
319 
321 {
322  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
323  if (!prot) {
324  return QString();
325  }
326 
327  return prot->m_docPath;
328 }
329 
331 {
332  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
333  if (!prot) {
334  return QString();
335  }
336 
337  return prot->m_protClass;
338 }
339 
341 {
342  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
343  const bool defaultSetting = prot ? prot->m_showPreviews : false;
344 
345  KConfigGroup group(KSharedConfig::openConfig(), "PreviewSettings");
346  return group.readEntry(_protocol, defaultSetting);
347 }
348 
350 {
351  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
352  if (!prot) {
353  return QStringList();
354  }
355 
356  return prot->m_capabilities;
357 }
358 
360 {
361  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol);
362  if (!prot) {
363  return QStringList();
364  }
365 
366  return prot->m_archiveMimeTypes;
367 }
368 
370 {
371  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
372  if (!prot) {
373  return QStringList();
374  }
375 
376  return prot->m_slaveHandlesNotify;
377 }
378 
380 {
381  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(_protocol);
382  if (!prot) {
383  return QString();
384  }
385 
386  return prot->m_proxyProtocol;
387 }
388 
390 {
391  return isFilterProtocol(url.scheme());
392 }
393 
395 {
396  return isHelperProtocol(url.scheme());
397 }
398 
400 {
401  // We call the findProtocol directly (not via KProtocolManager) to bypass any proxy settings.
402  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol);
403  if (prot) {
404  return prot->m_isHelperProtocol;
405  }
406  return false;
407 }
408 
410 {
411  return isKnownProtocol(url.scheme());
412 }
413 
415 {
416  // We call the findProtocol (const QString&) to bypass any proxy settings.
417  KProtocolInfoPrivate *prot = KProtocolInfoFactory::self()->findProtocol(protocol);
418  return prot;
419 }
no information about the type available
Definition: kprotocolinfo.h:83
static QJsonValue readTranslatedValue(const QJsonObject &jo, const QString &key, const QJsonValue &defaultValue=QJsonValue())
static bool isFilterProtocol(const QUrl &url)
Returns whether the protocol can act as a filter protocol.
void clear()
static bool determineMimetypeFromExtension(const QString &protocol)
Returns whether mimetypes can be determined based on extension for this protocol. ...
static int maxSlaves(const QString &protocol)
Returns the soft limit on the number of slaves for this protocol.
Definition of an extra field in the UDS entries, returned by a listDir operation. ...
Definition: kprotocolinfo.h:95
structured directory
Definition: kprotocolinfo.h:82
static QString config(const QString &protocol)
Returns the name of the config file associated with the specified protocol.
QString & prepend(QChar ch)
static QStringList protocols()
Returns list of all known protocols.
static QStringList capabilities(const QString &protocol)
Returns the list of capabilities provided by the kioslave implementing this protocol.
static QString docPath(const QString &protocol)
Returns the documentation path for the specified protocol.
static int maxSlavesPerHost(const QString &protocol)
Returns the limit on the number of slaves for this protocol per host.
KSharedConfigPtr config()
int toInt(int defaultValue) const const
QString toString() const const
static QString icon(const QString &protocol)
Returns the name of the icon, associated with the specified protocol.
Type type(const QSqlDatabase &db)
QVariant::Type nameToType(const char *name)
bool toBool(bool defaultValue) const const
static QString proxiedBy(const QString &protocol)
Returns the name of the protocol through which the request will be routed if proxy support is enabled...
static QString protocolClass(const QString &protocol)
Returns the protocol class for the specified protocol.
QString scheme() const const
QVariant toVariant() const const
stream of data (e.g. single file)
Definition: kprotocolinfo.h:81
static ExtraFieldList extraFields(const QUrl &url)
Definition of extra fields in the UDS entries, returned by a listDir operation.
static QStringList archiveMimetypes(const QString &protocol)
Returns the list of archive mimetypes handled by the kioslave implementing this protocol.
QList::iterator end()
QString toLower() const const
static bool showFilePreview(const QString &protocol)
Returns whether file previews should be shown for the specified protocol.
static QString exec(const QString &protocol)
Returns the library / executable to open for the protocol protocol Example : "kio_ftp", meaning either the executable "kio_ftp" or the library "kio_ftp.la" (recommended), whichever is available.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QStringList toStringList() const const
static bool isHelperProtocol(const QUrl &url)
Returns whether the protocol can act as a helper protocol.
static QStringList slaveHandlesNotify(const QString &protocol)
Returns the list of notification types the kioslave implementing this protocol will produce on its ow...
QJsonValue value(const QString &key) const const
static bool isKnownProtocol(const QUrl &url)
Returns whether a protocol is installed that is able to handle url.
T readEntry(const QString &key, const T &aDefault) const
QList::iterator begin()
static QString defaultMimetype(const QString &protocol)
Returns the default mimetype for the specified protocol, if one exists.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:59:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.