KUriFilter Class

Header: #include <KUriFilter>
CMake: find_package(KF6 REQUIRED COMPONENTS KIO)
target_link_libraries(mytarget PRIVATE KIOGui)

Public Types

enum SearchFilterType { NormalTextFilter, WebShortcutFilter }
flags SearchFilterTypes

Public Functions

~KUriFilter()
bool filterSearchUri(KUriFilterData &data, KUriFilter::SearchFilterTypes types)
bool filterUri(KUriFilterData &data, const QStringList &filters = QStringList())
bool filterUri(QString &uri, const QStringList &filters = QStringList())
bool filterUri(QUrl &uri, const QStringList &filters = QStringList())
QString filteredUri(const QString &uri, const QStringList &filters = QStringList())
QUrl filteredUri(const QUrl &uri, const QStringList &filters = QStringList())
QStringList pluginNames() const

Static Public Members

KUriFilter *self()

Protected Functions

Detailed Description

@class KUriFilter kurifilter.h <KUriFilter>

KUriFilter applies a number of filters to a URI and returns a filtered version if any filter matches. A simple example is "kde.org" to "http://www.kde.org", which is commonplace in web browsers.

The filters are implemented as plugins in @ref KUriFilterPlugin subclasses.

KUriFilter is a singleton object: obtain the instance by calling @p KUriFilter::self() and use the public member functions to perform the filtering.

Example

To simply filter a given string:

QString url("kde.org");
bool filtered = KUriFilter::self()->filteredUri( url );

You can alternatively use a QUrl:

QUrl url("kde.org");
bool filtered = KUriFilter::self()->filterUri( url );

If you have a constant string or a constant URL, simply invoke the corresponding function to obtain the filtered string or URL instead of a boolean flag:

QString filteredText = KUriFilter::self()->filteredUri( "kde.org" );

All of the above examples should result in "kde.org" being filtered into "http://kde.org".

You can also restrict the filters to be used by supplying the name of the filters you want to use. By default all available filters are used.

To use specific filters, add the names of the filters you want to use to a QStringList and invoke the appropriate filtering function.

The examples below show the use of specific filters. KDE ships with the following filter plugins by default:

kshorturifilter: This is used for filtering potentially valid url inputs such as "kde.org" Additionally it filters shell variables and shortcuts such as $HOME and ~ as well as man and info page shortcuts, # and ## respectively.

kuriikwsfilter: This is used for filtering normal input text into a web search url using the configured fallback search engine selected by the user.

kurisearchfilter: This is used for filtering KDE webshortcuts. For example "gg:KDE" will be converted to a url for searching the work "KDE" using the Google search engine.

localdomainfilter: This is used for doing a DNS lookup to determine whether the input is a valid local address.

fixuphosturifilter: This is used to append "www." to the host name of a pre filtered http url if the original url cannot be resolved.

QString text ("kde.org");
bool filtered = KUriFilter::self()->filterUri(text, QLatin1String("kshorturifilter"));

The above code should result in "kde.org" being filtered into "http://kde.org".

QStringList list;
list << QLatin1String("kshorturifilter") << QLatin1String("localdomainfilter");
bool filtered = KUriFilter::self()->filterUri( text, list );

Additionally if you only want to do search related filtering, you can use the search specific function, @ref filterSearchUri, that is available in KDE 4.5 and higher. For example, to search for a given input on the web you can do the following:

KUriFilterData filterData ("foo"); bool filtered = KUriFilter::self()->filterSearchUri(filterData, KUriFilterData::NormalTextFilter);

KUriFilter converts all filtering requests to use @ref KUriFilterData internally. The use of this bi-directional class allows you to send specific instructions to the filter plugins as well as receive detailed information about the filtered request from them. See the documentation of KUriFilterData class for more examples and details.

All functions in this class are thread safe and reentrant.

@short Filters the given input into a valid url whenever possible.

Member Type Documentation

enum KUriFilter::SearchFilterType
flags KUriFilter::SearchFilterTypes

This enum describes the types of search plugin filters available.

@li NormalTextFilter The plugin used to filter normal text, e.g. "some term to search". @li WebShortcutFilter The plugin used to filter web shortcuts, e.g. gg:KDE.

@see SearchFilterTypes

The SearchFilterTypes type is a typedef for QFlags<SearchFilterType>. It stores an OR combination of SearchFilterType values.

Member Function Documentation

[protected] KUriFilter::KUriFilter()

Constructor.

Creates a KUriFilter object and calls loads all available URI filter plugins.

[noexcept] KUriFilter::~KUriFilter()

Destructor

bool KUriFilter::filterSearchUri(KUriFilterData &data, KUriFilter::SearchFilterTypes types)

Filter @p data using the criteria specified by @p types.

The search filter type can be individual value of @ref SearchFilterTypes or a combination of those types using the bitwise OR operator.

You can also use the flags from @ref KUriFilterData::SearchFilterOption to alter the filtering mechanisms of the search filter providers.

@param data object that contains the URI to be filtered. @param types the search filters used to filter the request. @return true if the specified @p data was successfully filtered.

@see KUriFilterData::setSearchFilteringOptions

bool KUriFilter::filterUri(KUriFilterData &data, const QStringList &filters = QStringList())

Filters @p data using the specified @p filters.

If no named filters are specified, the default, then all the URI filter plugins found will be used.

@param data object that contains the URI to be filtered. @param filters specify the list of filters to be used.

@return a boolean indicating whether the URI has been changed

bool KUriFilter::filterUri(QString &uri, const QStringList &filters = QStringList())

Filters a string representing a URI.

The given URL is filtered based on the specified list of filters. If the list is empty all available filters would be used.

@param uri The URI to filter. @param filters specify the list of filters to be used.

@return a boolean indicating whether the URI has been changed

bool KUriFilter::filterUri(QUrl &uri, const QStringList &filters = QStringList())

Filters the URI given by the URL.

The given URL is filtered based on the specified list of filters. If the list is empty all available filters would be used.

@param uri the URI to filter. @param filters specify the list of filters to be used.

@return a boolean indicating whether the URI has been changed

QString KUriFilter::filteredUri(const QString &uri, const QStringList &filters = QStringList())

Return a filtered string representation of a URI.

The given URL is filtered based on the specified list of filters. If the list is empty all available filters would be used.

@param uri the URI to filter. @param filters specify the list of filters to be used.

@return the filtered URI or null if it cannot be filtered

QUrl KUriFilter::filteredUri(const QUrl &uri, const QStringList &filters = QStringList())

Returns the filtered URI.

The given URL is filtered based on the specified list of filters. If the list is empty all available filters would be used.

@param uri The URI to filter. @param filters specify the list of filters to be used.

@return the filtered URI or null if it cannot be filtered

QStringList KUriFilter::pluginNames() const

Return a list of the names of all loaded plugins.

@return a QStringList of plugin names

[static] KUriFilter *KUriFilter::self()

Returns an instance of KUriFilter.