2
* Copyright (C) 2007 Teemu Rytilahti <tpr@iki.fi>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License version 2 as
6
* published by the Free Software Foundation
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details
13
* You should have received a copy of the GNU Library General Public
14
* License along with this program; if not, write to the
15
* Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include "webshortcutrunner.h"
24
#include <KServiceTypeTrader>
25
#include <KStandardDirs>
27
#include <KToolInvocation>
31
#include <QtDBus/QtDBus>
33
WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args)
34
: Plasma::AbstractRunner(parent, args),
35
m_match(this), m_filterBeforeRun(false)
38
setObjectName( QLatin1String("Web Shortcut" ));
39
setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | Plasma::RunnerContext::Executable);
41
m_icon = KIcon("internet-web-browser");
43
m_match.setType(Plasma::QueryMatch::ExactMatch);
44
m_match.setRelevance(0.9);
46
// Listen for KUriFilter plugin config changes and update state...
47
QDBusConnection sessionDbus = QDBusConnection::sessionBus();
48
sessionDbus.connect(QString(), QString(), "org.kde.KUriFilterPlugin",
49
"configure", this, SLOT(readFiltersConfig()));
51
connect(this, SIGNAL(teardown()), this, SLOT(resetState()));
55
WebshortcutRunner::~WebshortcutRunner()
59
void WebshortcutRunner::readFiltersConfig()
61
// Make sure that the searchEngines cache, etc. is refreshed when the config file is changed.
65
void WebshortcutRunner::loadSyntaxes()
67
KUriFilterData filterData (QLatin1String(":q"));
68
filterData.setSearchFilteringOptions(KUriFilterData::RetrieveAvailableSearchProvidersOnly);
69
if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::NormalTextFilter)) {
70
m_delimiter = filterData.searchTermSeparator();
73
//kDebug() << "keyword delimiter:" << m_delimiter;
74
//kDebug() << "search providers:" << filterData.preferredSearchProviders();
76
QList<Plasma::RunnerSyntax> syns;
77
Q_FOREACH (const QString &provider, filterData.preferredSearchProviders()) {
78
//kDebug() << "checking out" << provider;
79
Plasma::RunnerSyntax s(filterData.queryForPreferredSearchProvider(provider), /*":q:",*/
80
i18n("Opens \"%1\" in a web browser with the query :q:.", provider));
87
void WebshortcutRunner::resetState()
90
m_lastFailedKey.clear();
91
m_lastProvider.clear();
95
void WebshortcutRunner::match(Plasma::RunnerContext &context)
97
const QString term = context.query();
99
if (term.length() < 3 || !term.contains(m_delimiter))
102
// kDebug() << "checking term" << term;
104
const int delimIndex = term.indexOf(m_delimiter);
105
if (delimIndex == term.length() - 1)
108
const QString key = term.left(delimIndex);
110
if (key == m_lastFailedKey) {
111
return; // we already know it's going to suck ;)
114
if (!context.isValid()) {
115
kDebug() << "invalid context";
119
// Do a fake user feedback text update if the keyword has not changed.
120
// There is no point filtering the request on every key stroke.
122
if (m_lastKey == key) {
123
m_filterBeforeRun = true;
124
m_match.setText(i18n("Search %1 for %2", m_lastProvider, term.mid(delimIndex)));
125
context.addMatch(term, m_match);
129
KUriFilterData filterData(term);
130
if (!KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter)) {
131
m_lastFailedKey = key;
135
m_lastFailedKey.clear();
137
m_lastProvider = filterData.searchProvider();
139
m_match.setData(filterData.uri().url());
140
m_match.setId("WebShortcut:" + key);
142
m_match.setIcon(KIcon(filterData.iconName()));
143
m_match.setText(i18n("Search %1 for %2", m_lastProvider, filterData.searchTerm()));
144
context.addMatch(term, m_match);
147
void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
151
//kDebug() << "filter before run?" << m_filterBeforeRun;
152
if (m_filterBeforeRun) {
153
m_filterBeforeRun = false;
154
//kDebug() << "look up webshortcut:" << context.query();
155
KUriFilterData filterData (context.query());
156
if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter))
157
location = filterData.uri().url();
159
location = match.data().toString();
162
//kDebug() << location;
163
if (!location.isEmpty()) {
164
KToolInvocation::invokeBrowser(location);
168
#include "webshortcutrunner.moc"