File indexing completed on 2024-05-12 11:54:37

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0004     SPDX-FileCopyrightText: 2000 Yves Arrouye <yves@realnames.com>
0005     SPDX-FileCopyrightText: 2002, 2003 Dawit Alemayehu <adawit@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kurisearchfilter.h"
0011 #include "ikwsopts.h"
0012 #include "kuriikwsfiltereng.h"
0013 #include "searchprovider.h"
0014 
0015 #include <KLocalizedString>
0016 #include <KPluginFactory>
0017 
0018 #include <QDBusConnection>
0019 #include <QLoggingCategory>
0020 
0021 /**
0022  * IMPORTANT: If you change anything here, make sure you run the kurifiltertest
0023  * regression test (this should be included as part of "make test").
0024  */
0025 
0026 K_PLUGIN_FACTORY_WITH_JSON(KUriSearchFilterFactory, "kurisearchfilter.json", registerPlugin<KUriSearchFilter>(); registerPlugin<FilterOptions>();)
0027 
0028 namespace
0029 {
0030 Q_LOGGING_CATEGORY(category, "kf.kio.urifilters.ikws", QtWarningMsg)
0031 }
0032 
0033 KUriSearchFilter::KUriSearchFilter(QObject *parent, const QVariantList &)
0034     : KUriFilterPlugin(QStringLiteral("kurisearchfilter"), parent)
0035 {
0036     QDBusConnection::sessionBus()
0037         .connect(QString(), QStringLiteral("/"), QStringLiteral("org.kde.KUriFilterPlugin"), QStringLiteral("configure"), this, SLOT(configure()));
0038 }
0039 
0040 KUriSearchFilter::~KUriSearchFilter()
0041 {
0042 }
0043 
0044 void KUriSearchFilter::configure()
0045 {
0046     qCDebug(category) << "Config reload requested...";
0047     KURISearchFilterEngine::self()->loadConfig();
0048 }
0049 
0050 bool KUriSearchFilter::filterUri(KUriFilterData &data) const
0051 {
0052     qCDebug(category) << data.typedString() << ":" << data.uri() << ", type =" << data.uriType();
0053 
0054     // some URLs like gg:www.kde.org are not accepted by QUrl, but we still want them
0055     // This means we also have to allow KUriFilterData::Error
0056     if (data.uriType() != KUriFilterData::Unknown && data.uriType() != KUriFilterData::Error) {
0057         return false;
0058     }
0059 
0060     QString searchTerm;
0061     KURISearchFilterEngine *filter = KURISearchFilterEngine::self();
0062     SearchProvider *provider(filter->webShortcutQuery(data.typedString(), searchTerm));
0063     if (!provider) {
0064         return false;
0065     }
0066 
0067     const QUrl result = filter->formatResult(provider->query(), provider->charset(), QString(), searchTerm, true);
0068     setFilteredUri(data, result);
0069     setUriType(data, KUriFilterData::NetProtocol);
0070     setSearchProvider(data, provider->name(), searchTerm, QLatin1Char(filter->keywordDelimiter()));
0071     return true;
0072 }
0073 
0074 // TODO KF6: unused, remove
0075 KCModule *KUriSearchFilter::configModule(QWidget *, const char *) const
0076 {
0077     return nullptr;
0078 }
0079 
0080 // TODO KF6: unused, remove
0081 QString KUriSearchFilter::configName() const
0082 {
0083     return i18n("Search F&ilters");
0084 }
0085 
0086 #include "kurisearchfilter.moc"
0087 
0088 #include "moc_kurisearchfilter.cpp"