• Skip to content
  • Skip to link menu
KDE 4.2 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("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     // Note that before asking for the content of e.g. Keywords and GenericName we need to ask if
00079     // they exist to prevent a tree evaluation error if they are not defined.
00080     query = QString("exist Exec and ( (exist Keywords and '%1' ~subin Keywords) or (exist GenericName and '%1' ~~ GenericName) or (exist Name and '%1' ~~ Name) )").arg(term);
00081     services = KServiceTypeTrader::self()->query("Application", query);
00082     services += KServiceTypeTrader::self()->query("KCModule", query);
00083 
00084     //kDebug() << "got " << services.count() << " services from " << query;
00085     foreach (const KService::Ptr &service, services) {
00086         if (service->noDisplay()) {
00087             continue;
00088         }
00089 
00090         QString id = service->storageId();
00091         QString exec = service->exec();
00092         if (seen.contains(id) || seen.contains(exec)) {
00093             //kDebug() << "already seen" << id << exec;
00094             continue;
00095         }
00096 
00097         //kDebug() << "haven't seen" << id << "so processing now";
00098         seen[id] = true;
00099         seen[exec] = true;
00100 
00101         Plasma::QueryMatch match(this);
00102         match.setType(Plasma::QueryMatch::PossibleMatch);
00103         setupAction(service, match);
00104         qreal relevance(0.6);
00105 
00106         if (service->name().contains(term, Qt::CaseInsensitive)) {
00107             relevance = 0.8;
00108 
00109             if (service->name().startsWith(term, Qt::CaseInsensitive)) {
00110                 relevance += 0.5;
00111             }
00112         } else if (service->genericName().contains(term, Qt::CaseInsensitive)) {
00113             relevance = 0.7;
00114 
00115             if (service->genericName().startsWith(term, Qt::CaseInsensitive)) {
00116                 relevance += 0.5;
00117             }
00118         }
00119 
00120         if (service->categories().contains("KDE") || service->serviceTypes().contains("KCModule")) {
00121             //kDebug() << "found a kde thing" << id << match.subtext() << relevance;
00122             if (id.startsWith("kde-")) {
00123                 // This is an older version, let's disambiguate it
00124                 QString subtext("KDE3");
00125 
00126                 //kDebug() << "old" << service->type();
00127                 if (service->type() == "KCModule") {
00128                     // avoid showing old kcms
00129                     continue;
00130                 }
00131 
00132                 if (!match.subtext().isEmpty()) {
00133                     subtext.append(", " + match.subtext());
00134                 }
00135 
00136                 match.setSubtext(subtext);
00137             } else {
00138                 relevance += .1;
00139             }
00140         }
00141 
00142         //kDebug() << service->name() << "is this relevant:" << relevance;
00143         match.setRelevance(relevance);
00144         matches << match;
00145     }
00146 
00147     context.addMatches(term, matches);
00148 }
00149 
00150 void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00151 {
00152     Q_UNUSED(context);
00153     QMutexLocker lock(bigLock());
00154     KService::Ptr service = KService::serviceByStorageId(match.data().toString());
00155     if (service) {
00156         KRun::run(*service, KUrl::List(), 0);
00157     }
00158 }
00159 
00160 void ServiceRunner::setupAction(const KService::Ptr &service, Plasma::QueryMatch &match)
00161 {
00162     const QString name = service->name();
00163 
00164     match.setText(name);
00165     match.setData(service->storageId());
00166 
00167     if (!service->genericName().isEmpty() && service->genericName() != name) {
00168         match.setSubtext(service->genericName());
00169     } else if (!service->comment().isEmpty()) {
00170         match.setSubtext(service->comment());
00171     }
00172 
00173     if (!service->icon().isEmpty()) {
00174         match.setIcon(KIcon(service->icon()));
00175     }
00176 }
00177 
00178 #include "servicerunner.moc"
00179 

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