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(i18n("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 query = QString("exist Exec and ('%1' ~subin Keywords or '%1' ~~ GenericName or '%1' ~~ Name)").arg(term);
00079 services = KServiceTypeTrader::self()->query("Application", query);
00080
00081
00082 foreach (const KService::Ptr &service, services) {
00083 if (service->noDisplay()) {
00084 continue;
00085 }
00086
00087 QString id = service->storageId();
00088 QString exec = service->exec();
00089 if (seen.contains(id) || seen.contains(exec)) {
00090
00091 continue;
00092 }
00093
00094
00095 seen[id] = true;
00096 seen[exec] = true;
00097
00098 Plasma::QueryMatch match(this);
00099 match.setType(Plasma::QueryMatch::PossibleMatch);
00100 setupAction(service, match);
00101 qreal relevance(0.6);
00102
00103 if (service->name().contains(term, Qt::CaseInsensitive)) {
00104 relevance = 0.8;
00105
00106 if (service->name().startsWith(term, Qt::CaseInsensitive)) {
00107 relevance += 0.5;
00108 }
00109 } else if (service->genericName().contains(term, Qt::CaseInsensitive)) {
00110 relevance = 0.7;
00111
00112 if (service->genericName().startsWith(term, Qt::CaseInsensitive)) {
00113 relevance += 0.5;
00114 }
00115 }
00116
00117 if (service->categories().contains("KDE")) {
00118 if (id.startsWith("kde-")) {
00119
00120 QString subtext("KDE3");
00121 if (!match.subtext().isEmpty()) {
00122 subtext.append(", " + match.subtext());
00123 }
00124
00125 match.setSubtext(subtext);
00126 } else {
00127 relevance += .1;
00128 }
00129 }
00130
00131
00132 match.setRelevance(relevance);
00133 matches << match;
00134 }
00135
00136 context.addMatches(term, matches);
00137 }
00138
00139 void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00140 {
00141 Q_UNUSED(context);
00142 QMutexLocker lock(bigLock());
00143 KService::Ptr service = KService::serviceByStorageId(match.data().toString());
00144 if (service) {
00145 KRun::run(*service, KUrl::List(), 0);
00146 }
00147 }
00148
00149 void ServiceRunner::setupAction(const KService::Ptr &service, Plasma::QueryMatch &match)
00150 {
00151 const QString name = service->name();
00152
00153 match.setText(name);
00154 match.setData(service->storageId());
00155
00156 if (!service->genericName().isEmpty() && service->genericName() != name) {
00157 match.setSubtext(service->genericName());
00158 } else if (!service->comment().isEmpty()) {
00159 match.setSubtext(service->comment());
00160 }
00161
00162 if (!service->icon().isEmpty()) {
00163 match.setIcon(KIcon(service->icon()));
00164 }
00165 }
00166
00167 #include "servicerunner.moc"
00168