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 <QDBusConnection> |
18 | #include <QLoggingCategory> |
19 | |
20 | using namespace KIO; |
21 | namespace |
22 | { |
23 | Q_LOGGING_CATEGORY(category, "kf.kio.urifilters.ikws" , QtWarningMsg) |
24 | } |
25 | |
26 | K_PLUGIN_CLASS_WITH_JSON(KAutoWebSearch, "kuriikwsfilter.json" ) |
27 | |
28 | void KAutoWebSearch::populateProvidersList(QList<KUriFilterSearchProvider *> &searchProviders, const KUriFilterData &data, bool allproviders) const |
29 | { |
30 | QList<SearchProvider *> providers; |
31 | KURISearchFilterEngine *filter = KURISearchFilterEngine::self(); |
32 | |
33 | if (allproviders) { |
34 | providers = filter->registry()->findAll(); |
35 | } else { |
36 | // Start with the search engines marked as preferred... |
37 | QStringList favEngines = filter->favoriteEngineList(); |
38 | if (favEngines.isEmpty()) { |
39 | favEngines = data.alternateSearchProviders(); |
40 | } |
41 | |
42 | // Get rid of duplicates... |
43 | favEngines.removeDuplicates(); |
44 | |
45 | // Sort the items... |
46 | std::stable_sort(first: favEngines.begin(), last: favEngines.end()); |
47 | |
48 | // Add the search engine set as the default provider... |
49 | const QString defaultEngine = filter->defaultSearchEngine(); |
50 | if (!defaultEngine.isEmpty()) { |
51 | favEngines.removeAll(t: defaultEngine); |
52 | favEngines.insert(i: 0, t: defaultEngine); |
53 | } |
54 | |
55 | QStringListIterator it(favEngines); |
56 | while (it.hasNext()) { |
57 | SearchProvider *favProvider = filter->registry()->findByDesktopName(desktopName: it.next()); |
58 | if (favProvider) { |
59 | providers << favProvider; |
60 | } |
61 | } |
62 | } |
63 | |
64 | for (SearchProvider *p : std::as_const(t&: providers)) { |
65 | searchProviders << p; |
66 | } |
67 | } |
68 | |
69 | bool KAutoWebSearch::filterUri(KUriFilterData &data) const |
70 | { |
71 | qCDebug(category) << data.typedString(); |
72 | |
73 | KUriFilterData::SearchFilterOptions option = data.searchFilteringOptions(); |
74 | |
75 | // Handle the flag to retrieve only preferred providers, no filtering... |
76 | if (option & KUriFilterData::RetrievePreferredSearchProvidersOnly) { |
77 | QList<KUriFilterSearchProvider *> searchProviders; |
78 | populateProvidersList(searchProviders, data); |
79 | if (searchProviders.isEmpty()) { |
80 | if (!(option & KUriFilterData::RetrieveSearchProvidersOnly)) { |
81 | setUriType(data, type: KUriFilterData::Error); |
82 | setErrorMsg(data, i18n("No preferred search providers were found." )); |
83 | return false; |
84 | } |
85 | } else { |
86 | setSearchProvider(data, provider: nullptr, term: data.typedString(), separator: QLatin1Char(KURISearchFilterEngine::self()->keywordDelimiter())); |
87 | setSearchProviders(data, providers: searchProviders); |
88 | return true; |
89 | } |
90 | } |
91 | |
92 | if (option & KUriFilterData::RetrieveSearchProvidersOnly) { |
93 | QList<KUriFilterSearchProvider *> searchProviders; |
94 | populateProvidersList(searchProviders, data, allproviders: true); |
95 | if (searchProviders.isEmpty()) { |
96 | setUriType(data, type: KUriFilterData::Error); |
97 | setErrorMsg(data, i18n("No search providers were found." )); |
98 | return false; |
99 | } |
100 | |
101 | setSearchProvider(data, provider: nullptr, term: data.typedString(), separator: QLatin1Char(KURISearchFilterEngine::self()->keywordDelimiter())); |
102 | setSearchProviders(data, providers: searchProviders); |
103 | return true; |
104 | } |
105 | |
106 | if (data.uriType() == KUriFilterData::Unknown && data.uri().password().isEmpty()) { |
107 | KURISearchFilterEngine *filter = KURISearchFilterEngine::self(); |
108 | SearchProvider *provider = filter->autoWebSearchQuery(typedString: data.typedString(), defaultShortcut: data.alternateDefaultSearchProvider()); |
109 | if (provider) { |
110 | const QUrl result = filter->formatResult(url: provider->query(), cset1: provider->charset(), cset2: QString(), query: data.typedString(), isMalformed: true); |
111 | setFilteredUri(data, uri: result); |
112 | setUriType(data, type: KUriFilterData::NetProtocol); |
113 | setSearchProvider(data, provider, term: data.typedString(), separator: QLatin1Char(filter->keywordDelimiter())); |
114 | |
115 | QList<KUriFilterSearchProvider *> searchProviders; |
116 | populateProvidersList(searchProviders, data); |
117 | setSearchProviders(data, providers: searchProviders); |
118 | return true; |
119 | } |
120 | } |
121 | return false; |
122 | } |
123 | |
124 | #include "kuriikwsfilter.moc" |
125 | #include "moc_kuriikwsfilter.cpp" |
126 | |