KIO

kuriikwsfilter.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
4 SPDX-FileCopyrightText: 2000 Yves Arrouye <yves@realnames.com>
5 SPDX-FileCopyrightText: 2002, 2003 Dawit Alemayehu <adawit@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include "kuriikwsfilter.h"
11#include "kuriikwsfiltereng_p.h"
12#include "searchprovider.h"
13
14#include <KLocalizedString>
15#include <KPluginFactory>
16
17#include <QLoggingCategory>
18
19using namespace KIO;
20namespace
21{
22Q_LOGGING_CATEGORY(category, "kf.kio.urifilters.ikws", QtWarningMsg)
23}
24
25K_PLUGIN_CLASS_WITH_JSON(KAutoWebSearch, "kuriikwsfilter.json")
26
27void KAutoWebSearch::populateProvidersList(QList<KUriFilterSearchProvider *> &searchProviders, const KUriFilterData &data, bool allproviders) const
28{
30 KURISearchFilterEngine *filter = KURISearchFilterEngine::self();
31
32 if (allproviders) {
33 providers = filter->registry()->findAll();
34 } else {
35 // Start with the search engines marked as preferred...
36 QStringList favEngines = filter->favoriteEngineList();
37 if (favEngines.isEmpty()) {
38 favEngines = data.alternateSearchProviders();
39 }
40
41 // Get rid of duplicates...
42 favEngines.removeDuplicates();
43
44 // Sort the items...
45 std::stable_sort(favEngines.begin(), favEngines.end());
46
47 // Add the search engine set as the default provider...
48 const QString defaultEngine = filter->defaultSearchEngine();
49 if (!defaultEngine.isEmpty()) {
50 favEngines.removeAll(defaultEngine);
51 favEngines.insert(0, defaultEngine);
52 }
53
54 QStringListIterator it(favEngines);
55 while (it.hasNext()) {
56 SearchProvider *favProvider = filter->registry()->findByDesktopName(it.next());
57 if (favProvider) {
58 providers << favProvider;
59 }
60 }
61 }
62
63 for (SearchProvider *p : std::as_const(providers)) {
64 searchProviders << p;
65 }
66}
67
68bool KAutoWebSearch::filterUri(KUriFilterData &data) const
69{
70 qCDebug(category) << data.typedString();
71
73
74 // Handle the flag to retrieve only preferred providers, no filtering...
75 if (option & KUriFilterData::RetrievePreferredSearchProvidersOnly) {
77 populateProvidersList(searchProviders, data);
78 if (searchProviders.isEmpty()) {
79 if (!(option & KUriFilterData::RetrieveSearchProvidersOnly)) {
80 setUriType(data, KUriFilterData::Error);
81 setErrorMsg(data, i18n("No preferred search providers were found."));
82 return false;
83 }
84 } else {
85 setSearchProvider(data, nullptr, data.typedString(), QLatin1Char(KURISearchFilterEngine::self()->keywordDelimiter()));
86 setSearchProviders(data, searchProviders);
87 return true;
88 }
89 }
90
91 if (option & KUriFilterData::RetrieveSearchProvidersOnly) {
93 populateProvidersList(searchProviders, data, true);
94 if (searchProviders.isEmpty()) {
95 setUriType(data, KUriFilterData::Error);
96 setErrorMsg(data, i18n("No search providers were found."));
97 return false;
98 }
99
100 setSearchProvider(data, nullptr, data.typedString(), QLatin1Char(KURISearchFilterEngine::self()->keywordDelimiter()));
101 setSearchProviders(data, searchProviders);
102 return true;
103 }
104
105 if (data.uriType() == KUriFilterData::Unknown && data.uri().password().isEmpty()) {
106 KURISearchFilterEngine *filter = KURISearchFilterEngine::self();
107 SearchProvider *provider = filter->autoWebSearchQuery(data.typedString(), data.alternateDefaultSearchProvider());
108 if (provider) {
109 const QUrl result = filter->formatResult(provider->query(), provider->charset(), QString(), data.typedString(), true);
110 setFilteredUri(data, result);
111 setUriType(data, KUriFilterData::NetProtocol);
112 setSearchProvider(data, provider, data.typedString(), QLatin1Char(filter->keywordDelimiter()));
113
114 QList<KUriFilterSearchProvider *> searchProviders;
115 populateProvidersList(searchProviders, data);
116 setSearchProviders(data, searchProviders);
117 return true;
118 }
119 }
120 return false;
121}
122
123#include "kuriikwsfilter.moc"
124#include "moc_kuriikwsfilter.cpp"
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
This class is a basic messaging class used to exchange filtering information between the filter plugi...
Definition kurifilter.h:153
QUrl uri() const
Returns the filtered or the original URL.
SearchFilterOptions searchFilteringOptions() const
Returns the specified search filter options.
QString typedString() const
The string as typed by the user, before any URL processing is done.
@ Error
An incorrect URI (ex: "~johndoe" when user johndoe does not exist in that system)
Definition kurifilter.h:166
@ NetProtocol
Any network protocol: http, ftp, nttp, pop3, etc...
Definition kurifilter.h:159
@ Unknown
A URI that is not identified. Default value when a KUriFilterData is first created.
Definition kurifilter.h:167
UriTypes uriType() const
Returns the URI type.
QString alternateDefaultSearchProvider() const
Returns the search provider to use when a default provider is not available.
Class that holds information about a search provider.
Definition kurifilter.h:39
QString i18n(const char *text, const TYPE &arg...)
A namespace for KIO globals.
QCA_EXPORT ProviderList providers()
iterator begin()
iterator end()
iterator insert(const_iterator before, parameter_type value)
bool isEmpty() const const
qsizetype removeAll(const AT &t)
bool isEmpty() const const
qsizetype removeDuplicates()
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QString password(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.