File indexing completed on 2024-05-05 17:45:01

0001 /*
0002     SPDX-FileCopyrightText: 2007 Teemu Rytilahti <tpr@iki.fi>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "locationrunner.h"
0008 
0009 #include <QDir>
0010 #include <QIcon>
0011 #include <QMimeData>
0012 #include <QUrl>
0013 
0014 #include <KApplicationTrader>
0015 #include <KIO/DesktopExecParser>
0016 #include <KIO/Global>
0017 #include <KIO/OpenUrlJob>
0018 #include <KLocalizedString>
0019 #include <KNotificationJobUiDelegate>
0020 #include <KProtocolInfo>
0021 #include <KShell>
0022 #include <KUriFilter>
0023 #include <QDebug>
0024 
0025 K_PLUGIN_CLASS_WITH_JSON(LocationsRunner, "plasma-runner-locations.json")
0026 
0027 LocationsRunner::LocationsRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
0028     : Plasma::AbstractRunner(parent, metaData, args)
0029 {
0030     // set the name shown after the result in krunner window
0031     setObjectName(QStringLiteral("Locations"));
0032     addSyntax(
0033         Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Finds local directories and files, network locations and Internet sites with paths matching :q:.")));
0034 }
0035 
0036 LocationsRunner::~LocationsRunner()
0037 {
0038 }
0039 
0040 void LocationsRunner::match(Plasma::RunnerContext &context)
0041 {
0042     QString term = context.query();
0043     // If we have a query with an executable and optionally arguments, BUG: 433053
0044     const QStringList split = KShell::splitArgs(term);
0045     if (!split.isEmpty()) {
0046         QFileInfo tmpInfo(KShell::tildeExpand(split.constFirst()));
0047         if (tmpInfo.isFile() && tmpInfo.isExecutable()) {
0048             return;
0049         }
0050     }
0051     // We want to expand ENV variables like $HOME to get the actual path, BUG: 358221
0052     KUriFilter::self()->filterUri(term, {QStringLiteral("kshorturifilter")});
0053     const QUrl url(term);
0054     // The uri filter takes care of the shell expansion
0055     const QFileInfo fileInfo = QFileInfo(url.toLocalFile());
0056 
0057     if (fileInfo.exists()) {
0058         Plasma::QueryMatch match(this);
0059         match.setText(i18n("Open %1", context.query()));
0060         match.setIconName(fileInfo.isFile() ? KIO::iconNameForUrl(url) : QStringLiteral("system-file-manager"));
0061 
0062         match.setRelevance(1);
0063         match.setData(url);
0064         match.setUrls({url});
0065         match.setType(Plasma::QueryMatch::ExactMatch);
0066         context.addMatch(match);
0067     } else if (!url.isLocalFile() && !url.isEmpty() && !url.scheme().isEmpty()) {
0068         const QString protocol = url.scheme();
0069         Plasma::QueryMatch match(this);
0070         match.setType(Plasma::QueryMatch::PossibleMatch);
0071         match.setData(url);
0072         match.setUrls({url});
0073 
0074         if (!KProtocolInfo::isKnownProtocol(protocol) || KProtocolInfo::isHelperProtocol(protocol)) {
0075             const KService::Ptr service = KApplicationTrader::preferredService(QLatin1String("x-scheme-handler/") + protocol);
0076             if (service) {
0077                 match.setIconName(service->icon());
0078                 match.setText(i18n("Launch with %1", service->name()));
0079             } else if (KProtocolInfo::isKnownProtocol(protocol)) {
0080                 Q_ASSERT(KProtocolInfo::isHelperProtocol(protocol));
0081                 match.setIconName(KProtocolInfo::icon(protocol));
0082                 match.setText(i18n("Launch with %1", KIO::DesktopExecParser::executableName(KProtocolInfo::exec(protocol))));
0083             } else {
0084                 return;
0085             }
0086         } else {
0087             match.setIconName(KProtocolInfo::icon(protocol));
0088             match.setText(i18n("Go to %1", url.toDisplayString(QUrl::PreferLocalFile)));
0089             // in case of https://phabricator.kde.org we add a slash at the end to make it comparable to results of other runners
0090             if (url.scheme() == QLatin1String("https") && url.toString().endsWith(url.host())) {
0091                 match.setId(url.toString() + QLatin1Char('/'));
0092             }
0093         }
0094 
0095         if (url.scheme() == QLatin1String("mailto")) {
0096             match.setText(i18n("Send email to %1", url.path()));
0097         }
0098         context.addMatch(match);
0099     }
0100 }
0101 
0102 void LocationsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
0103 {
0104     Q_UNUSED(context)
0105 
0106     auto *job = new KIO::OpenUrlJob(match.data().toUrl());
0107     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
0108     job->setRunExecutables(false);
0109     job->start();
0110 }
0111 
0112 #include "locationrunner.moc"