Commit 6443792e authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Add action to launch webshortcut in private/incognito window

FEATURE: 390009
parent bf81b755
......@@ -22,6 +22,9 @@
#include <QDesktopServices>
#include <KLocalizedString>
#include <KUriFilter>
#include <KSharedConfig>
#include <KMimeTypeTrader>
#include <KIO/CommandLauncherJob>
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<QAction *> 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);
}
}
}
......
......@@ -30,10 +30,12 @@ class WebshortcutRunner : public Plasma::AbstractRunner
~WebshortcutRunner() override;
void match(Plasma::RunnerContext &context) override;
QList<QAction *> 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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment