File: commandexecutor.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (144 lines) | stat: -rw-r--r-- 4,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*  This file is part of KDevelop
    Copyright 2007 Andreas Pakulat <apaku@gmx.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef KDEVPLATFORM_COMMANDEXECUTOR_H
#define KDEVPLATFORM_COMMANDEXECUTOR_H

#include <QObject>
#include <QProcess>
#include "utilexport.h"

namespace KDevelop {
class CommandExecutorPrivate;

/**
 * Simplifying the execution of a Command through (QK)Process.
 *
 * This class emits only very basic signals when the process writes something
 * to stdout or stderr and for signaling completed and failed status of running
 * the process. This means that a process that is executed without a crash or so
 * is considered to be completed, even if it indicates an error during execution
 * using a non-zero return value. This needs to be handled by the user of the class
 * using the argument in the completed signal
 *
 * If you need more fine-grained control use (QK)Process directly and also
 * check whether you can use \ref KDevelop::ProcessLineMaker to use properly
 * terminated lines of output.
 *
 * Also this class provides only asynchronous operation, it doesn't allow to
 * wait for the program to finish.
 *
 * @author Andreas Pakulat <apaku@gmx.de>
 * TODO: Should this be a KJob??
 */
class KDEVPLATFORMUTIL_EXPORT CommandExecutor : public QObject
{
    Q_OBJECT

public:
    /**
     * Create a command using the given executable, arguments and environment
     *
     * The process is not started immediately, instead start() has to be called.
     */
    explicit CommandExecutor(const QString& command, QObject* parent = nullptr);
    ~CommandExecutor() override;

    /**
     * set additional arguments to be used when executing the command
     */
    void setArguments(const QStringList& args);
    /**
     * set additional environment variables to be used when executing the command
     */
    void setEnvironment(const QMap<QString, QString>& env);

    /**
     * set additional environment variables to be used when executing the command
     */
    void setEnvironment(const QStringList& env);

    /**
     * Sets the working directory of the command
     */
    void setWorkingDirectory(const QString& dir);

    /**
     * start the command, after this has been called signals may be emitted
     */
    void start();

    /**
     * kill the process, failed() will likely be emitted
     */
    void kill();

    /**
     * set the Command that should be started, now a commandexecutor can be reused
     */
    void setCommand(const QString& command);

    /**
     * whether the commands are executed from a shell
     */
    bool useShell() const;

    /**
     * if @p shell is true, the command is executed from a shell
     */
    void setUseShell(bool shell);

    /**
     * @returns the arguments
     */
    QStringList arguments() const;

    /**
     * @returns the command
     */
    QString command() const;

    /**
     * @returns the working directory
     */
    QString workingDirectory() const;

Q_SIGNALS:
    void receivedStandardError(const QStringList&);
    void receivedStandardOutput(const QStringList&);
    /**
     * Emitted when there was a severe problem executing the process, for example it
     * could not be started or crashed during execution.
     */
    void failed(QProcess::ProcessError);
    /**
     * Emitted when the process was successfully started and finished without crashing
     * The @p code parameter indicates the return value from executing the process
     */
    void completed(int code);

private:
    const QScopedPointer<class CommandExecutorPrivate> d_ptr;
    Q_DECLARE_PRIVATE(CommandExecutor)
    friend class CommandExecutorPrivate;
};

}

#endif