FileFilterHotspot.cpp 9.4 KB
Newer Older
1
/*
2
3
    SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
    SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@gmail.com>
4

5
    SPDX-License-Identifier: GPL-2.0-or-later
6
7
8
9
10
11
12
13
14
15
16
*/

#include "FileFilterHotspot.h"

#include <QApplication>
#include <QAction>
#include <QBuffer>
#include <QClipboard>
#include <QMenu>
#include <QTimer>
#include <QToolTip>
17
#include <QMouseEvent>
18
#include <QKeyEvent>
19
#include <QRegularExpression>
20

21
22
#include <kio_version.h>
#if KIO_VERSION < QT_VERSION_CHECK(5, 71, 0)
23
#include <KRun>
24
25
26
27
28
29
#else
#include <KIO/ApplicationLauncherJob>
#include <KIO/OpenUrlJob>
#endif

#include <KIO/JobUiDelegate>
30
31
#include <KLocalizedString>
#include <KFileItemListProperties>
32
33
#include <KMessageBox>
#include <KShell>
34
35
36

#include "konsoledebug.h"
#include "KonsoleSettings.h"
37
38
#include "profile/Profile.h"
#include "session/SessionManager.h"
39
#include "terminalDisplay/TerminalDisplay.h"
40
41
42
43
44

using namespace Konsole;


FileFilterHotSpot::FileFilterHotSpot(int startLine, int startColumn, int endLine, int endColumn,
45
46
47
48
                                     const QStringList &capturedTexts, const QString &filePath,
                                     Session *session)
  : RegExpFilterHotSpot(startLine, startColumn, endLine, endColumn, capturedTexts),
    _filePath(filePath),
49
50
    _session(session),
    _thumbnailFinished(false)
51
52
53
54
55
56
{
    setType(Link);
}

void FileFilterHotSpot::activate(QObject *)
{
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
    // Used to fallback to opening the url with the system default application
    auto openUrl = [](const QString &filePath) {
#if KIO_VERSION < QT_VERSION_CHECK(5, 71, 0)
        new KRun(QUrl::fromLocalFile(filePath), QApplication::activeWindow());
#else
        auto *job = new KIO::OpenUrlJob(QUrl::fromLocalFile(filePath));
        job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, QApplication::activeWindow()));
        job->setRunExecutables(false); // Always open, e.g. shell scripts, as text
        job->start();
#endif
    };

    // Output of e.g.:
    // - grep with line numbers: "path/to/some/file:123:"
    // - compiler errors with line/column numbers: "/path/to/file.cpp:123:123:"
    // - ctest failing unit tests: "/path/to/file(204)"
    const auto re(QRegularExpression(QStringLiteral(R"foo((?:[:\(]?(\d+)(?::?|\)\]))(?:(\d+):)?$)foo")));
    QRegularExpressionMatch match = re.match(_filePath);
    if (match.hasMatch()) {
        // The file path without the ":123" ... etc bits
        const QString path = _filePath.mid(0, match.capturedStart(0));

        if (!_session) {
            openUrl(path);
            return;
        }

        Profile::Ptr profile = SessionManager::instance()->sessionProfile(_session);
        QString editorCmd = profile->textEditorCmd();

        // If the cmd is empty or PATH and LINE don't exist, then the command
        // is malformed, ignore it and open with the default editor
        // TODO: show an error message to the user?
        if (editorCmd.isEmpty()
            || ! (editorCmd.contains(QLatin1String("PATH")) && editorCmd.contains(QLatin1String("LINE")))) {
            openUrl(path);
            return;
        }

        editorCmd.replace(QLatin1String("LINE"), match.captured(1));

        const QString col = match.captured(2);

        editorCmd.replace(QLatin1String("COLUMN"), !col.isEmpty() ? col : QLatin1String("0"));

        editorCmd.replace(QLatin1String("PATH"), path);

        qCDebug(KonsoleDebug) << "editorCmd:" << editorCmd;

        KService::Ptr service(new KService(QString(), editorCmd, QString()));

#if KIO_VERSION < QT_VERSION_CHECK(5, 71, 0)
        const bool success = KRun::runService(*service, {}, QApplication::activeWindow());
        if (!success) {
            openUrl(path);
        }
#else
        // ApplicationLauncherJob is better at reporting errors to the user than
        // CommandLauncherJob; no need to call job->setUrls() because the url is
        // already part of editorCmd
        auto *job = new KIO::ApplicationLauncherJob(service);
118
        connect(job, &KJob::result, this, [path, job, openUrl]() {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
            if (job->error()) {
                // TODO: use KMessageWidget (like the "terminal is read-only" message)
                KMessageBox::sorry(QApplication::activeWindow(),
                                   i18n("Could not open file with the text editor specified in the profile settings;\n"
                                        "it will be opened with the system default editor."));

                openUrl(path);
            }
        });
        job->start();
#endif
        return;
    }

    // There was no match, i.e. regular url "path/to/file", open it with
    // the system default editor
    // In case the regex above didn't match for any reason, clean up the file path
    QString path(_filePath);
    path.remove(QRegularExpression(QStringLiteral(R"foo((:\d+[:]?|:)$)foo"), QRegularExpression::DontCaptureOption));
    openUrl(path);
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
}


FileFilterHotSpot::~FileFilterHotSpot() = default;

