File indexing completed on 2024-05-05 04:38:47

0001 /*
0002     SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
0003     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0004     SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef _PROCESSLINEMAKER_H_
0010 #define _PROCESSLINEMAKER_H_
0011 
0012 #include <QObject>
0013 #include "utilexport.h"
0014 
0015 /**
0016 @file processlinemaker.h
0017 Utility objects for process output views.
0018 */
0019 
0020 class QProcess;
0021 
0022 class QStringList;
0023 
0024 /**
0025 Convenience class to catch output of QProcess.
0026 */
0027 
0028 namespace KDevelop {
0029 class ProcessLineMakerPrivate;
0030 
0031 class KDEVPLATFORMUTIL_EXPORT ProcessLineMaker : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     explicit ProcessLineMaker(QObject* parent = nullptr);
0037     explicit ProcessLineMaker(QProcess* process, QObject* parent = nullptr);
0038 
0039     ~ProcessLineMaker() override;
0040 
0041     /**
0042      * clears out the internal buffers, this drops any data without
0043      * emitting the related signal
0044      */
0045     void discardBuffers();
0046 
0047     /**
0048      * Flush the data from the buffers and then clear them.
0049      * This should be called once when the process has
0050      * exited to make sure all data that was received from the
0051      * process is properly converted and emitted.
0052      *
0053      * Note: Connecting this class to the process finished signal
0054      * is not going to work, as the user of this class will do
0055      * that itself too and possibly delete the process, making
0056      * it impossible to fetch the last output.
0057      */
0058     void flushBuffers();
0059 
0060 public Q_SLOTS:
0061     /**
0062      * This should be used (instead of hand-crafted code) when
0063      * you need to do custom things with the process output
0064      * before feeding it to the linemaker and have it convert
0065      * it to QString lines.
0066      * @param buffer the output from the process
0067      */
0068     void slotReceivedStdout(const QByteArray& buffer);
0069 
0070     /**
0071      * This should be used (instead of hand-crafted code) when
0072      * you need to do custom things with the process error output
0073      * before feeding it to the linemaker and have it convert
0074      * it to QString lines.
0075      * @param buffer the output from the process
0076      */
0077     void slotReceivedStderr(const QByteArray& buffer);
0078 
0079 Q_SIGNALS:
0080     /**
0081      * Emitted whenever the process prints something
0082      * to its standard output. The output is converted
0083      * to a QString using fromLocal8Bit() and will
0084      * be split on '\n'.
0085      * @param lines the lines that the process printed
0086      */
0087     void receivedStdoutLines(const QStringList& lines);
0088 
0089     /**
0090      * Emitted whenever the process prints something
0091      * to its error output. The output is converted
0092      * to a QString using fromLocal8Bit() and will
0093      * be split on '\n'.
0094      * @param lines the lines that the process printed
0095      */
0096     void receivedStderrLines(const QStringList& lines);
0097 
0098 private:
0099     const QScopedPointer<class ProcessLineMakerPrivate> d_ptr;
0100     Q_DECLARE_PRIVATE(ProcessLineMaker)
0101     friend class ProcessLineMakerPrivate;
0102 };
0103 
0104 }
0105 
0106 #endif