externalcommand.h 3.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
    SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
    SPDX-FileCopyrightText: 2013-2020 Andrius Štikonas <andrius@stikonas.eu>
    SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
    SPDX-FileCopyrightText: 2018 Huzaifa Faruqui <huzaifafaruqui@gmail.com>
    SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
    SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>

    SPDX-License-Identifier: GPL-3.0-or-later
*/
11

12
#ifndef KPMCORE_EXTERNALCOMMAND_H
13
#define KPMCORE_EXTERNALCOMMAND_H
14

15
#include "util/libpartitionmanagerexport.h"
16

17
#include <QDebug>
18
#include <QProcess>
19
20
#include <QString>
#include <QStringList>
Huzaifa Faruqui's avatar
Huzaifa Faruqui committed
21
#include <QtGlobal>
22
#include <QThread>
23
#include <QVariant>
24

25
26
#include <memory>

27
class KJob;
28
class Report;
29
30
class CopySource;
class CopyTarget;
31
class QDBusInterface;
32
33
class QDBusPendingCall;
class OrgKdeKpmcoreExternalcommandInterface;
34

35
struct ExternalCommandPrivate;
36
37
38

/** An external command.

39
    Runs an external command as a child process.
40

41
42
    @author Volker Lanz <vl@fidra.de>
    @author Andrius Štikonas <andrius@stikonas.eu>
43
*/
44
class LIBKPMCORE_EXPORT ExternalCommand : public QObject
45
{
46
    Q_OBJECT
47
48
49
    Q_DISABLE_COPY(ExternalCommand)

public:
50
51
    explicit ExternalCommand(const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
    explicit ExternalCommand(Report& report, const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
52

53
    ~ExternalCommand() override;
54

55
public:
Shubham  .'s avatar
Shubham . committed
56
57
    bool copyBlocks(const CopySource& source, CopyTarget& target);
    bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray
58
    bool createFile(const QByteArray& filePath, const QString& fileContents); // similar to writeData but creates a new file
59

60
61
62
63
64
65
66
67
68
69
70
71
72
    /**< @param cmd the command to run */
    void setCommand(const QString& cmd);
     /**< @return the command to run */
    const QString& command() const;

    /**< @return the arguments */
    const QStringList& args() const;

    /**< @param s the argument to add */
    void addArg(const QString& s);
    /**< @param args the new arguments */
    void setArgs(const QStringList& args);

73
    bool write(const QByteArray& input); /**< @param input the input for the program */
74
75
76
77

    bool start(int timeout = 30000);
    bool run(int timeout = 30000);

78
79
    /**< @return the exit code */
    int exitCode() const;
80

81
82
83
84
    /**< @return the command output */
    const QString output() const;
    /**< @return the command output */
    const QByteArray& rawOutput() const;
85

86
87
    /**< @return pointer to the Report or nullptr */
    Report* report();
88

89
Q_SIGNALS:
90
    void progress(int);
91
    void reportSignal(const QString&);
92

93
private:
94
    void setExitCode(int i);
95
    void onReadOutput();
96
97
    bool waitForDbusReply(QDBusPendingCall &pcall);
    OrgKdeKpmcoreExternalcommandInterface* helperInterface();
98
99

private:
100
    std::unique_ptr<ExternalCommandPrivate> d;
101

102
103
104
};

#endif