File indexing completed on 2024-05-05 04:38:43
0001 /* 0002 SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef KDEVPLATFORM_COMMANDEXECUTOR_H 0008 #define KDEVPLATFORM_COMMANDEXECUTOR_H 0009 0010 #include <QObject> 0011 #include <QProcess> 0012 #include "utilexport.h" 0013 0014 namespace KDevelop { 0015 class CommandExecutorPrivate; 0016 0017 /** 0018 * Simplifying the execution of a Command through (QK)Process. 0019 * 0020 * This class emits only very basic signals when the process writes something 0021 * to stdout or stderr and for signaling completed and failed status of running 0022 * the process. This means that a process that is executed without a crash or so 0023 * is considered to be completed, even if it indicates an error during execution 0024 * using a non-zero return value. This needs to be handled by the user of the class 0025 * using the argument in the completed signal 0026 * 0027 * If you need more fine-grained control use (QK)Process directly and also 0028 * check whether you can use \ref KDevelop::ProcessLineMaker to use properly 0029 * terminated lines of output. 0030 * 0031 * Also this class provides only asynchronous operation, it doesn't allow to 0032 * wait for the program to finish. 0033 * 0034 * @author Andreas Pakulat <apaku@gmx.de> 0035 * TODO: Should this be a KJob?? 0036 */ 0037 class KDEVPLATFORMUTIL_EXPORT CommandExecutor : public QObject 0038 { 0039 Q_OBJECT 0040 0041 public: 0042 /** 0043 * Create a command using the given executable, arguments and environment 0044 * 0045 * The process is not started immediately, instead start() has to be called. 0046 */ 0047 explicit CommandExecutor(const QString& command, QObject* parent = nullptr); 0048 ~CommandExecutor() override; 0049 0050 /** 0051 * set additional arguments to be used when executing the command 0052 */ 0053 void setArguments(const QStringList& args); 0054 /** 0055 * set additional environment variables to be used when executing the command 0056 */ 0057 void setEnvironment(const QMap<QString, QString>& env); 0058 0059 /** 0060 * set additional environment variables to be used when executing the command 0061 */ 0062 void setEnvironment(const QStringList& env); 0063 0064 /** 0065 * Sets the working directory of the command 0066 */ 0067 void setWorkingDirectory(const QString& dir); 0068 0069 /** 0070 * start the command, after this has been called signals may be emitted 0071 */ 0072 void start(); 0073 0074 /** 0075 * kill the process, failed() will likely be emitted 0076 */ 0077 void kill(); 0078 0079 /** 0080 * set the Command that should be started, now a commandexecutor can be reused 0081 */ 0082 void setCommand(const QString& command); 0083 0084 /** 0085 * whether the commands are executed from a shell 0086 */ 0087 bool useShell() const; 0088 0089 /** 0090 * if @p shell is true, the command is executed from a shell 0091 */ 0092 void setUseShell(bool shell); 0093 0094 /** 0095 * @returns the arguments 0096 */ 0097 QStringList arguments() const; 0098 0099 /** 0100 * @returns the command 0101 */ 0102 QString command() const; 0103 0104 /** 0105 * @returns the working directory 0106 */ 0107 QString workingDirectory() const; 0108 0109 Q_SIGNALS: 0110 void receivedStandardError(const QStringList&); 0111 void receivedStandardOutput(const QStringList&); 0112 /** 0113 * Emitted when there was a severe problem executing the process, for example it 0114 * could not be started or crashed during execution. 0115 */ 0116 void failed(QProcess::ProcessError); 0117 /** 0118 * Emitted when the process was successfully started and finished without crashing 0119 * The @p code parameter indicates the return value from executing the process 0120 */ 0121 void completed(int code); 0122 0123 private: 0124 const QScopedPointer<class CommandExecutorPrivate> d_ptr; 0125 Q_DECLARE_PRIVATE(CommandExecutor) 0126 friend class CommandExecutorPrivate; 0127 }; 0128 0129 } 0130 0131 #endif