2015-06-04 01:29:22 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
|
2018-03-22 17:29:40 +00:00
|
|
|
* Copyright (C) 2016-2018 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
2017-09-10 20:12:52 +01:00
|
|
|
#if !defined(KPMCORE_EXTERNALCOMMAND_H)
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2017-09-10 20:12:52 +01:00
|
|
|
#define KPMCORE_EXTERNALCOMMAND_H
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2016-05-04 22:14:38 +01:00
|
|
|
#include "util/libpartitionmanagerexport.h"
|
2018-01-24 15:22:42 +00:00
|
|
|
#include "core/copysourcedevice.h"
|
|
|
|
#include "core/copytargetfile.h"
|
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
#include <QDebug>
|
2015-06-04 01:29:22 +01:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QString>
|
2018-01-27 18:54:48 +00:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QtGlobal>
|
2017-11-07 22:55:28 +00:00
|
|
|
#include <QVariant>
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2018-03-31 16:30:53 +01:00
|
|
|
class KJob;
|
2015-06-04 01:29:22 +01:00
|
|
|
class Report;
|
|
|
|
|
|
|
|
/** An external command.
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
Runs an external command as a child process.
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
@author Volker Lanz <vl@fidra.de>
|
|
|
|
@author Andrius Štikonas <andrius@stikonas.eu>
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
2017-11-07 22:55:28 +00:00
|
|
|
class LIBKPMCORE_EXPORT ExternalCommand : public QObject
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
2017-11-07 22:55:28 +00:00
|
|
|
Q_OBJECT
|
2015-07-13 15:16:36 +01:00
|
|
|
Q_DISABLE_COPY(ExternalCommand)
|
|
|
|
|
|
|
|
public:
|
2017-11-07 22:55:28 +00:00
|
|
|
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);
|
2015-07-13 15:16:36 +01:00
|
|
|
|
|
|
|
public:
|
2018-03-22 17:29:40 +00:00
|
|
|
bool copyBlocks(CopySource& source, CopyTarget& target);
|
2016-05-09 00:41:44 +01:00
|
|
|
void setCommand(const QString& cmd) { m_Command = cmd; } /**< @param cmd the command to run */
|
|
|
|
const QString& command() const { return m_Command; } /**< @return the command to run */
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2016-05-09 00:41:44 +01:00
|
|
|
void addArg(const QString& s) { m_Args << s; } /**< @param s the argument to add */
|
|
|
|
const QStringList& args() const { return m_Args; } /**< @return the arguments */
|
|
|
|
void setArgs(const QStringList& args) { m_Args = args; } /**< @param args the new arguments */
|
2017-11-07 22:55:28 +00:00
|
|
|
bool write(const QByteArray& input); /**< @param input the input for the program */
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2018-02-06 16:24:32 +00:00
|
|
|
bool startCopyBlocks();
|
2015-07-13 15:16:36 +01:00
|
|
|
bool start(int timeout = 30000);
|
|
|
|
bool waitFor(int timeout = 30000);
|
|
|
|
bool run(int timeout = 30000);
|
|
|
|
|
|
|
|
int exitCode() const {
|
|
|
|
return m_ExitCode; /**< @return the exit code */
|
|
|
|
}
|
|
|
|
|
2017-10-03 02:25:56 +01:00
|
|
|
const QString output() const {
|
|
|
|
return QString::fromLocal8Bit(m_Output); /**< @return the command output */
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray& rawOutput() const {
|
2015-07-13 15:16:36 +01:00
|
|
|
return m_Output; /**< @return the command output */
|
|
|
|
}
|
|
|
|
|
|
|
|
Report* report() {
|
2015-07-22 14:48:03 +01:00
|
|
|
return m_Report; /**< @return pointer to the Report or nullptr */
|
2015-07-13 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-27 18:54:48 +00:00
|
|
|
void emitReport(const QVariantMap& report) { emit reportSignal(report); }
|
|
|
|
|
2017-11-07 22:55:28 +00:00
|
|
|
Q_SIGNALS:
|
2018-01-24 15:22:42 +00:00
|
|
|
void progress(int);
|
2018-01-27 18:54:48 +00:00
|
|
|
void reportSignal(const QVariantMap&);
|
2017-11-07 22:55:28 +00:00
|
|
|
|
2018-01-24 15:22:42 +00:00
|
|
|
public Q_SLOTS:
|
|
|
|
void emitProgress(KJob*, unsigned long percent) { emit progress(percent); };
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
protected:
|
|
|
|
void setExitCode(int i) {
|
|
|
|
m_ExitCode = i;
|
|
|
|
}
|
2017-08-26 18:40:28 +01:00
|
|
|
void setup(const QProcess::ProcessChannelMode processChannelMode);
|
2015-07-13 15:16:36 +01:00
|
|
|
|
2016-05-18 19:54:36 +01:00
|
|
|
void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
2015-07-13 15:16:36 +01:00
|
|
|
void onReadOutput();
|
|
|
|
|
|
|
|
private:
|
2017-11-07 22:55:28 +00:00
|
|
|
QVariantMap arguments;
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
Report *m_Report;
|
2016-05-09 00:41:44 +01:00
|
|
|
QString m_Command;
|
|
|
|
QStringList m_Args;
|
2015-07-13 15:16:36 +01:00
|
|
|
int m_ExitCode;
|
2017-10-03 02:25:56 +01:00
|
|
|
QByteArray m_Output;
|
2017-11-07 22:55:28 +00:00
|
|
|
QByteArray m_Input;
|
2015-06-04 01:29:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|