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
31KUriFilterSearchProviderActions::KUriFilterSearchProviderActions(QObject *parent)
32 : QObject(parent)
33 , d(new KIO::WebShortcutsMenuManagerPrivate)
34{
35}
36
37KUriFilterSearchProviderActions::~KUriFilterSearchProviderActions() = default;
38
39QString KUriFilterSearchProviderActions::selectedText() const
40{
41 return d->mSelectedText;
42}
43
44void KUriFilterSearchProviderActions::setSelectedText(const QString &selectedText)
45{
46 d->mSelectedText = selectedText;
47}
48
49void KUriFilterSearchProviderActions::slotConfigureWebShortcuts()
50{
51 auto *job = new KIO::CommandLauncherJob(QStringLiteral("kcmshell6"), {QStringLiteral("webshortcuts")});
52 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
53 job->start();
54}
55
56void KUriFilterSearchProviderActions::addWebShortcutsToMenu(QMenu *menu)
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(data&: filterData, types: 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(str: searchText, maxlen: 21);
80 webShortcutsMenu->setTitle(i18n("Search for '%1' with", squeezedText));
81
82 QActionGroup *actionGroup = new QActionGroup(this);
83 connect(sender: actionGroup, signal: &QActionGroup::triggered, context: this, slot: &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(name: filterData.iconNameForPreferredSearchProvider(provider: searchProvider)));
87 action->setData(filterData.queryForPreferredSearchProvider(provider: searchProvider));
88 webShortcutsMenu->addAction(action);
89 actionGroup->addAction(a: action);
90 }
91
92 if (!QStandardPaths::findExecutable(QStringLiteral("kcmshell6")).isEmpty()) {
93 webShortcutsMenu->addSeparator();
94 QAction *action = new QAction(i18n("Configure Web Shortcuts..."), webShortcutsMenu);
95 action->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
96 connect(sender: action, signal: &QAction::triggered, context: this, slot: &KUriFilterSearchProviderActions::slotConfigureWebShortcuts);
97 webShortcutsMenu->addAction(action);
98 }
99
100 menu->addMenu(menu: webShortcutsMenu);
101 }
102 }
103}
104
105void KUriFilterSearchProviderActions::slotHandleWebShortcutAction(QAction *action)
106{
107 KUriFilterData filterData(action->data().toString());
108 if (KUriFilter::self()->filterSearchUri(data&: filterData, types: KUriFilter::WebShortcutFilter)) {
109 QDesktopServices::openUrl(url: filterData.uri());
110 }
111}
112
113#include "moc_kurifiltersearchprovideractions.cpp"
114

source code of kio/src/widgets/kurifiltersearchprovideractions.cpp