• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

webshortcutrunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Teemu Rytilahti <tpr@iki.fi>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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     // query ktrader for all available searchproviders and preload the default icon
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     // TODO: KDirWatch :)
00053     KConfig kuriconfig("kuriikwsfilterrc", KConfig::NoGlobals);
00054     KConfigGroup generalgroup(&kuriconfig, "General");
00055     QString delimiter = generalgroup.readEntry("KeywordDelimiter", QString(':'));
00056     //kDebug() << "keyworddelimiter is: " << delimiter;
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     //kDebug() << "checking with" << term;
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         // we already know it's going to suck ;)
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     //kDebug() << "url is" << url << "!!!!!!!!!!!!!!!!!!!!!!!";
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     // FIXME? currently only basic searches are supported
00123     finalQuery.replace("\\{@}", searchWord);
00124     KUrl url(finalQuery);
00125     return url.url();
00126 }
00127 
00128 KIcon WebshortcutRunner::iconForUrl(const KUrl &url)
00129 {
00130     // query the favicons module
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     // locate the favicon
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"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal