locationrunner.cpp 4.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *   Copyright (C) 2007 Teemu Rytilahti <tpr@iki.fi>
 *
 *   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 "locationrunner.h"

#include <QMimeData>
Vishesh Handa's avatar
Vishesh Handa committed
22
#include <QIcon>
23
#include <QUrl>
24
#include <QDir>
25 26

#include <QDebug>
27
#include <KApplicationTrader>
28
#include <KLocalizedString>
29 30
#include <KProtocolInfo>
#include <KUriFilter>
31
#include <KIO/DesktopExecParser>
32
#include <KIO/Global>
33
#include <KShell>
34 35
#include <KIO/OpenUrlJob>
#include <KNotificationJobUiDelegate>
36

37
K_EXPORT_PLASMA_RUNNER_WITH_JSON(LocationsRunner, "plasma-runner-locations.json")
38 39


40 41
LocationsRunner::LocationsRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList& args)
    : Plasma::AbstractRunner(parent, metaData, args)
42 43
{
    // set the name shown after the result in krunner window
44 45
    setObjectName(QStringLiteral("Locations"));
    addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"),
46 47 48 49 50 51 52 53 54 55
              i18n("Finds local directories and files, network locations and Internet sites with paths matching :q:.")));
}

LocationsRunner::~LocationsRunner()
{
}

void LocationsRunner::match(Plasma::RunnerContext &context)
{
    QString term = context.query();
56 57 58
    QUrl url(term);
    const bool isLocalFile = url.isLocalFile();
    const QFileInfo fileInfo = QFileInfo(isLocalFile ? url.toLocalFile() : KShell::tildeExpand(term));
59

60
    if (fileInfo.exists()) {
61 62 63
        Plasma::QueryMatch match(this);
        match.setType(Plasma::QueryMatch::ExactMatch);
        match.setText(i18n("Open %1", term));
64
        match.setIconName(fileInfo.isFile() ? KIO::iconNameForUrl(url) : QStringLiteral("system-file-manager"));
65 66

        match.setRelevance(1);
67
        match.setData(isLocalFile ? url : QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
68 69
        match.setType(Plasma::QueryMatch::ExactMatch);
        context.addMatch(match);
70 71
    } else if (!isLocalFile && !url.isEmpty()) {
        if (!KUriFilter::self()->filterUri(term, {QStringLiteral("kshorturifilter")})) {
72 73 74
            return;
        }

75 76
        url = QUrl(term); // The KUriFilter changed the term
        if (url.scheme().isEmpty() || url.isLocalFile()) { // We handled existend files above
77 78
            return;
        }
79
        const QString protocol = url.scheme();
80 81
        Plasma::QueryMatch match(this);
        match.setData(url);
82 83 84 85 86 87 88 89 90 91 92

        if (!KProtocolInfo::isKnownProtocol(protocol) || KProtocolInfo::isHelperProtocol(protocol)) {
            const KService::Ptr service = KApplicationTrader::preferredService(QLatin1String("x-scheme-handler/") + protocol);
            if (service) {
                match.setIconName(service->icon());
                match.setText(i18n("Launch with %1", service->name()));
            } else if (KProtocolInfo::isKnownProtocol(protocol)) {
                Q_ASSERT(KProtocolInfo::isHelperProtocol(protocol));
                match.setIconName(KProtocolInfo::icon(protocol));
                match.setText(i18n("Launch with %1", KIO::DesktopExecParser::executableName(
                                       KProtocolInfo::exec(protocol))));
93
            } else {
94
                return;
95 96
            }
        } else {
97 98 99 100 101 102
            match.setIconName(KProtocolInfo::icon(protocol));
            match.setText(i18n("Go to %1", url.toDisplayString(QUrl::PreferLocalFile)));
        }

        if (url.scheme() == QLatin1String("mailto")) {
            match.setText(i18n("Send email to %1", url.path()));
103 104 105 106 107 108 109
        }
        context.addMatch(match);
    }
}

void LocationsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
110
    Q_UNUSED(context)
111

112
    auto *job = new KIO::OpenUrlJob(match.data().toUrl());
113 114 115
    job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
    job->setRunExecutables(false);
    job->start();
116 117
}

118
QMimeData * LocationsRunner::mimeDataForMatch(const Plasma::QueryMatch &match)
119
{
120 121 122
    QMimeData *result = new QMimeData();
    result->setUrls({match.data().toUrl()});
    return result;
123 124 125 126
}


#include "locationrunner.moc"