Plasma
servicerunner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00052
00053
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
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
00075
00076
00077
00078
00079
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
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
00094 continue;
00095 }
00096
00097
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
00122 if (id.startsWith("kde-")) {
00123
00124 QString subtext("KDE3");
00125
00126
00127 if (service->type() == "KCModule") {
00128
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
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