File indexing completed on 2024-05-05 05:51:23

0001 /* This file is part of the KDE project
0002  *
0003  *  SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QByteArray>
0011 #include <QObject>
0012 #include <QPointer>
0013 #include <QProcess>
0014 #include <QString>
0015 
0016 #include <memory>
0017 
0018 class KateExternalTool;
0019 namespace KTextEditor
0020 {
0021 class View;
0022 }
0023 
0024 /**
0025  * Helper class to run a KateExternalTool.
0026  */
0027 class KateToolRunner : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     /**
0033      * Constructor that will run @p tool in the run() method.
0034      * The @p view can later be retrieved again with view() to process the data when the tool is finished.
0035      */
0036     KateToolRunner(std::unique_ptr<KateExternalTool> tool, KTextEditor::View *view, QObject *parent = nullptr);
0037 
0038     KateToolRunner(const KateToolRunner &) = delete;
0039     void operator=(const KateToolRunner &) = delete;
0040 
0041     ~KateToolRunner() override;
0042 
0043     /**
0044      * Returns the view that was active when running the tool.
0045      * @warning May be a nullptr, since the view could have been closed in the meantime.
0046      */
0047     KTextEditor::View *view() const;
0048 
0049     /**
0050      * Returns the tool that was passed in the constructor.
0051      */
0052     KateExternalTool *tool() const;
0053 
0054     /**
0055      * Starts a child process that executes the tool.
0056      */
0057     void run();
0058 
0059     /**
0060      * Blocking call that waits until the tool is finished.
0061      * Used internally for unit testing.
0062      */
0063     void waitForFinished();
0064 
0065     /**
0066      * Returns the data that was collected on stdout.
0067      */
0068     QString outputData() const;
0069 
0070     /**
0071      * Returns the data that was collected on stderr.
0072      */
0073     QString errorData() const;
0074 
0075 Q_SIGNALS:
0076     /**
0077      * This signal is emitted when the tool is finished.
0078      */
0079     void toolFinished(KateToolRunner *runner, int exitCode, bool crashed);
0080 
0081 private:
0082     //! Use QPointer here, since the View may be closed in the meantime.
0083     QPointer<KTextEditor::View> m_view;
0084 
0085     //! We are the owner of the tool (it was copied)
0086     std::unique_ptr<KateExternalTool> m_tool;
0087 
0088     //! Child process that runs the tool
0089     std::unique_ptr<QProcess> m_process;
0090 
0091     //! Collect stdout
0092     QByteArray m_stdout;
0093 
0094     //! Collect stderr
0095     QByteArray m_stderr;
0096 };
0097 
0098 // kate: space-indent on; indent-width 4; replace-tabs on;