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 | | ||||
27 | KateToolRunner::KateToolRunner(KateExternalTool* tool, QObject* parent) | ||||
28 | : QObject(parent) | ||||
29 | , m_tool(tool) | ||||
30 | , m_process(new QProcess()) | ||||
31 | { | ||||
32 | } | ||||
33 | | ||||
34 | KateToolRunner::~KateToolRunner() | ||||
35 | { | ||||
36 | delete m_tool; | ||||
37 | m_tool = nullptr; | ||||
38 | | ||||
39 | delete m_process; | ||||
40 | m_process = nullptr; | ||||
41 | } | ||||
42 | | ||||
43 | void KateToolRunner::run() | ||||
44 | { | ||||
45 | if (m_tool->includeStderr) { | ||||
46 | m_process->setProcessChannelMode(QProcess::MergedChannels); | ||||
47 | } | ||||
48 | | ||||
49 | if (!m_tool->workingDir.isEmpty()) { | ||||
50 | m_process->setWorkingDirectory(m_tool->workingDir); | ||||
51 | } | ||||
52 | | ||||
53 | QObject::connect(m_process, &QProcess::readyRead, this, &KateToolRunner::slotReadyRead); | ||||
54 | QObject::connect(m_process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, | ||||
55 | &KateToolRunner::handleToolFinished); | ||||
56 | | ||||
57 | // Write stdin to process, if applicable, then close write channel | ||||
58 | QObject::connect(m_process, &QProcess::started, [this]() { | ||||
59 | if (!m_tool->input.isEmpty()) { | ||||
60 | m_process->write(m_tool->input.toLocal8Bit()); | ||||
61 | } | ||||
62 | m_process->closeWriteChannel(); | ||||
63 | }); | ||||
64 | | ||||
65 | const QStringList args = KShell::splitArgs(m_tool->arguments); | ||||
66 | m_process->start(m_tool->executable, args); | ||||
67 | } | ||||
68 | | ||||
69 | void KateToolRunner::waitForFinished() | ||||
70 | { | ||||
71 | m_process->waitForFinished(); | ||||
72 | } | ||||
73 | | ||||
74 | QString KateToolRunner::outputData() const | ||||
75 | { | ||||
76 | return QString::fromLocal8Bit(m_output); | ||||
77 | } | ||||
78 | | ||||
79 | void KateToolRunner::slotReadyRead() | ||||
80 | { | ||||
81 | m_output += m_process->readAll(); | ||||
82 | } | ||||
83 | | ||||
84 | void KateToolRunner::handleToolFinished(int exitCode, QProcess::ExitStatus exitStatus) | ||||
85 | { | ||||
86 | if (exitCode != 0) { | ||||
87 | // FIXME: somehow tell user | ||||
88 | return; | ||||
89 | } | ||||
90 | | ||||
91 | if (exitStatus != QProcess::NormalExit) { | ||||
92 | // FIXME: somehow tell user | ||||
93 | return; | ||||
94 | } | ||||
95 | | ||||
96 | // FIXME: process m_output depending on the tool's outputMode | ||||
97 | | ||||
98 | Q_EMIT toolFinished(this); | ||||
99 | } | ||||
100 | | ||||
101 | // kate: space-indent on; indent-width 4; replace-tabs on; |