1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
* Copyright (C) 2014 Vishesh Handa <vhanda@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "servicerunner.h"
#include <QMimeData>
#include <QIcon>
#include <QDebug>
#include <QUrl>
#include <KActivities/ResourceInstance>
#include <KLocalizedString>
#include <KRun>
#include <KService>
#include <KServiceTypeTrader>
ServiceRunner::ServiceRunner(QObject *parent, const QVariantList &args)
: Plasma::AbstractRunner(parent, args)
{
Q_UNUSED(args)
setObjectName( QStringLiteral("Application" ));
setPriority(AbstractRunner::HighestPriority);
addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds applications whose name or description match :q:")));
}
ServiceRunner::~ServiceRunner()
{
}
QStringList ServiceRunner::categories() const
{
QStringList cat;
cat << i18n("Applications") << i18n("System Settings");
return cat;
}
QIcon ServiceRunner::categoryIcon(const QString& category) const
{
if (category == i18n("Applications")) {
return QIcon::fromTheme(QStringLiteral("applications-other"));
} else if (category == i18n("System Settings")) {
return QIcon::fromTheme(QStringLiteral("preferences-system"));
}
return Plasma::AbstractRunner::categoryIcon(category);
}
void ServiceRunner::match(Plasma::RunnerContext &context)
{
const QString term = context.query();
QList<Plasma::QueryMatch> matches;
QSet<QString> seen;
QString query;
if (term.length() > 1) {
// Search for applications which are executable and case-insensitively match the search term
// See http://techbase.kde.org/Development/Tutorials/Services/Traders#The_KTrader_Query_Language
// if the following is unclear to you.
query = QStringLiteral("exist Exec and ('%1' =~ Name)").arg(term);
KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
if (!services.isEmpty()) {
//qDebug() << service->name() << "is an exact match!" << service->storageId() << service->exec();
foreach (const KService::Ptr &service, services) {
if (!service->noDisplay() && service->property(QStringLiteral("NotShowIn"), QVariant::String) != "KDE") {
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::ExactMatch);
setupMatch(service, match);
match.setRelevance(1);
matches << match;
seen.insert(service->storageId());
seen.insert(service->exec());
}
}
}
}
if (!context.isValid()) {
return;
}
// If the term length is < 3, no real point searching the Keywords and GenericName
if (term.length() < 3) {
query = QStringLiteral("exist Exec and ( (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) )").arg(term);
} else {
// Search for applications which are executable and the term case-insensitive matches any of
// * a substring of one of the keywords
// * a substring of the GenericName field
// * a substring of the Name field
// Note that before asking for the content of e.g. Keywords and GenericName we need to ask if
// they exist to prevent a tree evaluation error if they are not defined.
query = QStringLiteral("exist Exec and ( (exist Keywords and '%1' ~subin Keywords) or (exist GenericName and '%1' ~~ GenericName) or (exist Name and '%1' ~~ Name) or ('%1' ~~ Exec) or (exist Comment and '%1' ~~ Comment) )").arg(term);
}
KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
services += KServiceTypeTrader::self()->query(QStringLiteral("KCModule"), query);
//qDebug() << "got " << services.count() << " services from " << query;
foreach (const KService::Ptr &service, services) {
if (service->noDisplay()) {
continue;
}
const QString id = service->storageId();
const QString name = service->desktopEntryName();
const QString exec = service->exec();
if (seen.contains(id) || seen.contains(exec)) {
//qDebug() << "already seen" << id << exec;
continue;
}
//qDebug() << "haven't seen" << id << "so processing now";
seen.insert(id);
seen.insert(exec);
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::PossibleMatch);
setupMatch(service, match);
qreal relevance(0.6);
// If the term was < 3 chars and NOT at the beginning of the App's name or Exec, then
// chances are the user doesn't want that app.
if (term.length() < 3) {
if (name.startsWith(term) || exec.startsWith(term)) {
relevance = 0.9;
} else {
continue;
}
} else if (service->name().contains(term, Qt::CaseInsensitive)) {
relevance = 0.8;
if (service->name().startsWith(term, Qt::CaseInsensitive)) {
relevance += 0.1;
}
} else if (service->genericName().contains(term, Qt::CaseInsensitive)) {
relevance = 0.65;
if (service->genericName().startsWith(term, Qt::CaseInsensitive)) {
relevance += 0.05;
}
} else if (service->exec().contains(term, Qt::CaseInsensitive)) {
relevance = 0.7;
if (service->exec().startsWith(term, Qt::CaseInsensitive)) {
relevance += 0.05;
}
} else if (service->comment().contains(term, Qt::CaseInsensitive)) {
relevance = 0.5;
if (service->comment().startsWith(term, Qt::CaseInsensitive)) {
relevance += 0.05;
}
}
if (service->categories().contains(QStringLiteral("KDE")) || service->serviceTypes().contains(QStringLiteral("KCModule"))) {
//qDebug() << "found a kde thing" << id << match.subtext() << relevance;
if (id.startsWith(QLatin1String("kde-"))) {
//qDebug() << "old" << service->type();
if (!service->isApplication()) {
// avoid showing old kcms and what not
continue;
}
// This is an older version, let's disambiguate it
QString subtext(QStringLiteral("KDE3"));
if (!match.subtext().isEmpty()) {
subtext.append(", " + match.subtext());
}
match.setSubtext(subtext);
} else {
relevance += .09;
}
}
//qDebug() << service->name() << "is this relevant:" << relevance;
match.setRelevance(relevance);
if (service->serviceTypes().contains(QStringLiteral("KCModule"))) {
match.setMatchCategory(i18n("System Settings"));
}
matches << match;
}
//search for applications whose categories contains the query
query = QStringLiteral("exist Exec and (exist Categories and '%1' ~subin Categories)").arg(term);
services = KServiceTypeTrader::self()->query(QStringLiteral("Application"), query);
//qDebug() << service->name() << "is an exact match!" << service->storageId() << service->exec();
foreach (const KService::Ptr &service, services) {
if (!service->noDisplay()) {
QString id = service->storageId();
QString exec = service->exec();
if (seen.contains(id) || seen.contains(exec)) {
//qDebug() << "already seen" << id << exec;
continue;
}
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::PossibleMatch);
setupMatch(service, match);
qreal relevance = 0.6;
if (service->categories().contains(QStringLiteral("X-KDE-More")) ||
!service->showInCurrentDesktop()) {
relevance = 0.5;
}
if (service->isApplication()) {
relevance += .04;
}
match.setRelevance(relevance);
matches << match;
}
}
// search for jump list actions
if (term.length() >= 3) {
query = QStringLiteral("exist Actions"); // doesn't work
services = KServiceTypeTrader::self()->query(QStringLiteral("Application"));//, query);
foreach (const KService::Ptr &service, services) {
if (service->noDisplay()) {
continue;
}
foreach (const KServiceAction &action, service->actions()) {
if (action.text().isEmpty() || action.exec().isEmpty() || seen.contains(action.exec())) {
continue;
}
if (!action.text().contains(term, Qt::CaseInsensitive)) {
continue;
}
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::HelperMatch);
if (!action.icon().isEmpty()) {
match.setIconName(action.icon());
} else {
match.setIconName(service->icon());
}
match.setText(i18nc("Jump list search result, %1 is action (eg. open new tab), %2 is application (eg. browser)",
"%1 - %2", action.text(), service->name()));
match.setData(action.exec());
qreal relevance = 0.5;
if (action.text().startsWith(term, Qt::CaseInsensitive)) {
relevance += 0.05;
}
match.setRelevance(relevance);
matches << match;
}
}
}
//context.addMatches(term, matches);
context.addMatches(matches);
}
void ServiceRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
Q_UNUSED(context);
if (match.type() == Plasma::QueryMatch::HelperMatch) { // Jump List Action
KRun::run(match.data().toString(), {}, nullptr);
return;
}
KService::Ptr service = KService::serviceByStorageId(match.data().toString());
if (service) {
KActivities::ResourceInstance::notifyAccessed(
QUrl(QStringLiteral("applications:") + service->storageId()),
QStringLiteral("org.kde.krunner")
);
KRun::runService(*service, {}, nullptr, true);
}
}
void ServiceRunner::setupMatch(const KService::Ptr &service, Plasma::QueryMatch &match)
{
const QString name = service->name();
match.setText(name);
match.setData(service->storageId());
if (!service->genericName().isEmpty() && service->genericName() != name) {
match.setSubtext(service->genericName());
} else if (!service->comment().isEmpty()) {
match.setSubtext(service->comment());
}
if (!service->icon().isEmpty()) {
match.setIconName(service->icon());
}
}
QMimeData * ServiceRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
{
KService::Ptr service = KService::serviceByStorageId(match.data().toString());
if (service) {
QMimeData * result = new QMimeData();
QList<QUrl> urls;
urls << QUrl::fromLocalFile(service->entryPath());
qDebug() << urls;
result->setUrls(urls);
return result;
}
return 0;
}
K_EXPORT_PLASMA_RUNNER(services, ServiceRunner)
#include "servicerunner.moc"
|