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 m_match(this)
00037 {
00038 KGlobal::locale()->insertCatalog("krunner_webshortcutsrunner");
00039 Q_UNUSED(args);
00040 setObjectName("Web Shortcut");
00041
00042 m_icon = KIcon("internet-web-browser");
00043 m_delimiter = loadDelimiter();
00044 setIgnoredTypes(Plasma::RunnerContext::FileSystem);
00045
00046 m_match.setType(Plasma::QueryMatch::ExactMatch);
00047 m_match.setRelevance(0.9);
00048 }
00049
00050 QString WebshortcutRunner::loadDelimiter()
00051 {
00052
00053 KConfig kuriconfig("kuriikwsfilterrc", KConfig::NoGlobals);
00054 KConfigGroup generalgroup(&kuriconfig, "General");
00055 QString delimiter = generalgroup.readEntry("KeywordDelimiter", QString(':'));
00056
00057 return delimiter;
00058 }
00059
00060 WebshortcutRunner::~WebshortcutRunner()
00061 {
00062 }
00063
00064 void WebshortcutRunner::match(Plasma::RunnerContext &context)
00065 {
00066 const QString term = context.query();
00067
00068 if (term.length() < 3 || !term.contains(m_delimiter)) {
00069 return;
00070 }
00071
00072
00073
00074 int delimIndex = term.indexOf(m_delimiter);
00075
00076 if (delimIndex == term.length() - 1) {
00077 return;
00078 }
00079
00080 QString key = term.left(delimIndex);
00081
00082 if (key == m_lastFailedKey) {
00083
00084 return;
00085 }
00086
00087 if (key != m_lastKey) {
00088 KService::List offers = serviceQuery("SearchProvider", QString("'%1' in Keys").arg(key));
00089
00090 if (offers.isEmpty()) {
00091 m_lastFailedKey = key;
00092 return;
00093 }
00094
00095 KService::Ptr service = offers.at(0);
00096 m_lastKey = key;
00097 m_lastFailedKey.clear();
00098 m_lastServiceName = service->name();
00099
00100 QString query = service->property("Query").toString();
00101 m_match.setData(query);
00102
00103 m_match.setIcon(iconForUrl(query));
00104 }
00105
00106 QString actionText = i18n("Search %1 for %2", m_lastServiceName,
00107 term.right(term.length() - delimIndex - 1));
00108
00109
00110 m_match.setText(actionText);
00111 context.addMatch(term, m_match);
00112 }
00113
00114 QString WebshortcutRunner::searchQuery(const QString &query, const QString &term)
00115 {
00116 QString searchWord = term.right(term.length() - term.indexOf(m_delimiter) - 1);
00117 if (searchWord.isEmpty()) {
00118 return QString();
00119 }
00120
00121 QString finalQuery(query);
00122
00123 finalQuery.replace("\\{@}", searchWord);
00124 KUrl url(finalQuery);
00125 return url.url();
00126 }
00127
00128 KIcon WebshortcutRunner::iconForUrl(const KUrl &url)
00129 {
00130
00131 QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
00132 QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
00133
00134 if (!reply.isValid()) {
00135 return m_icon;
00136 }
00137
00138
00139 QString iconFile = KGlobal::dirs()->locateLocal("cache", reply.value() + ".png");
00140
00141 if (iconFile.isNull()) {
00142 return m_icon;
00143 }
00144
00145 m_lastIcon = KIcon(iconFile);
00146 return m_lastIcon;
00147 }
00148
00149 void WebshortcutRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00150 {
00151 QString location = searchQuery(match.data().toString(), context.query());
00152
00153 if (!location.isEmpty()) {
00154 KToolInvocation::invokeBrowser(location);
00155 }
00156 }
00157
00158 #include "webshortcutrunner.moc"