• Skip to content
  • Skip to link menu
KDE 4.1 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 {
00037     KGlobal::locale()->insertCatalog("krunner_webshortcutsrunner");
00038     Q_UNUSED(args);
00039     // set the name shown after the result in krunner window
00040     setObjectName(i18n("Web Shortcut"));
00041     // query ktrader for all available searchproviders and preload the default icon
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     //kDebug() << "checking with" << term;
00061 
00062     QMutexLocker lock(bigLock());
00063     foreach (const KService::Ptr &service, m_offers) {
00064         //TODO: how about getting the keys for the localized sites?
00065         foreach (QString key, service->property("Keys").toStringList()) {
00066             // FIXME? should we look for the used separator from the konqi's settings?
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                 //kDebug() << "url is" << url << "!!!!!!!!!!!!!!!!!!!!!!!";
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                 // let's try if we can get a proper icon from the favicon cache
00082                 // getting the favicon is too slow and can easily lead to starving the thread pool out
00083                 /*
00084                 KIcon icon = getFavicon(url);
00085                 if (icon.isNull()){
00086                     match.setIcon(m_icon);
00087                 } else {
00088                     match.setIcon(icon);
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     // FIXME delimiter check like for above?
00103     QString searchWord = term.right(term.length() - term.indexOf(':') - 1);
00104     if (searchWord.isEmpty()) {
00105         return QString();
00106     }
00107 
00108     QString finalQuery(query);
00109     // FIXME? currently only basic searches are supported
00110     finalQuery.replace("\\{@}", searchWord);
00111     KUrl url(finalQuery);
00112     return url.url();
00113 }
00114 
00115 KIcon WebshortcutRunner::getFavicon(const KUrl &url) {
00116     // query the favicons module
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     // locate the favicon
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"

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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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