QList<QAction *> FileFilterHotSpot::actions()
{
    QAction *action = new QAction(i18n("Copy Location"), this);
    action->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
    connect(action, &QAction::triggered, this, [this] {
        QGuiApplication::clipboard()->setText(_filePath);
    });
    return {action};
}

void FileFilterHotSpot::setupMenu(QMenu *menu)
{
    const KFileItem fileItem(QUrl::fromLocalFile(_filePath));
    const KFileItemList itemList({fileItem});
    const KFileItemListProperties itemProperties(itemList);
    _menuActions.setParent(this);
    _menuActions.setItemListProperties(itemProperties);
    _menuActions.addOpenWithActionsTo(menu);

    // Here we added the actions to the last part of the menu, but we need to move them up.
    // TODO: As soon as addOpenWithActionsTo accepts a index, change this.
    // https://bugs.kde.org/show_bug.cgi?id=423765
    QAction *firstAction = menu->actions().at(0);
    for (auto *action : menu->actions()) {
        if (action->text().toLower().remove(QLatin1Char('&')).contains(i18n("open with"))) {
            menu->removeAction(action);
            menu->insertAction(firstAction, action);
        }
    }
    auto *separator = new QAction(this);
    separator->setSeparator(true);
    menu->insertAction(firstAction, separator);
}

// Static variables for the HotSpot
bool FileFilterHotSpot::_canGenerateThumbnail = false;
QPointer<KIO::PreviewJob> FileFilterHotSpot::_previewJob;

void FileFilterHotSpot::requestThumbnail(Qt::KeyboardModifiers modifiers, const QPoint &pos) {
    _canGenerateThumbnail = true;
    _eventModifiers = modifiers;
    _eventPos = pos;

    // Defer the real creation of the thumbnail by a few msec.
    QTimer::singleShot(250, this, [this]{
        thumbnailRequested();
    });
}

void FileFilterHotSpot::stopThumbnailGeneration()
{
    _canGenerateThumbnail = false;
    if (_previewJob != nullptr) {
        _previewJob->deleteLater();
        QToolTip::hideText();
    }
}

void FileFilterHotSpot::showThumbnail(const KFileItem& item, const QPixmap& preview)
{
    if (!_canGenerateThumbnail) {
        return;
    }
    _thumbnailFinished = true;
    Q_UNUSED(item)
    QByteArray data;
    QBuffer buffer(&data);
    preview.save(&buffer, "PNG", 100);

    const auto tooltipString = QStringLiteral("<img src='data:image/png;base64, %0'>")
        .arg(QString::fromLocal8Bit(data.toBase64()));

    QToolTip::showText(_thumbnailPos, tooltipString, qApp->focusWidget());
}

void FileFilterHotSpot::thumbnailRequested() {
    if (!_canGenerateThumbnail) {
        return;
    }

    auto *settings = KonsoleSettings::self();

    Qt::KeyboardModifiers modifiers = settings->thumbnailCtrl() ? Qt::ControlModifier : Qt::NoModifier;
    modifiers |= settings->thumbnailAlt() ? Qt::AltModifier : Qt::NoModifier;
    modifiers |= settings->thumbnailShift() ? Qt::ShiftModifier : Qt::NoModifier;

    if (_eventModifiers != modifiers) {
        return;
    }

    _thumbnailPos = QPoint(_eventPos.x() + 100, _eventPos.y() - settings->thumbnailSize() / 2);

    const int size = KonsoleSettings::thumbnailSize();
    if (_previewJob != nullptr) {
        _previewJob->deleteLater();
    }

    _thumbnailFinished = false;

    // Show a "Loading" if Preview takes a long time.
    QTimer::singleShot(10, this, [this]{
        if (_previewJob == nullptr) {
            return;
        }
        if (!_thumbnailFinished) {
            QToolTip::showText(_thumbnailPos, i18n("Generating Thumbnail"), qApp->focusWidget());
        }
    });

    _previewJob = new KIO::PreviewJob(KFileItemList({fileItem()}), QSize(size, size));
    connect(_previewJob, &KIO::PreviewJob::gotPreview, this, &FileFilterHotSpot::showThumbnail);
    connect(_previewJob, &KIO::PreviewJob::failed, this, []{
        qCDebug(KonsoleDebug) << "Error generating the preview" << _previewJob->errorString();
        QToolTip::hideText();
    });

    _previewJob->setAutoDelete(true);
    _previewJob->start();
}

KFileItem FileFilterHotSpot::fileItem() const
{
    return KFileItem(QUrl::fromLocalFile(_filePath));
}
268

269
void FileFilterHotSpot::mouseEnterEvent(TerminalDisplay *td, QMouseEvent *ev)
270
{
271
    HotSpot::mouseEnterEvent(td, ev);
272
273
274
275
276
277
278
279
280
281
282
    requestThumbnail(ev->modifiers(), ev->globalPos());
}

void FileFilterHotSpot::mouseLeaveEvent(TerminalDisplay *td, QMouseEvent *ev)
{
    HotSpot::mouseLeaveEvent(td, ev);
    stopThumbnailGeneration();
}

void Konsole::FileFilterHotSpot::keyPressEvent(Konsole::TerminalDisplay* td, QKeyEvent* ev)
{
283
    HotSpot::keyPressEvent(td, ev);
284
    requestThumbnail(ev->modifiers(), QCursor::pos());
285
}