From 6443792eb946033542ad5ea26cbb2212be162c81 Mon Sep 17 00:00:00 2001 From: Alexander Lohnau Date: Wed, 17 Jun 2020 06:39:57 +0000 Subject: [PATCH] Add action to launch webshortcut in private/incognito window FEATURE: 390009 --- runners/webshortcuts/webshortcutrunner.cpp | 45 +++++++++++++++++++++- runners/webshortcuts/webshortcutrunner.h | 4 ++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/runners/webshortcuts/webshortcutrunner.cpp b/runners/webshortcuts/webshortcutrunner.cpp index 0917edb33..3f0f2e605 100644 --- a/runners/webshortcuts/webshortcutrunner.cpp +++ b/runners/webshortcuts/webshortcutrunner.cpp @@ -22,6 +22,9 @@ #include #include #include +#include +#include +#include WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args) : Plasma::AbstractRunner(parent, args), @@ -38,6 +41,7 @@ WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args) sessionDbus.connect(QString(), QStringLiteral("/"), QStringLiteral("org.kde.KUriFilterPlugin"), QStringLiteral("configure"), this, SLOT(loadSyntaxes())); loadSyntaxes(); + configurePrivateBrowsingActions(); } WebshortcutRunner::~WebshortcutRunner() @@ -66,6 +70,34 @@ void WebshortcutRunner::loadSyntaxes() m_lastKey.clear(); } +void WebshortcutRunner::configurePrivateBrowsingActions() +{ + clearActions(); + const QString browserFile = KSharedConfig::openConfig(QStringLiteral("kdeglobals"))->group("General").readEntry("BrowserApplication"); + KService::Ptr service; + if (!browserFile.isEmpty()) { + service = KService::serviceByStorageId(browserFile); + } + if (!service) { + service = KMimeTypeTrader::self()->preferredService(QStringLiteral("text/html")); + } + if (!service) { + return; + } + const auto actions = service->actions(); + for (const auto &action : actions) { + bool containsPrivate = action.text().contains(QLatin1String("private"), Qt::CaseInsensitive); + bool containsIncognito = action.text().contains(QLatin1String("incognito"), Qt::CaseInsensitive); + if (containsPrivate || containsIncognito) { + m_privateAction = action; + const QString actionText = containsPrivate ? i18n("Search in private window") : i18n("Search in incognito window"); + const QIcon icon = QIcon::fromTheme(QStringLiteral("view-private"), QIcon::fromTheme(QStringLiteral("view-hidden"))); + addAction(QStringLiteral("privateSearch"), icon, actionText); + return; + } + } +} + void WebshortcutRunner::match(Plasma::RunnerContext &context) { const QString term = context.query(); @@ -111,6 +143,12 @@ void WebshortcutRunner::match(Plasma::RunnerContext &context) context.addMatch(m_match); } +QList WebshortcutRunner::actionsForMatch(const Plasma::QueryMatch &match) +{ + Q_UNUSED(match) + return actions().values(); +} + void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) { QUrl location; @@ -124,7 +162,12 @@ void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma:: } if (!location.isEmpty()) { - QDesktopServices::openUrl(location); + if (match.selectedAction()) { + auto *job = new KIO::CommandLauncherJob(m_privateAction.exec() + QLatin1Char(' ') + location.toString()); + job->start(); + } else { + QDesktopServices::openUrl(location); + } } } diff --git a/runners/webshortcuts/webshortcutrunner.h b/runners/webshortcuts/webshortcutrunner.h index d70a4db76..0be0fada9 100644 --- a/runners/webshortcuts/webshortcutrunner.h +++ b/runners/webshortcuts/webshortcutrunner.h @@ -30,10 +30,12 @@ class WebshortcutRunner : public Plasma::AbstractRunner ~WebshortcutRunner() override; void match(Plasma::RunnerContext &context) override; + QList actionsForMatch(const Plasma::QueryMatch &match) override; void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) override; private Q_SLOTS: void loadSyntaxes(); + void configurePrivateBrowsingActions(); private: Plasma::QueryMatch m_match; @@ -43,6 +45,8 @@ class WebshortcutRunner : public Plasma::AbstractRunner QString m_lastFailedKey; QString m_lastKey; QString m_lastProvider; + + KServiceAction m_privateAction; }; #endif