Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / runners / services / servicerunner.cpp
blob925a81f6c0ae39fe2c54d799394deddd047d69c8
1 /*
2 * Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "servicerunner.h"
21 #include <QWidget>
22 #include <KIcon>
24 #include <KDebug>
25 #include <KLocale>
26 #include <KRun>
27 #include <KService>
28 #include <KServiceTypeTrader>
30 ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args)
31 : Plasma::AbstractRunner( parent )
33 Q_UNUSED(args)
35 setObjectName(i18n("Application"));
36 setPriority(AbstractRunner::HighestPriority);
39 ServiceRunner::~ServiceRunner()
43 void ServiceRunner::match(Plasma::SearchContext *search)
45 const QString term = search->searchTerm();
46 if (term.length() < 3) {
47 return;
50 QMutexLocker lock(bigLock());
51 KService::Ptr service = KService::serviceByName(term);
53 QList<Plasma::SearchMatch*> matches;
55 QHash<QString, bool> seen;
56 if (service && !service->exec().isEmpty()) {
57 //kDebug() << service->name() << "is an exact match!";
58 Plasma::SearchMatch *match = new Plasma::SearchMatch(this);
59 match->setType(Plasma::SearchMatch::ExactMatch);
60 setupAction(service, match);
61 match->setRelevance(1);
62 matches << match;
63 seen[service->storageId()] = true;
64 seen[service->exec()] = true;
67 QString query = QString("exist Exec and ('%1' ~subin Keywords or '%2' ~~ GenericName or '%3' ~~ Name)").arg(term, term, term);
68 const KService::List services = KServiceTypeTrader::self()->query("Application", query);
70 //kDebug() << "got " << services.count() << " services from " << query;
71 foreach (const KService::Ptr service, services) {
72 QString id = service->storageId();
73 QString exec = service->exec();
74 if (seen.contains(id) || seen.contains(exec)) {
75 //kDebug() << "already seen" << id << exec;
76 continue;
79 //kDebug() << "haven't seen" << id << "so processing now";
80 seen[id] = true;
81 seen[exec] = true;
83 Plasma::SearchMatch *match = new Plasma::SearchMatch(this);
84 setupAction(service, match);
85 qreal relevance(0.6);
87 if (service->name().contains(term, Qt::CaseInsensitive)) {
88 relevance = .8;
89 } else if (service->genericName().contains(term, Qt::CaseInsensitive)) {
90 relevance = .7;
93 if (service->categories().contains("KDE")) {
94 relevance += .1;
97 //kDebug() << service->name() << "is this relevant:" << relevance;
98 match->setRelevance(relevance);
99 matches << match;
102 search->addMatches(term, matches);
105 void ServiceRunner::exec(const Plasma::SearchContext *search, const Plasma::SearchMatch *action)
107 Q_UNUSED(search);
108 QMutexLocker lock(bigLock());
109 KService::Ptr service = KService::serviceByStorageId(action->data().toString());
110 if (service) {
111 KRun::run(*service, KUrl::List(), 0);
115 void ServiceRunner::setupAction(const KService::Ptr &service, Plasma::SearchMatch *action)
117 const QString name = service->name();
119 action->setText(name);
120 action->setData(service->storageId());
122 if (!service->genericName().isEmpty() && service->genericName() != name) {
123 action->setSubtext(service->genericName());
124 } else if (!service->comment().isEmpty()) {
125 action->setSubtext(service->comment());
128 if (!service->icon().isEmpty()) {
129 action->setIcon(KIcon(service->icon()));
133 #include "servicerunner.moc"