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>
31
ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args)
32
: Plasma::AbstractRunner(parent, args)
36
setObjectName( QLatin1String("Application" ));
37
setPriority(AbstractRunner::HighestPriority);
39
addSyntax(Plasma::RunnerSyntax(":q:", i18n("Finds applications whose name or description match :q:")));
42
ServiceRunner::~ServiceRunner()
46
void ServiceRunner::match(Plasma::RunnerContext &context)
48
const QString term = context.query();
49
if (term.length() < 3) {
53
// Search for applications which are executable and case-insensitively match the search term
54
// See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language
55
// if the following is unclear to you.
56
QString query = QString("exist Exec and ('%1' =~ Name)").arg(term);
57
KService::List services = KServiceTypeTrader::self()->query("Application", query);
59
QList<Plasma::QueryMatch> matches;
62
if (!services.isEmpty()) {
63
//kDebug() << service->name() << "is an exact match!" << service->storageId() << service->exec();
64
foreach (const KService::Ptr &service, services) {
65
if (!service->noDisplay() && service->property("NotShowIn", QVariant::String) != "KDE") {
66
Plasma::QueryMatch match(this);
67
match.setType(Plasma::QueryMatch::ExactMatch);
68
setupMatch(service, match);
69
match.setRelevance(1);
71
seen.insert(service->storageId());
72
seen.insert(service->exec());
77
if (!context.isValid()) {
81
// Search for applications which are executable and the term case-insensitive matches any of
82
// * a substring of one of the keywords
83
// * a substring of the GenericName field
84
// * a substring of the Name field
85
// Note that before asking for the content of e.g. Keywords and GenericName we need to ask if
86
// they exist to prevent a tree evaluation error if they are not defined.
87
query = QString("exist Exec and ( (exist Keywords and '%1' ~subin Keywords) or (exist GenericName and '%1' ~~ GenericName) or (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term);
88
services = KServiceTypeTrader::self()->query("Application", query);
89
services += KServiceTypeTrader::self()->query("KCModule", query);
91
//kDebug() << "got " << services.count() << " services from " << query;
92
foreach (const KService::Ptr &service, services) {
93
if (!context.isValid()) {
97
if (service->noDisplay()) {
101
const QString id = service->storageId();
102
const QString exec = service->exec();
103
if (seen.contains(id) || seen.contains(exec)) {
104
//kDebug() << "already seen" << id << exec;
108
//kDebug() << "haven't seen" << id << "so processing now";
112
Plasma::QueryMatch match(this);
113
match.setType(Plasma::QueryMatch::PossibleMatch);
114
setupMatch(service, match);
115
qreal relevance(0.6);
117
if (service->name().contains(term, Qt::CaseInsensitive)) {
120
if (service->name().startsWith(term, Qt::CaseInsensitive)) {
123
} else if (service->genericName().contains(term, Qt::CaseInsensitive)) {
126
if (service->genericName().startsWith(term, Qt::CaseInsensitive)) {
131
if (service->categories().contains("KDE") || service->serviceTypes().contains("KCModule")) {
132
//kDebug() << "found a kde thing" << id << match.subtext() << relevance;
133
if (id.startsWith("kde-")) {
134
//kDebug() << "old" << service->type();
135
if (!service->isApplication()) {
136
// avoid showing old kcms and what not
140
// This is an older version, let's disambiguate it
141
QString subtext("KDE3");
143
if (!match.subtext().isEmpty()) {
144
subtext.append(", " + match.subtext());
147
match.setSubtext(subtext);
153
//kDebug() << service->name() << "is this relevant:" << relevance;
154
match.setRelevance(relevance);
158
//search for applications whose categories contains the query
159
query = QString("exist Exec and (exist Categories and '%1' ~subin Categories)").arg(term);
160
services = KServiceTypeTrader::self()->query("Application", query);
162
//kDebug() << service->name() << "is an exact match!" << service->storageId() << service->exec();
163
foreach (const KService::Ptr &service, services) {
164
if (!context.isValid()) {
168
if (!service->noDisplay()) {
169
QString id = service->storageId();
170
QString exec = service->exec();
171
if (seen.contains(id) || seen.contains(exec)) {
172
//kDebug() << "already seen" << id << exec;
175
Plasma::QueryMatch match(this);
176
match.setType(Plasma::QueryMatch::PossibleMatch);
177
setupMatch(service, match);
179
qreal relevance = 0.6;
180
if (service->categories().contains("X-KDE-More") ||
181
!service->showInKDE()) {
185
if (service->isApplication()) {
189
match.setRelevance(relevance);
194
context.addMatches(term, matches);
197
void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
200
KService::Ptr service = KService::serviceByStorageId(match.data().toString());
202
KRun::run(*service, KUrl::List(), 0);
206
void ServiceRunner::setupMatch(const KService::Ptr &service, Plasma::QueryMatch &match)
208
const QString name = service->name();
211
match.setData(service->storageId());
213
if (!service->genericName().isEmpty() && service->genericName() != name) {
214
match.setSubtext(service->genericName());
215
} else if (!service->comment().isEmpty()) {
216
match.setSubtext(service->comment());
219
if (!service->icon().isEmpty()) {
220
match.setIcon(KIcon(service->icon()));
224
QMimeData * ServiceRunner::mimeDataForMatch(const Plasma::QueryMatch *match)
226
KService::Ptr service = KService::serviceByStorageId(match->data().toString());
228
QMimeData * result = new QMimeData();
230
urls << KUrl(service->entryPath());
232
result->setUrls(urls);
239
#include "servicerunner.moc"