1 /*
2     SPDX-FileCopyrightText: 2008-2010 Volker Lanz <vl@fidra.de>
3     SPDX-FileCopyrightText: 2013-2020 Andrius Štikonas <andrius@stikonas.eu>
4     SPDX-FileCopyrightText: 2015 Teo Mrnjavac <teo@kde.org>
5     SPDX-FileCopyrightText: 2018 Huzaifa Faruqui <huzaifafaruqui@gmail.com>
6     SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>
7     SPDX-FileCopyrightText: 2019 Shubham Jangra <aryan100jangid@gmail.com>
8 
9     SPDX-License-Identifier: GPL-3.0-or-later
10 */
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 #include <QString>
20 #include <QStringList>
21 #include <QtGlobal>
22 #include <QThread>
23 #include <QVariant>
24 
25 #include <memory>
26 
27 class KJob;
28 class Report;
29 class CopySource;
30 class CopySourceDevice;
31 class CopyTarget;
32 class QDBusInterface;
33 class QDBusPendingCall;
34 class OrgKdeKpmcoreExternalcommandInterface;
35 
36 struct ExternalCommandPrivate;
37 
38 /** An external command.
39 
40     Runs an external command as a child process.
41 
42     @author Volker Lanz <vl@fidra.de>
43     @author Andrius Štikonas <andrius@stikonas.eu>
44 */
45 class LIBKPMCORE_EXPORT ExternalCommand : public QObject
46 {
47     Q_OBJECT
48     Q_DISABLE_COPY(ExternalCommand)
49 
50 public:
51     explicit ExternalCommand(const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
52     explicit ExternalCommand(Report& report, const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
53 
54     ~ExternalCommand() override;
55 
56 public:
57     bool copyBlocks(const CopySource& source, CopyTarget& target);
58     QByteArray readData(const CopySourceDevice& source);
59     bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray
60     bool createFile(const QByteArray& filePath, const QString& fileContents); // similar to writeData but creates a new file
61 
62     /**< @param cmd the command to run */
63     void setCommand(const QString& cmd);
64      /**< @return the command to run */
65     const QString& command() const;
66 
67     /**< @return the arguments */
68     const QStringList& args() const;
69 
70     /**< @param s the argument to add */
71     void addArg(const QString& s);
72     /**< @param args the new arguments */
73     void setArgs(const QStringList& args);
74 
75     bool write(const QByteArray& input); /**< @param input the input for the program */
76 
77     bool start(int timeout = 30000);
78     bool run(int timeout = 30000);
79 
80     /**< @return the exit code */
81     int exitCode() const;
82 
83     /**< @return the command output */
84     const QString output() const;
85     /**< @return the command output */
86     const QByteArray& rawOutput() const;
87 
88     /**< @return pointer to the Report or nullptr */
89     Report* report();
90 
91 Q_SIGNALS:
92     void progress(int);
93     void reportSignal(const QString&);
94 
95 private:
96     void setExitCode(int i);
97     void onReadOutput();
98     bool waitForDbusReply(QDBusPendingCall &pcall);
99     OrgKdeKpmcoreExternalcommandInterface* helperInterface();
100 
101 private:
102     std::unique_ptr<ExternalCommandPrivate> d;
103 
104 };
105 
106 #endif
107