Plasma
webshortcutrunner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "webshortcutrunner.h"
00020
00021 #include <QAction>
00022 #include <QStringList>
00023 #include <QDBusInterface>
00024 #include <QDBusReply>
00025
00026 #include <KDebug>
00027 #include <KRun>
00028 #include <KLocale>
00029 #include <KMimeType>
00030 #include <KStandardDirs>
00031 #include <KToolInvocation>
00032 #include <KUrl>
00033
00034 WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args)
00035 : Plasma::AbstractRunner(parent, args)
00036 {
00037 KGlobal::locale()->insertCatalog("krunner_webshortcutsrunner");
00038 Q_UNUSED(args);
00039
00040 setObjectName(i18n("Web Shortcut"));
00041
00042 m_offers = serviceQuery("SearchProvider");
00043 m_icon = KIcon("internet-web-browser");
00044 setIgnoredTypes(Plasma::RunnerContext::FileSystem);
00045 }
00046
00047 WebshortcutRunner::~WebshortcutRunner()
00048 {
00049 }
00050
00051 void WebshortcutRunner::match(Plasma::RunnerContext &context)
00052 {
00053 const QString term = context.query();
00054 const char separator = ':';
00055
00056 if (term.length() < 3 || !term.contains(separator)) {
00057 return;
00058 }
00059
00060
00061
00062 QMutexLocker lock(bigLock());
00063 foreach (const KService::Ptr &service, m_offers) {
00064
00065 foreach (QString key, service->property("Keys").toStringList()) {
00066
00067 key = key.toLower() + separator;
00068 if (term.size() > key.size() &&
00069 term.startsWith(key, Qt::CaseInsensitive)) {
00070 QString actionText = i18n("Search %1 for %2",service->name(),
00071 term.right(term.length() - term.indexOf(':') - 1));
00072 QString url = getSearchQuery(service->property("Query").toString(), term);
00073
00074
00075 Plasma::QueryMatch match(this);
00076 match.setType(Plasma::QueryMatch::ExactMatch);
00077 match.setText(actionText);
00078 match.setData(service->property("Query").toString());
00079 match.setRelevance(0.9);
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 match.setIcon(m_icon);
00092
00093 context.addMatch(term, match);
00094 return;
00095 }
00096 }
00097 }
00098 }
00099
00100 QString WebshortcutRunner::getSearchQuery(const QString &query, const QString &term)
00101 {
00102
00103 QString searchWord = term.right(term.length() - term.indexOf(':') - 1);
00104 if (searchWord.isEmpty()) {
00105 return QString();
00106 }
00107
00108 QString finalQuery(query);
00109
00110 finalQuery.replace("\\{@}", searchWord);
00111 KUrl url(finalQuery);
00112 return url.url();
00113 }
00114
00115 KIcon WebshortcutRunner::getFavicon(const KUrl &url) {
00116
00117 QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
00118 QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
00119
00120 if (!reply.isValid()) {
00121 return KIcon();
00122 }
00123
00124
00125 QString iconFile = KGlobal::dirs()->findResource("cache", reply.value()+".png");
00126
00127 if (iconFile.isNull()) {
00128 return KIcon();
00129 }
00130 KIcon icon = KIcon(iconFile);
00131
00132 return icon;
00133 }
00134
00135 void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00136 {
00137 QString location = getSearchQuery(match.data().toString(), context.query());
00138
00139 if (!location.isEmpty()) {
00140 KToolInvocation::invokeBrowser(location);
00141 }
00142 }
00143
00144 #include "webshortcutrunner.moc"