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 | m_process->setProcessChannelMode(QProcess::SeparateChannels); | ||||
36 | } | ||||
37 | | ||||
38 | KateToolRunner::~KateToolRunner() | ||||
39 | { | ||||
40 | } | ||||
41 | | ||||
42 | KTextEditor::View* KateToolRunner::view() const | ||||
43 | { | ||||
44 | return m_view; | ||||
45 | } | ||||
46 | | ||||
47 | KateExternalTool* KateToolRunner::tool() const | ||||
48 | { | ||||
49 | return m_tool.get(); | ||||
50 | } | ||||
51 | | ||||
52 | void KateToolRunner::run() | ||||
53 | { | ||||
54 | if (!m_tool->workingDir.isEmpty()) { | ||||
55 | m_process->setWorkingDirectory(m_tool->workingDir); | ||||
56 | } else if (m_view) { | ||||
57 | // if nothing is set, use the current document's directory | ||||
58 | const auto url = m_view->document()->url(); | ||||
59 | if (url.isValid()) { | ||||
60 | const QString path = m_view->document()->url().toString(QUrl::RemoveScheme | QUrl::RemoveFilename); | ||||
61 | m_process->setWorkingDirectory(path); | ||||
62 | } | ||||
63 | } | ||||
64 | | ||||
65 | QObject::connect(m_process.get(), &QProcess::readyReadStandardOutput, [this]() { | ||||
66 | m_stdout += m_process->readAllStandardOutput(); | ||||
67 | }); | ||||
68 | QObject::connect(m_process.get(), &QProcess::readyReadStandardError, [this]() { | ||||
69 | m_stderr += m_process->readAllStandardError(); | ||||
70 | }); | ||||
71 | QObject::connect(m_process.get(), static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), | ||||
72 | [this](int exitCode, QProcess::ExitStatus exitStatus) { | ||||
73 | Q_EMIT toolFinished(this, exitCode, exitStatus == QProcess::CrashExit); | ||||
74 | }); | ||||
75 | | ||||
76 | // Write stdin to process, if applicable, then close write channel | ||||
77 | QObject::connect(m_process.get(), &QProcess::started, [this]() { | ||||
78 | if (!m_tool->input.isEmpty()) { | ||||
79 | m_process->write(m_tool->input.toLocal8Bit()); | ||||
80 | } | ||||
81 | m_process->closeWriteChannel(); | ||||
82 | }); | ||||
83 | | ||||
84 | const QStringList args = KShell::splitArgs(m_tool->arguments); | ||||
85 | m_process->start(m_tool->executable, args); | ||||
86 | } | ||||
87 | | ||||
88 | void KateToolRunner::waitForFinished() | ||||
89 | { | ||||
90 | m_process->waitForFinished(); | ||||
91 | } | ||||
92 | | ||||
93 | QString KateToolRunner::outputData() const | ||||
94 | { | ||||
95 | return QString::fromLocal8Bit(m_stdout); | ||||
96 | } | ||||
97 | | ||||
98 | QString KateToolRunner::errorData() const | ||||
99 | { | ||||
100 | return QString::fromLocal8Bit(m_stderr); | ||||
101 | } | ||||
102 | | ||||
103 | // kate: space-indent on; indent-width 4; replace-tabs on; |