recentdocuments.cpp 4.38 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 2008 Sebastian Kügler <sebas@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 as
 *   published by the Free Software Foundation; either version 2, or
 *   (at your option) any later version.
 *
 *   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 "recentdocuments.h"

22 23
#include <QAction>
#include <QDir>
24 25 26
#include <QMimeData>

#include <KLocalizedString>
27
#include <KIO/OpenFileManagerWindowJob>
28
#include <KIO/OpenUrlJob>
29
#include <KIO/Job>
30
#include <KNotificationJobUiDelegate>
31 32 33 34 35 36 37 38
#include <KShell>

#include <KActivities/Stats/ResultModel>
#include <KActivities/Stats/Terms>
#include <KActivities/Stats/Query>

using namespace KActivities::Stats;
using namespace KActivities::Stats::Terms;
39

40
K_EXPORT_PLASMA_RUNNER_WITH_JSON(RecentDocuments, "plasma-runner-recentdocuments.json")
41

42 43
RecentDocuments::RecentDocuments(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
    : Plasma::AbstractRunner(parent, metaData, args)
44
{
Alexander Lohnau's avatar
Alexander Lohnau committed
45
    setObjectName(QStringLiteral("Recent Documents"));
46

47
    addSyntax(Plasma::RunnerSyntax(QStringLiteral(":q:"), i18n("Looks for documents recently used with names matching :q:.")));
48

Alexander Lohnau's avatar
Alexander Lohnau committed
49
    addAction(QStringLiteral("openParentDir"), QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Open Containing Folder"));
50
    setMinLetterCount(3);
51 52 53 54 55 56 57 58
}

RecentDocuments::~RecentDocuments()
{
}

void RecentDocuments::match(Plasma::RunnerContext &context)
{
59
    if (!context.isValid()) {
60 61 62
        return;
    }

63
    // clang-format off
64
    const QString term = context.query();
65 66 67 68 69 70
    auto query = UsedResources
            | Activity::current()
            | Order::RecentlyUsedFirst
            | Agent::any()
            // we search only on file name, as KActivity does not support better options
            | Url("/*/" + term + "*")
Alexander Lohnau's avatar
Alexander Lohnau committed
71
            | Limit(20);
72
    // clang-format on
73 74 75

    const auto result = new ResultModel(query);

Alexander Lohnau's avatar
Alexander Lohnau committed
76
    for (int i = 0; i < result->rowCount(); ++i) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        const auto index = result->index(i, 0);

        const auto url = QUrl::fromUserInput(result->data(index, ResultModel::ResourceRole).toString(),
                                             QString(),
                                             // We can assume local file thanks to the request Url
                                             QUrl::AssumeLocalFile);
        const auto name = result->data(index, ResultModel::TitleRole).toString();

        Plasma::QueryMatch match(this);

        auto relevance = 0.5;
        match.setType(Plasma::QueryMatch::PossibleMatch);
        if (url.fileName() == term) {
            relevance = 1.0;
            match.setType(Plasma::QueryMatch::ExactMatch);
Alexander Lohnau's avatar
Alexander Lohnau committed
92
        } else if (url.fileName().startsWith(term)) {
93 94
            relevance = 0.9;
            match.setType(Plasma::QueryMatch::PossibleMatch);
95
        }
96 97 98
        match.setIconName(KIO::iconNameForUrl(url));
        match.setRelevance(relevance);
        match.setData(QVariant(url));
99 100 101
        if (url.isLocalFile()) {
            match.setActions(actions().values());
        }
102
        match.setText(name);
103

104 105
        QString destUrlString = KShell::tildeCollapse(url.adjusted(QUrl::RemoveFilename).path());
        match.setSubtext(destUrlString);
106

107
        context.addMatch(match);
108 109 110 111 112 113
    }
}

void RecentDocuments::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(context)
114

115
    const QUrl url = match.data().toUrl();
116

Alexander Lohnau's avatar
Alexander Lohnau committed
117
    if (match.selectedAction()) {
118
        KIO::highlightInFileManager({url});
119 120 121
        return;
    }

122 123 124 125
    auto *job = new KIO::OpenUrlJob(url);
    job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
    job->setRunExecutables(false);
    job->start();
126 127
}

128 129 130
QMimeData * RecentDocuments::mimeDataForMatch(const Plasma::QueryMatch& match)
{
    QMimeData *result = new QMimeData();
131
    result->setUrls({match.data().toUrl()});
132 133 134 135
    return result;
}

#include "recentdocuments.moc"