Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/katetoolrunner.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2019 Dominik Haumann <dhaumann@kde.org> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "katetoolrunner.h" | ||||
22 | | ||||
23 | #include "kateexternaltool.h" | ||||
24 | | ||||
25 | #include <KShell> | ||||
26 | #include <KTextEditor/View> | ||||
27 | #include <KLocalizedString> | ||||
28 | | ||||
29 | KateToolRunner::KateToolRunner(std::unique_ptr<KateExternalTool> tool, KTextEditor::View * view, QObject* parent) | ||||
30 | : QObject(parent) | ||||
31 | , m_view(view) | ||||
32 | , m_tool(std::move(tool)) | ||||
33 | , m_process(new QProcess()) | ||||
34 | { | ||||
35 | } | ||||
36 | | ||||
37 | KateToolRunner::~KateToolRunner() | ||||
38 | { | ||||
39 | } | ||||
40 | | ||||
41 | KTextEditor::View* KateToolRunner::view() const | ||||
42 | { | ||||
43 | return m_view; | ||||
44 | } | ||||
45 | | ||||
46 | KateExternalTool* KateToolRunner::tool() const | ||||
47 | { | ||||
48 | return m_tool.get(); | ||||
49 | } | ||||
50 | | ||||
51 | void KateToolRunner::run() | ||||
52 | { | ||||
53 | if (m_tool->includeStderr) { | ||||
54 | m_process->setProcessChannelMode(QProcess::MergedChannels); | ||||
55 | } | ||||
56 | | ||||
57 | if (!m_tool->workingDir.isEmpty()) { | ||||
58 | m_process->setWorkingDirectory(m_tool->workingDir); | ||||
59 | } | ||||
60 | | ||||
61 | QObject::connect(m_process.get(), &QProcess::readyRead, this, &KateToolRunner::slotReadyRead); | ||||
62 | QObject::connect(m_process.get(), static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, | ||||
63 | &KateToolRunner::handleToolFinished); | ||||
64 | | ||||
65 | // Write stdin to process, if applicable, then close write channel | ||||
66 | QObject::connect(m_process.get(), &QProcess::started, [this]() { | ||||
67 | if (!m_tool->input.isEmpty()) { | ||||
68 | m_process->write(m_tool->input.toLocal8Bit()); | ||||
69 | } | ||||
70 | m_process->closeWriteChannel(); | ||||
71 | }); | ||||
72 | | ||||
73 | const QStringList args = KShell::splitArgs(m_tool->arguments); | ||||
74 | m_process->start(m_tool->executable, args); | ||||
75 | } | ||||
76 | | ||||
77 | void KateToolRunner::waitForFinished() | ||||
78 | { | ||||
79 | m_process->waitForFinished(); | ||||
80 | } | ||||
81 | | ||||
82 | QString KateToolRunner::outputData() const | ||||
83 | { | ||||
84 | return QString::fromLocal8Bit(m_output); | ||||
85 | } | ||||
86 | | ||||
87 | void KateToolRunner::slotReadyRead() | ||||
88 | { | ||||
89 | m_output += m_process->readAll(); | ||||
90 | } | ||||
91 | | ||||
92 | void KateToolRunner::handleToolFinished(int exitCode, QProcess::ExitStatus exitStatus) | ||||
93 | { | ||||
94 | if (exitCode != 0) { | ||||
95 | // FIXME: somehow tell user | ||||
96 | qWarning() << i18n("External tool %1 finished with non-zero exit code: %2", m_tool->name, exitCode); | ||||
97 | } | ||||
98 | | ||||
99 | if (exitStatus != QProcess::NormalExit) { | ||||
100 | // FIXME: somehow tell user | ||||
101 | qWarning() << i18n("External tool crashed: %1", m_tool->name); | ||||
102 | } | ||||
103 | | ||||
104 | Q_EMIT toolFinished(this); | ||||
105 | } | ||||
106 | | ||||
107 | // kate: space-indent on; indent-width 4; replace-tabs on; |