File indexing completed on 2024-07-14 14:34:05

0001 /*
0002     SPDX-FileCopyrightText: 2015 Montel Laurent <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kurifiltersearchprovideractions.h"
0008 #include <KDialogJobUiDelegate>
0009 #include <KIO/CommandLauncherJob>
0010 #include <KLocalizedString>
0011 #include <KStringHandler>
0012 #include <QActionGroup>
0013 #include <QDesktopServices>
0014 #include <QIcon>
0015 #include <QMenu>
0016 #include <QStandardPaths>
0017 #include <kurifilter.h>
0018 
0019 using namespace KIO;
0020 
0021 class KIO::WebShortcutsMenuManagerPrivate
0022 {
0023 public:
0024     WebShortcutsMenuManagerPrivate()
0025     {
0026     }
0027 
0028     QString mSelectedText;
0029 };
0030 
0031 KUriFilterSearchProviderActions::KUriFilterSearchProviderActions(QObject *parent)
0032     : QObject(parent)
0033     , d(new KIO::WebShortcutsMenuManagerPrivate)
0034 {
0035 }
0036 
0037 KUriFilterSearchProviderActions::~KUriFilterSearchProviderActions() = default;
0038 
0039 QString KUriFilterSearchProviderActions::selectedText() const
0040 {
0041     return d->mSelectedText;
0042 }
0043 
0044 void KUriFilterSearchProviderActions::setSelectedText(const QString &selectedText)
0045 {
0046     d->mSelectedText = selectedText;
0047 }
0048 
0049 void KUriFilterSearchProviderActions::slotConfigureWebShortcuts()
0050 {
0051     auto *job = new KIO::CommandLauncherJob(QStringLiteral("kcmshell5"), {QStringLiteral("webshortcuts")});
0052     job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
0053     job->start();
0054 }
0055 
0056 void KUriFilterSearchProviderActions::addWebShortcutsToMenu(QMenu *menu)
0057 {
0058     if (d->mSelectedText.isEmpty()) {
0059         return;
0060     }
0061 
0062     const QString searchText = d->mSelectedText.simplified();
0063 
0064     if (searchText.isEmpty()) {
0065         return;
0066     }
0067 
0068     KUriFilterData filterData(searchText);
0069 
0070     filterData.setSearchFilteringOptions(KUriFilterData::RetrievePreferredSearchProvidersOnly);
0071 
0072     if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::NormalTextFilter)) {
0073         const QStringList searchProviders = filterData.preferredSearchProviders();
0074 
0075         if (!searchProviders.isEmpty()) {
0076             QMenu *webShortcutsMenu = new QMenu(menu);
0077             webShortcutsMenu->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-shortcuts")));
0078 
0079             const QString squeezedText = KStringHandler::rsqueeze(searchText, 21);
0080             webShortcutsMenu->setTitle(i18n("Search for '%1' with", squeezedText));
0081 
0082             QActionGroup *actionGroup = new QActionGroup(this);
0083             connect(actionGroup, &QActionGroup::triggered, this, &KUriFilterSearchProviderActions::slotHandleWebShortcutAction);
0084             for (const QString &searchProvider : searchProviders) {
0085                 QAction *action = new QAction(i18nc("@action:inmenu Search for <text> with", "%1", searchProvider), webShortcutsMenu);
0086                 action->setIcon(QIcon::fromTheme(filterData.iconNameForPreferredSearchProvider(searchProvider)));
0087                 action->setData(filterData.queryForPreferredSearchProvider(searchProvider));
0088                 webShortcutsMenu->addAction(action);
0089                 actionGroup->addAction(action);
0090             }
0091 
0092             if (!QStandardPaths::findExecutable(QStringLiteral("kcmshell5")).isEmpty()) {
0093                 webShortcutsMenu->addSeparator();
0094                 QAction *action = new QAction(i18n("Configure Web Shortcuts..."), webShortcutsMenu);
0095                 action->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
0096                 connect(action, &QAction::triggered, this, &KUriFilterSearchProviderActions::slotConfigureWebShortcuts);
0097                 webShortcutsMenu->addAction(action);
0098             }
0099 
0100             menu->addMenu(webShortcutsMenu);
0101         }
0102     }
0103 }
0104 
0105 void KUriFilterSearchProviderActions::slotHandleWebShortcutAction(QAction *action)
0106 {
0107     KUriFilterData filterData(action->data().toString());
0108     if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter)) {
0109         QDesktopServices::openUrl(filterData.uri());
0110     }
0111 }
0112 
0113 #include "moc_kurifiltersearchprovideractions.cpp"