KIO

kurifiltersearchprovideractions.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Montel Laurent <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kurifiltersearchprovideractions.h"
8#include <KDialogJobUiDelegate>
9#include <KIO/CommandLauncherJob>
10#include <KLocalizedString>
11#include <KStringHandler>
12#include <QActionGroup>
13#include <QDesktopServices>
14#include <QIcon>
15#include <QMenu>
16#include <QStandardPaths>
17#include <kurifilter.h>
18
19using namespace KIO;
20
21class KIO::WebShortcutsMenuManagerPrivate
22{
23public:
24 WebShortcutsMenuManagerPrivate()
25 {
26 }
27
28 QString mSelectedText;
29};
30
32 : QObject(parent)
33 , d(new KIO::WebShortcutsMenuManagerPrivate)
34{
35}
36
37KUriFilterSearchProviderActions::~KUriFilterSearchProviderActions() = default;
38
40{
41 return d->mSelectedText;
42}
43
45{
46 d->mSelectedText = selectedText;
47}
48
49void KUriFilterSearchProviderActions::slotConfigureWebShortcuts()
50{
51 auto *job = new KIO::CommandLauncherJob(QStringLiteral("kcmshell6"), {QStringLiteral("webshortcuts")});
53 job->start();
54}
55
57{
58 if (d->mSelectedText.isEmpty()) {
59 return;
60 }
61
62 const QString searchText = d->mSelectedText.simplified();
63
64 if (searchText.isEmpty()) {
65 return;
66 }
67
68 KUriFilterData filterData(searchText);
69
70 filterData.setSearchFilteringOptions(KUriFilterData::RetrievePreferredSearchProvidersOnly);
71
72 if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::NormalTextFilter)) {
73 const QStringList searchProviders = filterData.preferredSearchProviders();
74
75 if (!searchProviders.isEmpty()) {
76 QMenu *webShortcutsMenu = new QMenu(menu);
77 webShortcutsMenu->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-shortcuts")));
78
79 const QString squeezedText = KStringHandler::rsqueeze(searchText, 21);
80 webShortcutsMenu->setTitle(i18n("Search for '%1' with", squeezedText));
81
82 QActionGroup *actionGroup = new QActionGroup(this);
83 connect(actionGroup, &QActionGroup::triggered, this, &KUriFilterSearchProviderActions::slotHandleWebShortcutAction);
84 for (const QString &searchProvider : searchProviders) {
85 QAction *action = new QAction(i18nc("@action:inmenu Search for <text> with", "%1", searchProvider), webShortcutsMenu);
86 action->setIcon(QIcon::fromTheme(filterData.iconNameForPreferredSearchProvider(searchProvider)));
87 action->setData(filterData.queryForPreferredSearchProvider(searchProvider));
88 webShortcutsMenu->addAction(action);
89 actionGroup->addAction(action);
90 }
91
92 if (!QStandardPaths::findExecutable(QStringLiteral("kcmshell6")).isEmpty()) {
93 webShortcutsMenu->addSeparator();
94 QAction *action = new QAction(i18nc("@action:inmenu", "Configure Web Shortcuts…"), webShortcutsMenu);
95 action->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
96 connect(action, &QAction::triggered, this, &KUriFilterSearchProviderActions::slotConfigureWebShortcuts);
97 webShortcutsMenu->addAction(action);
98 }
99
100 menu->addMenu(webShortcutsMenu);
101 }
102 }
103}
104
105void KUriFilterSearchProviderActions::slotHandleWebShortcutAction(QAction *action)
106{
107 KUriFilterData filterData(action->data().toString());
108 if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter)) {
109 QDesktopServices::openUrl(filterData.uri());
110 }
111}
112
113#include "moc_kurifiltersearchprovideractions.cpp"
CommandLauncherJob runs a command and watches it while running.
void addWebShortcutsToMenu(QMenu *menu)
addWebShortcutsToMenu Manage to add web shortcut actions to existing menu.
KUriFilterSearchProviderActions(QObject *parent=nullptr)
Constructs a webshorts menu manager.
QString selectedText() const
return the selected text
void setSelectedText(const QString &selectedText)
Set selected text.
void setUiDelegate(KJobUiDelegate *delegate)
This class is a basic messaging class used to exchange filtering information between the filter plugi...
Definition kurifilter.h:153
void setSearchFilteringOptions(SearchFilterOptions options)
Sets the options used by search filter plugins to filter requests.
QString iconNameForPreferredSearchProvider(const QString &provider) const
Returns the icon associated with the given preferred search provider.
QStringList preferredSearchProviders() const
Returns a list of the names of preferred or available search providers.
QString queryForPreferredSearchProvider(const QString &provider) const
Returns the web shortcut url for the given preferred search provider.
static KUriFilter * self()
Returns an instance of KUriFilter.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
A namespace for KIO globals.
KCOREADDONS_EXPORT QString rsqueeze(const QString &str, int maxlen=40)
QVariant data() const const
void setIcon(const QIcon &icon)
void setData(const QVariant &data)
void triggered(bool checked)
QAction * addAction(QAction *action)
bool openUrl(const QUrl &url)
QIcon fromTheme(const QString &name)
bool isEmpty() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addMenu(QMenu *menu)
QAction * addSeparator()
void setIcon(const QIcon &icon)
void setTitle(const QString &title)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString findExecutable(const QString &executableName, const QStringList &paths)
bool isEmpty() const const
QString toString() 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.