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"
28 #include <KServiceTypeTrader>
30 ServiceRunner::ServiceRunner(QObject
*parent
, const QVariantList
&args
)
31 : Plasma::AbstractRunner( parent
)
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) {
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);
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;
79 //kDebug() << "haven't seen" << id << "so processing now";
83 Plasma::SearchMatch
*match
= new Plasma::SearchMatch(this);
84 setupAction(service
, match
);
87 if (service
->name().contains(term
, Qt::CaseInsensitive
)) {
89 } else if (service
->genericName().contains(term
, Qt::CaseInsensitive
)) {
93 if (service
->categories().contains("KDE")) {
97 //kDebug() << service->name() << "is this relevant:" << relevance;
98 match
->setRelevance(relevance
);
102 search
->addMatches(term
, matches
);
105 void ServiceRunner::exec(const Plasma::SearchContext
*search
, const Plasma::SearchMatch
*action
)
108 QMutexLocker
lock(bigLock());
109 KService::Ptr service
= KService::serviceByStorageId(action
->data().toString());
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"