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

Plasma

servicerunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
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 "servicerunner.h"
00020 
00021 #include <QWidget>
00022 #include <KIcon>
00023 
00024 #include <KDebug>
00025 #include <KLocale>
00026 #include <KRun>
00027 #include <KService>
00028 #include <KServiceTypeTrader>
00029 
00030 ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args)
00031     : Plasma::AbstractRunner( parent )
00032 {
00033     Q_UNUSED(args)
00034 
00035     setObjectName(i18n("Application"));
00036     setPriority(AbstractRunner::HighestPriority);
00037 }
00038 
00039 ServiceRunner::~ServiceRunner()
00040 {
00041 }
00042 
00043 void ServiceRunner::match(Plasma::RunnerContext &context)
00044 {
00045     const QString term = context.query();
00046     if (term.length() <  3) {
00047         return;
00048     }
00049 
00050     QMutexLocker lock(bigLock());
00051     // Search for applications which are executable and case-insensitively match the search term
00052     // See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language
00053     // if the following is unclear to you.
00054     QString query = QString("exist Exec and ('%1' =~ Name)").arg(term);
00055     KService::List services = KServiceTypeTrader::self()->query("Application", query);
00056 
00057     QList<Plasma::QueryMatch> matches;
00058 
00059     QHash<QString, bool> seen;
00060     if (!services.isEmpty()) {
00061         //kDebug() << service->name() << "is an exact match!" << service->storageId() << service->exec();
00062         KService::Ptr service = services.at(0);
00063         if (!service->noDisplay()) {
00064             Plasma::QueryMatch match(this);
00065             match.setType(Plasma::QueryMatch::ExactMatch);
00066             setupAction(service, match);
00067             match.setRelevance(1);
00068             matches << match;
00069             seen[service->storageId()] = true;
00070             seen[service->exec()] = true;
00071         }
00072     }
00073 
00074     // Search for applications which are executable and the term case-insensitive matches any of
00075     // * a substring of one of the keywords
00076     // * a substring of the GenericName field
00077     // * a substring of the Name field
00078     query = QString("exist Exec and ('%1' ~subin Keywords or '%1' ~~ GenericName or '%1' ~~ Name)").arg(term);
00079     services = KServiceTypeTrader::self()->query("Application", query);
00080 
00081     //kDebug() << "got " << services.count() << " services from " << query;
00082     foreach (const KService::Ptr &service, services) {
00083         if (service->noDisplay()) {
00084             continue;
00085         }
00086 
00087         QString id = service->storageId();
00088         QString exec = service->exec();
00089         if (seen.contains(id) || seen.contains(exec)) {
00090             //kDebug() << "already seen" << id << exec;
00091             continue;
00092         }
00093 
00094         //kDebug() << "haven't seen" << id << "so processing now";
00095         seen[id] = true;
00096         seen[exec] = true;
00097 
00098         Plasma::QueryMatch match(this);
00099         match.setType(Plasma::QueryMatch::PossibleMatch);
00100         setupAction(service, match);
00101         qreal relevance(0.6);
00102 
00103         if (service->name().contains(term, Qt::CaseInsensitive)) {
00104             relevance = 0.8;
00105 
00106             if (service->name().startsWith(term, Qt::CaseInsensitive)) {
00107                 relevance += 0.5;
00108             }
00109         } else if (service->genericName().contains(term, Qt::CaseInsensitive)) {
00110             relevance = 0.7;
00111 
00112             if (service->genericName().startsWith(term, Qt::CaseInsensitive)) {
00113                 relevance += 0.5;
00114             }
00115         }
00116 
00117         if (service->categories().contains("KDE")) {
00118             if (id.startsWith("kde-")) {
00119                 // This is an older version, let's disambiguate it
00120                 QString subtext("KDE3");
00121                 if (!match.subtext().isEmpty()) {
00122                     subtext.append(", " + match.subtext());
00123                 }
00124 
00125                 match.setSubtext(subtext);
00126             } else {
00127                 relevance += .1;
00128             }
00129         }
00130 
00131         //kDebug() << service->name() << "is this relevant:" << relevance;
00132         match.setRelevance(relevance);
00133         matches << match;
00134     }
00135 
00136     context.addMatches(term, matches);
00137 }
00138 
00139 void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00140 {
00141     Q_UNUSED(context);
00142     QMutexLocker lock(bigLock());
00143     KService::Ptr service = KService::serviceByStorageId(match.data().toString());
00144     if (service) {
00145         KRun::run(*service, KUrl::List(), 0);
00146     }
00147 }
00148 
00149 void ServiceRunner::setupAction(const KService::Ptr &service, Plasma::QueryMatch &match)
00150 {
00151     const QString name = service->name();
00152 
00153     match.setText(name);
00154     match.setData(service->storageId());
00155 
00156     if (!service->genericName().isEmpty() && service->genericName() != name) {
00157         match.setSubtext(service->genericName());
00158     } else if (!service->comment().isEmpty()) {
00159         match.setSubtext(service->comment());
00160     }
00161 
00162     if (!service->icon().isEmpty()) {
00163         match.setIcon(KIcon(service->icon()));
00164     }
00165 }
00166 
00167 #include "servicerunner.moc"
00168 

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