not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / runners / webshortcuts / webshortcutrunner.cpp
bloba863285c51dea636555e5010a9665a11410ba82e
1 /*
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"
21 #include <QAction>
22 #include <QStringList>
23 #include <QDBusInterface>
24 #include <QDBusReply>
26 #include <KDebug>
27 #include <KRun>
28 #include <KLocale>
29 #include <KMimeType>
30 #include <KStandardDirs>
31 #include <KToolInvocation>
32 #include <KUrl>
34 WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args)
35 : Plasma::AbstractRunner(parent, args),
36 m_match(this)
38 KGlobal::locale()->insertCatalog("krunner_webshortcutsrunner");
39 Q_UNUSED(args);
40 setObjectName("Web Shortcut");
41 // query ktrader for all available searchproviders and preload the default icon
42 m_icon = KIcon("internet-web-browser");
43 m_delimiter = loadDelimiter();
44 setIgnoredTypes(Plasma::RunnerContext::FileSystem);
46 m_match.setType(Plasma::QueryMatch::ExactMatch);
47 m_match.setRelevance(0.9);
50 QString WebshortcutRunner::loadDelimiter()
52 // TODO: KDirWatch :)
53 KConfig kuriconfig("kuriikwsfilterrc", KConfig::NoGlobals);
54 KConfigGroup generalgroup(&kuriconfig, "General");
55 QString delimiter = generalgroup.readEntry("KeywordDelimiter", QString(':'));
56 //kDebug() << "keyworddelimiter is: " << delimiter;
57 return delimiter;
60 WebshortcutRunner::~WebshortcutRunner()
64 void WebshortcutRunner::match(Plasma::RunnerContext &context)
66 const QString term = context.query();
68 if (term.length() < 3 || !term.contains(m_delimiter)) {
69 return;
72 //kDebug() << "checking with" << term;
74 int delimIndex = term.indexOf(m_delimiter);
76 if (delimIndex == term.length() - 1) {
77 return;
80 QString key = term.left(delimIndex);
82 if (key == m_lastFailedKey) {
83 // we already know it's going to suck ;)
84 return;
87 if (key != m_lastKey) {
88 KService::List offers = serviceQuery("SearchProvider", QString("'%1' in Keys").arg(key));
90 if (offers.isEmpty()) {
91 m_lastFailedKey = key;
92 return;
95 KService::Ptr service = offers.at(0);
96 m_lastKey = key;
97 m_lastFailedKey.clear();
98 m_lastServiceName = service->name();
100 QString query = service->property("Query").toString();
101 m_match.setData(query);
103 m_match.setIcon(iconForUrl(query));
106 QString actionText = i18n("Search %1 for %2", m_lastServiceName,
107 term.right(term.length() - delimIndex - 1));
108 //kDebug() << "url is" << url << "!!!!!!!!!!!!!!!!!!!!!!!";
110 m_match.setText(actionText);
111 context.addMatch(term, m_match);
114 QString WebshortcutRunner::searchQuery(const QString &query, const QString &term)
116 QString searchWord = term.right(term.length() - term.indexOf(m_delimiter) - 1);
117 if (searchWord.isEmpty()) {
118 return QString();
121 QString finalQuery(query);
122 // FIXME? currently only basic searches are supported
123 finalQuery.replace("\\{@}", searchWord);
124 KUrl url(finalQuery);
125 return url.url();
128 KIcon WebshortcutRunner::iconForUrl(const KUrl &url)
130 // query the favicons module
131 QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
132 QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
134 if (!reply.isValid()) {
135 return m_icon;
138 // locate the favicon
139 QString iconFile = KGlobal::dirs()->locateLocal("cache", reply.value() + ".png");
141 if (iconFile.isNull()) {
142 return m_icon;
145 m_lastIcon = KIcon(iconFile);
146 return m_lastIcon;
149 void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
151 QString location = searchQuery(match.data().toString(), context.query());
153 if (!location.isEmpty()) {
154 KToolInvocation::invokeBrowser(location);
158 #include "webshortcutrunner.moc"