KIO

kurifilter.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Yves Arrouye <yves@realnames.com>
4 SPDX-FileCopyrightText: 2000, 2010 Dawit Alemayehu <adawit at kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kurifilter.h"
10#include "kurifilterdata_p.h"
11
12#include <KService>
13#include <kio/global.h>
14
15#include <KPluginFactory>
16#include <KPluginMetaData>
17
18#include <QHashIterator>
19#include <QHostAddress>
20#include <QHostInfo>
21#include <QIcon>
22
23#include "kurifilterplugin_p.h"
24
25QString KUriFilterDataPrivate::lookupIconNameFor(const QUrl &url, KUriFilterData::UriTypes type)
26{
27 QString iconName;
28
29 switch (type) {
31 iconName = KIO::iconNameForUrl(url);
32 break;
34 QString exeName = url.path();
35 exeName.remove(0, exeName.lastIndexOf(QLatin1Char('/')) + 1); // strip path if given
37 if (service && service->icon() != QLatin1String("unknown")) {
38 iconName = service->icon();
39 }
40 // Try to find an icon with the same name as the binary (useful for non-kde apps)
41 else if (!QIcon::fromTheme(exeName).isNull()) {
42 iconName = exeName;
43 } else {
44 // not found, use default
45 iconName = QStringLiteral("system-run");
46 }
47 break;
48 }
50 iconName = QStringLiteral("khelpcenter");
51 break;
52 }
54 iconName = QStringLiteral("konsole");
55 break;
56 }
59 iconName = QStringLiteral("error");
60 break;
61 }
62 default:
63 break;
64 }
65
66 return iconName;
67}
68
69class Q_DECL_HIDDEN KUriFilterSearchProvider::KUriFilterSearchProviderPrivate
70{
71public:
72 KUriFilterSearchProviderPrivate()
73 {
74 }
75 KUriFilterSearchProviderPrivate(const KUriFilterSearchProviderPrivate &other)
76 : desktopEntryName(other.desktopEntryName)
77 , iconName(other.iconName)
78 , name(other.name)
79 , keys(other.keys)
80 {
81 }
82
83 QString desktopEntryName;
84 QString iconName;
86 QStringList keys;
87};
88
90 : d(new KUriFilterSearchProvider::KUriFilterSearchProviderPrivate)
91{
92}
93
95 : d(new KUriFilterSearchProvider::KUriFilterSearchProviderPrivate(*(other.d)))
96{
97}
98
100
102{
103 return d->desktopEntryName;
104}
105
107{
108 return d->iconName;
109}
110
112{
113 return d->name;
114}
115
117{
118 return d->keys;
119}
120
122{
123 if (d->keys.isEmpty()) {
124 return QString();
125 }
126
127 return d->keys.first();
128}
129
131{
132 d->desktopEntryName = other.d->desktopEntryName;
133 d->iconName = other.d->iconName;
134 d->keys = other.d->keys;
135 d->name = other.d->name;
136 return *this;
137}
138
139void KUriFilterSearchProvider::setDesktopEntryName(const QString &desktopEntryName)
140{
141 d->desktopEntryName = desktopEntryName;
142}
143
144void KUriFilterSearchProvider::setIconName(const QString &iconName)
145{
146 d->iconName = iconName;
147}
148
149void KUriFilterSearchProvider::setName(const QString &name)
150{
151 d->name = name;
152}
153
154void KUriFilterSearchProvider::setKeys(const QStringList &keys)
155{
156 d->keys = keys;
157}
158
160 : d(new KUriFilterDataPrivate(QUrl(), QString()))
161{
162}
163
165 : d(new KUriFilterDataPrivate(url, url.toString()))
166{
167}
168
170 : d(new KUriFilterDataPrivate(QUrl::fromUserInput(url), url))
171{
172}
173
175 : d(new KUriFilterDataPrivate(*other.d))
176{
177}
178
180
182{
183 return d->url;
184}
185
187{
188 return d->errMsg;
189}
190
192{
193 return d->uriType;
194}
195
197{
198 return d->absPath;
199}
200
202{
203 return !d->absPath.isEmpty();
204}
205
207{
208 return d->args;
209}
210
212{
213 return !d->args.isEmpty();
214}
215
217{
218 return d->checkForExecs;
219}
220
222{
223 return d->typedString;
224}
225
227{
228 return d->searchTerm;
229}
230
232{
233 return d->searchTermSeparator;
234}
235
237{
238 return d->searchProvider;
239}
240
242{
243 return d->searchProviderList;
244}
245
247{
248 const KUriFilterSearchProvider *searchProvider = d->searchProviderMap.value(provider);
249
250 if (searchProvider) {
251 return *(searchProvider);
252 }
253
255}
256
258{
259 const KUriFilterSearchProvider *searchProvider = d->searchProviderMap.value(provider);
260 if (searchProvider) {
261 return (searchProvider->defaultKey() % searchTermSeparator() % searchTerm());
262 }
263 return QString();
264}
265
267{
268 const KUriFilterSearchProvider *searchProvider = d->searchProviderMap.value(provider);
269 if (searchProvider) {
270 return searchProvider->keys();
271 }
272 return QStringList();
273}
274
276{
277 const KUriFilterSearchProvider *searchProvider = d->searchProviderMap.value(provider);
278 if (searchProvider) {
279 return searchProvider->iconName();
280 }
281 return QString();
282}
283
285{
286 return d->alternateSearchProviders;
287}
288
290{
291 return d->alternateDefaultSearchProvider;
292}
293
295{
296 return d->defaultUrlScheme;
297}
298
300{
301 return d->searchFilterOptions;
302}
303
305{
306 auto foundProvider = d->searchProviderMap.constFind(searchProvider());
307 if (foundProvider != d->searchProviderMap.cend() && !foundProvider.value()->iconName().isEmpty()) {
308 return foundProvider.value()->iconName();
309 }
310
311 if (d->wasModified) {
312 d->iconName = KUriFilterDataPrivate::lookupIconNameFor(d->url, d->uriType);
313 d->wasModified = false;
314 }
315
316 return d->iconName;
317}
318
320{
321 d->setData(url, url.toString());
322}
323
325{
326 d->setData(QUrl(url), url);
327}
328
330{
331 // Since a malformed URL could possibly be a relative
332 // URL we tag it as a possible local resource...
333 if ((d->url.scheme().isEmpty() || d->url.isLocalFile())) {
334 d->absPath = absPath;
335 return true;
336 }
337 return false;
338}
339
341{
342 d->checkForExecs = check;
343}
344
346{
347 d->alternateSearchProviders = providers;
348}
349
351{
352 d->alternateDefaultSearchProvider = provider;
353}
354
356{
357 d->defaultUrlScheme = scheme;
358}
359
361{
362 d->searchFilterOptions = options;
363}
364
366{
367 d->setData(url, url.toString());
368 return *this;
369}
370
372{
373 d->setData(QUrl(url), url);
374 return *this;
375}
376
377/******************************* KUriFilter ******************************/
378
379class KUriFilterPrivate
380{
381public:
382 KUriFilterPrivate()
383 {
384 }
385 ~KUriFilterPrivate()
386 {
387 qDeleteAll(pluginList);
388 pluginList.clear();
389 }
390 QList<KUriFilterPlugin *> pluginList;
391};
392
393class KUriFilterSingleton
394{
395public:
396 KUriFilter instance;
397};
398
399Q_GLOBAL_STATIC(KUriFilterSingleton, m_self)
400
402{
403 return &m_self()->instance;
404}
405
407 : d(new KUriFilterPrivate())
408{
409 QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("kf6/urifilters"));
410 const QString prefKey = QStringLiteral("X-KDE-InitialPreference");
411 // Sort the plugins by order of priority
412 std::sort(plugins.begin(), plugins.end(), [prefKey](const KPluginMetaData &a, const KPluginMetaData &b) {
413 return a.value(prefKey, 0) > b.value(prefKey, 0);
414 });
415
416 for (const KPluginMetaData &pluginMetaData : std::as_const(plugins)) {
417 if (auto plugin = KPluginFactory::instantiatePlugin<KUriFilterPlugin>(pluginMetaData).plugin) {
418 d->pluginList << plugin;
419 }
420 }
421}
422
423KUriFilter::~KUriFilter() = default;
424
426{
427 bool filtered = false;
428
429 for (KUriFilterPlugin *plugin : std::as_const(d->pluginList)) {
430 // If no specific filters were requested, iterate through all the plugins.
431 // Otherwise, only use available filters.
432 if (filters.isEmpty() || filters.contains(plugin->objectName())) {
433 if (plugin->filterUri(data)) {
434 filtered = true;
435 }
436 }
437 }
438
439 return filtered;
440}
441
442bool KUriFilter::filterUri(QUrl &uri, const QStringList &filters)
443{
444 KUriFilterData data(uri);
445 bool filtered = filterUri(data, filters);
446 if (filtered) {
447 uri = data.uri();
448 }
449 return filtered;
450}
451
452bool KUriFilter::filterUri(QString &uri, const QStringList &filters)
453{
454 KUriFilterData data(uri);
455 bool filtered = filterUri(data, filters);
456 if (filtered) {
457 uri = data.uri().toString();
458 }
459 return filtered;
460}
461
462QUrl KUriFilter::filteredUri(const QUrl &uri, const QStringList &filters)
463{
464 KUriFilterData data(uri);
465 filterUri(data, filters);
466 return data.uri();
467}
468
470{
471 KUriFilterData data(uri);
472 filterUri(data, filters);
473 return data.uri().toString();
474}
475
477{
478 QStringList filters;
479
480 if (types & WebShortcutFilter) {
481 filters << QStringLiteral("kurisearchfilter");
482 }
483
484 if (types & NormalTextFilter) {
485 filters << QStringLiteral("kuriikwsfilter");
486 }
487
488 return filterUri(data, filters);
489}
490
492{
493 QStringList res;
494 res.reserve(d->pluginList.size());
495 std::transform(d->pluginList.constBegin(), d->pluginList.constEnd(), std::back_inserter(res), [](const KUriFilterPlugin *plugin) {
496 return plugin->objectName();
497 });
498 return res;
499}
static QList< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={}, KPluginMetaDataOptions options={})
static Ptr serviceByDesktopName(const QString &_name)
QString icon() const
This class is a basic messaging class used to exchange filtering information between the filter plugi...
Definition kurifilter.h:153
QStringList allQueriesForSearchProvider(const QString &provider) const
Returns all the query urls for the given search provider.
QString absolutePath() const
Returns the absolute path if one has already been set.
KUriFilterData()
Default constructor.
KUriFilterSearchProvider queryForSearchProvider(const QString &provider) const
Returns information about provider.
void setSearchFilteringOptions(SearchFilterOptions options)
Sets the options used by search filter plugins to filter requests.
bool setAbsolutePath(const QString &abs_path)
Sets the absolute path to be used whenever the supplied data is a relative local URL.
QString errorMsg() const
Returns an error message.
QString iconNameForPreferredSearchProvider(const QString &provider) const
Returns the icon associated with the given preferred search provider.
void setDefaultUrlScheme(const QString &)
Sets the default scheme used when filtering potentially valid url inputs.
QString argsAndOptions() const
Returns the command line options and arguments for a local resource when present.
QString iconName()
The name of the icon that matches the current filtered URL.
bool hasAbsolutePath() const
Checks whether the supplied data had an absolute path.
QUrl uri() const
Returns the filtered or the original URL.
SearchFilterOptions searchFilteringOptions() const
Returns the specified search filter options.
QStringList preferredSearchProviders() const
Returns a list of the names of preferred or available search providers.
QStringList alternateSearchProviders() const
Returns the list of alternate search providers.
bool checkForExecutables() const
QString searchTerm() const
Returns the search term portion of the typed string.
QString typedString() const
The string as typed by the user, before any URL processing is done.
UriTypes
Describes the type of the URI that was filtered.
Definition kurifilter.h:158
@ Error
An incorrect URI (ex: "~johndoe" when user johndoe does not exist in that system)
Definition kurifilter.h:166
@ Blocked
A URI that should be blocked/filtered (ex: ad filtering)
Definition kurifilter.h:165
@ NetProtocol
Any network protocol: http, ftp, nttp, pop3, etc...
Definition kurifilter.h:159
@ Shell
A shell executable (ex: echo "Test..." >> ~/testfile)
Definition kurifilter.h:164
@ Executable
A local file whose executable flag is set.
Definition kurifilter.h:162
@ Help
A man or info page.
Definition kurifilter.h:163
QString defaultUrlScheme() const
Returns the default protocol to use when filtering potentially valid url inputs.
UriTypes uriType() const
Returns the URI type.
void setAlternateDefaultSearchProvider(const QString &provider)
Sets the search provider to use in case no default provider is available.
QString queryForPreferredSearchProvider(const QString &provider) const
Returns the web shortcut url for the given preferred search provider.
KUriFilterData & operator=(const QUrl &url)
Overloaded assignment operator.
QString searchProvider() const
Returns the name of the search service provider, e.g. Google.
void setCheckForExecutables(bool check)
Check whether the provided uri is executable or not.
bool hasArgsAndOptions() const
Checks whether the current data is a local resource with command line options and arguments.
~KUriFilterData()
Destructor.
void setData(const QUrl &url)
Same as above except the argument is a URL.
QString alternateDefaultSearchProvider() const
Returns the search provider to use when a default provider is not available.
void setAlternateSearchProviders(const QStringList &providers)
Sets a list of search providers to use in case no preferred search providers are available.
QChar searchTermSeparator() const
Returns the character that is used to separate the search term from the keyword.
Class that holds information about a search provider.
Definition kurifilter.h:39
QStringList keys() const
Returns all the web shortcut keys associated with this search provider.
KUriFilterSearchProvider & operator=(const KUriFilterSearchProvider &)
Assignment operator.
virtual QString iconName() const
Returns the icon name associated with the search provider when available.
virtual ~KUriFilterSearchProvider()
Destructor.
QString defaultKey() const
Returns the default web shortcut key for this search provider.
QString name() const
Returns the descriptive name of the search provider, e.g. "Google News".
KUriFilterSearchProvider()
Default constructor.
QString desktopEntryName() const
Returns the desktop filename of the search provider without any extension.
KUriFilter applies a number of filters to a URI and returns a filtered version if any filter matches.
Definition kurifilter.h:701
bool filterSearchUri(KUriFilterData &data, SearchFilterTypes types)
Filter data using the criteria specified by types.
QUrl filteredUri(const QUrl &uri, const QStringList &filters=QStringList())
Returns the filtered URI.
KUriFilter()
Constructor.
~KUriFilter()
Destructor.
QStringList pluginNames() const
Return a list of the names of all loaded plugins.
bool filterUri(KUriFilterData &data, const QStringList &filters=QStringList())
Filters data using the specified filters.
KIOCORE_EXPORT QString iconNameForUrl(const QUrl &url)
Return the icon name for a URL.
Definition global.cpp:186
QString name(StandardShortcut id)
QIcon fromTheme(const QString &name)
iterator begin()
void clear()
iterator end()
bool isEmpty() const const
void reserve(qsizetype size)
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QString path(ComponentFormattingOptions options) const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.