1 /*
2     SPDX-FileCopyrightText: 2007 Teemu Rytilahti <tpr@iki.fi>
3 
4     SPDX-License-Identifier: LGPL-2.0-only
5 */
6 
7 #include "webshortcutrunner.h"
8 
9 #include <KApplicationTrader>
10 #include <KIO/CommandLauncherJob>
11 #include <KLocalizedString>
12 #include <KSharedConfig>
13 #include <KShell>
14 #include <KSycoca>
15 #include <KUriFilter>
16 #include <QAction>
17 #include <QDBusConnection>
18 #include <QDesktopServices>
19 
WebshortcutRunner(QObject * parent,const KPluginMetaData & metaData,const QVariantList & args)20 WebshortcutRunner::WebshortcutRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
21     : Plasma::AbstractRunner(parent, metaData, args)
22     , m_match(this)
23     , m_filterBeforeRun(false)
24 {
25     setObjectName(QStringLiteral("Web Shortcut"));
26     m_match.setType(Plasma::QueryMatch::ExactMatch);
27     m_match.setRelevance(0.9);
28 
29     // Listen for KUriFilter plugin config changes and update state...
30     QDBusConnection sessionDbus = QDBusConnection::sessionBus();
31     sessionDbus.connect(QString(), QStringLiteral("/"), QStringLiteral("org.kde.KUriFilterPlugin"), QStringLiteral("configure"), this, SLOT(loadSyntaxes()));
32     loadSyntaxes();
33     configurePrivateBrowsingActions();
34     connect(KSycoca::self(), QOverload<>::of(&KSycoca::databaseChanged), this, &WebshortcutRunner::configurePrivateBrowsingActions);
35     setMinLetterCount(3);
36 }
37 
~WebshortcutRunner()38 WebshortcutRunner::~WebshortcutRunner()
39 {
40 }
41 
loadSyntaxes()42 void WebshortcutRunner::loadSyntaxes()
43 {
44     KUriFilterData filterData(QStringLiteral(":q"));
45     filterData.setSearchFilteringOptions(KUriFilterData::RetrieveAvailableSearchProvidersOnly);
46     if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::NormalTextFilter)) {
47         m_delimiter = filterData.searchTermSeparator();
48     }
49 
50     QList<Plasma::RunnerSyntax> syns;
51     const QStringList providers = filterData.preferredSearchProviders();
52     for (const QString &provider : providers) {
53         Plasma::RunnerSyntax s(filterData.queryForPreferredSearchProvider(provider), /*":q:",*/
54                                i18n("Opens \"%1\" in a web browser with the query :q:.", provider));
55         syns << s;
56     }
57 
58     setSyntaxes(syns);
59     m_lastFailedKey.clear();
60     m_lastProvider.clear();
61     m_lastKey.clear();
62 }
63 
configurePrivateBrowsingActions()64 void WebshortcutRunner::configurePrivateBrowsingActions()
65 {
66     qDeleteAll(m_match.actions());
67     m_match.setActions({});
68     const QString browserFile = KSharedConfig::openConfig(QStringLiteral("kdeglobals"))->group("General").readEntry("BrowserApplication");
69     KService::Ptr service;
70     if (!browserFile.isEmpty()) {
71         service = KService::serviceByStorageId(browserFile);
72     }
73     if (!service) {
74         service = KApplicationTrader::preferredService(QStringLiteral("text/html"));
75     }
76     if (!service) {
77         return;
78     }
79     const auto actions = service->actions();
80     for (const auto &action : actions) {
81         bool containsPrivate = action.text().contains(QLatin1String("private"), Qt::CaseInsensitive);
82         bool containsIncognito = action.text().contains(QLatin1String("incognito"), Qt::CaseInsensitive);
83         if (containsPrivate || containsIncognito) {
84             m_privateAction = action;
85             const QString actionText = containsPrivate ? i18n("Search in private window") : i18n("Search in incognito window");
86             const QIcon icon = QIcon::fromTheme(QStringLiteral("view-private"), QIcon::fromTheme(QStringLiteral("view-hidden")));
87             m_match.setActions({new QAction(icon, actionText, this)});
88             return;
89         }
90     }
91 }
92 
match(Plasma::RunnerContext & context)93 void WebshortcutRunner::match(Plasma::RunnerContext &context)
94 {
95     const QString term = context.query();
96     const static QRegularExpression bangRegex(QStringLiteral("!([^ ]+).*"));
97     const static QRegularExpression normalRegex(QStringLiteral("^([^ ]+)%1").arg(QRegularExpression::escape(m_delimiter)));
98     const auto bangMatch = bangRegex.match(term);
99     QString key;
100     QString rawQuery = term;
101 
102     if (bangMatch.hasMatch()) {
103         key = bangMatch.captured(1);
104         rawQuery = rawQuery.remove(rawQuery.indexOf(key) - 1, key.size() + 1);
105     } else {
106         const auto normalMatch = normalRegex.match(term);
107         if (normalMatch.hasMatch()) {
108             key = normalMatch.captured(0);
109             rawQuery = rawQuery.mid(key.length());
110         }
111     }
112     if (key.isEmpty() || key == m_lastFailedKey) {
113         return; // we already know it's going to suck ;)
114     }
115 
116     // Do a fake user feedback text update if the keyword has not changed.
117     // There is no point filtering the request on every key stroke.
118     // filtering
119     if (m_lastKey == key) {
120         m_filterBeforeRun = true;
121         m_match.setText(i18n("Search %1 for %2", m_lastProvider, rawQuery));
122         context.addMatch(m_match);
123         return;
124     }
125 
126     KUriFilterData filterData(term);
127     if (!KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter)) {
128         m_lastFailedKey = key;
129         return;
130     }
131 
132     // Reuse key/provider for next matches. Other variables ca be reused, because the same match object is used
133     m_lastKey = key;
134     m_lastProvider = filterData.searchProvider();
135     m_match.setIconName(filterData.iconName());
136     m_match.setId(QStringLiteral("WebShortcut:") + key);
137 
138     m_match.setText(i18n("Search %1 for %2", m_lastProvider, filterData.searchTerm()));
139     m_match.setData(filterData.uri());
140     context.addMatch(m_match);
141 }
142 
run(const Plasma::RunnerContext & context,const Plasma::QueryMatch & match)143 void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
144 {
145     QUrl location;
146     if (m_filterBeforeRun) {
147         m_filterBeforeRun = false;
148         KUriFilterData filterData(context.query());
149         if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter))
150             location = filterData.uri();
151     } else {
152         location = match.data().toUrl();
153     }
154 
155     if (!location.isEmpty()) {
156         if (match.selectedAction()) {
157             QString command;
158 
159             // Chrome's exec line does not have a URL placeholder
160             // Firefox's does, but only sometimes, depending on the distro
161             // Replace placeholders if found, otherwise append at the end
162             if (m_privateAction.exec().contains("%u")) {
163                 command = m_privateAction.exec().replace("%u", KShell::quoteArg(location.toString()));
164             } else if (m_privateAction.exec().contains("%U")) {
165                 command = m_privateAction.exec().replace("%U", KShell::quoteArg(location.toString()));
166             } else {
167                 command = m_privateAction.exec() + QLatin1Char(' ') + KShell::quoteArg(location.toString());
168             }
169 
170             auto *job = new KIO::CommandLauncherJob(command);
171             job->start();
172         } else {
173             QDesktopServices::openUrl(location);
174         }
175     }
176 }
177 
178 K_PLUGIN_CLASS_WITH_JSON(WebshortcutRunner, "plasma-runner-webshortcuts.json")
179 
180 #include "webshortcutrunner.moc"
181